11 func TestNew(t *testing.T) {
17 {"foo", fmt.Errorf("foo")},
19 {"string with format specifiers: %v", errors.New("string with format specifiers: %v")},
22 for _, tt := range tests {
24 if got.Error() != tt.want.Error() {
25 t.Errorf("New.Error(): got: %q, want %q", got, tt.want)
30 func TestWrapNil(t *testing.T) {
31 got := Wrap(nil, "no error")
33 t.Errorf("Wrap(nil, \"no error\"): got %#v, expected nil", got)
37 func TestWrap(t *testing.T) {
43 {io.EOF, "read error", "read error: EOF"},
44 {Wrap(io.EOF, "read error"), "client error", "client error: read error: EOF"},
47 for _, tt := range tests {
48 got := Wrap(tt.err, tt.message).Error()
50 t.Errorf("Wrap(%v, %q): got: %v, want %v", tt.err, tt.message, got, tt.want)
55 type nilError struct{}
57 func (nilError) Error() string { return "nil error" }
59 func TestCause(t *testing.T) {
69 // explicit nil error is nil
74 err: (*nilError)(nil),
75 want: (*nilError)(nil),
77 // uncaused error is unaffected
81 // caused error returns cause
82 err: Wrap(io.EOF, "ignored"),
85 err: x, // return from errors.New
88 WithMessage(nil, "whoops"),
91 WithMessage(io.EOF, "whoops"),
101 for i, tt := range tests {
103 if !reflect.DeepEqual(got, tt.want) {
104 t.Errorf("test %d: got %#v, want %#v", i+1, got, tt.want)
109 func TestWrapfNil(t *testing.T) {
110 got := Wrapf(nil, "no error")
112 t.Errorf("Wrapf(nil, \"no error\"): got %#v, expected nil", got)
116 func TestWrapf(t *testing.T) {
122 {io.EOF, "read error", "read error: EOF"},
123 {Wrapf(io.EOF, "read error without format specifiers"), "client error", "client error: read error without format specifiers: EOF"},
124 {Wrapf(io.EOF, "read error with %d format specifier", 1), "client error", "client error: read error with 1 format specifier: EOF"},
127 for _, tt := range tests {
128 got := Wrapf(tt.err, tt.message).Error()
130 t.Errorf("Wrapf(%v, %q): got: %v, want %v", tt.err, tt.message, got, tt.want)
135 func TestErrorf(t *testing.T) {
140 {Errorf("read error without format specifiers"), "read error without format specifiers"},
141 {Errorf("read error with %d format specifier", 1), "read error with 1 format specifier"},
144 for _, tt := range tests {
145 got := tt.err.Error()
147 t.Errorf("Errorf(%v): got: %q, want %q", tt.err, got, tt.want)
152 func TestWithStackNil(t *testing.T) {
153 got := WithStack(nil)
155 t.Errorf("WithStack(nil): got %#v, expected nil", got)
159 func TestWithStack(t *testing.T) {
165 {WithStack(io.EOF), "EOF"},
168 for _, tt := range tests {
169 got := WithStack(tt.err).Error()
171 t.Errorf("WithStack(%v): got: %v, want %v", tt.err, got, tt.want)
176 func TestWithMessageNil(t *testing.T) {
177 got := WithMessage(nil, "no error")
179 t.Errorf("WithMessage(nil, \"no error\"): got %#v, expected nil", got)
183 func TestWithMessage(t *testing.T) {
189 {io.EOF, "read error", "read error: EOF"},
190 {WithMessage(io.EOF, "read error"), "client error", "client error: read error: EOF"},
193 for _, tt := range tests {
194 got := WithMessage(tt.err, tt.message).Error()
196 t.Errorf("WithMessage(%v, %q): got: %q, want %q", tt.err, tt.message, got, tt.want)
201 // errors.New, etc values are not expected to be compared by value
202 // but the change in errors#27 made them incomparable. Assert that
203 // various kinds of errors have a functional equality operator, even
204 // if the result of that equality is always false.
205 func TestErrorEquality(t *testing.T) {
213 Wrapf(io.EOF, "EOF%d", 2),
214 WithMessage(nil, "whoops"),
215 WithMessage(io.EOF, "whoops"),
220 for i := range vals {
221 for j := range vals {
222 _ = vals[i] == vals[j] // mustn't panic