Home | History | Annotate | Download | only in cmp
      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 
      5 // Package cmp determines equality of values.
      6 //
      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 //
     10 // The primary features of cmp are:
     11 //
     12 //  When the default behavior of equality does not suit the needs of the test,
     13 // custom equality functions can override the equality operation.
     14 // For example, an equality function may report floats as equal so long as they
     15 // are within some tolerance of each other.
     16 //
     17 //  Types that have an Equal method may use that method to determine equality.
     18 // This allows package authors to determine the equality operation for the types
     19 // that they define.
     20 //
     21 //  If no custom equality functions are used and no Equal method is defined,
     22 // equality is determined by recursively comparing the primitive kinds on both
     23 // values, much like reflect.DeepEqual. Unlike reflect.DeepEqual, unexported
     24 // fields are not compared by default; they result in panics unless suppressed
     25 // by using an Ignore option (see cmpopts.IgnoreUnexported) or explictly compared
     26 // using the AllowUnexported option.
     27 package cmp
     28 
     29 import (
     30 	"fmt"
     31 	"reflect"
     32 
     33 	"github.com/google/go-cmp/cmp/internal/diff"
     34 	"github.com/google/go-cmp/cmp/internal/function"
     35 	"github.com/google/go-cmp/cmp/internal/value"
     36 )
     37 
     38 // BUG: Maps with keys containing NaN values cannot be properly compared due to
     39 // the reflection package's inability to retrieve such entries. Equal will panic
     40 // anytime it comes across a NaN key, but this behavior may change.
     41 //
     42 // See https://golang.org/issue/11104 for more details.
     43 
     44 var nothing = reflect.Value{}
     45 
     46 // Equal reports whether x and y are equal by recursively applying the
     47 // following rules in the given order to x and y and all of their sub-values:
     48 //
     49 //  If two values are not of the same type, then they are never equal
     50 // and the overall result is false.
     51 //
     52 //  Let S be the set of all Ignore, Transformer, and Comparer options that
     53 // remain after applying all path filters, value filters, and type filters.
     54 // If at least one Ignore exists in S, then the comparison is ignored.
     55 // If the number of Transformer and Comparer options in S is greater than one,
     56 // then Equal panics because it is ambiguous which option to use.
     57 // If S contains a single Transformer, then use that to transform the current
     58 // values and recursively call Equal on the output values.
     59 // If S contains a single Comparer, then use that to compare the current values.
     60 // Otherwise, evaluation proceeds to the next rule.
     61 //
     62 //  If the values have an Equal method of the form "(T) Equal(T) bool" or
     63 // "(T) Equal(I) bool" where T is assignable to I, then use the result of
     64 // x.Equal(y). Otherwise, no such method exists and evaluation proceeds to
     65 // the next rule.
     66 //
     67 //  Lastly, try to compare x and y based on their basic kinds.
     68 // Simple kinds like booleans, integers, floats, complex numbers, strings, and
     69 // channels are compared using the equivalent of the == operator in Go.
     70 // Functions are only equal if they are both nil, otherwise they are unequal.
     71 // Pointers are equal if the underlying values they point to are also equal.
     72 // Interfaces are equal if their underlying concrete values are also equal.
     73 //
     74 // Structs are equal if all of their fields are equal. If a struct contains
     75 // unexported fields, Equal panics unless the AllowUnexported option is used or
     76 // an Ignore option (e.g., cmpopts.IgnoreUnexported) ignores that field.
     77 //
     78 // Arrays, slices, and maps are equal if they are both nil or both non-nil
     79 // with the same length and the elements at each index or key are equal.
     80 // Note that a non-nil empty slice and a nil slice are not equal.
     81 // To equate empty slices and maps, consider using cmpopts.EquateEmpty.
     82 // Map keys are equal according to the == operator.
     83 // To use custom comparisons for map keys, consider using cmpopts.SortMaps.
     84 func Equal(x, y interface{}, opts ...Option) bool {
     85 	s := newState(opts)
     86 	s.compareAny(reflect.ValueOf(x), reflect.ValueOf(y))
     87 	return s.result.Equal()
     88 }
     89 
     90 // Diff returns a human-readable report of the differences between two values.
     91 // It returns an empty string if and only if Equal returns true for the same
     92 // input values and options. The output string will use the "-" symbol to
     93 // indicate elements removed from x, and the "+" symbol to indicate elements
     94 // added to y.
     95 //
     96 // Do not depend on this output being stable.
     97 func Diff(x, y interface{}, opts ...Option) string {
     98 	r := new(defaultReporter)
     99 	opts = Options{Options(opts), r}
    100 	eq := Equal(x, y, opts...)
    101 	d := r.String()
    102 	if (d == "") != eq {
    103 		panic("inconsistent difference and equality results")
    104 	}
    105 	return d
    106 }
    107 
    108 type state struct {
    109 	// These fields represent the "comparison state".
    110 	// Calling statelessCompare must not result in observable changes to these.
    111 	result   diff.Result // The current result of comparison
    112 	curPath  Path        // The current path in the value tree
    113 	reporter reporter    // Optional reporter used for difference formatting
    114 
    115 	// dynChecker triggers pseudo-random checks for option correctness.
    116 	// It is safe for statelessCompare to mutate this value.
    117 	dynChecker dynChecker
    118 
    119 	// These fields, once set by processOption, will not change.
    120 	exporters map[reflect.Type]bool // Set of structs with unexported field visibility
    121 	opts      Options               // List of all fundamental and filter options
    122 }
    123 
    124 func newState(opts []Option) *state {
    125 	s := new(state)
    126 	for _, opt := range opts {
    127 		s.processOption(opt)
    128 	}
    129 	return s
    130 }
    131 
    132 func (s *state) processOption(opt Option) {
    133 	switch opt := opt.(type) {
    134 	case nil:
    135 	case Options:
    136 		for _, o := range opt {
    137 			s.processOption(o)
    138 		}
    139 	case coreOption:
    140 		type filtered interface {
    141 			isFiltered() bool
    142 		}
    143 		if fopt, ok := opt.(filtered); ok && !fopt.isFiltered() {
    144 			panic(fmt.Sprintf("cannot use an unfiltered option: %v", opt))
    145 		}
    146 		s.opts = append(s.opts, opt)
    147 	case visibleStructs:
    148 		if s.exporters == nil {
    149 			s.exporters = make(map[reflect.Type]bool)
    150 		}
    151 		for t := range opt {
    152 			s.exporters[t] = true
    153 		}
    154 	case reporter:
    155 		if s.reporter != nil {
    156 			panic("difference reporter already registered")
    157 		}
    158 		s.reporter = opt
    159 	default:
    160 		panic(fmt.Sprintf("unknown option %T", opt))
    161 	}
    162 }
    163 
    164 // statelessCompare compares two values and returns the result.
    165 // This function is stateless in that it does not alter the current result,
    166 // or output to any registered reporters.
    167 func (s *state) statelessCompare(vx, vy reflect.Value) diff.Result {
    168 	// We do not save and restore the curPath because all of the compareX
    169 	// methods should properly push and pop from the path.
    170 	// It is an implementation bug if the contents of curPath differs from
    171 	// when calling this function to when returning from it.
    172 
    173 	oldResult, oldReporter := s.result, s.reporter
    174 	s.result = diff.Result{} // Reset result
    175 	s.reporter = nil         // Remove reporter to avoid spurious printouts
    176 	s.compareAny(vx, vy)
    177 	res := s.result
    178 	s.result, s.reporter = oldResult, oldReporter
    179 	return res
    180 }
    181 
    182 func (s *state) compareAny(vx, vy reflect.Value) {
    183 	// TODO: Support cyclic data structures.
    184 
    185 	// Rule 0: Differing types are never equal.
    186 	if !vx.IsValid() || !vy.IsValid() {
    187 		s.report(vx.IsValid() == vy.IsValid(), vx, vy)
    188 		return
    189 	}
    190 	if vx.Type() != vy.Type() {
    191 		s.report(false, vx, vy) // Possible for path to be empty
    192 		return
    193 	}
    194 	t := vx.Type()
    195 	if len(s.curPath) == 0 {
    196 		s.curPath.push(&pathStep{typ: t})
    197 		defer s.curPath.pop()
    198 	}
    199 	vx, vy = s.tryExporting(vx, vy)
    200 
    201 	// Rule 1: Check whether an option applies on this node in the value tree.
    202 	if s.tryOptions(vx, vy, t) {
    203 		return
    204 	}
    205 
    206 	// Rule 2: Check whether the type has a valid Equal method.
    207 	if s.tryMethod(vx, vy, t) {
    208 		return
    209 	}
    210 
    211 	// Rule 3: Recursively descend into each value's underlying kind.
    212 	switch t.Kind() {
    213 	case reflect.Bool:
    214 		s.report(vx.Bool() == vy.Bool(), vx, vy)
    215 		return
    216 	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
    217 		s.report(vx.Int() == vy.Int(), vx, vy)
    218 		return
    219 	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
    220 		s.report(vx.Uint() == vy.Uint(), vx, vy)
    221 		return
    222 	case reflect.Float32, reflect.Float64:
    223 		s.report(vx.Float() == vy.Float(), vx, vy)
    224 		return
    225 	case reflect.Complex64, reflect.Complex128:
    226 		s.report(vx.Complex() == vy.Complex(), vx, vy)
    227 		return
    228 	case reflect.String:
    229 		s.report(vx.String() == vy.String(), vx, vy)
    230 		return
    231 	case reflect.Chan, reflect.UnsafePointer:
    232 		s.report(vx.Pointer() == vy.Pointer(), vx, vy)
    233 		return
    234 	case reflect.Func:
    235 		s.report(vx.IsNil() && vy.IsNil(), vx, vy)
    236 		return
    237 	case reflect.Ptr:
    238 		if vx.IsNil() || vy.IsNil() {
    239 			s.report(vx.IsNil() && vy.IsNil(), vx, vy)
    240 			return
    241 		}
    242 		s.curPath.push(&indirect{pathStep{t.Elem()}})
    243 		defer s.curPath.pop()
    244 		s.compareAny(vx.Elem(), vy.Elem())
    245 		return
    246 	case reflect.Interface:
    247 		if vx.IsNil() || vy.IsNil() {
    248 			s.report(vx.IsNil() && vy.IsNil(), vx, vy)
    249 			return
    250 		}
    251 		if vx.Elem().Type() != vy.Elem().Type() {
    252 			s.report(false, vx.Elem(), vy.Elem())
    253 			return
    254 		}
    255 		s.curPath.push(&typeAssertion{pathStep{vx.Elem().Type()}})
    256 		defer s.curPath.pop()
    257 		s.compareAny(vx.Elem(), vy.Elem())
    258 		return
    259 	case reflect.Slice:
    260 		if vx.IsNil() || vy.IsNil() {
    261 			s.report(vx.IsNil() && vy.IsNil(), vx, vy)
    262 			return
    263 		}
    264 		fallthrough
    265 	case reflect.Array:
    266 		s.compareArray(vx, vy, t)
    267 		return
    268 	case reflect.Map:
    269 		s.compareMap(vx, vy, t)
    270 		return
    271 	case reflect.Struct:
    272 		s.compareStruct(vx, vy, t)
    273 		return
    274 	default:
    275 		panic(fmt.Sprintf("%v kind not handled", t.Kind()))
    276 	}
    277 }
    278 
    279 func (s *state) tryExporting(vx, vy reflect.Value) (reflect.Value, reflect.Value) {
    280 	if sf, ok := s.curPath[len(s.curPath)-1].(*structField); ok && sf.unexported {
    281 		if sf.force {
    282 			// Use unsafe pointer arithmetic to get read-write access to an
    283 			// unexported field in the struct.
    284 			vx = unsafeRetrieveField(sf.pvx, sf.field)
    285 			vy = unsafeRetrieveField(sf.pvy, sf.field)
    286 		} else {
    287 			// We are not allowed to export the value, so invalidate them
    288 			// so that tryOptions can panic later if not explicitly ignored.
    289 			vx = nothing
    290 			vy = nothing
    291 		}
    292 	}
    293 	return vx, vy
    294 }
    295 
    296 func (s *state) tryOptions(vx, vy reflect.Value, t reflect.Type) bool {
    297 	// If there were no FilterValues, we will not detect invalid inputs,
    298 	// so manually check for them and append invalid if necessary.
    299 	// We still evaluate the options since an ignore can override invalid.
    300 	opts := s.opts
    301 	if !vx.IsValid() || !vy.IsValid() {
    302 		opts = Options{opts, invalid{}}
    303 	}
    304 
    305 	// Evaluate all filters and apply the remaining options.
    306 	if opt := opts.filter(s, vx, vy, t); opt != nil {
    307 		return opt.apply(s, vx, vy)
    308 	}
    309 	return false
    310 }
    311 
    312 func (s *state) tryMethod(vx, vy reflect.Value, t reflect.Type) bool {
    313 	// Check if this type even has an Equal method.
    314 	m, ok := t.MethodByName("Equal")
    315 	if !ok || !function.IsType(m.Type, function.EqualAssignable) {
    316 		return false
    317 	}
    318 
    319 	eq := s.callTTBFunc(m.Func, vx, vy)
    320 	s.report(eq, vx, vy)
    321 	return true
    322 }
    323 
    324 func (s *state) callTRFunc(f, v reflect.Value) reflect.Value {
    325 	if !s.dynChecker.Next() {
    326 		return f.Call([]reflect.Value{v})[0]
    327 	}
    328 
    329 	// Run the function twice and ensure that we get the same results back.
    330 	// We run in goroutines so that the race detector (if enabled) can detect
    331 	// unsafe mutations to the input.
    332 	c := make(chan reflect.Value)
    333 	go detectRaces(c, f, v)
    334 	want := f.Call([]reflect.Value{v})[0]
    335 	if got := <-c; !s.statelessCompare(got, want).Equal() {
    336 		// To avoid false-positives with non-reflexive equality operations,
    337 		// we sanity check whether a value is equal to itself.
    338 		if !s.statelessCompare(want, want).Equal() {
    339 			return want
    340 		}
    341 		fn := getFuncName(f.Pointer())
    342 		panic(fmt.Sprintf("non-deterministic function detected: %s", fn))
    343 	}
    344 	return want
    345 }
    346 
    347 func (s *state) callTTBFunc(f, x, y reflect.Value) bool {
    348 	if !s.dynChecker.Next() {
    349 		return f.Call([]reflect.Value{x, y})[0].Bool()
    350 	}
    351 
    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)
    358 	want := f.Call([]reflect.Value{x, y})[0].Bool()
    359 	if got := <-c; !got.IsValid() || got.Bool() != want {
    360 		fn := getFuncName(f.Pointer())
    361 		panic(fmt.Sprintf("non-deterministic or non-symmetric function detected: %s", fn))
    362 	}
    363 	return want
    364 }
    365 
    366 func detectRaces(c chan<- reflect.Value, f reflect.Value, vs ...reflect.Value) {
    367 	var ret reflect.Value
    368 	defer func() {
    369 		recover() // Ignore panics, let the other call to f panic instead
    370 		c <- ret
    371 	}()
    372 	ret = f.Call(vs)[0]
    373 }
    374 
    375 func (s *state) compareArray(vx, vy reflect.Value, t reflect.Type) {
    376 	step := &sliceIndex{pathStep{t.Elem()}, 0, 0}
    377 	s.curPath.push(step)
    378 
    379 	// Compute an edit-script for slices vx and vy.
    380 	eq, es := diff.Difference(vx.Len(), vy.Len(), func(ix, iy int) diff.Result {
    381 		step.xkey, step.ykey = ix, iy
    382 		return s.statelessCompare(vx.Index(ix), vy.Index(iy))
    383 	})
    384 
    385 	// Equal or no edit-script, so report entire slices as is.
    386 	if eq || es == nil {
    387 		s.curPath.pop() // Pop first since we are reporting the whole slice
    388 		s.report(eq, vx, vy)
    389 		return
    390 	}
    391 
    392 	// Replay the edit-script.
    393 	var ix, iy int
    394 	for _, e := range es {
    395 		switch e {
    396 		case diff.UniqueX:
    397 			step.xkey, step.ykey = ix, -1
    398 			s.report(false, vx.Index(ix), nothing)
    399 			ix++
    400 		case diff.UniqueY:
    401 			step.xkey, step.ykey = -1, iy
    402 			s.report(false, nothing, vy.Index(iy))
    403 			iy++
    404 		default:
    405 			step.xkey, step.ykey = ix, iy
    406 			if e == diff.Identity {
    407 				s.report(true, vx.Index(ix), vy.Index(iy))
    408 			} else {
    409 				s.compareAny(vx.Index(ix), vy.Index(iy))
    410 			}
    411 			ix++
    412 			iy++
    413 		}
    414 	}
    415 	s.curPath.pop()
    416 	return
    417 }
    418 
    419 func (s *state) compareMap(vx, vy reflect.Value, t reflect.Type) {
    420 	if vx.IsNil() || vy.IsNil() {
    421 		s.report(vx.IsNil() && vy.IsNil(), vx, vy)
    422 		return
    423 	}
    424 
    425 	// We combine and sort the two map keys so that we can perform the
    426 	// comparisons in a deterministic order.
    427 	step := &mapIndex{pathStep: pathStep{t.Elem()}}
    428 	s.curPath.push(step)
    429 	defer s.curPath.pop()
    430 	for _, k := range value.SortKeys(append(vx.MapKeys(), vy.MapKeys()...)) {
    431 		step.key = k
    432 		vvx := vx.MapIndex(k)
    433 		vvy := vy.MapIndex(k)
    434 		switch {
    435 		case vvx.IsValid() && vvy.IsValid():
    436 			s.compareAny(vvx, vvy)
    437 		case vvx.IsValid() && !vvy.IsValid():
    438 			s.report(false, vvx, nothing)
    439 		case !vvx.IsValid() && vvy.IsValid():
    440 			s.report(false, nothing, vvy)
    441 		default:
    442 			// It is possible for both vvx and vvy to be invalid if the
    443 			// key contained a NaN value in it. There is no way in
    444 			// reflection to be able to retrieve these values.
    445 			// See https://golang.org/issue/11104
    446 			panic(fmt.Sprintf("%#v has map key with NaNs", s.curPath))
    447 		}
    448 	}
    449 }
    450 
    451 func (s *state) compareStruct(vx, vy reflect.Value, t reflect.Type) {
    452 	var vax, vay reflect.Value // Addressable versions of vx and vy
    453 
    454 	step := &structField{}
    455 	s.curPath.push(step)
    456 	defer s.curPath.pop()
    457 	for i := 0; i < t.NumField(); i++ {
    458 		vvx := vx.Field(i)
    459 		vvy := vy.Field(i)
    460 		step.typ = t.Field(i).Type
    461 		step.name = t.Field(i).Name
    462 		step.idx = i
    463 		step.unexported = !isExported(step.name)
    464 		if step.unexported {
    465 			// Defer checking of unexported fields until later to give an
    466 			// Ignore a chance to ignore the field.
    467 			if !vax.IsValid() || !vay.IsValid() {
    468 				// For unsafeRetrieveField to work, the parent struct must
    469 				// be addressable. Create a new copy of the values if
    470 				// necessary to make them addressable.
    471 				vax = makeAddressable(vx)
    472 				vay = makeAddressable(vy)
    473 			}
    474 			step.force = s.exporters[t]
    475 			step.pvx = vax
    476 			step.pvy = vay
    477 			step.field = t.Field(i)
    478 		}
    479 		s.compareAny(vvx, vvy)
    480 	}
    481 }
    482 
    483 // report records the result of a single comparison.
    484 // It also calls Report if any reporter is registered.
    485 func (s *state) report(eq bool, vx, vy reflect.Value) {
    486 	if eq {
    487 		s.result.NSame++
    488 	} else {
    489 		s.result.NDiff++
    490 	}
    491 	if s.reporter != nil {
    492 		s.reporter.Report(vx, vy, eq, s.curPath)
    493 	}
    494 }
    495 
    496 // dynChecker tracks the state needed to periodically perform checks that
    497 // user provided functions are symmetric and deterministic.
    498 // The zero value is safe for immediate use.
    499 type dynChecker struct{ curr, next int }
    500 
    501 // Next increments the state and reports whether a check should be performed.
    502 //
    503 // Checks occur every Nth function call, where N is a triangular number:
    504 //	0 1 3 6 10 15 21 28 36 45 55 66 78 91 105 120 136 153 171 190 ...
    505 // See https://en.wikipedia.org/wiki/Triangular_number
    506 //
    507 // This sequence ensures that the cost of checks drops significantly as
    508 // the number of functions calls grows larger.
    509 func (dc *dynChecker) Next() bool {
    510 	ok := dc.curr == dc.next
    511 	if ok {
    512 		dc.curr = 0
    513 		dc.next++
    514 	}
    515 	dc.curr++
    516 	return ok
    517 }
    518 
    519 // makeAddressable returns a value that is always addressable.
    520 // It returns the input verbatim if it is already addressable,
    521 // otherwise it creates a new value and returns an addressable copy.
    522 func makeAddressable(v reflect.Value) reflect.Value {
    523 	if v.CanAddr() {
    524 		return v
    525 	}
    526 	vc := reflect.New(v.Type()).Elem()
    527 	vc.Set(v)
    528 	return vc
    529 }
    530