Skip to content

Commit

Permalink
feat: ignore nil argument in Panics
Browse files Browse the repository at this point in the history
  • Loading branch information
tmzane committed Mar 29, 2024
1 parent b5b7ed3 commit 7bac443
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
3 changes: 2 additions & 1 deletion assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,15 @@ func AsErr[T Param](t TB, err error, target any, formatAndArgs ...any) {
}

// Panics asserts that the given function panics with the argument v.
// If v is nil, the panic argument is ignored.
func Panics[T Param](t TB, fn func(), v any, formatAndArgs ...any) {
t.Helper()
defer func() {
t.Helper()
switch r := recover(); {
case r == nil:
fail[T](t, formatAndArgs, "the function didn't panic")
case !reflect.DeepEqual(r, v):
case v != nil && !reflect.DeepEqual(r, v):
fail[T](t, nil, "panic argument mismatch\ngot: %v\nwant: %v", r, v)
}
}()
Expand Down
8 changes: 7 additions & 1 deletion assert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,19 @@ func TestPanics(t *testing.T) {
v: 42,
want: assertCall{helperCalls: 2},
},
"ok [E] (nil argument)": {
fn: assert.Panics[E],
panicFn: func() { panic(42) },
v: nil,
want: assertCall{helperCalls: 2},
},
"fail [E] (didn't panic)": {
fn: assert.Panics[E],
panicFn: func() {},
v: 42,
want: assertCall{helperCalls: 3, errorfCalled: true, message: "the function didn't panic"},
},
"fail [F] (unexpected argument)": {
"fail [F] (argument mismatch)": {
fn: assert.Panics[F],
panicFn: func() { panic(41) },
v: 42,
Expand Down

0 comments on commit 7bac443

Please sign in to comment.