You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

617 lines
20 KiB

  1. // Copyright 2017, The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE.md file.
  4. // Package cmp determines equality of values.
  5. //
  6. // This package is intended to be a more powerful and safer alternative to
  7. // reflect.DeepEqual for comparing whether two values are semantically equal.
  8. //
  9. // The primary features of cmp are:
  10. //
  11. // • When the default behavior of equality does not suit the needs of the test,
  12. // custom equality functions can override the equality operation.
  13. // For example, an equality function may report floats as equal so long as they
  14. // are within some tolerance of each other.
  15. //
  16. // • Types that have an Equal method may use that method to determine equality.
  17. // This allows package authors to determine the equality operation for the types
  18. // that they define.
  19. //
  20. // • If no custom equality functions are used and no Equal method is defined,
  21. // equality is determined by recursively comparing the primitive kinds on both
  22. // values, much like reflect.DeepEqual. Unlike reflect.DeepEqual, unexported
  23. // fields are not compared by default; they result in panics unless suppressed
  24. // by using an Ignore option (see cmpopts.IgnoreUnexported) or explicitly compared
  25. // using the AllowUnexported option.
  26. package cmp
  27. import (
  28. "fmt"
  29. "reflect"
  30. "strings"
  31. "github.com/google/go-cmp/cmp/internal/diff"
  32. "github.com/google/go-cmp/cmp/internal/flags"
  33. "github.com/google/go-cmp/cmp/internal/function"
  34. "github.com/google/go-cmp/cmp/internal/value"
  35. )
  36. // Equal reports whether x and y are equal by recursively applying the
  37. // following rules in the given order to x and y and all of their sub-values:
  38. //
  39. // • Let S be the set of all Ignore, Transformer, and Comparer options that
  40. // remain after applying all path filters, value filters, and type filters.
  41. // If at least one Ignore exists in S, then the comparison is ignored.
  42. // If the number of Transformer and Comparer options in S is greater than one,
  43. // then Equal panics because it is ambiguous which option to use.
  44. // If S contains a single Transformer, then use that to transform the current
  45. // values and recursively call Equal on the output values.
  46. // If S contains a single Comparer, then use that to compare the current values.
  47. // Otherwise, evaluation proceeds to the next rule.
  48. //
  49. // • If the values have an Equal method of the form "(T) Equal(T) bool" or
  50. // "(T) Equal(I) bool" where T is assignable to I, then use the result of
  51. // x.Equal(y) even if x or y is nil. Otherwise, no such method exists and
  52. // evaluation proceeds to the next rule.
  53. //
  54. // • Lastly, try to compare x and y based on their basic kinds.
  55. // Simple kinds like booleans, integers, floats, complex numbers, strings, and
  56. // channels are compared using the equivalent of the == operator in Go.
  57. // Functions are only equal if they are both nil, otherwise they are unequal.
  58. //
  59. // Structs are equal if recursively calling Equal on all fields report equal.
  60. // If a struct contains unexported fields, Equal panics unless an Ignore option
  61. // (e.g., cmpopts.IgnoreUnexported) ignores that field or the AllowUnexported
  62. // option explicitly permits comparing the unexported field.
  63. //
  64. // Slices are equal if they are both nil or both non-nil, where recursively
  65. // calling Equal on all non-ignored slice or array elements report equal.
  66. // Empty non-nil slices and nil slices are not equal; to equate empty slices,
  67. // consider using cmpopts.EquateEmpty.
  68. //
  69. // Maps are equal if they are both nil or both non-nil, where recursively
  70. // calling Equal on all non-ignored map entries report equal.
  71. // Map keys are equal according to the == operator.
  72. // To use custom comparisons for map keys, consider using cmpopts.SortMaps.
  73. // Empty non-nil maps and nil maps are not equal; to equate empty maps,
  74. // consider using cmpopts.EquateEmpty.
  75. //
  76. // Pointers and interfaces are equal if they are both nil or both non-nil,
  77. // where they have the same underlying concrete type and recursively
  78. // calling Equal on the underlying values reports equal.
  79. func Equal(x, y interface{}, opts ...Option) bool {
  80. vx := reflect.ValueOf(x)
  81. vy := reflect.ValueOf(y)
  82. // If the inputs are different types, auto-wrap them in an empty interface
  83. // so that they have the same parent type.
  84. var t reflect.Type
  85. if !vx.IsValid() || !vy.IsValid() || vx.Type() != vy.Type() {
  86. t = reflect.TypeOf((*interface{})(nil)).Elem()
  87. if vx.IsValid() {
  88. vvx := reflect.New(t).Elem()
  89. vvx.Set(vx)
  90. vx = vvx
  91. }
  92. if vy.IsValid() {
  93. vvy := reflect.New(t).Elem()
  94. vvy.Set(vy)
  95. vy = vvy
  96. }
  97. } else {
  98. t = vx.Type()
  99. }
  100. s := newState(opts)
  101. s.compareAny(&pathStep{t, vx, vy})
  102. return s.result.Equal()
  103. }
  104. // Diff returns a human-readable report of the differences between two values.
  105. // It returns an empty string if and only if Equal returns true for the same
  106. // input values and options.
  107. //
  108. // The output is displayed as a literal in pseudo-Go syntax.
  109. // At the start of each line, a "-" prefix indicates an element removed from x,
  110. // a "+" prefix to indicates an element added to y, and the lack of a prefix
  111. // indicates an element common to both x and y. If possible, the output
  112. // uses fmt.Stringer.String or error.Error methods to produce more humanly
  113. // readable outputs. In such cases, the string is prefixed with either an
  114. // 's' or 'e' character, respectively, to indicate that the method was called.
  115. //
  116. // Do not depend on this output being stable. If you need the ability to
  117. // programmatically interpret the difference, consider using a custom Reporter.
  118. func Diff(x, y interface{}, opts ...Option) string {
  119. r := new(defaultReporter)
  120. eq := Equal(x, y, Options(opts), Reporter(r))
  121. d := r.String()
  122. if (d == "") != eq {
  123. panic("inconsistent difference and equality results")
  124. }
  125. return d
  126. }
  127. type state struct {
  128. // These fields represent the "comparison state".
  129. // Calling statelessCompare must not result in observable changes to these.
  130. result diff.Result // The current result of comparison
  131. curPath Path // The current path in the value tree
  132. reporters []reporter // Optional reporters
  133. // recChecker checks for infinite cycles applying the same set of
  134. // transformers upon the output of itself.
  135. recChecker recChecker
  136. // dynChecker triggers pseudo-random checks for option correctness.
  137. // It is safe for statelessCompare to mutate this value.
  138. dynChecker dynChecker
  139. // These fields, once set by processOption, will not change.
  140. exporters map[reflect.Type]bool // Set of structs with unexported field visibility
  141. opts Options // List of all fundamental and filter options
  142. }
  143. func newState(opts []Option) *state {
  144. // Always ensure a validator option exists to validate the inputs.
  145. s := &state{opts: Options{validator{}}}
  146. s.processOption(Options(opts))
  147. return s
  148. }
  149. func (s *state) processOption(opt Option) {
  150. switch opt := opt.(type) {
  151. case nil:
  152. case Options:
  153. for _, o := range opt {
  154. s.processOption(o)
  155. }
  156. case coreOption:
  157. type filtered interface {
  158. isFiltered() bool
  159. }
  160. if fopt, ok := opt.(filtered); ok && !fopt.isFiltered() {
  161. panic(fmt.Sprintf("cannot use an unfiltered option: %v", opt))
  162. }
  163. s.opts = append(s.opts, opt)
  164. case visibleStructs:
  165. if s.exporters == nil {
  166. s.exporters = make(map[reflect.Type]bool)
  167. }
  168. for t := range opt {
  169. s.exporters[t] = true
  170. }
  171. case reporter:
  172. s.reporters = append(s.reporters, opt)
  173. default:
  174. panic(fmt.Sprintf("unknown option %T", opt))
  175. }
  176. }
  177. // statelessCompare compares two values and returns the result.
  178. // This function is stateless in that it does not alter the current result,
  179. // or output to any registered reporters.
  180. func (s *state) statelessCompare(step PathStep) diff.Result {
  181. // We do not save and restore the curPath because all of the compareX
  182. // methods should properly push and pop from the path.
  183. // It is an implementation bug if the contents of curPath differs from
  184. // when calling this function to when returning from it.
  185. oldResult, oldReporters := s.result, s.reporters
  186. s.result = diff.Result{} // Reset result
  187. s.reporters = nil // Remove reporters to avoid spurious printouts
  188. s.compareAny(step)
  189. res := s.result
  190. s.result, s.reporters = oldResult, oldReporters
  191. return res
  192. }
  193. func (s *state) compareAny(step PathStep) {
  194. // Update the path stack.
  195. s.curPath.push(step)
  196. defer s.curPath.pop()
  197. for _, r := range s.reporters {
  198. r.PushStep(step)
  199. defer r.PopStep()
  200. }
  201. s.recChecker.Check(s.curPath)
  202. // Obtain the current type and values.
  203. t := step.Type()
  204. vx, vy := step.Values()
  205. // Rule 1: Check whether an option applies on this node in the value tree.
  206. if s.tryOptions(t, vx, vy) {
  207. return
  208. }
  209. // Rule 2: Check whether the type has a valid Equal method.
  210. if s.tryMethod(t, vx, vy) {
  211. return
  212. }
  213. // Rule 3: Compare based on the underlying kind.
  214. switch t.Kind() {
  215. case reflect.Bool:
  216. s.report(vx.Bool() == vy.Bool(), 0)
  217. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  218. s.report(vx.Int() == vy.Int(), 0)
  219. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  220. s.report(vx.Uint() == vy.Uint(), 0)
  221. case reflect.Float32, reflect.Float64:
  222. s.report(vx.Float() == vy.Float(), 0)
  223. case reflect.Complex64, reflect.Complex128:
  224. s.report(vx.Complex() == vy.Complex(), 0)
  225. case reflect.String:
  226. s.report(vx.String() == vy.String(), 0)
  227. case reflect.Chan, reflect.UnsafePointer:
  228. s.report(vx.Pointer() == vy.Pointer(), 0)
  229. case reflect.Func:
  230. s.report(vx.IsNil() && vy.IsNil(), 0)
  231. case reflect.Struct:
  232. s.compareStruct(t, vx, vy)
  233. case reflect.Slice, reflect.Array:
  234. s.compareSlice(t, vx, vy)
  235. case reflect.Map:
  236. s.compareMap(t, vx, vy)
  237. case reflect.Ptr:
  238. s.comparePtr(t, vx, vy)
  239. case reflect.Interface:
  240. s.compareInterface(t, vx, vy)
  241. default:
  242. panic(fmt.Sprintf("%v kind not handled", t.Kind()))
  243. }
  244. }
  245. func (s *state) tryOptions(t reflect.Type, vx, vy reflect.Value) bool {
  246. // Evaluate all filters and apply the remaining options.
  247. if opt := s.opts.filter(s, t, vx, vy); opt != nil {
  248. opt.apply(s, vx, vy)
  249. return true
  250. }
  251. return false
  252. }
  253. func (s *state) tryMethod(t reflect.Type, vx, vy reflect.Value) bool {
  254. // Check if this type even has an Equal method.
  255. m, ok := t.MethodByName("Equal")
  256. if !ok || !function.IsType(m.Type, function.EqualAssignable) {
  257. return false
  258. }
  259. eq := s.callTTBFunc(m.Func, vx, vy)
  260. s.report(eq, reportByMethod)
  261. return true
  262. }
  263. func (s *state) callTRFunc(f, v reflect.Value, step Transform) reflect.Value {
  264. v = sanitizeValue(v, f.Type().In(0))
  265. if !s.dynChecker.Next() {
  266. return f.Call([]reflect.Value{v})[0]
  267. }
  268. // Run the function twice and ensure that we get the same results back.
  269. // We run in goroutines so that the race detector (if enabled) can detect
  270. // unsafe mutations to the input.
  271. c := make(chan reflect.Value)
  272. go detectRaces(c, f, v)
  273. got := <-c
  274. want := f.Call([]reflect.Value{v})[0]
  275. if step.vx, step.vy = got, want; !s.statelessCompare(step).Equal() {
  276. // To avoid false-positives with non-reflexive equality operations,
  277. // we sanity check whether a value is equal to itself.
  278. if step.vx, step.vy = want, want; !s.statelessCompare(step).Equal() {
  279. return want
  280. }
  281. panic(fmt.Sprintf("non-deterministic function detected: %s", function.NameOf(f)))
  282. }
  283. return want
  284. }
  285. func (s *state) callTTBFunc(f, x, y reflect.Value) bool {
  286. x = sanitizeValue(x, f.Type().In(0))
  287. y = sanitizeValue(y, f.Type().In(1))
  288. if !s.dynChecker.Next() {
  289. return f.Call([]reflect.Value{x, y})[0].Bool()
  290. }
  291. // Swapping the input arguments is sufficient to check that
  292. // f is symmetric and deterministic.
  293. // We run in goroutines so that the race detector (if enabled) can detect
  294. // unsafe mutations to the input.
  295. c := make(chan reflect.Value)
  296. go detectRaces(c, f, y, x)
  297. got := <-c
  298. want := f.Call([]reflect.Value{x, y})[0].Bool()
  299. if !got.IsValid() || got.Bool() != want {
  300. panic(fmt.Sprintf("non-deterministic or non-symmetric function detected: %s", function.NameOf(f)))
  301. }
  302. return want
  303. }
  304. func detectRaces(c chan<- reflect.Value, f reflect.Value, vs ...reflect.Value) {
  305. var ret reflect.Value
  306. defer func() {
  307. recover() // Ignore panics, let the other call to f panic instead
  308. c <- ret
  309. }()
  310. ret = f.Call(vs)[0]
  311. }
  312. // sanitizeValue converts nil interfaces of type T to those of type R,
  313. // assuming that T is assignable to R.
  314. // Otherwise, it returns the input value as is.
  315. func sanitizeValue(v reflect.Value, t reflect.Type) reflect.Value {
  316. // TODO(dsnet): Workaround for reflect bug (https://golang.org/issue/22143).
  317. if !flags.AtLeastGo110 {
  318. if v.Kind() == reflect.Interface && v.IsNil() && v.Type() != t {
  319. return reflect.New(t).Elem()
  320. }
  321. }
  322. return v
  323. }
  324. func (s *state) compareStruct(t reflect.Type, vx, vy reflect.Value) {
  325. var vax, vay reflect.Value // Addressable versions of vx and vy
  326. step := StructField{&structField{}}
  327. for i := 0; i < t.NumField(); i++ {
  328. step.typ = t.Field(i).Type
  329. step.vx = vx.Field(i)
  330. step.vy = vy.Field(i)
  331. step.name = t.Field(i).Name
  332. step.idx = i
  333. step.unexported = !isExported(step.name)
  334. if step.unexported {
  335. if step.name == "_" {
  336. continue
  337. }
  338. // Defer checking of unexported fields until later to give an
  339. // Ignore a chance to ignore the field.
  340. if !vax.IsValid() || !vay.IsValid() {
  341. // For retrieveUnexportedField to work, the parent struct must
  342. // be addressable. Create a new copy of the values if
  343. // necessary to make them addressable.
  344. vax = makeAddressable(vx)
  345. vay = makeAddressable(vy)
  346. }
  347. step.mayForce = s.exporters[t]
  348. step.pvx = vax
  349. step.pvy = vay
  350. step.field = t.Field(i)
  351. }
  352. s.compareAny(step)
  353. }
  354. }
  355. func (s *state) compareSlice(t reflect.Type, vx, vy reflect.Value) {
  356. isSlice := t.Kind() == reflect.Slice
  357. if isSlice && (vx.IsNil() || vy.IsNil()) {
  358. s.report(vx.IsNil() && vy.IsNil(), 0)
  359. return
  360. }
  361. // TODO: Support cyclic data structures.
  362. step := SliceIndex{&sliceIndex{pathStep: pathStep{typ: t.Elem()}}}
  363. withIndexes := func(ix, iy int) SliceIndex {
  364. if ix >= 0 {
  365. step.vx, step.xkey = vx.Index(ix), ix
  366. } else {
  367. step.vx, step.xkey = reflect.Value{}, -1
  368. }
  369. if iy >= 0 {
  370. step.vy, step.ykey = vy.Index(iy), iy
  371. } else {
  372. step.vy, step.ykey = reflect.Value{}, -1
  373. }
  374. return step
  375. }
  376. // Ignore options are able to ignore missing elements in a slice.
  377. // However, detecting these reliably requires an optimal differencing
  378. // algorithm, for which diff.Difference is not.
  379. //
  380. // Instead, we first iterate through both slices to detect which elements
  381. // would be ignored if standing alone. The index of non-discarded elements
  382. // are stored in a separate slice, which diffing is then performed on.
  383. var indexesX, indexesY []int
  384. var ignoredX, ignoredY []bool
  385. for ix := 0; ix < vx.Len(); ix++ {
  386. ignored := s.statelessCompare(withIndexes(ix, -1)).NumDiff == 0
  387. if !ignored {
  388. indexesX = append(indexesX, ix)
  389. }
  390. ignoredX = append(ignoredX, ignored)
  391. }
  392. for iy := 0; iy < vy.Len(); iy++ {
  393. ignored := s.statelessCompare(withIndexes(-1, iy)).NumDiff == 0
  394. if !ignored {
  395. indexesY = append(indexesY, iy)
  396. }
  397. ignoredY = append(ignoredY, ignored)
  398. }
  399. // Compute an edit-script for slices vx and vy (excluding ignored elements).
  400. edits := diff.Difference(len(indexesX), len(indexesY), func(ix, iy int) diff.Result {
  401. return s.statelessCompare(withIndexes(indexesX[ix], indexesY[iy]))
  402. })
  403. // Replay the ignore-scripts and the edit-script.
  404. var ix, iy int
  405. for ix < vx.Len() || iy < vy.Len() {
  406. var e diff.EditType
  407. switch {
  408. case ix < len(ignoredX) && ignoredX[ix]:
  409. e = diff.UniqueX
  410. case iy < len(ignoredY) && ignoredY[iy]:
  411. e = diff.UniqueY
  412. default:
  413. e, edits = edits[0], edits[1:]
  414. }
  415. switch e {
  416. case diff.UniqueX:
  417. s.compareAny(withIndexes(ix, -1))
  418. ix++
  419. case diff.UniqueY:
  420. s.compareAny(withIndexes(-1, iy))
  421. iy++
  422. default:
  423. s.compareAny(withIndexes(ix, iy))
  424. ix++
  425. iy++
  426. }
  427. }
  428. }
  429. func (s *state) compareMap(t reflect.Type, vx, vy reflect.Value) {
  430. if vx.IsNil() || vy.IsNil() {
  431. s.report(vx.IsNil() && vy.IsNil(), 0)
  432. return
  433. }
  434. // TODO: Support cyclic data structures.
  435. // We combine and sort the two map keys so that we can perform the
  436. // comparisons in a deterministic order.
  437. step := MapIndex{&mapIndex{pathStep: pathStep{typ: t.Elem()}}}
  438. for _, k := range value.SortKeys(append(vx.MapKeys(), vy.MapKeys()...)) {
  439. step.vx = vx.MapIndex(k)
  440. step.vy = vy.MapIndex(k)
  441. step.key = k
  442. if !step.vx.IsValid() && !step.vy.IsValid() {
  443. // It is possible for both vx and vy to be invalid if the
  444. // key contained a NaN value in it.
  445. //
  446. // Even with the ability to retrieve NaN keys in Go 1.12,
  447. // there still isn't a sensible way to compare the values since
  448. // a NaN key may map to multiple unordered values.
  449. // The most reasonable way to compare NaNs would be to compare the
  450. // set of values. However, this is impossible to do efficiently
  451. // since set equality is provably an O(n^2) operation given only
  452. // an Equal function. If we had a Less function or Hash function,
  453. // this could be done in O(n*log(n)) or O(n), respectively.
  454. //
  455. // Rather than adding complex logic to deal with NaNs, make it
  456. // the user's responsibility to compare such obscure maps.
  457. const help = "consider providing a Comparer to compare the map"
  458. panic(fmt.Sprintf("%#v has map key with NaNs\n%s", s.curPath, help))
  459. }
  460. s.compareAny(step)
  461. }
  462. }
  463. func (s *state) comparePtr(t reflect.Type, vx, vy reflect.Value) {
  464. if vx.IsNil() || vy.IsNil() {
  465. s.report(vx.IsNil() && vy.IsNil(), 0)
  466. return
  467. }
  468. // TODO: Support cyclic data structures.
  469. vx, vy = vx.Elem(), vy.Elem()
  470. s.compareAny(Indirect{&indirect{pathStep{t.Elem(), vx, vy}}})
  471. }
  472. func (s *state) compareInterface(t reflect.Type, vx, vy reflect.Value) {
  473. if vx.IsNil() || vy.IsNil() {
  474. s.report(vx.IsNil() && vy.IsNil(), 0)
  475. return
  476. }
  477. vx, vy = vx.Elem(), vy.Elem()
  478. if vx.Type() != vy.Type() {
  479. s.report(false, 0)
  480. return
  481. }
  482. s.compareAny(TypeAssertion{&typeAssertion{pathStep{vx.Type(), vx, vy}}})
  483. }
  484. func (s *state) report(eq bool, rf resultFlags) {
  485. if rf&reportByIgnore == 0 {
  486. if eq {
  487. s.result.NumSame++
  488. rf |= reportEqual
  489. } else {
  490. s.result.NumDiff++
  491. rf |= reportUnequal
  492. }
  493. }
  494. for _, r := range s.reporters {
  495. r.Report(Result{flags: rf})
  496. }
  497. }
  498. // recChecker tracks the state needed to periodically perform checks that
  499. // user provided transformers are not stuck in an infinitely recursive cycle.
  500. type recChecker struct{ next int }
  501. // Check scans the Path for any recursive transformers and panics when any
  502. // recursive transformers are detected. Note that the presence of a
  503. // recursive Transformer does not necessarily imply an infinite cycle.
  504. // As such, this check only activates after some minimal number of path steps.
  505. func (rc *recChecker) Check(p Path) {
  506. const minLen = 1 << 16
  507. if rc.next == 0 {
  508. rc.next = minLen
  509. }
  510. if len(p) < rc.next {
  511. return
  512. }
  513. rc.next <<= 1
  514. // Check whether the same transformer has appeared at least twice.
  515. var ss []string
  516. m := map[Option]int{}
  517. for _, ps := range p {
  518. if t, ok := ps.(Transform); ok {
  519. t := t.Option()
  520. if m[t] == 1 { // Transformer was used exactly once before
  521. tf := t.(*transformer).fnc.Type()
  522. ss = append(ss, fmt.Sprintf("%v: %v => %v", t, tf.In(0), tf.Out(0)))
  523. }
  524. m[t]++
  525. }
  526. }
  527. if len(ss) > 0 {
  528. const warning = "recursive set of Transformers detected"
  529. const help = "consider using cmpopts.AcyclicTransformer"
  530. set := strings.Join(ss, "\n\t")
  531. panic(fmt.Sprintf("%s:\n\t%s\n%s", warning, set, help))
  532. }
  533. }
  534. // dynChecker tracks the state needed to periodically perform checks that
  535. // user provided functions are symmetric and deterministic.
  536. // The zero value is safe for immediate use.
  537. type dynChecker struct{ curr, next int }
  538. // Next increments the state and reports whether a check should be performed.
  539. //
  540. // Checks occur every Nth function call, where N is a triangular number:
  541. // 0 1 3 6 10 15 21 28 36 45 55 66 78 91 105 120 136 153 171 190 ...
  542. // See https://en.wikipedia.org/wiki/Triangular_number
  543. //
  544. // This sequence ensures that the cost of checks drops significantly as
  545. // the number of functions calls grows larger.
  546. func (dc *dynChecker) Next() bool {
  547. ok := dc.curr == dc.next
  548. if ok {
  549. dc.curr = 0
  550. dc.next++
  551. }
  552. dc.curr++
  553. return ok
  554. }
  555. // makeAddressable returns a value that is always addressable.
  556. // It returns the input verbatim if it is already addressable,
  557. // otherwise it creates a new value and returns an addressable copy.
  558. func makeAddressable(v reflect.Value) reflect.Value {
  559. if v.CanAddr() {
  560. return v
  561. }
  562. vc := reflect.New(v.Type()).Elem()
  563. vc.Set(v)
  564. return vc
  565. }