Skip to content

Commit

Permalink
refactor(test): move maybeAssertPanic to own function
Browse files Browse the repository at this point in the history
  • Loading branch information
costela committed Dec 27, 2023
1 parent aaac6de commit ec15e1b
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions hoglet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,17 +191,22 @@ func TestHoglet_Do(t *testing.T) {
}

var err error
maybeAssertPanic := assert.NotPanics
if call.wantPanic != nil {
maybeAssertPanic = func(t assert.TestingT, f assert.PanicTestFunc, msgAndArgs ...interface{}) bool {
return assert.PanicsWithValue(t, call.wantPanic, f, msgAndArgs...)
}
}
maybeAssertPanic(t, func() {
_, err = h.Call(context.Background(), call.arg)
})
}, call.wantPanic)
assert.Equal(t, call.wantErr, err, "unexpected error on call %d: %v", i, err)
}
})
}
}

// maybeAssertPanic is a test-table helper to assert that a function panics or not, depending on the value of wantPanic.
func maybeAssertPanic(t *testing.T, f func(), wantPanic any) {
wrapped := assert.NotPanics
if wantPanic != nil {
wrapped = func(t assert.TestingT, f assert.PanicTestFunc, msgAndArgs ...interface{}) bool {
return assert.PanicsWithValue(t, wantPanic, f, msgAndArgs...)
}
}
wrapped(t, f)
}

0 comments on commit ec15e1b

Please sign in to comment.