forked from k0sproject/rig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors_test.go
75 lines (66 loc) · 1.63 KB
/
errors_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package rig
import (
"errors"
"testing"
"github.com/stretchr/testify/require"
)
func TestErrorStringer(t *testing.T) {
type testCase struct {
name string
err error
expected string
}
for _, scenario := range []testCase{
{
name: "non-wrapped error",
err: ErrOS,
expected: "local os",
},
{
name: "error wrapped in error",
err: ErrOS.Wrap(ErrInvalidPath),
expected: "local os: invalid path",
},
{
name: "string wrapped error",
err: ErrOS.Wrapf("test"),
expected: "local os: test",
},
{
name: "double wrapped string error",
err: ErrOS.Wrapf("test: %w", ErrInvalidPath),
expected: "local os: test: invalid path",
},
} {
t.Run(scenario.name, func(t *testing.T) {
require.Error(t, scenario.err)
require.Equal(t, scenario.expected, scenario.err.Error())
})
}
}
func TestUnwrap(t *testing.T) {
err := ErrOS.Wrap(ErrInvalidPath)
require.Equal(t, ErrInvalidPath, errors.Unwrap(err))
}
func TestErrorsIs(t *testing.T) {
err := ErrOS.Wrap(ErrInvalidPath.Wrap(ErrNotFound))
require.True(t, errors.Is(err, ErrOS))
require.True(t, errors.Is(err, ErrInvalidPath))
require.True(t, errors.Is(err, ErrNotFound))
require.False(t, errors.Is(err, ErrNotConnected))
}
type testErr struct {
msg string
}
func (t *testErr) Error() string {
return "foo " + t.msg
}
func TestErrorsAs(t *testing.T) {
err := ErrOS.Wrap(ErrInvalidPath.Wrap(&testErr{msg: "test"}))
var cmp *testErr
require.True(t, errors.As(err, &cmp))
require.Equal(t, "local os: invalid path: foo test", err.Error())
if errors.As(err, &cmp) {
require.Equal(t, "foo test", cmp.Error())
}
}