-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors_test.go
129 lines (119 loc) · 2.66 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
package cerrors
import (
"errors"
"io"
"strings"
"testing"
)
func fooErr(code interface{}, msg string) Error {
return New(code, msg)
}
type foo int
func (f foo) err(code interface{}, msg string) Error {
return New(code, msg)
}
func TestNew(t *testing.T) {
tests := []struct {
maker func(interface{}, string) Error
code interface{}
msg string
want error
wantFrame string
unwantCode interface{}
}{
{
fooErr,
nil,
"nil code",
errors.New("nil code"),
"/errors_test.go:11\tfooErr",
"non-nil",
}, {
fooErr,
123,
"int code",
errors.New("Error 123: int code"),
"/errors_test.go:11\tfooErr",
321,
}, {
fooErr,
"NotFound",
"",
errors.New("Error NotFound"),
"/errors_test.go:11\tfooErr",
321,
}, {
foo(0).err,
"BAD-INPUT",
"string code",
errors.New("Error BAD-INPUT: string code"),
"/errors_test.go:17\tfoo.err", "GOOD-INPUT",
},
}
for _, tt := range tests {
got := tt.maker(tt.code, tt.msg)
if got.Code() != tt.code || got.Msg() != tt.msg ||
got.Error() != tt.want.Error() {
t.Errorf("New(): got: %v, want %v", got, tt.want)
}
if got.Code() != tt.code || got.Code() == tt.unwantCode ||
!got.CodeIs(tt.code) || got.CodeIs(tt.unwantCode) ||
!got.CodeIn(tt.code) || got.CodeIn(tt.unwantCode) ||
!got.CodeIn(tt.unwantCode, tt.code) || got.CodeIn(tt.unwantCode, "NEVER-BE") {
t.Errorf("New.Code*() check failed: got: %v, want %v, unwant %v", got.Code(), tt.code, tt.unwantCode)
}
if !strings.HasSuffix(got.StackV()[0], tt.wantFrame) ||
!strings.Contains(got.Stack(), tt.wantFrame+"\n") {
t.Errorf("New.Stack*(): got stack:\n%v\nlack of frame: ...%v", got.Stack(), tt.wantFrame)
}
}
}
func TestNewf(t *testing.T) {
tests := []struct {
err error
want string
}{
{
Newf(123, "read error without format specifiers"),
"Error 123: read error without format specifiers",
}, {
Newf("EBADF", "read error with %d format specifier", 1),
"Error EBADF: read error with 1 format specifier",
},
}
for _, tt := range tests {
got := tt.err.Error()
if got != tt.want {
t.Errorf("Newf(%v): got: %q, want %q", tt.err, got, tt.want)
}
}
}
func TestCode(t *testing.T) {
tests := []struct {
err error
want interface{}
}{
{
nil,
nil,
}, {
errors.New("simple error"),
nil,
}, {
Wrap(Wrap(io.EOF, "read error"), "client error"),
nil,
}, {
New(123, "error with an int code"),
123,
}, {
Wrap(New("EBADF", "error with a string code"), "read error"),
"EBADF",
},
}
for _, tt := range tests {
got := Code(tt.err)
if got != tt.want {
t.Errorf("Code(%v): got: %q, want %q", tt.err, got, tt.want)
}
}
}