diff options
Diffstat (limited to 'cmp/cmpopts')
-rw-r--r-- | cmp/cmpopts/equate.go | 8 | ||||
-rw-r--r-- | cmp/cmpopts/errors_go113.go | 16 | ||||
-rw-r--r-- | cmp/cmpopts/errors_xerrors.go | 19 | ||||
-rw-r--r-- | cmp/cmpopts/example_test.go | 2 | ||||
-rw-r--r-- | cmp/cmpopts/sort.go | 14 | ||||
-rw-r--r-- | cmp/cmpopts/struct_filter.go | 2 | ||||
-rw-r--r-- | cmp/cmpopts/util_test.go | 15 | ||||
-rw-r--r-- | cmp/cmpopts/xform.go | 1 |
8 files changed, 26 insertions, 51 deletions
diff --git a/cmp/cmpopts/equate.go b/cmp/cmpopts/equate.go index 62837c9..e54a76c 100644 --- a/cmp/cmpopts/equate.go +++ b/cmp/cmpopts/equate.go @@ -6,6 +6,7 @@ package cmpopts import ( + "errors" "math" "reflect" "time" @@ -41,6 +42,7 @@ func isEmpty(x, y interface{}) bool { // The fraction and margin must be non-negative. // // The mathematical expression used is equivalent to: +// // |x-y| ≤ max(fraction*min(|x|, |y|), margin) // // EquateApprox can be used in conjunction with EquateNaNs. @@ -146,3 +148,9 @@ func areConcreteErrors(x, y interface{}) bool { _, ok2 := y.(error) return ok1 && ok2 } + +func compareErrors(x, y interface{}) bool { + xe := x.(error) + ye := y.(error) + return errors.Is(xe, ye) || errors.Is(ye, xe) +} diff --git a/cmp/cmpopts/errors_go113.go b/cmp/cmpopts/errors_go113.go deleted file mode 100644 index 8eb2b84..0000000 --- a/cmp/cmpopts/errors_go113.go +++ /dev/null @@ -1,16 +0,0 @@ -// Copyright 2021, The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -//go:build go1.13 -// +build go1.13 - -package cmpopts - -import "errors" - -func compareErrors(x, y interface{}) bool { - xe := x.(error) - ye := y.(error) - return errors.Is(xe, ye) || errors.Is(ye, xe) -} diff --git a/cmp/cmpopts/errors_xerrors.go b/cmp/cmpopts/errors_xerrors.go deleted file mode 100644 index 60b0727..0000000 --- a/cmp/cmpopts/errors_xerrors.go +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright 2021, The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -//go:build !go1.13 -// +build !go1.13 - -// TODO(≥go1.13): For support on <go1.13, we use the xerrors package. -// Drop this file when we no longer support older Go versions. - -package cmpopts - -import "golang.org/x/xerrors" - -func compareErrors(x, y interface{}) bool { - xe := x.(error) - ye := y.(error) - return xerrors.Is(xe, ye) || xerrors.Is(ye, xe) -} diff --git a/cmp/cmpopts/example_test.go b/cmp/cmpopts/example_test.go index 0cf2513..4b9a8ab 100644 --- a/cmp/cmpopts/example_test.go +++ b/cmp/cmpopts/example_test.go @@ -39,7 +39,7 @@ func ExampleIgnoreFields_testing() { // SSID: "CoffeeShopWiFi", // - IPAddress: s"192.168.0.2", // + IPAddress: s"192.168.0.1", - // NetMask: {0xff, 0xff, 0x00, 0x00}, + // NetMask: s"ffff0000", // Clients: []cmpopts_test.Client{ // ... // 3 identical elements // {Hostname: "espresso", ...}, diff --git a/cmp/cmpopts/sort.go b/cmp/cmpopts/sort.go index a646d74..0eb2a75 100644 --- a/cmp/cmpopts/sort.go +++ b/cmp/cmpopts/sort.go @@ -18,9 +18,9 @@ import ( // sort any slice with element type V that is assignable to T. // // The less function must be: -// • Deterministic: less(x, y) == less(x, y) -// • Irreflexive: !less(x, x) -// • Transitive: if !less(x, y) and !less(y, z), then !less(x, z) +// - Deterministic: less(x, y) == less(x, y) +// - Irreflexive: !less(x, x) +// - Transitive: if !less(x, y) and !less(y, z), then !less(x, z) // // The less function does not have to be "total". That is, if !less(x, y) and // !less(y, x) for two elements x and y, their relative order is maintained. @@ -91,10 +91,10 @@ func (ss sliceSorter) less(v reflect.Value, i, j int) bool { // use Comparers on K or the K.Equal method if it exists. // // The less function must be: -// • Deterministic: less(x, y) == less(x, y) -// • Irreflexive: !less(x, x) -// • Transitive: if !less(x, y) and !less(y, z), then !less(x, z) -// • Total: if x != y, then either less(x, y) or less(y, x) +// - Deterministic: less(x, y) == less(x, y) +// - Irreflexive: !less(x, x) +// - Transitive: if !less(x, y) and !less(y, z), then !less(x, z) +// - Total: if x != y, then either less(x, y) or less(y, x) // // SortMaps can be used in conjunction with EquateEmpty. func SortMaps(lessFunc interface{}) cmp.Option { diff --git a/cmp/cmpopts/struct_filter.go b/cmp/cmpopts/struct_filter.go index a09829c..ca11a40 100644 --- a/cmp/cmpopts/struct_filter.go +++ b/cmp/cmpopts/struct_filter.go @@ -67,12 +67,14 @@ func (sf structFilter) filter(p cmp.Path) bool { // fieldTree represents a set of dot-separated identifiers. // // For example, inserting the following selectors: +// // Foo // Foo.Bar.Baz // Foo.Buzz // Nuka.Cola.Quantum // // Results in a tree of the form: +// // {sub: { // "Foo": {ok: true, sub: { // "Bar": {sub: { diff --git a/cmp/cmpopts/util_test.go b/cmp/cmpopts/util_test.go index b19bcab..7adeb9b 100644 --- a/cmp/cmpopts/util_test.go +++ b/cmp/cmpopts/util_test.go @@ -17,7 +17,6 @@ import ( "time" "github.com/google/go-cmp/cmp" - "golang.org/x/xerrors" ) type ( @@ -531,14 +530,14 @@ func TestOptions(t *testing.T) { reason: "user-defined EOF is not exactly equal", }, { label: "EquateErrors", - x: xerrors.Errorf("wrapped: %w", io.EOF), + x: fmt.Errorf("wrapped: %w", io.EOF), y: io.EOF, opts: []cmp.Option{EquateErrors()}, wantEqual: true, reason: "wrapped io.EOF is equal according to errors.Is", }, { label: "EquateErrors", - x: xerrors.Errorf("wrapped: %w", io.EOF), + x: fmt.Errorf("wrapped: %w", io.EOF), y: io.EOF, wantEqual: false, reason: "wrapped io.EOF is not equal without EquateErrors option", @@ -585,14 +584,14 @@ func TestOptions(t *testing.T) { reason: "user-defined EOF is not exactly equal", }, { label: "EquateErrors", - x: xerrors.Errorf("wrapped: %w", io.EOF), + x: fmt.Errorf("wrapped: %w", io.EOF), y: io.EOF, opts: []cmp.Option{EquateErrors()}, wantEqual: true, reason: "wrapped io.EOF is equal according to errors.Is", }, { label: "EquateErrors", - x: xerrors.Errorf("wrapped: %w", io.EOF), + x: fmt.Errorf("wrapped: %w", io.EOF), y: io.EOF, wantEqual: false, reason: "wrapped io.EOF is not equal without EquateErrors option", @@ -639,14 +638,14 @@ func TestOptions(t *testing.T) { reason: "user-defined EOF is not exactly equal", }, { label: "EquateErrors", - x: struct{ E error }{xerrors.Errorf("wrapped: %w", io.EOF)}, + x: struct{ E error }{fmt.Errorf("wrapped: %w", io.EOF)}, y: struct{ E error }{io.EOF}, opts: []cmp.Option{EquateErrors()}, wantEqual: true, reason: "wrapped io.EOF is equal according to errors.Is", }, { label: "EquateErrors", - x: struct{ E error }{xerrors.Errorf("wrapped: %w", io.EOF)}, + x: struct{ E error }{fmt.Errorf("wrapped: %w", io.EOF)}, y: struct{ E error }{io.EOF}, wantEqual: false, reason: "wrapped io.EOF is not equal without EquateErrors option", @@ -1073,7 +1072,7 @@ func TestOptions(t *testing.T) { }, { label: "AcyclicTransformer", x: "this is a sentence", - y: "this is a sentence", + y: "this is a sentence", opts: []cmp.Option{ AcyclicTransformer("", strings.Fields), }, diff --git a/cmp/cmpopts/xform.go b/cmp/cmpopts/xform.go index 4eb49d6..8812443 100644 --- a/cmp/cmpopts/xform.go +++ b/cmp/cmpopts/xform.go @@ -23,6 +23,7 @@ func (xf xformFilter) filter(p cmp.Path) bool { // that the transformer cannot be recursively applied upon its own output. // // An example use case is a transformer that splits a string by lines: +// // AcyclicTransformer("SplitLines", func(s string) []string{ // return strings.Split(s, "\n") // }) |