Skip to content

Commit

Permalink
fix usage of errors.As in starlarktruth tests
Browse files Browse the repository at this point in the history
Signed-off-by: Pierre Fenoll <[email protected]>
  • Loading branch information
fenollp committed Oct 12, 2022
1 parent 95121d4 commit afd95cd
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions pkg/starlarktruth/truth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,12 @@ func helper(t *testing.T, as asWhat, program string) (starlark.StringDict, error
}

func testEach(t *testing.T, m map[string]error, asSlice ...asWhat) {
if len(asSlice) > 1 {
panic("too many items in slice")
}
as := asFunc
for _, as = range asSlice {
break
}
for code, expectedErr := range m {
t.Run(code, func(t *testing.T) {
Expand All @@ -77,9 +81,16 @@ func testEach(t *testing.T, m map[string]error, asSlice ...asWhat) {
} else {
require.Error(t, err)
require.EqualError(t, err, expectedErr.Error())
if _, ok := err.(UnhandledError); !ok {
require.True(t, errors.As(err, &expectedErr))
require.IsType(t, expectedErr, err)
switch err := err.(type) {
case *starlark.EvalError:
e := err.Unwrap()
if _, ok := e.(*UnhandledError); !ok {
require.Exactly(t, expectedErr, e)
}
case *UnresolvedError:
require.Exactly(t, expectedErr, err)
default:
panic("unreachable")
}
}
})
Expand Down

0 comments on commit afd95cd

Please sign in to comment.