1 // Copyright 2020, 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 file.
12 "github.com/google/go-cmp/cmp"
13 "github.com/google/go-cmp/cmp/cmpopts"
14 "github.com/google/go-cmp/cmp/internal/flags"
18 flags.Deterministic = true
21 // Use IgnoreFields to ignore fields on a struct type when comparing
22 // by providing a value of the type and the field names to ignore.
23 // Typically, a zero value of the type is used (e.g., foo.MyStruct{}).
24 func ExampleIgnoreFields_testing() {
25 // Let got be the hypothetical value obtained from some logic under test
26 // and want be the expected golden data.
27 got, want := MakeGatewayInfo()
29 // While the specified fields will be semantically ignored for the comparison,
30 // the fields may be printed in the diff when displaying entire values
31 // that are already determined to be different.
32 if diff := cmp.Diff(want, got, cmpopts.IgnoreFields(Client{}, "IPAddress")); diff != "" {
33 t.Errorf("MakeGatewayInfo() mismatch (-want +got):\n%s", diff)
37 // MakeGatewayInfo() mismatch (-want +got):
38 // cmpopts_test.Gateway{
39 // SSID: "CoffeeShopWiFi",
40 // - IPAddress: s"192.168.0.2",
41 // + IPAddress: s"192.168.0.1",
42 // NetMask: {0xff, 0xff, 0x00, 0x00},
43 // Clients: []cmpopts_test.Client{
44 // ... // 3 identical elements
45 // {Hostname: "espresso", ...},
46 // {Hostname: "latte", LastSeen: s"2009-11-10 23:00:23 +0000 UTC", ...},
48 // + Hostname: "americano",
49 // + IPAddress: s"192.168.0.188",
50 // + LastSeen: s"2009-11-10 23:03:05 +0000 UTC",
70 func MakeGatewayInfo() (x, y Gateway) {
72 SSID: "CoffeeShopWiFi",
73 IPAddress: net.IPv4(192, 168, 0, 1),
74 NetMask: net.IPv4Mask(255, 255, 0, 0),
76 Hostname: "ristretto",
77 IPAddress: net.IPv4(192, 168, 0, 116),
80 IPAddress: net.IPv4(192, 168, 0, 104),
81 LastSeen: time.Date(2009, time.November, 10, 23, 6, 32, 0, time.UTC),
83 Hostname: "macchiato",
84 IPAddress: net.IPv4(192, 168, 0, 153),
85 LastSeen: time.Date(2009, time.November, 10, 23, 39, 43, 0, time.UTC),
88 IPAddress: net.IPv4(192, 168, 0, 121),
91 IPAddress: net.IPv4(192, 168, 0, 219),
92 LastSeen: time.Date(2009, time.November, 10, 23, 0, 23, 0, time.UTC),
94 Hostname: "americano",
95 IPAddress: net.IPv4(192, 168, 0, 188),
96 LastSeen: time.Date(2009, time.November, 10, 23, 3, 5, 0, time.UTC),
100 SSID: "CoffeeShopWiFi",
101 IPAddress: net.IPv4(192, 168, 0, 2),
102 NetMask: net.IPv4Mask(255, 255, 0, 0),
104 Hostname: "ristretto",
105 IPAddress: net.IPv4(192, 168, 0, 116),
108 IPAddress: net.IPv4(192, 168, 0, 104),
109 LastSeen: time.Date(2009, time.November, 10, 23, 6, 32, 0, time.UTC),
111 Hostname: "macchiato",
112 IPAddress: net.IPv4(192, 168, 0, 153),
113 LastSeen: time.Date(2009, time.November, 10, 23, 39, 43, 0, time.UTC),
115 Hostname: "espresso",
116 IPAddress: net.IPv4(192, 168, 0, 121),
119 IPAddress: net.IPv4(192, 168, 0, 221),
120 LastSeen: time.Date(2009, time.November, 10, 23, 0, 23, 0, time.UTC),
130 func (t fakeT) Errorf(format string, args ...interface{}) { fmt.Printf(format+"\n", args...) }