-
Notifications
You must be signed in to change notification settings - Fork 5
/
mirror_test.go
173 lines (164 loc) · 3.13 KB
/
mirror_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
169
170
171
172
173
package errors
import (
"errors"
"fmt"
"net/http"
"strings"
"testing"
)
func TestUnwrap(t *testing.T) {
type args struct {
err error
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "wrapped",
args: args{err: NewWithErrMsgType(
New("level 0"),
"level 1",
TypeInternal,
)},
wantErr: true,
},
{
name: "no wrapping",
args: args{err: NewWithErrMsgType(
nil,
"level 1",
TypeInternal,
)},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := Unwrap(tt.args.err); (err != nil) != tt.wantErr {
t.Errorf("Unwrap() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestIs(t *testing.T) {
target := New("level 0")
type args struct {
err error
target error
}
tests := []struct {
name string
args args
want bool
}{
{
name: "has err in the chain",
args: args{
err: NewWithErrMsgType(
NewWithErrMsgType(
target,
"level 1",
TypeInputBody,
),
"level 2",
TypeNotFound,
),
target: target,
},
want: true,
},
{
name: "no matching err in the chain",
args: args{
err: NewWithErrMsgType(
NewWithErrMsgType(
New("level 0"),
"level 1",
TypeInputBody,
),
"level 2",
TypeNotFound,
),
target: target,
},
want: false,
},
{
name: "external target error",
args: args{
err: NewWithErrMsgType(
nil,
"level 2",
TypeNotFound,
),
target: errors.New("external"),
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Is(tt.args.err, tt.args.target); got != tt.want {
t.Errorf("Is() = %v, want %v", got, tt.want)
}
})
}
}
func TestAs(t *testing.T) {
// ref: https://github.com/golang/go/issues/37625#issuecomment-594045710
err := fmt.Errorf("fmt error")
target := &Error{}
if errors.As(err, &target) {
t.Error("As() = true, want false")
}
err = New("type *Error")
if !errors.As(err, &target) {
t.Error("As() = false, want true")
}
out := target.Error()
want := "/errors/mirror_test.go:121: type *Error"
if !strings.Contains(out, want) {
t.Errorf("Error() = %s, want %s", out, want)
}
}
func TestJoin(t *testing.T) {
joined := Join(
errors.New("[1] std"),
New("[2] custom"),
errors.New("[3] std"),
Validation("[4] validation"),
)
got := fmt.Sprint(joined)
if !strings.Contains(got, "[1] std") ||
!strings.Contains(got, "mirror_test.go:136: [2] custom") ||
!strings.Contains(got, "[3] std") ||
!strings.Contains(got, "mirror_test.go:138: [4] validation") {
t.Error(got)
}
msg, ok := Message(joined)
if ok {
t.Errorf(
"Expected: false, got: %v",
ok,
)
}
expectedMsg := `[2] custom
[4] validation`
if msg != expectedMsg {
t.Error(msg)
}
expectedCode := http.StatusUnprocessableEntity
code, msg, ok := HTTPStatusCodeMessage(joined)
if ok {
t.Errorf("expected false, got: %v", ok)
}
if expectedMsg != msg ||
expectedCode != code {
t.Errorf(
"Expected msg: %s, expected code: %d, got msg: %s, got code: %d",
expectedMsg, expectedCode, msg, code,
)
}
}