forked from duke-git/lancet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xerror_test.go
117 lines (80 loc) · 2.52 KB
/
xerror_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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package xerror
import (
"errors"
"strconv"
"strings"
"testing"
"github.com/duke-git/lancet/v2/internal"
)
func TestTryUnwrap(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestTryUnwrap")
assert.Equal(42, TryUnwrap(strconv.Atoi("42")))
_, err := strconv.Atoi("4o2")
defer func() {
v := recover()
assert.Equal(err.Error(), v.(*strconv.NumError).Error())
}()
TryUnwrap(strconv.Atoi("4o2"))
}
func TestNew(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestNew")
err := New("error occurs")
assert.Equal("error occurs", err.Error())
}
func TestWrap(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestWrap")
err := New("wrong password")
wrapErr := Wrap(err, "error")
assert.Equal("error: wrong password", wrapErr.Error())
}
func TestXError_Wrap(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestXError_Wrap")
err1 := New("error").With("level", "high")
err2 := err1.Wrap(errors.New("bad"))
assert.Equal("error: bad", err2.Error())
}
func TestXError_Unwrap(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestXError_Unwrap")
err1 := New("error").With("level", "high")
err2 := err1.Wrap(errors.New("bad"))
err := err2.Unwrap()
assert.Equal("bad", err.Error())
}
func TestXError_StackTrace(t *testing.T) {
assert := internal.NewAssert(t, "TestXError_StackTrace")
err := New("error")
stacks := err.Stacks()
assert.Equal(3, len(stacks))
assert.Equal("github.com/duke-git/lancet/v2/xerror.TestXError_StackTrace", stacks[0].Func)
assert.Equal(75, stacks[0].Line)
assert.Equal(true, strings.Contains(stacks[0].File, "xerror_test.go"))
}
func TestXError_With_Id_Is_Values(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestXError_With_Id_Is_Values")
baseErr := New("baseError")
err1 := New("error1").Id("e001").With("level", "high")
err2 := New("error2").Id("e002").With("level", "low")
err := err1.Wrap(baseErr).With("v", "1.0")
assert.Equal(true, errors.Is(err, baseErr))
assert.NotEqual(err, err1)
assert.IsNotNil(err.Values()["v"])
assert.IsNil(err1.Values()["v"])
assert.Equal(false, errors.Is(err, err2))
}
func TestXError_Info(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestXError_Info")
cause := errors.New("error")
err := Wrap(cause, "invalid username").Id("e001").With("level", "high")
errInfo := err.Info()
assert.Equal("invalid username", errInfo.Message)
assert.Equal("e001", errInfo.Id)
assert.Equal(cause, errInfo.Cause)
assert.Equal("high", errInfo.Values["level"])
}