@@ -15,13 +15,21 @@ const expectedError = "Expected error but none received"
1515type TestingT interface {
1616 Errorf (format string , args ... interface {})
1717 FailNow ()
18+ Helper ()
1819}
1920
2021type ErrorAssertionFunc func (t TestingT , err error ) bool
2122
2223func (e ErrorAssertionFunc ) AsRequire () require.ErrorAssertionFunc {
2324 return func (tt require.TestingT , err error , _ ... any ) {
24- if suc := e (tt , err ); ! suc {
25+ t , is := tt .(TestingT )
26+ if ! is { // not possible
27+ tt .Errorf ("Wrong TestingT type %T" , tt )
28+ tt .FailNow ()
29+ return
30+ }
31+
32+ if suc := e (t , err ); ! suc {
2533 tt .FailNow ()
2634 }
2735 }
@@ -30,21 +38,18 @@ func (e ErrorAssertionFunc) AsRequire() require.ErrorAssertionFunc {
3038func (e ErrorAssertionFunc ) AsAssert () assert.ErrorAssertionFunc {
3139 return func (tt assert.TestingT , err error , _ ... any ) bool {
3240 t , is := tt .(TestingT )
33- if is {
34- return e (t , err )
41+ if ! is { // not possible
42+ tt .Errorf ("Wrong TestingT type %T" , tt )
43+ return false
3544 }
3645
37- // not possible
38- tt .Errorf ("Wrong TestingT type %T" , tt )
39- return false
46+ return e (t , err )
4047 }
4148}
4249
4350func NoError () ErrorAssertionFunc {
4451 return func (t TestingT , err error ) bool {
45- if h , ok := t .(interface { Helper () }); ok {
46- h .Helper ()
47- }
52+ t .Helper ()
4853
4954 if err != nil {
5055 t .Errorf ("Expected nil error but received : %T(%s)" , err , err .Error ())
@@ -57,9 +62,7 @@ func NoError() ErrorAssertionFunc {
5762
5863func Error () ErrorAssertionFunc {
5964 return func (t TestingT , err error ) bool {
60- if h , ok := t .(interface { Helper () }); ok {
61- h .Helper ()
62- }
65+ t .Helper ()
6366
6467 if err == nil {
6568 t .Errorf (expectedError )
@@ -76,9 +79,8 @@ func Error() ErrorAssertionFunc {
7679// Returns false if the error is nil or doesn't match any expected errors.
7780func ErrorIs (allExpectedErrors ... error ) ErrorAssertionFunc {
7881 return func (t TestingT , err error ) bool {
79- if h , ok := t .(interface { Helper () }); ok {
80- h .Helper ()
81- }
82+ t .Helper ()
83+
8284 if err == nil {
8385 t .Errorf (expectedError )
8486 return false
@@ -121,9 +123,7 @@ func ErrorIs(allExpectedErrors ...error) ErrorAssertionFunc {
121123
122124func ErrorOfType [T error ](typedAsserts ... func (TestingT , T )) ErrorAssertionFunc {
123125 return func (t TestingT , err error ) bool {
124- if h , ok := t .(interface { Helper () }); ok {
125- h .Helper ()
126- }
126+ t .Helper ()
127127
128128 if err == nil {
129129 t .Errorf (expectedError )
@@ -152,9 +152,7 @@ func ErrorOfType[T error](typedAsserts ...func(TestingT, T)) ErrorAssertionFunc
152152
153153func ErrorStringContains (s string ) ErrorAssertionFunc {
154154 return func (t TestingT , err error ) bool {
155- if h , ok := t .(interface { Helper () }); ok {
156- h .Helper ()
157- }
155+ t .Helper ()
158156
159157 if err == nil {
160158 t .Errorf (expectedError )
@@ -173,9 +171,7 @@ func ErrorStringContains(s string) ErrorAssertionFunc {
173171
174172func All (expected ... ErrorAssertionFunc ) ErrorAssertionFunc {
175173 return func (t TestingT , err error ) bool {
176- if h , ok := t .(interface { Helper () }); ok {
177- h .Helper ()
178- }
174+ t .Helper ()
179175
180176 ret := true
181177 for _ , fn := range expected {
0 commit comments