Skip to content

Commit e734186

Browse files
committed
feat: add PanicOfNow, NotPanicOf, and NotPanicOfNow.
1 parent c90a919 commit e734186

File tree

4 files changed

+171
-2
lines changed

4 files changed

+171
-2
lines changed

builtin.go

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -831,13 +831,78 @@ 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.
834+
// PanicOf expects the function fn to panic by the expected error. If the function does not panic
835+
// or panic for another reason, it will set the result to fail.
836+
//
837+
// PanicOf(t, func() {
838+
// panic("expected error")
839+
// }, "expected error") // success
840+
// PanicOf(t, func() {
841+
// panic("unexpected error")
842+
// }, "expected error") // fail
843+
// PanicOf(t, func() {
844+
// // ..., no panic
845+
// }, "expected error") // fail
835846
func PanicOf(t *testing.T, fn func(), expectErr any, message ...any) error {
836847
t.Helper()
837848

838849
return tryPanicOf(t, false, fn, expectErr, message...)
839850
}
840851

852+
// PanicOfNow expects the function fn to panic by the expected error. If the function does not
853+
// panic or panic for another reason, it will set the result to fail and terminate the execution.
854+
//
855+
// PanicOfNow(t, func() {
856+
// panic("expected error")
857+
// }, "expected error") // success
858+
// PanicOfNow(t, func() {
859+
// panic("unexpected error")
860+
// }, "expected error") // fail and terminated
861+
// // never runs
862+
func PanicOfNow(t *testing.T, fn func(), expectErr any, message ...any) error {
863+
t.Helper()
864+
865+
return tryPanicOf(t, true, fn, expectErr, message...)
866+
}
867+
868+
// NotPanicOf expects the function fn not panic, or the function does not panic by the unexpected
869+
// error. If the function panics by the unexpected error, it will set the result to fail.
870+
//
871+
// NotPanicOf(t, func() {
872+
// panic("other error")
873+
// }, "unexpected error") // success
874+
// NotPanicOf(t, func() {
875+
// // ..., no panic
876+
// }, "unexpected error") // success
877+
// NotPanicOf(t, func() {
878+
// panic("unexpected error")
879+
// }, "unexpected error") // fail
880+
func NotPanicOf(t *testing.T, fn func(), unexpectedErr any, message ...any) error {
881+
t.Helper()
882+
883+
return tryNotPanicOf(t, false, fn, unexpectedErr, message...)
884+
}
885+
886+
// NotPanicOfNow expects the function fn not panic, or the function does not panic by the
887+
// unexpected error. If the function panics by the unexpected error, it will set the result to fail
888+
// and stop the execution.
889+
//
890+
// NotPanicOfNow(t, func() {
891+
// panic("other error")
892+
// }, "unexpected error") // success
893+
// NotPanicOfNow(t, func() {
894+
// // ..., no panic
895+
// }, "unexpected error") // success
896+
// NotPanicOfNow(t, func() {
897+
// panic("unexpected error")
898+
// }, "unexpected error") // fail and terminate
899+
// // never runs
900+
func NotPanicOfNow(t *testing.T, fn func(), unexpectedErr any, message ...any) error {
901+
t.Helper()
902+
903+
return tryNotPanicOf(t, true, fn, unexpectedErr, message...)
904+
}
905+
841906
// True tests whether a value is truthy or not. It'll set the result to fail if the value is a
842907
// false value. For most types of value, a falsy value is the zero value for its type. For a
843908
// 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
@@ -23,6 +23,7 @@ const (
2323
defaultErrMessagePanic string = "missing expected panic"
2424
defaultErrMessageNotPanic string = "got unwanted error: %v"
2525
defaultErrMessagePanicOf string = "expect panic by %v, got %v"
26+
defaultErrMessageNotPanicOf string = "got unexpected panic error: %v"
2627
defaultErrMessageTrue string = "the expression evaluated to a falsy value"
2728
defaultErrMessageNotTrue string = "the expression evaluated to a truthy value"
2829
defaultErrMessageMapHasKey string = "expect map has key %v"

panic.go

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,78 @@ 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.
105+
// PanicOf expects the function fn to panic by the expected error. If the function does not panic
106+
// or panic for another reason, it will set the result to fail.
107+
//
108+
// assertion.PanicOf(func() {
109+
// panic("expected error")
110+
// }, "expected error") // success
111+
// assertion.PanicOf(func() {
112+
// panic("unexpected error")
113+
// }, "expected error") // fail
114+
// assertion.PanicOf(func() {
115+
// // ..., no panic
116+
// }, "expected error") // fail
106117
func (a *Assertion) PanicOf(fn func(), expectErr any, message ...any) error {
107118
a.Helper()
108119

109120
return tryPanicOf(a.T, false, fn, expectErr, message...)
110121
}
111122

123+
// PanicOfNow expects the function fn to panic by the expected error. If the function does not
124+
// panic or panic for another reason, it will set the result to fail and terminate the execution.
125+
//
126+
// assertion.PanicOfNow(func() {
127+
// panic("expected error")
128+
// }, "expected error") // success
129+
// assertion.PanicOfNow(func() {
130+
// panic("unexpected error")
131+
// }, "expected error") // fail and terminated
132+
// // never runs
133+
func (a *Assertion) PanicOfNow(fn func(), expectErr any, message ...any) error {
134+
a.Helper()
135+
136+
return tryPanicOf(a.T, true, fn, expectErr, message...)
137+
}
138+
139+
// NotPanicOf expects the function fn not panic, or the function does not panic by the unexpected
140+
// error. If the function panics by the unexpected error, it will set the result to fail.
141+
//
142+
// assertion.NotPanicOf(func() {
143+
// panic("other error")
144+
// }, "unexpected error") // success
145+
// assertion.NotPanicOf(func() {
146+
// // ..., no panic
147+
// }, "unexpected error") // success
148+
// assertion.NotPanicOf(func() {
149+
// panic("unexpected error")
150+
// }, "unexpected error") // fail
151+
func (a *Assertion) NotPanicOf(fn func(), unexpectedErr any, message ...any) error {
152+
a.Helper()
153+
154+
return tryNotPanicOf(a.T, false, fn, unexpectedErr, message...)
155+
}
156+
157+
// NotPanicOfNow expects the function fn not panic, or the function does not panic by the
158+
// unexpected error. If the function panics by the unexpected error, it will set the result to fail
159+
// and stop the execution.
160+
//
161+
// assertion.NotPanicOfNow(func() {
162+
// panic("other error")
163+
// }, "unexpected error") // success
164+
// assertion.NotPanicOfNow(func() {
165+
// // ..., no panic
166+
// }, "unexpected error") // success
167+
// assertion.NotPanicOfNow(func() {
168+
// panic("unexpected error")
169+
// }, "unexpected error") // fail and terminate
170+
// // never runs
171+
func (a *Assertion) NotPanicOfNow(fn func(), unexpectedErr any, message ...any) error {
172+
a.Helper()
173+
174+
return tryNotPanicOf(a.T, true, fn, unexpectedErr, message...)
175+
}
176+
112177
// tryPanicOf executes the function fn, and it expects the function to panic by the expected error.
113178
func tryPanicOf(t *testing.T, failedNow bool, fn func(), expectError any, message ...any) error {
114179
t.Helper()
@@ -124,6 +189,20 @@ func tryPanicOf(t *testing.T, failedNow bool, fn func(), expectError any, messag
124189
return err
125190
}
126191

192+
func tryNotPanicOf(t *testing.T, failedNow bool, fn func(), unexpectedError any, message ...any) error {
193+
t.Helper()
194+
195+
e := isPanic(fn)
196+
if !isEqual(e, unexpectedError) {
197+
return nil
198+
}
199+
200+
err := newAssertionError(fmt.Sprintf(defaultErrMessageNotPanicOf, unexpectedError))
201+
failed(t, err, failedNow)
202+
203+
return err
204+
}
205+
127206
// isPanic executes the function, and tries to catching and returns the return value from
128207
// recover().
129208
func isPanic(fn func()) (err any) {

panic_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ func TestPanicOf(t *testing.T) {
6666
testPanicOf(a, mockA, func() {
6767
panic("not expected error")
6868
}, expectedErr, false)
69+
testPanicOf(a, mockA, func() {
70+
panic("expected error")
71+
}, "expected error", true)
6972
}
7073

7174
func testPanicOf(a, mockA *Assertion, fn func(), expectErr any, isExpectedPanic bool) {
@@ -77,6 +80,27 @@ func testPanicOf(a, mockA *Assertion, fn func(), expectErr any, isExpectedPanic
7780
testAssertionFunction(a, "Assertion.PanicOf", func() error {
7881
return mockA.PanicOf(fn, expectErr)
7982
}, isExpectedPanic)
83+
84+
testAssertionFunction(a, "NotPanicOf", func() error {
85+
return NotPanicOf(mockA.T, fn, expectErr)
86+
}, !isExpectedPanic)
87+
testAssertionFunction(a, "Assertion.NotPanicOf", func() error {
88+
return mockA.NotPanicOf(fn, expectErr)
89+
}, !isExpectedPanic)
90+
91+
testAssertionNowFunction(a, "PanicOfNow", func() {
92+
PanicOfNow(mockA.T, fn, expectErr)
93+
}, !isExpectedPanic)
94+
testAssertionNowFunction(a, "Assertion.PanicOfNow", func() {
95+
mockA.PanicOfNow(fn, expectErr)
96+
}, !isExpectedPanic)
97+
98+
testAssertionNowFunction(a, "NotPanicOfNow", func() {
99+
NotPanicOfNow(mockA.T, fn, expectErr)
100+
}, isExpectedPanic)
101+
testAssertionNowFunction(a, "Assertion.NotPanicOfNow", func() {
102+
mockA.NotPanicOfNow(fn, expectErr)
103+
}, isExpectedPanic)
80104
}
81105

82106
func TestIsPanic(t *testing.T) {

0 commit comments

Comments
 (0)