1
1
package goassert
2
2
3
3
import (
4
+ "reflect"
4
5
"testing"
5
6
)
6
7
@@ -38,7 +39,7 @@ func NotPanic(t testing.TB, underTest func()) {
38
39
Asserts that the given function panics with the specified error.
39
40
The actual error must be of the same type and value as the given error
40
41
*/
41
- func PanicWithError [K comparable ](t testing.TB , expectedError K , underTest func ()) {
42
+ func PanicWithError [T any ](t testing.TB , expectedError T , underTest func ()) {
42
43
t .Helper ()
43
44
44
45
defer func () {
@@ -48,15 +49,8 @@ func PanicWithError[K comparable](t testing.TB, expectedError K, underTest func(
48
49
return
49
50
}
50
51
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 )
60
54
}
61
55
}()
62
56
@@ -67,7 +61,7 @@ func PanicWithError[K comparable](t testing.TB, expectedError K, underTest func(
67
61
Asserts that the given function does not panic with the specified error.
68
62
The assertion succeeds if the given function does not panic or panics with a different error
69
63
*/
70
- func NotPanicWithError [K comparable ](t testing.TB , expectedError K , underTest func ()) {
64
+ func NotPanicWithError [T any ](t testing.TB , expectedError T , underTest func ()) {
71
65
t .Helper ()
72
66
73
67
defer func () {
@@ -76,9 +70,8 @@ func NotPanicWithError[K comparable](t testing.TB, expectedError K, underTest fu
76
70
return
77
71
}
78
72
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 )
82
75
}
83
76
}()
84
77
0 commit comments