From 86c02c97e68da340490e81712bb45da809c38c24 Mon Sep 17 00:00:00 2001 From: Colin Newell Date: Sun, 31 May 2020 15:57:36 +0100 Subject: [PATCH 1/5] Create a simple example for the IgnoreFields --- cmp/cmpopts/example_test.go | 119 ++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 cmp/cmpopts/example_test.go diff --git a/cmp/cmpopts/example_test.go b/cmp/cmpopts/example_test.go new file mode 100644 index 0000000..70ab8f7 --- /dev/null +++ b/cmp/cmpopts/example_test.go @@ -0,0 +1,119 @@ +package cmpopts_test + +import ( + "fmt" + "net" + "time" + + "github.com/google/go-cmp/cmp" + "github.com/google/go-cmp/cmp/cmpopts" +) + +// Use IgnoreFields to ignore fields on a type when comparing. +// Provide an interface of the type, and the field names to ignore. +func ExampleIgnoreFields_testing() { + // Let got be the hypothetical value obtained from some logic under test + // and want be the expected golden data. + got, want := MakeGatewayInfo() + + // Note that the Diff will still potentially show ignored fields as different, + // but only because they are adjacent to other fields that are also different. + if diff := cmp.Diff(want, got, cmpopts.IgnoreFields(Client{}, "IPAddress")); diff != "" { + t.Errorf("MakeGatewayInfo() mismatch (-want +got):\n%s", diff) + } + + // Output: + // MakeGatewayInfo() mismatch (-want +got): + // cmpopts_test.Gateway{ + // SSID: "CoffeeShopWiFi", + // - IPAddress: s"192.168.0.2", + // + IPAddress: s"192.168.0.1", + // NetMask: net.IPMask{0xff, 0xff, 0x00, 0x00}, + // Clients: []cmpopts_test.Client{ + // ... // 3 identical elements + // {Hostname: "espresso", ...}, + // {Hostname: "latte", LastSeen: s"2009-11-10 23:00:23 +0000 UTC", ...}, + // + { + // + Hostname: "americano", + // + IPAddress: s"192.168.0.188", + // + LastSeen: s"2009-11-10 23:03:05 +0000 UTC", + // + }, + // }, + // } +} + +type ( + Gateway struct { + SSID string + IPAddress net.IP + NetMask net.IPMask + Clients []Client + } + Client struct { + Hostname string + IPAddress net.IP + LastSeen time.Time + } +) + +func MakeGatewayInfo() (x, y Gateway) { + x = Gateway{ + SSID: "CoffeeShopWiFi", + IPAddress: net.IPv4(192, 168, 0, 1), + NetMask: net.IPv4Mask(255, 255, 0, 0), + Clients: []Client{{ + Hostname: "ristretto", + IPAddress: net.IPv4(192, 168, 0, 116), + }, { + Hostname: "aribica", + IPAddress: net.IPv4(192, 168, 0, 104), + LastSeen: time.Date(2009, time.November, 10, 23, 6, 32, 0, time.UTC), + }, { + Hostname: "macchiato", + IPAddress: net.IPv4(192, 168, 0, 153), + LastSeen: time.Date(2009, time.November, 10, 23, 39, 43, 0, time.UTC), + }, { + Hostname: "espresso", + IPAddress: net.IPv4(192, 168, 0, 121), + }, { + Hostname: "latte", + IPAddress: net.IPv4(192, 168, 0, 219), + LastSeen: time.Date(2009, time.November, 10, 23, 0, 23, 0, time.UTC), + }, { + Hostname: "americano", + IPAddress: net.IPv4(192, 168, 0, 188), + LastSeen: time.Date(2009, time.November, 10, 23, 3, 5, 0, time.UTC), + }}, + } + y = Gateway{ + SSID: "CoffeeShopWiFi", + IPAddress: net.IPv4(192, 168, 0, 2), + NetMask: net.IPv4Mask(255, 255, 0, 0), + Clients: []Client{{ + Hostname: "ristretto", + IPAddress: net.IPv4(192, 168, 0, 116), + }, { + Hostname: "aribica", + IPAddress: net.IPv4(192, 168, 0, 104), + LastSeen: time.Date(2009, time.November, 10, 23, 6, 32, 0, time.UTC), + }, { + Hostname: "macchiato", + IPAddress: net.IPv4(192, 168, 0, 153), + LastSeen: time.Date(2009, time.November, 10, 23, 39, 43, 0, time.UTC), + }, { + Hostname: "espresso", + IPAddress: net.IPv4(192, 168, 0, 121), + }, { + Hostname: "latte", + IPAddress: net.IPv4(192, 168, 0, 221), + LastSeen: time.Date(2009, time.November, 10, 23, 0, 23, 0, time.UTC), + }}, + } + return x, y +} + +var t fakeT + +type fakeT struct{} + +func (t fakeT) Errorf(format string, args ...interface{}) { fmt.Printf(format+"\n", args...) } From 6f9a084c866ab453942a21b76a2158e96ad6f5d9 Mon Sep 17 00:00:00 2001 From: Colin Newell Date: Sat, 3 Oct 2020 22:39:07 +0100 Subject: [PATCH 2/5] Fixed test issue with non deterministic output --- cmp/cmpopts/example_test.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/cmp/cmpopts/example_test.go b/cmp/cmpopts/example_test.go index 70ab8f7..01a2879 100644 --- a/cmp/cmpopts/example_test.go +++ b/cmp/cmpopts/example_test.go @@ -7,8 +7,13 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" + "github.com/google/go-cmp/cmp/internal/flags" ) +func init() { + flags.Deterministic = true +} + // Use IgnoreFields to ignore fields on a type when comparing. // Provide an interface of the type, and the field names to ignore. func ExampleIgnoreFields_testing() { @@ -28,7 +33,7 @@ func ExampleIgnoreFields_testing() { // SSID: "CoffeeShopWiFi", // - IPAddress: s"192.168.0.2", // + IPAddress: s"192.168.0.1", - // NetMask: net.IPMask{0xff, 0xff, 0x00, 0x00}, + // NetMask: {0xff, 0xff, 0x00, 0x00}, // Clients: []cmpopts_test.Client{ // ... // 3 identical elements // {Hostname: "espresso", ...}, @@ -40,6 +45,7 @@ func ExampleIgnoreFields_testing() { // + }, // }, // } + } type ( From 41bf7808f744ad15a8a68fb9f805ff2f887852c9 Mon Sep 17 00:00:00 2001 From: Colin Newell Date: Sun, 4 Oct 2020 08:07:55 +0100 Subject: [PATCH 3/5] Code review fixes --- cmp/cmpopts/example_test.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/cmp/cmpopts/example_test.go b/cmp/cmpopts/example_test.go index 01a2879..06457c2 100644 --- a/cmp/cmpopts/example_test.go +++ b/cmp/cmpopts/example_test.go @@ -1,3 +1,7 @@ +// Copyright 2017, 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. + package cmpopts_test import ( @@ -15,7 +19,7 @@ func init() { } // Use IgnoreFields to ignore fields on a type when comparing. -// Provide an interface of the type, and the field names to ignore. +// Provide an interface of the type and the field names to ignore. func ExampleIgnoreFields_testing() { // Let got be the hypothetical value obtained from some logic under test // and want be the expected golden data. @@ -45,7 +49,6 @@ func ExampleIgnoreFields_testing() { // + }, // }, // } - } type ( From cfdd12b89a0d8d02ffa2457343069deb10ef1245 Mon Sep 17 00:00:00 2001 From: Colin Newell Date: Sun, 4 Oct 2020 08:40:24 +0100 Subject: [PATCH 4/5] More code review tweaks. Updated copyright year and used clearer language to explain differences showing up. --- cmp/cmpopts/example_test.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/cmp/cmpopts/example_test.go b/cmp/cmpopts/example_test.go index 06457c2..15a68d5 100644 --- a/cmp/cmpopts/example_test.go +++ b/cmp/cmpopts/example_test.go @@ -1,4 +1,4 @@ -// Copyright 2017, The Go Authors. All rights reserved. +// Copyright 2020, 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. @@ -25,8 +25,9 @@ func ExampleIgnoreFields_testing() { // and want be the expected golden data. got, want := MakeGatewayInfo() - // Note that the Diff will still potentially show ignored fields as different, - // but only because they are adjacent to other fields that are also different. + // While the specified fields will be semantically ignored for the comparison, + // the fields may be printed in the diff when displaying entire values + // that are already determined to be different. if diff := cmp.Diff(want, got, cmpopts.IgnoreFields(Client{}, "IPAddress")); diff != "" { t.Errorf("MakeGatewayInfo() mismatch (-want +got):\n%s", diff) } From 0ae3bec36cf309abc60a4bbb8195416570d70895 Mon Sep 17 00:00:00 2001 From: Colin Newell Date: Sun, 4 Oct 2020 08:43:49 +0100 Subject: [PATCH 5/5] Used better suggestion of wording from code review --- cmp/cmpopts/example_test.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cmp/cmpopts/example_test.go b/cmp/cmpopts/example_test.go index 15a68d5..0cf2513 100644 --- a/cmp/cmpopts/example_test.go +++ b/cmp/cmpopts/example_test.go @@ -18,8 +18,9 @@ func init() { flags.Deterministic = true } -// Use IgnoreFields to ignore fields on a type when comparing. -// Provide an interface of the type and the field names to ignore. +// Use IgnoreFields to ignore fields on a struct type when comparing +// by providing a value of the type and the field names to ignore. +// Typically, a zero value of the type is used (e.g., foo.MyStruct{}). func ExampleIgnoreFields_testing() { // Let got be the hypothetical value obtained from some logic under test // and want be the expected golden data.