Skip to content

Commit

Permalink
Turn a panicking cmp.Diff into a bad check
Browse files Browse the repository at this point in the history
Using Not(DeepEquals) on a type with an unexported field currently
succeeds without an error. This is because the unexported field is
raised as a panic by go-cmp, which is turned into a plain error by
the library. Not then interprets the error to mean "not equal" instead
of forwarding it to the user.

Fix this by turning any go-cmp panics into a bad check.

Fixes #153
  • Loading branch information
lmb committed Apr 3, 2023
1 parent a619a7e commit 636ce19
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
2 changes: 1 addition & 1 deletion checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (c *cmpEqualsChecker) Check(got interface{}, args []interface{}, note func(
// structs with unexported fields and neither AllowUnexported nor
// cmpopts.IgnoreUnexported are provided.
if r := recover(); r != nil {
err = fmt.Errorf("%s", r)
err = BadCheckf("%s", r)
}
}()
want := args[0]
Expand Down
12 changes: 7 additions & 5 deletions checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,13 +537,15 @@ want:
},
expectedCheckFailure: `
error:
cannot handle unexported field at root.answer:
bad check: cannot handle unexported field at root.answer:
"github.com/frankban/quicktest_test".(struct { answer int })
consider using a custom Comparer; if you control the implementation of type, you can also consider using an Exporter, AllowUnexported, or cmpopts.IgnoreUnexported
`,
expectedNegateFailure: `
error:
bad check: cannot handle unexported field at root.answer:
"github.com/frankban/quicktest_test".(struct { answer int })
consider using a custom Comparer; if you control the implementation of type, you can also consider using an Exporter, AllowUnexported, or cmpopts.IgnoreUnexported
got:
struct { answer int }{answer:42}
want:
<same as "got">
`,
}, {
about: "CmpEquals: structs with unexported fields ignored",
Expand Down

0 comments on commit 636ce19

Please sign in to comment.