@@ -102,13 +102,78 @@ func tryNotPanic(t *testing.T, failedNow bool, fn func(), message ...any) error
102
102
return err
103
103
}
104
104
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
106
117
func (a * Assertion ) PanicOf (fn func (), expectErr any , message ... any ) error {
107
118
a .Helper ()
108
119
109
120
return tryPanicOf (a .T , false , fn , expectErr , message ... )
110
121
}
111
122
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
+
112
177
// tryPanicOf executes the function fn, and it expects the function to panic by the expected error.
113
178
func tryPanicOf (t * testing.T , failedNow bool , fn func (), expectError any , message ... any ) error {
114
179
t .Helper ()
@@ -124,6 +189,20 @@ func tryPanicOf(t *testing.T, failedNow bool, fn func(), expectError any, messag
124
189
return err
125
190
}
126
191
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
+
127
206
// isPanic executes the function, and tries to catching and returns the return value from
128
207
// recover().
129
208
func isPanic (fn func ()) (err any ) {
0 commit comments