-
Notifications
You must be signed in to change notification settings - Fork 209
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create a simple example for the IgnoreFields #205
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
86c02c9
Create a simple example for the IgnoreFields
colinnewell 6f9a084
Fixed test issue with non deterministic output
colinnewell 41bf780
Code review fixes
colinnewell cfdd12b
More code review tweaks.
colinnewell 0ae3bec
Used better suggestion of wording from code review
colinnewell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,130 @@ | ||
// 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. | ||
|
||
package cmpopts_test | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"time" | ||
|
||
"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 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. | ||
got, want := MakeGatewayInfo() | ||
|
||
// 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) | ||
} | ||
|
||
// Output: | ||
// MakeGatewayInfo() mismatch (-want +got): | ||
// cmpopts_test.Gateway{ | ||
// SSID: "CoffeeShopWiFi", | ||
// - IPAddress: s"192.168.0.2", | ||
// + IPAddress: s"192.168.0.1", | ||
// NetMask: {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...) } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add license header.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know people aren't fond of 2020, but that's the current year ;)