-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a simple example for the IgnoreFields
- Loading branch information
1 parent
d713870
commit 86c02c9
Showing
1 changed file
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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...) } |