1 // Copyright 2019, 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.md file.
11 "github.com/google/go-cmp/cmp"
14 // DiffReporter is a simple custom reporter that only records differences
15 // detected during comparison.
16 type DiffReporter struct {
21 func (r *DiffReporter) PushStep(ps cmp.PathStep) {
22 r.path = append(r.path, ps)
25 func (r *DiffReporter) Report(rs cmp.Result) {
27 vx, vy := r.path.Last().Values()
28 r.diffs = append(r.diffs, fmt.Sprintf("%#v:\n\t-: %+v\n\t+: %+v\n", r.path, vx, vy))
32 func (r *DiffReporter) PopStep() {
33 r.path = r.path[:len(r.path)-1]
36 func (r *DiffReporter) String() string {
37 return strings.Join(r.diffs, "\n")
40 func ExampleReporter() {
41 x, y := MakeGatewayInfo()
44 cmp.Equal(x, y, cmp.Reporter(&r))
48 // {cmp_test.Gateway}.IPAddress:
52 // {cmp_test.Gateway}.Clients[4].IPAddress:
56 // {cmp_test.Gateway}.Clients[5->?]:
57 // -: {Hostname:americano IPAddress:192.168.0.188 LastSeen:2009-11-10 23:03:05 +0000 UTC}
58 // +: <invalid reflect.Value>