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.
5 // Package cmp determines equality of values.
7 // This package is intended to be a more powerful and safer alternative to
8 // reflect.DeepEqual for comparing whether two values are semantically equal.
9 // It is intended to only be used in tests, as performance is not a goal and
10 // it may panic if it cannot compare the values. Its propensity towards
11 // panicking means that its unsuitable for production environments where a
12 // spurious panic may be fatal.
14 // The primary features of cmp are:
16 // • When the default behavior of equality does not suit the needs of the test,
17 // custom equality functions can override the equality operation.
18 // For example, an equality function may report floats as equal so long as they
19 // are within some tolerance of each other.
21 // • Types that have an Equal method may use that method to determine equality.
22 // This allows package authors to determine the equality operation for the types
25 // • If no custom equality functions are used and no Equal method is defined,
26 // equality is determined by recursively comparing the primitive kinds on both
27 // values, much like reflect.DeepEqual. Unlike reflect.DeepEqual, unexported
28 // fields are not compared by default; they result in panics unless suppressed
29 // by using an Ignore option (see cmpopts.IgnoreUnexported) or explicitly
30 // compared using the Exporter option.
38 "github.com/google/go-cmp/cmp/internal/diff"
39 "github.com/google/go-cmp/cmp/internal/flags"
40 "github.com/google/go-cmp/cmp/internal/function"
41 "github.com/google/go-cmp/cmp/internal/value"
44 // Equal reports whether x and y are equal by recursively applying the
45 // following rules in the given order to x and y and all of their sub-values:
47 // • Let S be the set of all Ignore, Transformer, and Comparer options that
48 // remain after applying all path filters, value filters, and type filters.
49 // If at least one Ignore exists in S, then the comparison is ignored.
50 // If the number of Transformer and Comparer options in S is greater than one,
51 // then Equal panics because it is ambiguous which option to use.
52 // If S contains a single Transformer, then use that to transform the current
53 // values and recursively call Equal on the output values.
54 // If S contains a single Comparer, then use that to compare the current values.
55 // Otherwise, evaluation proceeds to the next rule.
57 // • If the values have an Equal method of the form "(T) Equal(T) bool" or
58 // "(T) Equal(I) bool" where T is assignable to I, then use the result of
59 // x.Equal(y) even if x or y is nil. Otherwise, no such method exists and
60 // evaluation proceeds to the next rule.
62 // • Lastly, try to compare x and y based on their basic kinds.
63 // Simple kinds like booleans, integers, floats, complex numbers, strings, and
64 // channels are compared using the equivalent of the == operator in Go.
65 // Functions are only equal if they are both nil, otherwise they are unequal.
67 // Structs are equal if recursively calling Equal on all fields report equal.
68 // If a struct contains unexported fields, Equal panics unless an Ignore option
69 // (e.g., cmpopts.IgnoreUnexported) ignores that field or the Exporter option
70 // explicitly permits comparing the unexported field.
72 // Slices are equal if they are both nil or both non-nil, where recursively
73 // calling Equal on all non-ignored slice or array elements report equal.
74 // Empty non-nil slices and nil slices are not equal; to equate empty slices,
75 // consider using cmpopts.EquateEmpty.
77 // Maps are equal if they are both nil or both non-nil, where recursively
78 // calling Equal on all non-ignored map entries report equal.
79 // Map keys are equal according to the == operator.
80 // To use custom comparisons for map keys, consider using cmpopts.SortMaps.
81 // Empty non-nil maps and nil maps are not equal; to equate empty maps,
82 // consider using cmpopts.EquateEmpty.
84 // Pointers and interfaces are equal if they are both nil or both non-nil,
85 // where they have the same underlying concrete type and recursively
86 // calling Equal on the underlying values reports equal.
88 // Before recursing into a pointer, slice element, or map, the current path
89 // is checked to detect whether the address has already been visited.
90 // If there is a cycle, then the pointed at values are considered equal
91 // only if both addresses were previously visited in the same path step.
92 func Equal(x, y interface{}, opts ...Option) bool {
94 s.compareAny(rootStep(x, y))
95 return s.result.Equal()
98 // Diff returns a human-readable report of the differences between two values.
99 // It returns an empty string if and only if Equal returns true for the same
100 // input values and options.
102 // The output is displayed as a literal in pseudo-Go syntax.
103 // At the start of each line, a "-" prefix indicates an element removed from x,
104 // a "+" prefix to indicates an element added to y, and the lack of a prefix
105 // indicates an element common to both x and y. If possible, the output
106 // uses fmt.Stringer.String or error.Error methods to produce more humanly
107 // readable outputs. In such cases, the string is prefixed with either an
108 // 's' or 'e' character, respectively, to indicate that the method was called.
110 // Do not depend on this output being stable. If you need the ability to
111 // programmatically interpret the difference, consider using a custom Reporter.
112 func Diff(x, y interface{}, opts ...Option) string {
115 // Optimization: If there are no other reporters, we can optimize for the
116 // common case where the result is equal (and thus no reported difference).
117 // This avoids the expensive construction of a difference tree.
118 if len(s.reporters) == 0 {
119 s.compareAny(rootStep(x, y))
120 if s.result.Equal() {
123 s.result = diff.Result{} // Reset results
126 r := new(defaultReporter)
127 s.reporters = append(s.reporters, reporter{r})
128 s.compareAny(rootStep(x, y))
130 if (d == "") != s.result.Equal() {
131 panic("inconsistent difference and equality results")
136 // rootStep constructs the first path step. If x and y have differing types,
137 // then they are stored within an empty interface type.
138 func rootStep(x, y interface{}) PathStep {
139 vx := reflect.ValueOf(x)
140 vy := reflect.ValueOf(y)
142 // If the inputs are different types, auto-wrap them in an empty interface
143 // so that they have the same parent type.
145 if !vx.IsValid() || !vy.IsValid() || vx.Type() != vy.Type() {
146 t = reflect.TypeOf((*interface{})(nil)).Elem()
148 vvx := reflect.New(t).Elem()
153 vvy := reflect.New(t).Elem()
161 return &pathStep{t, vx, vy}
165 // These fields represent the "comparison state".
166 // Calling statelessCompare must not result in observable changes to these.
167 result diff.Result // The current result of comparison
168 curPath Path // The current path in the value tree
169 curPtrs pointerPath // The current set of visited pointers
170 reporters []reporter // Optional reporters
172 // recChecker checks for infinite cycles applying the same set of
173 // transformers upon the output of itself.
174 recChecker recChecker
176 // dynChecker triggers pseudo-random checks for option correctness.
177 // It is safe for statelessCompare to mutate this value.
178 dynChecker dynChecker
180 // These fields, once set by processOption, will not change.
181 exporters []exporter // List of exporters for structs with unexported fields
182 opts Options // List of all fundamental and filter options
185 func newState(opts []Option) *state {
186 // Always ensure a validator option exists to validate the inputs.
187 s := &state{opts: Options{validator{}}}
189 s.processOption(Options(opts))
193 func (s *state) processOption(opt Option) {
194 switch opt := opt.(type) {
197 for _, o := range opt {
201 type filtered interface {
204 if fopt, ok := opt.(filtered); ok && !fopt.isFiltered() {
205 panic(fmt.Sprintf("cannot use an unfiltered option: %v", opt))
207 s.opts = append(s.opts, opt)
209 s.exporters = append(s.exporters, opt)
211 s.reporters = append(s.reporters, opt)
213 panic(fmt.Sprintf("unknown option %T", opt))
217 // statelessCompare compares two values and returns the result.
218 // This function is stateless in that it does not alter the current result,
219 // or output to any registered reporters.
220 func (s *state) statelessCompare(step PathStep) diff.Result {
221 // We do not save and restore curPath and curPtrs because all of the
222 // compareX methods should properly push and pop from them.
223 // It is an implementation bug if the contents of the paths differ from
224 // when calling this function to when returning from it.
226 oldResult, oldReporters := s.result, s.reporters
227 s.result = diff.Result{} // Reset result
228 s.reporters = nil // Remove reporters to avoid spurious printouts
231 s.result, s.reporters = oldResult, oldReporters
235 func (s *state) compareAny(step PathStep) {
236 // Update the path stack.
238 defer s.curPath.pop()
239 for _, r := range s.reporters {
243 s.recChecker.Check(s.curPath)
245 // Cycle-detection for slice elements (see NOTE in compareSlice).
247 vx, vy := step.Values()
248 if si, ok := step.(SliceIndex); ok && si.isSlice && vx.IsValid() && vy.IsValid() {
249 px, py := vx.Addr(), vy.Addr()
250 if eq, visited := s.curPtrs.Push(px, py); visited {
251 s.report(eq, reportByCycle)
254 defer s.curPtrs.Pop(px, py)
257 // Rule 1: Check whether an option applies on this node in the value tree.
258 if s.tryOptions(t, vx, vy) {
262 // Rule 2: Check whether the type has a valid Equal method.
263 if s.tryMethod(t, vx, vy) {
267 // Rule 3: Compare based on the underlying kind.
270 s.report(vx.Bool() == vy.Bool(), 0)
271 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
272 s.report(vx.Int() == vy.Int(), 0)
273 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
274 s.report(vx.Uint() == vy.Uint(), 0)
275 case reflect.Float32, reflect.Float64:
276 s.report(vx.Float() == vy.Float(), 0)
277 case reflect.Complex64, reflect.Complex128:
278 s.report(vx.Complex() == vy.Complex(), 0)
280 s.report(vx.String() == vy.String(), 0)
281 case reflect.Chan, reflect.UnsafePointer:
282 s.report(vx.Pointer() == vy.Pointer(), 0)
284 s.report(vx.IsNil() && vy.IsNil(), 0)
286 s.compareStruct(t, vx, vy)
287 case reflect.Slice, reflect.Array:
288 s.compareSlice(t, vx, vy)
290 s.compareMap(t, vx, vy)
292 s.comparePtr(t, vx, vy)
293 case reflect.Interface:
294 s.compareInterface(t, vx, vy)
296 panic(fmt.Sprintf("%v kind not handled", t.Kind()))
300 func (s *state) tryOptions(t reflect.Type, vx, vy reflect.Value) bool {
301 // Evaluate all filters and apply the remaining options.
302 if opt := s.opts.filter(s, t, vx, vy); opt != nil {
309 func (s *state) tryMethod(t reflect.Type, vx, vy reflect.Value) bool {
310 // Check if this type even has an Equal method.
311 m, ok := t.MethodByName("Equal")
312 if !ok || !function.IsType(m.Type, function.EqualAssignable) {
316 eq := s.callTTBFunc(m.Func, vx, vy)
317 s.report(eq, reportByMethod)
321 func (s *state) callTRFunc(f, v reflect.Value, step Transform) reflect.Value {
322 v = sanitizeValue(v, f.Type().In(0))
323 if !s.dynChecker.Next() {
324 return f.Call([]reflect.Value{v})[0]
327 // Run the function twice and ensure that we get the same results back.
328 // We run in goroutines so that the race detector (if enabled) can detect
329 // unsafe mutations to the input.
330 c := make(chan reflect.Value)
331 go detectRaces(c, f, v)
333 want := f.Call([]reflect.Value{v})[0]
334 if step.vx, step.vy = got, want; !s.statelessCompare(step).Equal() {
335 // To avoid false-positives with non-reflexive equality operations,
336 // we sanity check whether a value is equal to itself.
337 if step.vx, step.vy = want, want; !s.statelessCompare(step).Equal() {
340 panic(fmt.Sprintf("non-deterministic function detected: %s", function.NameOf(f)))
345 func (s *state) callTTBFunc(f, x, y reflect.Value) bool {
346 x = sanitizeValue(x, f.Type().In(0))
347 y = sanitizeValue(y, f.Type().In(1))
348 if !s.dynChecker.Next() {
349 return f.Call([]reflect.Value{x, y})[0].Bool()
352 // Swapping the input arguments is sufficient to check that
353 // f is symmetric and deterministic.
354 // We run in goroutines so that the race detector (if enabled) can detect
355 // unsafe mutations to the input.
356 c := make(chan reflect.Value)
357 go detectRaces(c, f, y, x)
359 want := f.Call([]reflect.Value{x, y})[0].Bool()
360 if !got.IsValid() || got.Bool() != want {
361 panic(fmt.Sprintf("non-deterministic or non-symmetric function detected: %s", function.NameOf(f)))
366 func detectRaces(c chan<- reflect.Value, f reflect.Value, vs ...reflect.Value) {
367 var ret reflect.Value
369 recover() // Ignore panics, let the other call to f panic instead
375 // sanitizeValue converts nil interfaces of type T to those of type R,
376 // assuming that T is assignable to R.
377 // Otherwise, it returns the input value as is.
378 func sanitizeValue(v reflect.Value, t reflect.Type) reflect.Value {
379 // TODO(≥go1.10): Workaround for reflect bug (https://golang.org/issue/22143).
380 if !flags.AtLeastGo110 {
381 if v.Kind() == reflect.Interface && v.IsNil() && v.Type() != t {
382 return reflect.New(t).Elem()
388 func (s *state) compareStruct(t reflect.Type, vx, vy reflect.Value) {
390 var vax, vay reflect.Value // Addressable versions of vx and vy
392 var mayForce, mayForceInit bool
393 step := StructField{&structField{}}
394 for i := 0; i < t.NumField(); i++ {
395 step.typ = t.Field(i).Type
396 step.vx = vx.Field(i)
397 step.vy = vy.Field(i)
398 step.name = t.Field(i).Name
400 step.unexported = !isExported(step.name)
402 if step.name == "_" {
405 // Defer checking of unexported fields until later to give an
406 // Ignore a chance to ignore the field.
407 if !vax.IsValid() || !vay.IsValid() {
408 // For retrieveUnexportedField to work, the parent struct must
409 // be addressable. Create a new copy of the values if
410 // necessary to make them addressable.
411 addr = vx.CanAddr() || vy.CanAddr()
412 vax = makeAddressable(vx)
413 vay = makeAddressable(vy)
416 for _, xf := range s.exporters {
417 mayForce = mayForce || xf(t)
421 step.mayForce = mayForce
425 step.field = t.Field(i)
431 func (s *state) compareSlice(t reflect.Type, vx, vy reflect.Value) {
432 isSlice := t.Kind() == reflect.Slice
433 if isSlice && (vx.IsNil() || vy.IsNil()) {
434 s.report(vx.IsNil() && vy.IsNil(), 0)
438 // NOTE: It is incorrect to call curPtrs.Push on the slice header pointer
439 // since slices represents a list of pointers, rather than a single pointer.
440 // The pointer checking logic must be handled on a per-element basis
443 // A slice header (see reflect.SliceHeader) in Go is a tuple of a starting
444 // pointer P, a length N, and a capacity C. Supposing each slice element has
445 // a memory size of M, then the slice is equivalent to the list of pointers:
446 // [P+i*M for i in range(N)]
448 // For example, v[:0] and v[:1] are slices with the same starting pointer,
449 // but they are clearly different values. Using the slice pointer alone
450 // violates the assumption that equal pointers implies equal values.
452 step := SliceIndex{&sliceIndex{pathStep: pathStep{typ: t.Elem()}, isSlice: isSlice}}
453 withIndexes := func(ix, iy int) SliceIndex {
455 step.vx, step.xkey = vx.Index(ix), ix
457 step.vx, step.xkey = reflect.Value{}, -1
460 step.vy, step.ykey = vy.Index(iy), iy
462 step.vy, step.ykey = reflect.Value{}, -1
467 // Ignore options are able to ignore missing elements in a slice.
468 // However, detecting these reliably requires an optimal differencing
469 // algorithm, for which diff.Difference is not.
471 // Instead, we first iterate through both slices to detect which elements
472 // would be ignored if standing alone. The index of non-discarded elements
473 // are stored in a separate slice, which diffing is then performed on.
474 var indexesX, indexesY []int
475 var ignoredX, ignoredY []bool
476 for ix := 0; ix < vx.Len(); ix++ {
477 ignored := s.statelessCompare(withIndexes(ix, -1)).NumDiff == 0
479 indexesX = append(indexesX, ix)
481 ignoredX = append(ignoredX, ignored)
483 for iy := 0; iy < vy.Len(); iy++ {
484 ignored := s.statelessCompare(withIndexes(-1, iy)).NumDiff == 0
486 indexesY = append(indexesY, iy)
488 ignoredY = append(ignoredY, ignored)
491 // Compute an edit-script for slices vx and vy (excluding ignored elements).
492 edits := diff.Difference(len(indexesX), len(indexesY), func(ix, iy int) diff.Result {
493 return s.statelessCompare(withIndexes(indexesX[ix], indexesY[iy]))
496 // Replay the ignore-scripts and the edit-script.
498 for ix < vx.Len() || iy < vy.Len() {
501 case ix < len(ignoredX) && ignoredX[ix]:
503 case iy < len(ignoredY) && ignoredY[iy]:
506 e, edits = edits[0], edits[1:]
510 s.compareAny(withIndexes(ix, -1))
513 s.compareAny(withIndexes(-1, iy))
516 s.compareAny(withIndexes(ix, iy))
523 func (s *state) compareMap(t reflect.Type, vx, vy reflect.Value) {
524 if vx.IsNil() || vy.IsNil() {
525 s.report(vx.IsNil() && vy.IsNil(), 0)
529 // Cycle-detection for maps.
530 if eq, visited := s.curPtrs.Push(vx, vy); visited {
531 s.report(eq, reportByCycle)
534 defer s.curPtrs.Pop(vx, vy)
536 // We combine and sort the two map keys so that we can perform the
537 // comparisons in a deterministic order.
538 step := MapIndex{&mapIndex{pathStep: pathStep{typ: t.Elem()}}}
539 for _, k := range value.SortKeys(append(vx.MapKeys(), vy.MapKeys()...)) {
540 step.vx = vx.MapIndex(k)
541 step.vy = vy.MapIndex(k)
543 if !step.vx.IsValid() && !step.vy.IsValid() {
544 // It is possible for both vx and vy to be invalid if the
545 // key contained a NaN value in it.
547 // Even with the ability to retrieve NaN keys in Go 1.12,
548 // there still isn't a sensible way to compare the values since
549 // a NaN key may map to multiple unordered values.
550 // The most reasonable way to compare NaNs would be to compare the
551 // set of values. However, this is impossible to do efficiently
552 // since set equality is provably an O(n^2) operation given only
553 // an Equal function. If we had a Less function or Hash function,
554 // this could be done in O(n*log(n)) or O(n), respectively.
556 // Rather than adding complex logic to deal with NaNs, make it
557 // the user's responsibility to compare such obscure maps.
558 const help = "consider providing a Comparer to compare the map"
559 panic(fmt.Sprintf("%#v has map key with NaNs\n%s", s.curPath, help))
565 func (s *state) comparePtr(t reflect.Type, vx, vy reflect.Value) {
566 if vx.IsNil() || vy.IsNil() {
567 s.report(vx.IsNil() && vy.IsNil(), 0)
571 // Cycle-detection for pointers.
572 if eq, visited := s.curPtrs.Push(vx, vy); visited {
573 s.report(eq, reportByCycle)
576 defer s.curPtrs.Pop(vx, vy)
578 vx, vy = vx.Elem(), vy.Elem()
579 s.compareAny(Indirect{&indirect{pathStep{t.Elem(), vx, vy}}})
582 func (s *state) compareInterface(t reflect.Type, vx, vy reflect.Value) {
583 if vx.IsNil() || vy.IsNil() {
584 s.report(vx.IsNil() && vy.IsNil(), 0)
587 vx, vy = vx.Elem(), vy.Elem()
588 if vx.Type() != vy.Type() {
592 s.compareAny(TypeAssertion{&typeAssertion{pathStep{vx.Type(), vx, vy}}})
595 func (s *state) report(eq bool, rf resultFlags) {
596 if rf&reportByIgnore == 0 {
605 for _, r := range s.reporters {
606 r.Report(Result{flags: rf})
610 // recChecker tracks the state needed to periodically perform checks that
611 // user provided transformers are not stuck in an infinitely recursive cycle.
612 type recChecker struct{ next int }
614 // Check scans the Path for any recursive transformers and panics when any
615 // recursive transformers are detected. Note that the presence of a
616 // recursive Transformer does not necessarily imply an infinite cycle.
617 // As such, this check only activates after some minimal number of path steps.
618 func (rc *recChecker) Check(p Path) {
619 const minLen = 1 << 16
623 if len(p) < rc.next {
628 // Check whether the same transformer has appeared at least twice.
630 m := map[Option]int{}
631 for _, ps := range p {
632 if t, ok := ps.(Transform); ok {
634 if m[t] == 1 { // Transformer was used exactly once before
635 tf := t.(*transformer).fnc.Type()
636 ss = append(ss, fmt.Sprintf("%v: %v => %v", t, tf.In(0), tf.Out(0)))
642 const warning = "recursive set of Transformers detected"
643 const help = "consider using cmpopts.AcyclicTransformer"
644 set := strings.Join(ss, "\n\t")
645 panic(fmt.Sprintf("%s:\n\t%s\n%s", warning, set, help))
649 // dynChecker tracks the state needed to periodically perform checks that
650 // user provided functions are symmetric and deterministic.
651 // The zero value is safe for immediate use.
652 type dynChecker struct{ curr, next int }
654 // Next increments the state and reports whether a check should be performed.
656 // Checks occur every Nth function call, where N is a triangular number:
657 // 0 1 3 6 10 15 21 28 36 45 55 66 78 91 105 120 136 153 171 190 ...
658 // See https://en.wikipedia.org/wiki/Triangular_number
660 // This sequence ensures that the cost of checks drops significantly as
661 // the number of functions calls grows larger.
662 func (dc *dynChecker) Next() bool {
663 ok := dc.curr == dc.next
672 // makeAddressable returns a value that is always addressable.
673 // It returns the input verbatim if it is already addressable,
674 // otherwise it creates a new value and returns an addressable copy.
675 func makeAddressable(v reflect.Value) reflect.Value {
679 vc := reflect.New(v.Type()).Elem()