1 // Copyright 2018 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
16 func TestIs(t *testing.T) {
17 err1 := errors.New("1")
18 erra := wrapped{"wrap 2", err1}
19 errb := wrapped{"wrap 3", erra}
21 err3 := errors.New("3")
23 poser := &poser{"either 1 or 3", func(err error) bool {
24 return err == err1 || err == err3
27 testCases := []struct {
44 {errorUncomparable{}, errorUncomparable{}, true},
45 {errorUncomparable{}, &errorUncomparable{}, false},
46 {&errorUncomparable{}, errorUncomparable{}, true},
47 {&errorUncomparable{}, &errorUncomparable{}, false},
48 {errorUncomparable{}, err1, false},
49 {&errorUncomparable{}, err1, false},
50 {multiErr{}, err1, false},
51 {multiErr{err1, err3}, err1, true},
52 {multiErr{err3, err1}, err1, true},
53 {multiErr{err1, err3}, errors.New("x"), false},
54 {multiErr{err3, errb}, errb, true},
55 {multiErr{err3, errb}, erra, true},
56 {multiErr{err3, errb}, err1, true},
57 {multiErr{errb, err3}, err1, true},
58 {multiErr{poser}, err1, true},
59 {multiErr{poser}, err3, true},
60 {multiErr{nil}, nil, false},
62 for _, tc := range testCases {
63 t.Run("", func(t *testing.T) {
64 if got := errors.Is(tc.err, tc.target); got != tc.match {
65 t.Errorf("Is(%v, %v) = %v, want %v", tc.err, tc.target, got, tc.match)
76 var poserPathErr = &fs.PathError{Op: "poser"}
78 func (p *poser) Error() string { return p.msg }
79 func (p *poser) Is(err error) bool { return p.f(err) }
80 func (p *poser) As(err any) bool {
81 switch x := err.(type) {
94 func TestAs(t *testing.T) {
96 var errP *fs.PathError
97 var timeout interface{ Timeout() bool }
99 _, errF := os.Open("non-existing")
100 poserErr := &poser{"oh no", nil}
102 testCases := []struct {
106 want any // value of target on match
113 wrapped{"pitied the fool", errorT{"T"}},
128 wrapped{"wrapped", nil},
133 &poser{"error", nil},
158 wrapped{"path error", errF},
168 multiErr{errors.New("a"), errorT{"T"}},
173 multiErr{errorT{"T"}, errors.New("a")},
178 multiErr{errorT{"a"}, errorT{"b"}},
183 multiErr{multiErr{errors.New("a"), errorT{"a"}}, errorT{"b"}},
188 multiErr{wrapped{"path error", errF}},
198 for i, tc := range testCases {
199 name := fmt.Sprintf("%d:As(Errorf(..., %v), %v)", i, tc.err, tc.target)
200 // Clear the target pointer, in case it was set in a previous test.
201 rtarget := reflect.ValueOf(tc.target)
202 rtarget.Elem().Set(reflect.Zero(reflect.TypeOf(tc.target).Elem()))
203 t.Run(name, func(t *testing.T) {
204 match := errors.As(tc.err, tc.target)
205 if match != tc.match {
206 t.Fatalf("match: got %v; want %v", match, tc.match)
211 if got := rtarget.Elem().Interface(); got != tc.want {
212 t.Fatalf("got %#v, want %#v", got, tc.want)
218 func TestAsValidation(t *testing.T) {
226 err := errors.New("error")
227 for _, tc := range testCases {
228 t.Run(fmt.Sprintf("%T(%v)", tc, tc), func(t *testing.T) {
232 if errors.As(err, tc) {
233 t.Errorf("As(err, %T(%v)) = true, want false", tc, tc)
236 t.Errorf("As(err, %T(%v)) did not panic", tc, tc)
241 func BenchmarkIs(b *testing.B) {
242 err1 := errors.New("1")
243 err2 := multiErr{multiErr{multiErr{err1, errorT{"a"}}, errorT{"b"}}}
245 for i := 0; i < b.N; i++ {
246 if !errors.Is(err2, err1) {
252 func BenchmarkAs(b *testing.B) {
253 err := multiErr{multiErr{multiErr{errors.New("a"), errorT{"a"}}, errorT{"b"}}}
254 for i := 0; i < b.N; i++ {
256 if !errors.As(err, &target) {
262 func TestUnwrap(t *testing.T) {
263 err1 := errors.New("1")
264 erra := wrapped{"wrap 2", err1}
266 testCases := []struct {
271 {wrapped{"wrapped", nil}, nil},
274 {wrapped{"wrap 3", erra}, erra},
276 for _, tc := range testCases {
277 if got := errors.Unwrap(tc.err); got != tc.want {
278 t.Errorf("Unwrap(%v) = %v, want %v", tc.err, got, tc.want)
283 type errorT struct{ s string }
285 func (e errorT) Error() string { return fmt.Sprintf("errorT(%s)", e.s) }
287 type wrapped struct {
292 func (e wrapped) Error() string { return e.msg }
293 func (e wrapped) Unwrap() error { return e.err }
295 type multiErr []error
297 func (m multiErr) Error() string { return "multiError" }
298 func (m multiErr) Unwrap() []error { return []error(m) }
300 type errorUncomparable struct {
304 func (errorUncomparable) Error() string {
305 return "uncomparable error"
308 func (errorUncomparable) Is(target error) bool {
309 _, ok := target.(errorUncomparable)