Skip to content

Commit 8d3e65f

Browse files
authored
[improvement] allow any type for error for PanicWithError and NotPanicWithError assertion methods (#16)
1 parent c4c738c commit 8d3e65f

File tree

1 file changed

+7
-14
lines changed

1 file changed

+7
-14
lines changed

assert_panic.go

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

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

@@ -38,7 +39,7 @@ func NotPanic(t testing.TB, underTest func()) {
3839
Asserts that the given function panics with the specified error.
3940
The actual error must be of the same type and value as the given error
4041
*/
41-
func PanicWithError[K comparable](t testing.TB, expectedError K, underTest func()) {
42+
func PanicWithError[T any](t testing.TB, expectedError T, underTest func()) {
4243
t.Helper()
4344

4445
defer func() {
@@ -48,15 +49,8 @@ func PanicWithError[K comparable](t testing.TB, expectedError K, underTest func(
4849
return
4950
}
5051

51-
error, converted := r.(K)
52-
if converted {
53-
Equal(t, expectedError, error)
54-
} else {
55-
t.Errorf(
56-
"Panic threw an unexpected error type. Expected Type: %T. Actual Type: %T",
57-
expectedError,
58-
r,
59-
)
52+
if !reflect.DeepEqual(expectedError, r) {
53+
t.Errorf("Expected panic with %v error but got %v error", expectedError, r)
6054
}
6155
}()
6256

@@ -67,7 +61,7 @@ func PanicWithError[K comparable](t testing.TB, expectedError K, underTest func(
6761
Asserts that the given function does not panic with the specified error.
6862
The assertion succeeds if the given function does not panic or panics with a different error
6963
*/
70-
func NotPanicWithError[K comparable](t testing.TB, expectedError K, underTest func()) {
64+
func NotPanicWithError[T any](t testing.TB, expectedError T, underTest func()) {
7165
t.Helper()
7266

7367
defer func() {
@@ -76,9 +70,8 @@ func NotPanicWithError[K comparable](t testing.TB, expectedError K, underTest fu
7670
return
7771
}
7872

79-
error, converted := r.(K)
80-
if converted {
81-
NotEqual(t, expectedError, error)
73+
if reflect.DeepEqual(expectedError, r) {
74+
t.Errorf("Expected panic with different error than %v error", expectedError)
8275
}
8376
}()
8477

0 commit comments

Comments
 (0)