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.
12 "golang.org/x/xerrors"
15 func TestIs(t *testing.T) {
16 err1 := xerrors.New("1")
17 erra := xerrors.Errorf("wrap 2: %w", err1)
18 errb := xerrors.Errorf("wrap 3: %w", erra)
19 erro := xerrors.Opaque(err1)
20 errco := xerrors.Errorf("opaque: %w", erro)
21 err3 := xerrors.New("3")
23 poser := &poser{"either 1 or 3", func(err error) bool {
24 return err == err1 || err == err3
27 testCases := []struct {
49 {poser, errco, false},
50 {errorUncomparable{}, errorUncomparable{}, true},
51 {errorUncomparable{}, &errorUncomparable{}, false},
52 {&errorUncomparable{}, errorUncomparable{}, true},
53 {&errorUncomparable{}, &errorUncomparable{}, false},
54 {errorUncomparable{}, err1, false},
55 {&errorUncomparable{}, err1, false},
57 for _, tc := range testCases {
58 t.Run("", func(t *testing.T) {
59 if got := xerrors.Is(tc.err, tc.target); got != tc.match {
60 t.Errorf("Is(%v, %v) = %v, want %v", tc.err, tc.target, got, tc.match)
71 func (p *poser) Error() string { return p.msg }
72 func (p *poser) Is(err error) bool { return p.f(err) }
73 func (p *poser) As(err interface{}) bool {
74 switch x := err.(type) {
87 func TestAs(t *testing.T) {
89 var errP *os.PathError
90 var timeout interface{ Timeout() bool }
92 _, errF := os.Open("non-existing")
94 testCases := []struct {
103 xerrors.Errorf("pittied the fool: %w", errorT{}),
111 xerrors.Opaque(errT),
123 &poser{"error", nil},
131 &poser{"oh no", nil},
143 xerrors.Errorf("path error: %w", errF),
147 for i, tc := range testCases {
148 name := fmt.Sprintf("%d:As(Errorf(..., %v), %v)", i, tc.err, tc.target)
149 t.Run(name, func(t *testing.T) {
150 match := xerrors.As(tc.err, tc.target)
151 if match != tc.match {
152 t.Fatalf("xerrors.As(%T, %T): got %v; want %v", tc.err, tc.target, match, tc.match)
157 if tc.target == nil {
158 t.Fatalf("non-nil result after match")
164 func TestAsValidation(t *testing.T) {
166 testCases := []interface{}{
172 err := xerrors.New("error")
173 for _, tc := range testCases {
174 t.Run(fmt.Sprintf("%T(%v)", tc, tc), func(t *testing.T) {
178 if xerrors.As(err, tc) {
179 t.Errorf("As(err, %T(%v)) = true, want false", tc, tc)
182 t.Errorf("As(err, %T(%v)) did not panic", tc, tc)
187 func TestUnwrap(t *testing.T) {
188 err1 := xerrors.New("1")
189 erra := xerrors.Errorf("wrap 2: %w", err1)
190 erro := xerrors.Opaque(err1)
192 testCases := []struct {
200 {xerrors.Errorf("wrap 3: %w", erra), erra},
203 {xerrors.Errorf("opaque: %w", erro), erro},
205 for _, tc := range testCases {
206 if got := xerrors.Unwrap(tc.err); got != tc.want {
207 t.Errorf("Unwrap(%v) = %v, want %v", tc.err, got, tc.want)
212 func TestOpaque(t *testing.T) {
213 got := fmt.Sprintf("%v", xerrors.Errorf("foo: %v", xerrors.Opaque(errorT{})))
214 want := "foo: errorT"
216 t.Errorf("error without Format: got %v; want %v", got, want)
219 got = fmt.Sprintf("%v", xerrors.Errorf("foo: %v", xerrors.Opaque(errorD{})))
222 t.Errorf("error with Format: got %v; want %v", got, want)
228 func (errorT) Error() string { return "errorT" }
232 func (errorD) Error() string { return "errorD" }
234 func (errorD) FormatError(p xerrors.Printer) error {
241 type errWrap struct{ error }
243 func (errWrap) Error() string { return "wrapped" }
245 func (errWrap) Unwrap() error { return nil }
247 type errorUncomparable struct {
251 func (errorUncomparable) Error() string {
252 return "uncomparable error"
255 func (errorUncomparable) Is(target error) bool {
256 _, ok := target.(errorUncomparable)