You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

60 lines
1.3 KiB

  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.
  4. package cmp_test
  5. import (
  6. "fmt"
  7. "strings"
  8. "github.com/google/go-cmp/cmp"
  9. )
  10. // DiffReporter is a simple custom reporter that only records differences
  11. // detected during comparison.
  12. type DiffReporter struct {
  13. path cmp.Path
  14. diffs []string
  15. }
  16. func (r *DiffReporter) PushStep(ps cmp.PathStep) {
  17. r.path = append(r.path, ps)
  18. }
  19. func (r *DiffReporter) Report(rs cmp.Result) {
  20. if !rs.Equal() {
  21. vx, vy := r.path.Last().Values()
  22. r.diffs = append(r.diffs, fmt.Sprintf("%#v:\n\t-: %+v\n\t+: %+v\n", r.path, vx, vy))
  23. }
  24. }
  25. func (r *DiffReporter) PopStep() {
  26. r.path = r.path[:len(r.path)-1]
  27. }
  28. func (r *DiffReporter) String() string {
  29. return strings.Join(r.diffs, "\n")
  30. }
  31. func ExampleReporter() {
  32. x, y := MakeGatewayInfo()
  33. var r DiffReporter
  34. cmp.Equal(x, y, cmp.Reporter(&r))
  35. fmt.Print(r.String())
  36. // Output:
  37. // {cmp_test.Gateway}.IPAddress:
  38. // -: 192.168.0.1
  39. // +: 192.168.0.2
  40. //
  41. // {cmp_test.Gateway}.Clients[4].IPAddress:
  42. // -: 192.168.0.219
  43. // +: 192.168.0.221
  44. //
  45. // {cmp_test.Gateway}.Clients[5->?]:
  46. // -: {Hostname:americano IPAddress:192.168.0.188 LastSeen:2009-11-10 23:03:05 +0000 UTC}
  47. // +: <invalid reflect.Value>
  48. }