1
2
3
4
5 package errors
6
7 import (
8 "errors"
9 "strings"
10 "testing"
11 )
12
13 func TestErrors(t *testing.T) {
14 var sentinel = New("sentinel")
15 var foreign = errors.New("foreign")
16 for _, test := range []struct {
17 what string
18 err error
19 wantText string
20 is []error
21 isNot []error
22 }{{
23 what: `New("abc")`,
24 err: New("abc"),
25 wantText: "abc",
26 }, {
27 what: `New("%v", sentinel)`,
28 err: New("%v", sentinel),
29 wantText: "sentinel",
30 isNot: []error{sentinel},
31 }, {
32 what: `Wrap(sentinel, "%v", "text")`,
33 err: Wrap(sentinel, "%v", "text"),
34 wantText: "text: sentinel",
35 is: []error{sentinel},
36 }, {
37 what: `New("%v", foreign)`,
38 err: New("%v", foreign),
39 wantText: "foreign",
40 isNot: []error{foreign},
41 }, {
42 what: `Wrap(foreign, "%v", "text")`,
43 err: Wrap(foreign, "%v", "text"),
44 wantText: "text: foreign",
45 is: []error{foreign},
46 }} {
47 if got, want := test.err.Error(), prefix; !strings.HasPrefix(got, want) {
48 t.Errorf("%v.Error() = %q, want prefix %q", test.what, got, want)
49 }
50 if got, want := test.err.Error(), prefix+test.wantText; got != want {
51 t.Errorf("%v.Error() = %q, want %q", test.what, got, want)
52 }
53 if got, want := Is(test.err, Error), true; got != want {
54 t.Errorf("errors.Is(%v, errors.Error) = %v, want %v", test.what, got, want)
55 }
56 for _, err := range test.is {
57 if got, want := Is(test.err, err), true; got != want {
58 t.Errorf("errors.Is(%v, %v) = %v, want %v", test.what, err, got, want)
59 }
60 }
61 for _, err := range test.isNot {
62 if got, want := Is(test.err, err), false; got != want {
63 t.Errorf("errors.Is(%v, %v) = %v, want %v", test.what, err, got, want)
64 }
65 }
66 }
67 }
68
View as plain text