-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror_test.go
More file actions
95 lines (76 loc) · 2.58 KB
/
error_test.go
File metadata and controls
95 lines (76 loc) · 2.58 KB
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
package errors
import (
"errors"
"reflect"
"testing"
"github.com/gravitton/assert"
)
func TestFields(t *testing.T) {
err := Newf("Test DataError #%d: %s", 5, "failed to spawn")
assert.Equal(t, err.Error(), "Test DataError #5: failed to spawn")
assert.Length(t, err.Fields(), 0)
err = Wrap("test")
assert.Equal(t, err.Error(), "test")
err = New("test3").WithField("action", "call")
assert.Equal(t, err.Error(), "test3")
assert.Equal(t, err.Fields()["action"], "call")
assert.NotContains(t, err.Fields(), "type")
err1 := Wrap(err)
assert.Same(t, err, err1)
err2 := err.WithFields(map[string]any{"type": "warning"})
assert.NotSame(t, err1, err2)
assert.NotContains(t, err.Fields(), "type")
assert.Equal(t, err.Error(), "test3")
assert.Equal(t, err2.Fields()["type"], "warning")
err3 := err.WithField("action", "send")
assert.Equal(t, err2.Fields()["action"], "call")
assert.Equal(t, err3.Fields()["action"], "send")
err4 := New("error")
err5 := New("error")
assert.Equal(t, err4, err5)
assert.NotSame(t, err4, err5)
}
func TestUnwrap(t *testing.T) {
err := New("original error 1")
cause := err.Unwrap()
assert.Equal(t, reflect.TypeOf(cause).String(), "*errors.errorString")
assert.Equal(t, "original error 1", cause.Error())
oErr := errors.New("original error 2")
err = Wrap(oErr)
assert.Same(t, oErr, err.Unwrap())
oErr2 := errors.New("original error 3")
err = err.WithCause(oErr2)
assert.Same(t, oErr2, err.Unwrap())
}
func TestErrorsIs(t *testing.T) {
tests := []struct {
name string
err error
}{
{
name: "*errors.errorString",
err: errors.New("dummy error"),
},
{
name: "*DataError",
err: New("dummy error"),
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
assert.ErrorIs(t, test.err, test.err)
assert.ErrorIs(t, Wrap(test.err), test.err)
assert.ErrorIs(t, Wrap(test.err).WithField("action", "call"), test.err)
assert.ErrorIs(t, Wrap(test.err).WithField("action", "call").WithField("type", "warning"), test.err)
assert.ErrorIs(t, Wrap(test.err).WithFields(map[string]any{"type": "warning"}), test.err)
})
}
}
func TestErrorsIsWithFields(t *testing.T) {
assert.ErrorIs(t, New("dummy error").WithField("action", "call"), New("dummy error"))
assert.NotErrorIs(t, New("dummy error"), New("dummy error").WithField("action", "call"))
assert.NotErrorIs(t, New("dummy error").WithField("action", "call"), New("dummy error").WithField("action", "send"))
err := New("dummy error").WithField("module", "http")
assert.ErrorIs(t, err.WithField("add", false), err)
assert.NotErrorIs(t, err, err.WithField("add", false))
}