-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror_wrap_test.go
94 lines (71 loc) · 2.02 KB
/
error_wrap_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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package goapperrors
import (
"testing"
"github.com/stretchr/testify/assert"
)
func Test_Wrap(t *testing.T) {
t.Run("with adding stack trace", func(t *testing.T) {
initConfig(okConfig)
assert.Nil(t, Wrap(nil))
ae1 := Wrap(errTest1)
ae2 := Wrap(ae1)
assert.ErrorIs(t, ae1, errTest1)
assert.ErrorIs(t, ae2, ae1)
})
t.Run("without adding stack trace", func(t *testing.T) {
initConfig(noStackTraceConfig)
assert.Nil(t, Wrap(nil))
ae1 := Wrap(errTest1)
ae2 := Wrap(ae1)
assert.ErrorIs(t, ae1, errTest1)
assert.ErrorIs(t, ae2, ae1)
})
}
func Test_Wrapf(t *testing.T) {
initConfig(okConfig)
ae1 := Wrapf("%w", errTest1)
ae2 := Wrapf("%w", ae1)
assert.ErrorIs(t, ae1, errTest1)
assert.ErrorIs(t, ae2, ae1)
}
func Test_GetStackTrace(t *testing.T) {
t.Run("errors have stack trace", func(t *testing.T) {
initConfig(okConfig)
ae1 := Wrap(errTest1)
ae2 := Wrap(ae1)
assert.True(t, len(GetStackTrace(ae1)) > 0)
assert.True(t, len(GetStackTrace(ae2)) > 0)
})
t.Run("errors have no stack trace", func(t *testing.T) {
initConfig(okConfig)
assert.True(t, len(GetStackTrace(nil)) == 0)
assert.True(t, len(GetStackTrace(errTest1)) == 0)
assert.True(t, len(GetStackTrace(errTest2)) == 0)
})
}
func Test_UnwrapToRoot(t *testing.T) {
assert.Nil(t, UnwrapToRoot(nil))
e1 := Wrap(errTest1)
e2 := Wrap(e1)
e3 := Wrap(e2)
assert.Equal(t, errTest1, UnwrapToRoot(e3))
}
type err1 struct{}
func (e err1) Error() string { return "err1" }
type err2 struct {
err error
}
func (e err2) Error() string { return "err2" }
func (e err2) Unwrap() error { return e.err }
type err3 struct {
errs []error
}
func (e err3) Error() string { return "err3" }
func (e err3) Unwrap() []error { return e.errs }
func Test_UnwrapMulti(t *testing.T) {
assert.Nil(t, UnwrapMulti(err1{}))
assert.Nil(t, UnwrapMulti(err2{}))
assert.Equal(t, []error{errTest1}, UnwrapMulti(err2{err: errTest1}))
assert.Nil(t, UnwrapMulti(err3{}))
assert.Equal(t, []error{errTest1, errTest2}, UnwrapMulti(err3{errs: []error{errTest1, errTest2}}))
}