Skip to content

Commit

Permalink
fix(equalto): go-cmp opts were not passed to failure diff (#17)
Browse files Browse the repository at this point in the history
This change passes any given go-cmp Opt instances to the `cmp.Diff` call
and not only to the cmp.Equal function.

This fixes a potential panic that can happen when structs with
unexported fields are given as actual & expected values.
  • Loading branch information
arikkfir authored May 18, 2024
1 parent 7412017 commit 2e914a3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion matchers_equal_to.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func EqualTo(expected ...any) EqualToMatcher {
comparator: func(t T, expected, actual any) {
GetHelper(t).Helper()
if !cmp.Equal(expected, actual, opts...) {
t.Fatalf("Unexpected difference (\"-\" lines are expected values; \"+\" lines are actual values):\n%s", strings.TrimSpace(cmp.Diff(expected, actual)))
t.Fatalf("Unexpected difference (\"-\" lines are expected values; \"+\" lines are actual values):\n%s", strings.TrimSpace(cmp.Diff(expected, actual, opts...)))
}
},
}
Expand Down
23 changes: 23 additions & 0 deletions matchers_equal_to_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"regexp"
"testing"

"github.com/google/go-cmp/cmp/cmpopts"

. "github.com/arikkfir/justest"
)

Expand Down Expand Up @@ -40,6 +42,27 @@ func TestEqualTo(t *testing.T) {
expected: []any{1, 2},
verifier: FailureVerifier(`^Unexpected difference: received 1 actual values and 2 expected values.*`),
},
"Failure diff uses opts": {
actuals: []any{
struct {
Public string
private string
}{
Public: "public-value",
private: "private-value",
},
},
expected: []any{
struct {
Public string
private string
}{Public: "incorrect-value", private: "private-value"},
cmpopts.IgnoreUnexported(struct {
Public string
private string
}{})},
verifier: FailureVerifier(regexp.QuoteMeta(`Unexpected difference ("-" lines are expected values; "+" lines are actual values):`) + "\n.*"),
},
}
for name, tc := range testCases {
tc := tc
Expand Down

0 comments on commit 2e914a3

Please sign in to comment.