-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_test.go
103 lines (91 loc) · 2.72 KB
/
error_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
package errs_test
import (
"errors"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/rtkym/errs-go"
)
func TestNew(t *testing.T) {
msg := "test message"
err := errs.New(msg)
assert.Equal(t, err.Error(), msg)
assert.NotEqual(t, len(err.StackTrace()), 0)
}
func TestWrap(t *testing.T) {
msg1 := "m1"
msg2 := "m2"
msg3 := "m3"
t.Run("LantanaError", func(t *testing.T) {
err := errs.Wrap(errs.New("test"))
assert.Equal(t, "test", err.Error())
assert.NotEqual(t, len(err.StackTrace()), 0)
})
t.Run("LantanaError with message", func(t *testing.T) {
err := errs.Wrap(errs.New("test"), msg1, msg2, msg3)
assert.Equal(t, fmt.Sprintf("%s: %s: %s: test", msg1, msg2, msg3), err.Error())
assert.NotEqual(t, len(err.StackTrace()), 0)
})
t.Run("OtherError", func(t *testing.T) {
err := errs.Wrap(errors.New("test"))
assert.ErrorContains(t, err, "test")
assert.NotEqual(t, len(err.StackTrace()), 0)
})
t.Run("OtherError with message", func(t *testing.T) {
err := errs.Wrap(errors.New("test"), msg1, msg2, msg3)
assert.Equal(t, fmt.Sprintf("%s: %s: %s: test", msg1, msg2, msg3), err.Error())
assert.NotEqual(t, len(err.StackTrace()), 0)
})
t.Run("nil", func(t *testing.T) {
assert.Panics(t, func() {
errs.Wrap(nil) //nolint
})
})
}
type dummy struct{}
func (dummy) Error() string {
return "dummy"
}
func TestValues(t *testing.T) {
msg := "test message"
t.Run("SingleError", func(t *testing.T) {
err := errs.New(msg).With("1", "a").With("2", "b").With("3", "c")
expect := map[string]interface{}{"1": "a", "2": "b", "3": "c"}
assert.Equal(t, len(err.Values()), len(expect))
for k, v := range err.Values() {
if v2, ok := expect[k]; ok {
assert.Equal(t, v, v2)
} else {
assert.Fail(t, "Not found key: %s", k)
}
}
})
t.Run("NestedError", func(t *testing.T) {
err := errs.New(msg).With("1", "a").With("2", "b").With("3", "c")
err = errs.Wrap(err).With("2", "B").With("@", "Z")
expect := map[string]interface{}{"1": "a", "2": "B", "3": "c", "@": "Z"}
assert.Equal(t, len(err.Values()), len(expect))
for k, v := range err.Values() {
if v2, ok := expect[k]; ok {
assert.Equal(t, v, v2)
} else {
assert.Fail(t, "Not found key: %s", k)
}
}
})
t.Run("NestedError2", func(t *testing.T) {
var exx error = &dummy{}
err := errs.Wrap(exx).With("1", "a").With("2", "b").With("3", "c")
exx = fmt.Errorf("other error: %w", err)
err = errs.Wrap(exx).With("2", "B").With("@", "Z")
expect := map[string]interface{}{"1": "a", "2": "B", "3": "c", "@": "Z"}
assert.Equal(t, len(err.Values()), len(expect))
for k, v := range err.Values() {
if v2, ok := expect[k]; ok {
assert.Equal(t, v, v2)
} else {
assert.Fail(t, "Not found key: %s", k)
}
}
})
}