| 1 |
// Package assert contains code from the examples |
| 2 |
// https://pkg.go.dev/github.com/google/go-cmp/cmp?tab=doc#Reporter |
| 3 |
package assert |
| 4 |
|
| 5 |
import ( |
| 6 |
"fmt" |
| 7 |
"strings" |
| 8 |
|
| 9 |
"github.com/google/go-cmp/cmp" |
| 10 |
) |
| 11 |
|
| 12 |
// DiffReporter is a simple custom reporter that only records differences |
| 13 |
// detected during comparison. |
| 14 |
type DiffReporter struct { |
| 15 |
path cmp.Path |
| 16 |
diffs []string |
| 17 |
} |
| 18 |
|
| 19 |
func (r *DiffReporter) PushStep(ps cmp.PathStep) { |
| 20 |
r.path = append(r.path, ps) |
| 21 |
} |
| 22 |
|
| 23 |
func (r *DiffReporter) Report(rs cmp.Result) { |
| 24 |
if !rs.Equal() { |
| 25 |
vx, vy := r.path.Last().Values() |
| 26 |
r.diffs = append(r.diffs, fmt.Sprintf("%#v:\n\t-: %+v\n\t+: %+v\n", r.path, vx, vy)) |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
func (r *DiffReporter) PopStep() { |
| 31 |
r.path = r.path[:len(r.path)-1] |
| 32 |
} |
| 33 |
|
| 34 |
func (r *DiffReporter) String() string { |
| 35 |
return strings.Join(r.diffs, "\n") |
| 36 |
} |