@@ -30,20 +30,32 @@ import (
3030)
3131
3232// assertErrorType checks that the error chain contains an error of the same type as expectedErr.
33+ // Used by table-driven tests where the expected type is known only at runtime;
34+ // for static call sites prefer the type-parameterized requireErrorAs helper.
3335func assertErrorType (t * testing.T , expectedErr , actualErr error ) bool {
3436 t .Helper ()
3537
36- expectedType := reflect .TypeOf (expectedErr )
37-
38- for err := actualErr ; err != nil ; err = errors .Unwrap (err ) {
39- if reflect .TypeOf (err ) == expectedType {
40- return true
41- }
38+ target := reflect .New (reflect .TypeOf (expectedErr )).Interface ()
39+ if errors .As (actualErr , target ) {
40+ return true
4241 }
4342
4443 return assert .Fail (t , "error type mismatch" , "expected error of type %T in chain, but got %T" , expectedErr , actualErr )
4544}
4645
46+ // requireErrorAs unwraps err looking for a value of type T and fails the test when none is found.
47+ // Type-parameterized wrapper over errors.As; prefer over assertErrorType when the target type is
48+ // known statically.
49+ func requireErrorAs [T error ](t * testing.T , err error ) T {
50+ t .Helper ()
51+
52+ var target T
53+
54+ require .ErrorAs (t , err , & target )
55+
56+ return target
57+ }
58+
4759func TestPathRelativeToInclude (t * testing.T ) {
4860 t .Parallel ()
4961
@@ -1424,8 +1436,7 @@ func TestStartsWithArityRegression(t *testing.T) {
14241436 require .NotPanics (t , func () {
14251437 _ , err := config .StartsWith (ctx , pctx , tc .args )
14261438 require .Error (t , err , "must return error for wrong arity (%d args)" , len (tc .args ))
1427- require .True (t , assertErrorType (t , config.WrongNumberOfParamsError {}, err ),
1428- "expected WrongNumberOfParamsError, got %T: %v" , err , err )
1439+ requireErrorAs [config.WrongNumberOfParamsError ](t , err )
14291440 }, "startswith with %d args must not panic" , len (tc .args ))
14301441 })
14311442 }
@@ -1453,8 +1464,7 @@ func TestEndsWithArityRegression(t *testing.T) {
14531464 require .NotPanics (t , func () {
14541465 _ , err := config .EndsWith (ctx , pctx , tc .args )
14551466 require .Error (t , err , "must return error for wrong arity (%d args)" , len (tc .args ))
1456- require .True (t , assertErrorType (t , config.WrongNumberOfParamsError {}, err ),
1457- "expected WrongNumberOfParamsError, got %T: %v" , err , err )
1467+ requireErrorAs [config.WrongNumberOfParamsError ](t , err )
14581468 }, "endswith with %d args must not panic" , len (tc .args ))
14591469 })
14601470 }
@@ -1482,8 +1492,7 @@ func TestStrContainsArityRegression(t *testing.T) {
14821492 require .NotPanics (t , func () {
14831493 _ , err := config .StrContains (ctx , pctx , tc .args )
14841494 require .Error (t , err , "must return error for wrong arity (%d args)" , len (tc .args ))
1485- require .True (t , assertErrorType (t , config.WrongNumberOfParamsError {}, err ),
1486- "expected WrongNumberOfParamsError, got %T: %v" , err , err )
1495+ requireErrorAs [config.WrongNumberOfParamsError ](t , err )
14871496 }, "strcontains with %d args must not panic" , len (tc .args ))
14881497 })
14891498 }
@@ -1515,8 +1524,7 @@ func TestRunCommandOptionsOnlyArityRegression(t *testing.T) {
15151524 require .NotPanics (t , func () {
15161525 _ , err := config .RunCommand (ctx , pctx , l , tc .params )
15171526 require .Error (t , err , "must return error when only option flags are supplied (%v)" , tc .params )
1518- require .True (t , assertErrorType (t , config .EmptyStringNotAllowedError ("" ), err ),
1519- "expected EmptyStringNotAllowedError, got %T: %v" , err , err )
1527+ requireErrorAs [config.EmptyStringNotAllowedError ](t , err )
15201528 }, "run_cmd with options-only %v must not panic" , tc .params )
15211529 })
15221530 }
0 commit comments