Skip to content

Commit

Permalink
Use go-cmp and protocmp for assertion diff printing (#14978)
Browse files Browse the repository at this point in the history
Fix assertion tests after switching to cmp

Changelog fragment
  • Loading branch information
prestonvanloon authored Feb 23, 2025
1 parent ffc1bf8 commit 2ee0154
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 14 deletions.
3 changes: 3 additions & 0 deletions changelog/pvl_go-cmp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Changed

- Use go-cmp for printing better diffs for assertions.DeepEqual
2 changes: 2 additions & 0 deletions testing/assertions/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ go_library(
deps = [
"//encoding/ssz/equality:go_default_library",
"@com_github_d4l3k_messagediff//:go_default_library",
"@com_github_google_go_cmp//cmp:go_default_library",
"@com_github_sirupsen_logrus//hooks/test:go_default_library",
"@org_golang_google_protobuf//proto:go_default_library",
"@org_golang_google_protobuf//testing/protocmp:go_default_library",
],
)

Expand Down
11 changes: 6 additions & 5 deletions testing/assertions/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import (
"strings"

"github.com/d4l3k/messagediff"
"github.com/google/go-cmp/cmp"
"github.com/prysmaticlabs/prysm/v5/encoding/ssz/equality"
"github.com/sirupsen/logrus/hooks/test"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/testing/protocmp"
)

// AssertionTestingTB exposes enough testing.TB methods for assertions.
Expand Down Expand Up @@ -52,13 +54,12 @@ func DeepEqual(loggerFn assertionLoggerFn, expected, actual interface{}, msg ...
if !isDeepEqual(expected, actual) {
errMsg := parseMsg("Values are not equal", msg...)
_, file, line, _ := runtime.Caller(2)
var diff string
opts := cmp.Options{cmp.AllowUnexported(expected), cmp.AllowUnexported(actual)}
if _, isProto := expected.(proto.Message); isProto {
diff = ProtobufPrettyDiff(expected, actual)
} else {
diff, _ = messagediff.PrettyDiff(expected, actual)
opts = append(opts, protocmp.Transform())
}
loggerFn("%s:%d %s, want: %#v, got: %#v, diff: %s", filepath.Base(file), line, errMsg, expected, actual, diff)
diff := cmp.Diff(expected, actual, opts...)
loggerFn("%s:%d %s, expected != actual, diff: %s", filepath.Base(file), line, errMsg, diff)
}
}

Expand Down
34 changes: 25 additions & 9 deletions testing/assertions/assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"strings"
"testing"
"unicode"

eth "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1"
testpb "github.com/prysmaticlabs/prysm/v5/proto/testing"
Expand Down Expand Up @@ -188,7 +189,7 @@ func TestAssert_DeepEqual(t *testing.T) {
expected: struct{ i int }{42},
actual: struct{ i int }{41},
},
expectedErr: "Values are not equal, want: struct { i int }{i:42}, got: struct { i int }{i:41}",
expectedErr: "Values are not equal, expected != actual, diff: struct{ i int }{\n- \ti: 42,\n+ \ti: 41,\n }\n",
},
{
name: "custom error message",
Expand All @@ -198,7 +199,7 @@ func TestAssert_DeepEqual(t *testing.T) {
actual: struct{ i int }{41},
msgs: []interface{}{"Custom values are not equal"},
},
expectedErr: "Custom values are not equal, want: struct { i int }{i:42}, got: struct { i int }{i:41}",
expectedErr: "Custom values are not equal, expected != actual, diff: struct{ i int }{\n- \ti: 42,\n+ \ti: 41,\n }",
},
{
name: "custom error message with params",
Expand All @@ -208,24 +209,39 @@ func TestAssert_DeepEqual(t *testing.T) {
actual: struct{ i int }{41},
msgs: []interface{}{"Custom values are not equal (for slot %d)", 12},
},
expectedErr: "Custom values are not equal (for slot 12), want: struct { i int }{i:42}, got: struct { i int }{i:41}",
expectedErr: "Custom values are not equal (for slot 12), expected != actual, diff: struct{ i int }{\n- \ti: 42,\n+ \ti: 41,\n }\n",
},
}
for _, tt := range tests {
verify := func() {
if tt.expectedErr == "" && tt.args.tb.ErrorfMsg != "" {
t.Errorf("Unexpected error: %v", tt.args.tb.ErrorfMsg)
} else if !strings.Contains(tt.args.tb.ErrorfMsg, tt.expectedErr) {
verify := func(t testing.TB) {
// Trim unicode space characters for an easier comparison.
got := strings.Map(func(r rune) rune {
if unicode.IsSpace(r) {
return -1
}
return r
}, tt.args.tb.ErrorfMsg)
want := strings.Map(func(r rune) rune {
if unicode.IsSpace(r) {
return -1
}
return r
}, tt.expectedErr)
if want == "" && got != "" {
t.Errorf("Unexpected error: %v", got)
} else if !strings.Contains(got, want) {
t.Logf("got=%q", got)
t.Logf("want=%q", want)
t.Errorf("got: %q, want: %q", tt.args.tb.ErrorfMsg, tt.expectedErr)
}
}
t.Run(fmt.Sprintf("Assert/%s", tt.name), func(t *testing.T) {
assert.DeepEqual(tt.args.tb, tt.args.expected, tt.args.actual, tt.args.msgs...)
verify()
verify(t)
})
t.Run(fmt.Sprintf("Require/%s", tt.name), func(t *testing.T) {
require.DeepEqual(tt.args.tb, tt.args.expected, tt.args.actual, tt.args.msgs...)
verify()
verify(t)
})
}
}
Expand Down

0 comments on commit 2ee0154

Please sign in to comment.