Skip to content

Commit 86c02c9

Browse files
committed
Create a simple example for the IgnoreFields
1 parent d713870 commit 86c02c9

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed

cmp/cmpopts/example_test.go

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package cmpopts_test
2+
3+
import (
4+
"fmt"
5+
"net"
6+
"time"
7+
8+
"github.com/google/go-cmp/cmp"
9+
"github.com/google/go-cmp/cmp/cmpopts"
10+
)
11+
12+
// Use IgnoreFields to ignore fields on a type when comparing.
13+
// Provide an interface of the type, and the field names to ignore.
14+
func ExampleIgnoreFields_testing() {
15+
// Let got be the hypothetical value obtained from some logic under test
16+
// and want be the expected golden data.
17+
got, want := MakeGatewayInfo()
18+
19+
// Note that the Diff will still potentially show ignored fields as different,
20+
// but only because they are adjacent to other fields that are also different.
21+
if diff := cmp.Diff(want, got, cmpopts.IgnoreFields(Client{}, "IPAddress")); diff != "" {
22+
t.Errorf("MakeGatewayInfo() mismatch (-want +got):\n%s", diff)
23+
}
24+
25+
// Output:
26+
// MakeGatewayInfo() mismatch (-want +got):
27+
// cmpopts_test.Gateway{
28+
// SSID: "CoffeeShopWiFi",
29+
// - IPAddress: s"192.168.0.2",
30+
// + IPAddress: s"192.168.0.1",
31+
// NetMask: net.IPMask{0xff, 0xff, 0x00, 0x00},
32+
// Clients: []cmpopts_test.Client{
33+
// ... // 3 identical elements
34+
// {Hostname: "espresso", ...},
35+
// {Hostname: "latte", LastSeen: s"2009-11-10 23:00:23 +0000 UTC", ...},
36+
// + {
37+
// + Hostname: "americano",
38+
// + IPAddress: s"192.168.0.188",
39+
// + LastSeen: s"2009-11-10 23:03:05 +0000 UTC",
40+
// + },
41+
// },
42+
// }
43+
}
44+
45+
type (
46+
Gateway struct {
47+
SSID string
48+
IPAddress net.IP
49+
NetMask net.IPMask
50+
Clients []Client
51+
}
52+
Client struct {
53+
Hostname string
54+
IPAddress net.IP
55+
LastSeen time.Time
56+
}
57+
)
58+
59+
func MakeGatewayInfo() (x, y Gateway) {
60+
x = Gateway{
61+
SSID: "CoffeeShopWiFi",
62+
IPAddress: net.IPv4(192, 168, 0, 1),
63+
NetMask: net.IPv4Mask(255, 255, 0, 0),
64+
Clients: []Client{{
65+
Hostname: "ristretto",
66+
IPAddress: net.IPv4(192, 168, 0, 116),
67+
}, {
68+
Hostname: "aribica",
69+
IPAddress: net.IPv4(192, 168, 0, 104),
70+
LastSeen: time.Date(2009, time.November, 10, 23, 6, 32, 0, time.UTC),
71+
}, {
72+
Hostname: "macchiato",
73+
IPAddress: net.IPv4(192, 168, 0, 153),
74+
LastSeen: time.Date(2009, time.November, 10, 23, 39, 43, 0, time.UTC),
75+
}, {
76+
Hostname: "espresso",
77+
IPAddress: net.IPv4(192, 168, 0, 121),
78+
}, {
79+
Hostname: "latte",
80+
IPAddress: net.IPv4(192, 168, 0, 219),
81+
LastSeen: time.Date(2009, time.November, 10, 23, 0, 23, 0, time.UTC),
82+
}, {
83+
Hostname: "americano",
84+
IPAddress: net.IPv4(192, 168, 0, 188),
85+
LastSeen: time.Date(2009, time.November, 10, 23, 3, 5, 0, time.UTC),
86+
}},
87+
}
88+
y = Gateway{
89+
SSID: "CoffeeShopWiFi",
90+
IPAddress: net.IPv4(192, 168, 0, 2),
91+
NetMask: net.IPv4Mask(255, 255, 0, 0),
92+
Clients: []Client{{
93+
Hostname: "ristretto",
94+
IPAddress: net.IPv4(192, 168, 0, 116),
95+
}, {
96+
Hostname: "aribica",
97+
IPAddress: net.IPv4(192, 168, 0, 104),
98+
LastSeen: time.Date(2009, time.November, 10, 23, 6, 32, 0, time.UTC),
99+
}, {
100+
Hostname: "macchiato",
101+
IPAddress: net.IPv4(192, 168, 0, 153),
102+
LastSeen: time.Date(2009, time.November, 10, 23, 39, 43, 0, time.UTC),
103+
}, {
104+
Hostname: "espresso",
105+
IPAddress: net.IPv4(192, 168, 0, 121),
106+
}, {
107+
Hostname: "latte",
108+
IPAddress: net.IPv4(192, 168, 0, 221),
109+
LastSeen: time.Date(2009, time.November, 10, 23, 0, 23, 0, time.UTC),
110+
}},
111+
}
112+
return x, y
113+
}
114+
115+
var t fakeT
116+
117+
type fakeT struct{}
118+
119+
func (t fakeT) Errorf(format string, args ...interface{}) { fmt.Printf(format+"\n", args...) }

0 commit comments

Comments
 (0)