|
1 | 1 | package must_test |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "errors" |
5 | | - "regexp" |
| 4 | + "net/url" |
6 | 5 | "testing" |
7 | 6 |
|
8 | 7 | "github.com/stretchr/testify/assert" |
9 | 8 |
|
| 9 | + "dev.gaijin.team/go/golib/e" |
10 | 10 | "dev.gaijin.team/go/golib/must" |
11 | 11 | ) |
12 | 12 |
|
13 | 13 | func TestOK(t *testing.T) { |
14 | 14 | t.Parallel() |
15 | 15 |
|
16 | 16 | assert.NotPanics(t, func() { |
17 | | - _ = must.OK(regexp.Compile("[a-z]")) //nolint:gocritic |
| 17 | + uri := must.OK(url.Parse("https://example.com")) |
| 18 | + assert.Equal(t, "https://example.com", uri.String()) |
18 | 19 | }) |
19 | 20 |
|
20 | | - assert.Panics(t, func() { |
21 | | - _ = must.OK(regexp.Compile("[a-z")) //nolint:staticcheck,gocritic |
22 | | - }) |
| 21 | + imminentError := func() (bool, error) { return false, e.New("imminent error") } |
23 | 22 |
|
24 | | - err := catchPanic(t, func() { |
25 | | - _ = must.OK(errorFn()) |
| 23 | + err := catchPanicError(t, func() { |
| 24 | + _ = must.OK(imminentError()) |
26 | 25 | }) |
27 | 26 |
|
28 | | - assert.ErrorIs(t, err, errTestError) |
29 | | - assert.Equal(t, "OK assurance failed: test error", err.Error()) |
30 | | -} |
31 | | - |
32 | | -var errTestError = errors.New("test error") |
33 | | - |
34 | | -func errorFn() (bool, error) { |
35 | | - return false, errTestError |
| 27 | + assert.ErrorContains(t, err, "must.OK assertion failed: imminent error") |
36 | 28 | } |
37 | 29 |
|
38 | | -func catchPanic(t *testing.T, panickingFn func()) (err error) { |
| 30 | +func catchPanicError(t *testing.T, fn func()) (err error) { |
39 | 31 | t.Helper() |
40 | 32 |
|
41 | 33 | defer func() { |
42 | | - err = recover().(error) //nolint:forcetypeassert |
| 34 | + if rv := recover(); rv != nil { |
| 35 | + re, ok := rv.(error) |
| 36 | + if !ok { |
| 37 | + t.Fatal("recovered panic value is not an error") |
| 38 | + } |
| 39 | + |
| 40 | + err = re |
| 41 | + } |
43 | 42 | }() |
44 | 43 |
|
45 | | - panickingFn() |
| 44 | + fn() |
46 | 45 |
|
47 | 46 | return nil |
48 | 47 | } |
0 commit comments