Source file
src/fmt/errors_test.go
1
2
3
4
5 package fmt_test
6
7 import (
8 "errors"
9 "fmt"
10 "reflect"
11 "testing"
12 )
13
14 func TestErrorf(t *testing.T) {
15
16
17 noVetErrorf := fmt.Errorf
18
19 wrapped := errors.New("inner error")
20 for _, test := range []struct {
21 err error
22 wantText string
23 wantUnwrap error
24 wantSplit []error
25 }{{
26 err: fmt.Errorf("%w", wrapped),
27 wantText: "inner error",
28 wantUnwrap: wrapped,
29 }, {
30 err: fmt.Errorf("added context: %w", wrapped),
31 wantText: "added context: inner error",
32 wantUnwrap: wrapped,
33 }, {
34 err: fmt.Errorf("%w with added context", wrapped),
35 wantText: "inner error with added context",
36 wantUnwrap: wrapped,
37 }, {
38 err: fmt.Errorf("%s %w %v", "prefix", wrapped, "suffix"),
39 wantText: "prefix inner error suffix",
40 wantUnwrap: wrapped,
41 }, {
42 err: fmt.Errorf("%[2]s: %[1]w", wrapped, "positional verb"),
43 wantText: "positional verb: inner error",
44 wantUnwrap: wrapped,
45 }, {
46 err: fmt.Errorf("%v", wrapped),
47 wantText: "inner error",
48 }, {
49 err: fmt.Errorf("added context: %v", wrapped),
50 wantText: "added context: inner error",
51 }, {
52 err: fmt.Errorf("%v with added context", wrapped),
53 wantText: "inner error with added context",
54 }, {
55 err: noVetErrorf("%w is not an error", "not-an-error"),
56 wantText: "%!w(string=not-an-error) is not an error",
57 }, {
58 err: fmt.Errorf("no verbs"),
59 wantText: "no verbs",
60 }, {
61 err: noVetErrorf("no verbs with extra arg", "extra"),
62 wantText: "no verbs with extra arg%!(EXTRA string=extra)",
63 }, {
64 err: noVetErrorf("too many verbs: %w %v"),
65 wantText: "too many verbs: %!w(MISSING) %!v(MISSING)",
66 }, {
67 err: noVetErrorf("wrapped two errors: %w %w", errString("1"), errString("2")),
68 wantText: "wrapped two errors: 1 2",
69 wantSplit: []error{errString("1"), errString("2")},
70 }, {
71 err: noVetErrorf("wrapped three errors: %w %w %w", errString("1"), errString("2"), errString("3")),
72 wantText: "wrapped three errors: 1 2 3",
73 wantSplit: []error{errString("1"), errString("2"), errString("3")},
74 }, {
75 err: noVetErrorf("wrapped nil error: %w %w %w", errString("1"), nil, errString("2")),
76 wantText: "wrapped nil error: 1 %!w(<nil>) 2",
77 wantSplit: []error{errString("1"), errString("2")},
78 }, {
79 err: noVetErrorf("wrapped one non-error: %w %w %w", errString("1"), "not-an-error", errString("3")),
80 wantText: "wrapped one non-error: 1 %!w(string=not-an-error) 3",
81 wantSplit: []error{errString("1"), errString("3")},
82 }, {
83 err: fmt.Errorf("wrapped errors out of order: %[3]w %[2]w %[1]w", errString("1"), errString("2"), errString("3")),
84 wantText: "wrapped errors out of order: 3 2 1",
85 wantSplit: []error{errString("1"), errString("2"), errString("3")},
86 }, {
87 err: fmt.Errorf("wrapped several times: %[1]w %[1]w %[2]w %[1]w", errString("1"), errString("2")),
88 wantText: "wrapped several times: 1 1 2 1",
89 wantSplit: []error{errString("1"), errString("2")},
90 }, {
91 err: fmt.Errorf("%w", nil),
92 wantText: "%!w(<nil>)",
93 wantUnwrap: nil,
94 }} {
95 if got, want := errors.Unwrap(test.err), test.wantUnwrap; got != want {
96 t.Errorf("Formatted error: %v\nerrors.Unwrap() = %v, want %v", test.err, got, want)
97 }
98 if got, want := splitErr(test.err), test.wantSplit; !reflect.DeepEqual(got, want) {
99 t.Errorf("Formatted error: %v\nUnwrap() []error = %v, want %v", test.err, got, want)
100 }
101 if got, want := test.err.Error(), test.wantText; got != want {
102 t.Errorf("err.Error() = %q, want %q", got, want)
103 }
104 }
105 }
106
107 func splitErr(err error) []error {
108 if e, ok := err.(interface{ Unwrap() []error }); ok {
109 return e.Unwrap()
110 }
111 return nil
112 }
113
114 type errString string
115
116 func (e errString) Error() string { return string(e) }
117
View as plain text