forked from pkg/errors
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
Latest commit
159 lines (143 loc) · 3.51 KB
/
Copy patherrors_test.go
File metadata and controls
159 lines (143 loc) · 3.51 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
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
package errors
import (
"errors"
"fmt"
"io"
"reflect"
"testing"
)
funcTestNew(t*testing.T) {
tests:= []struct {
errstring
wanterror
}{
{"", fmt.Errorf("")},
{"foo", fmt.Errorf("foo")},
{"foo", New("foo")},
{"string with format specifiers: %v", errors.New("string with format specifiers: %v")},
}
for_, tt:=rangetests {
got:=New(tt.err)
ifgot.Error() !=tt.want.Error() {
t.Errorf("New.Error(): got: %q, want %q", got, tt.want)
}
}
}
funcTestWrapNil(t*testing.T) {
got:=Wrap(nil, "no error")
ifgot!=nil {
t.Errorf("Wrap(nil, \"no error\"): got %#v, expected nil", got)
}
}
funcTestWrap(t*testing.T) {
tests:= []struct {
errerror
messagestring
wantstring
}{
{io.EOF, "read error", "read error: EOF"},
{Wrap(io.EOF, "read error"), "client error", "client error: read error: EOF"},
}
for_, tt:=rangetests {
got:=Wrap(tt.err, tt.message).Error()
ifgot!=tt.want {
t.Errorf("Wrap(%v, %q): got: %v, want %v", tt.err, tt.message, got, tt.want)
}
}
}
typenilErrorstruct{}
func (nilError) Error() string { return"nil error" }
funcTestCause(t*testing.T) {
x:=New("error")
tests:= []struct {
errerror
wanterror
}{{
// nil error is nil
err: nil,
want: nil,
}, {
// explicit nil error is nil
err: (error)(nil),
want: nil,
}, {
// typed nil is nil
err: (*nilError)(nil),
want: (*nilError)(nil),
}, {
// uncaused error is unaffected
err: io.EOF,
want: io.EOF,
}, {
// caused error returns cause
err: Wrap(io.EOF, "ignored"),
want: io.EOF,
}, {
err: x, // return from errors.New
want: x,
}}
fori, tt:=rangetests {
got:=Cause(tt.err)
if!reflect.DeepEqual(got, tt.want) {
t.Errorf("test %d: got %#v, want %#v", i+1, got, tt.want)
}
}
}
funcTestWrapfNil(t*testing.T) {
got:=Wrapf(nil, "no error")
ifgot!=nil {
t.Errorf("Wrapf(nil, \"no error\"): got %#v, expected nil", got)
}
}
funcTestWrapf(t*testing.T) {
tests:= []struct {
errerror
messagestring
wantstring
}{
{io.EOF, "read error", "read error: EOF"},
{Wrapf(io.EOF, "read error without format specifiers"), "client error", "client error: read error without format specifiers: EOF"},
{Wrapf(io.EOF, "read error with %d format specifier", 1), "client error", "client error: read error with 1 format specifier: EOF"},
}
for_, tt:=rangetests {
got:=Wrapf(tt.err, tt.message).Error()
ifgot!=tt.want {
t.Errorf("Wrapf(%v, %q): got: %v, want %v", tt.err, tt.message, got, tt.want)
}
}
}
funcTestErrorf(t*testing.T) {
tests:= []struct {
errerror
wantstring
}{
{Errorf("read error without format specifiers"), "read error without format specifiers"},
{Errorf("read error with %d format specifier", 1), "read error with 1 format specifier"},
}
for_, tt:=rangetests {
got:=tt.err.Error()
ifgot!=tt.want {
t.Errorf("Errorf(%v): got: %q, want %q", tt.err, got, tt.want)
}
}
}
// errors.New, etc values are not expected to be compared by value
// but the change in errors#27 made them incomparable. Assert that
// various kinds of errors have a functional equality operator, even
// if the result of that equality is always false.
funcTestErrorEquality(t*testing.T) {
tests:= []struct {
err1, err2error
}{
{io.EOF, io.EOF},
{io.EOF, nil},
{io.EOF, errors.New("EOF")},
{io.EOF, New("EOF")},
{New("EOF"), New("EOF")},
{New("EOF"), Errorf("EOF")},
{New("EOF"), Wrap(io.EOF, "EOF")},
}
for_, tt:=rangetests {
_=tt.err1==tt.err2// mustn't panic
}
}