Skip to content

Commit c90a919

Browse files
committed
feat: add PanicOf.
1 parent ba6bfed commit c90a919

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed

builtin.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,13 @@ func NotPanicNow(t *testing.T, fn func(), message ...any) error {
831831
return tryNotPanic(t, true, fn, message...)
832832
}
833833

834+
// PanicOf expects the function fn to panic by the expected error.
835+
func PanicOf(t *testing.T, fn func(), expectErr any, message ...any) error {
836+
t.Helper()
837+
838+
return tryPanicOf(t, false, fn, expectErr, message...)
839+
}
840+
834841
// True tests whether a value is truthy or not. It'll set the result to fail if the value is a
835842
// false value. For most types of value, a falsy value is the zero value for its type. For a
836843
// slice, a truthy value should not be nil, and its length must be greater than 0. For nil, the

error.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const (
2222
defaultErrMessageNotNil string = "expect not nil, got nil"
2323
defaultErrMessagePanic string = "missing expected panic"
2424
defaultErrMessageNotPanic string = "got unwanted error: %v"
25+
defaultErrMessagePanicOf string = "expect panic by %v, got %v"
2526
defaultErrMessageTrue string = "the expression evaluated to a falsy value"
2627
defaultErrMessageNotTrue string = "the expression evaluated to a truthy value"
2728
defaultErrMessageMapHasKey string = "expect map has key %v"

panic.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,28 @@ func tryNotPanic(t *testing.T, failedNow bool, fn func(), message ...any) error
102102
return err
103103
}
104104

105+
// PanicOf expects the function fn to panic by the expected error.
106+
func (a *Assertion) PanicOf(fn func(), expectErr any, message ...any) error {
107+
a.Helper()
108+
109+
return tryPanicOf(a.T, false, fn, expectErr, message...)
110+
}
111+
112+
// tryPanicOf executes the function fn, and it expects the function to panic by the expected error.
113+
func tryPanicOf(t *testing.T, failedNow bool, fn func(), expectError any, message ...any) error {
114+
t.Helper()
115+
116+
e := isPanic(fn)
117+
if isEqual(e, expectError) {
118+
return nil
119+
}
120+
121+
err := newAssertionError(fmt.Sprintf(defaultErrMessagePanicOf, expectError, e))
122+
failed(t, err, failedNow)
123+
124+
return err
125+
}
126+
105127
// isPanic executes the function, and tries to catching and returns the return value from
106128
// recover().
107129
func isPanic(fn func()) (err any) {

panic_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package assert
22

33
import (
4+
"errors"
45
"testing"
56
)
67

@@ -52,6 +53,32 @@ func testPanicAndNotPanic(a, mockA *Assertion, fn func(), isPanic bool) {
5253
}, isPanic)
5354
}
5455

56+
func TestPanicOf(t *testing.T) {
57+
a := New(t)
58+
mockA := New(new(testing.T))
59+
60+
expectedErr := errors.New("expected error")
61+
62+
testPanicOf(a, mockA, func() {}, expectedErr, false)
63+
testPanicOf(a, mockA, func() {
64+
panic(expectedErr)
65+
}, expectedErr, true)
66+
testPanicOf(a, mockA, func() {
67+
panic("not expected error")
68+
}, expectedErr, false)
69+
}
70+
71+
func testPanicOf(a, mockA *Assertion, fn func(), expectErr any, isExpectedPanic bool) {
72+
a.Helper()
73+
74+
testAssertionFunction(a, "PanicOf", func() error {
75+
return PanicOf(mockA.T, fn, expectErr)
76+
}, isExpectedPanic)
77+
testAssertionFunction(a, "Assertion.PanicOf", func() error {
78+
return mockA.PanicOf(fn, expectErr)
79+
}, isExpectedPanic)
80+
}
81+
5582
func TestIsPanic(t *testing.T) {
5683
Nil(t, isPanic(func() {
5784
// no panic

0 commit comments

Comments
 (0)