-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors_test.go
168 lines (151 loc) · 3.35 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Package errors has utilities to create and extend errors, easing the error handling process
package errors
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
)
func wrap1() error {
return Wrap(errors.New("some error"))
}
func wrap2() error {
return Wrap(wrap1())
}
func TestWrap(t *testing.T) {
assert.Equal(t, "go-errors.TestWrap ➡︎ some error", Wrap(errors.New("some error")).Error())
assert.Equal(t, "go-errors.wrap1 ➡︎ some error", wrap1().Error())
assert.Equal(t, "go-errors.wrap2 ➡︎ go-errors.wrap1 ➡︎ some error", wrap2().Error())
}
func TestIs(t *testing.T) {
t.Parallel()
errOne := errors.New("err 1")
errTwo := errors.New("err 2")
tests := []struct {
lhs error
rhs error
expected bool
}{
{
lhs: nil,
rhs: NewValidationError("a", "b"),
expected: false,
},
{
lhs: NewValidationError("a", "b"),
rhs: nil,
expected: false,
},
{
lhs: nil,
rhs: Wrap(NewValidationError("a", "b")),
expected: false,
},
{
lhs: Wrap(NewValidationError("a", "b")),
rhs: nil,
expected: false,
},
{
lhs: NewValidationError("a", "b"),
rhs: NewValidationError("b", "c"),
expected: false,
},
{
lhs: NewValidationError("a", "b"),
rhs: NewValidationError("a", "b"),
expected: false,
},
{
lhs: errOne,
rhs: errTwo,
expected: false,
},
{
lhs: errOne,
rhs: errOne,
expected: true,
},
{
lhs: Wrap(errOne),
rhs: errOne,
expected: true,
},
{
lhs: Wrap(errOne),
rhs: Wrap(errOne),
expected: true,
},
{
lhs: Wrap(Wrap(Wrap(errTwo))),
rhs: Wrap(errTwo),
expected: true,
},
{
lhs: Wrap(Wrap(Wrap(errOne))),
rhs: Wrap(errTwo),
expected: false,
},
}
for _, tt := range tests {
actual := Is(tt.lhs, tt.rhs)
assert.Equal(t, tt.expected, actual)
}
}
func TestEquals(t *testing.T) {
t.Parallel()
tests := []struct {
lhs error
rhs error
expected bool
}{
{
lhs: nil,
rhs: nil,
expected: true,
},
{
lhs: nil,
rhs: nil,
expected: true,
},
{
lhs: NewValidationError("card_id", "Cartão deve ser informado!"),
rhs: nil,
expected: false,
},
{
lhs: nil,
rhs: NewValidationError("card_id", "Cartão deve ser informado!"),
expected: false,
},
{
lhs: NewValidationError("card_id", "Cartão deve ser informado!"),
rhs: NewValidationError("card_id", "Cartão deve ser informado!"),
expected: true,
},
{
lhs: NewValidationError("card_id", "Cartão deve ser informado!"),
rhs: NewValidationError("card_id", "Cartão deve ser informado!"),
expected: true,
},
}
for _, tt := range tests {
actual := Equals(tt.lhs, tt.rhs)
assert.Equal(t, tt.expected, actual)
}
}
func TestWrapf(t *testing.T) {
t.Parallel()
t.Run("when error is nil", func(t *testing.T) {
t.Run("returns nil", func(t *testing.T) {
actual := Wrapf(nil, "%s", "test")
assert.Equal(t, nil, actual)
})
})
t.Run("when error is not nil", func(t *testing.T) {
t.Run("adds formatted string to wrapped error messages", func(t *testing.T) {
actual := Wrapf(New("oops"), "%s %d", "test", 1)
assert.Equal(t, "go-errors.TestWrapf.func2.1: test 1; ➡︎ oops", actual.Error())
})
})
}