|
| 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. |
| 4 | + |
| 5 | +package cmpopts_test |
| 6 | + |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | + "net" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/google/go-cmp/cmp" |
| 13 | + "github.com/google/go-cmp/cmp/cmpopts" |
| 14 | + "github.com/google/go-cmp/cmp/internal/flags" |
| 15 | +) |
| 16 | + |
| 17 | +func init() { |
| 18 | + flags.Deterministic = true |
| 19 | +} |
| 20 | + |
| 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() |
| 28 | + |
| 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) |
| 34 | + } |
| 35 | + |
| 36 | + // Output: |
| 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", ...}, |
| 47 | + // + { |
| 48 | + // + Hostname: "americano", |
| 49 | + // + IPAddress: s"192.168.0.188", |
| 50 | + // + LastSeen: s"2009-11-10 23:03:05 +0000 UTC", |
| 51 | + // + }, |
| 52 | + // }, |
| 53 | + // } |
| 54 | +} |
| 55 | + |
| 56 | +type ( |
| 57 | + Gateway struct { |
| 58 | + SSID string |
| 59 | + IPAddress net.IP |
| 60 | + NetMask net.IPMask |
| 61 | + Clients []Client |
| 62 | + } |
| 63 | + Client struct { |
| 64 | + Hostname string |
| 65 | + IPAddress net.IP |
| 66 | + LastSeen time.Time |
| 67 | + } |
| 68 | +) |
| 69 | + |
| 70 | +func MakeGatewayInfo() (x, y Gateway) { |
| 71 | + x = Gateway{ |
| 72 | + SSID: "CoffeeShopWiFi", |
| 73 | + IPAddress: net.IPv4(192, 168, 0, 1), |
| 74 | + NetMask: net.IPv4Mask(255, 255, 0, 0), |
| 75 | + Clients: []Client{{ |
| 76 | + Hostname: "ristretto", |
| 77 | + IPAddress: net.IPv4(192, 168, 0, 116), |
| 78 | + }, { |
| 79 | + Hostname: "aribica", |
| 80 | + IPAddress: net.IPv4(192, 168, 0, 104), |
| 81 | + LastSeen: time.Date(2009, time.November, 10, 23, 6, 32, 0, time.UTC), |
| 82 | + }, { |
| 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), |
| 86 | + }, { |
| 87 | + Hostname: "espresso", |
| 88 | + IPAddress: net.IPv4(192, 168, 0, 121), |
| 89 | + }, { |
| 90 | + Hostname: "latte", |
| 91 | + IPAddress: net.IPv4(192, 168, 0, 219), |
| 92 | + LastSeen: time.Date(2009, time.November, 10, 23, 0, 23, 0, time.UTC), |
| 93 | + }, { |
| 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), |
| 97 | + }}, |
| 98 | + } |
| 99 | + y = Gateway{ |
| 100 | + SSID: "CoffeeShopWiFi", |
| 101 | + IPAddress: net.IPv4(192, 168, 0, 2), |
| 102 | + NetMask: net.IPv4Mask(255, 255, 0, 0), |
| 103 | + Clients: []Client{{ |
| 104 | + Hostname: "ristretto", |
| 105 | + IPAddress: net.IPv4(192, 168, 0, 116), |
| 106 | + }, { |
| 107 | + Hostname: "aribica", |
| 108 | + IPAddress: net.IPv4(192, 168, 0, 104), |
| 109 | + LastSeen: time.Date(2009, time.November, 10, 23, 6, 32, 0, time.UTC), |
| 110 | + }, { |
| 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), |
| 114 | + }, { |
| 115 | + Hostname: "espresso", |
| 116 | + IPAddress: net.IPv4(192, 168, 0, 121), |
| 117 | + }, { |
| 118 | + Hostname: "latte", |
| 119 | + IPAddress: net.IPv4(192, 168, 0, 221), |
| 120 | + LastSeen: time.Date(2009, time.November, 10, 23, 0, 23, 0, time.UTC), |
| 121 | + }}, |
| 122 | + } |
| 123 | + return x, y |
| 124 | +} |
| 125 | + |
| 126 | +var t fakeT |
| 127 | + |
| 128 | +type fakeT struct{} |
| 129 | + |
| 130 | +func (t fakeT) Errorf(format string, args ...interface{}) { fmt.Printf(format+"\n", args...) } |
0 commit comments