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.
14 func TestErrorf(t *testing.T) {
15 // noVetErrorf is an alias for fmt.Errorf that does not trigger vet warnings for
17 noVetErrorf := fmt.Errorf
19 wrapped := errors.New("inner error")
20 for _, test := range []struct {
26 err: fmt.Errorf("%w", wrapped),
27 wantText: "inner error",
30 err: fmt.Errorf("added context: %w", wrapped),
31 wantText: "added context: inner error",
34 err: fmt.Errorf("%w with added context", wrapped),
35 wantText: "inner error with added context",
38 err: fmt.Errorf("%s %w %v", "prefix", wrapped, "suffix"),
39 wantText: "prefix inner error suffix",
42 err: fmt.Errorf("%[2]s: %[1]w", wrapped, "positional verb"),
43 wantText: "positional verb: inner error",
46 err: fmt.Errorf("%v", wrapped),
47 wantText: "inner error",
49 err: fmt.Errorf("added context: %v", wrapped),
50 wantText: "added context: inner error",
52 err: fmt.Errorf("%v with added context", wrapped),
53 wantText: "inner error with added context",
55 err: noVetErrorf("%w is not an error", "not-an-error"),
56 wantText: "%!w(string=not-an-error) is not an error",
58 err: noVetErrorf("wrapped two errors: %w %w", errString("1"), errString("2")),
59 wantText: "wrapped two errors: 1 2",
60 wantSplit: []error{errString("1"), errString("2")},
62 err: noVetErrorf("wrapped three errors: %w %w %w", errString("1"), errString("2"), errString("3")),
63 wantText: "wrapped three errors: 1 2 3",
64 wantSplit: []error{errString("1"), errString("2"), errString("3")},
66 err: noVetErrorf("wrapped nil error: %w %w %w", errString("1"), nil, errString("2")),
67 wantText: "wrapped nil error: 1 %!w(<nil>) 2",
68 wantSplit: []error{errString("1"), errString("2")},
70 err: noVetErrorf("wrapped one non-error: %w %w %w", errString("1"), "not-an-error", errString("3")),
71 wantText: "wrapped one non-error: 1 %!w(string=not-an-error) 3",
72 wantSplit: []error{errString("1"), errString("3")},
74 err: fmt.Errorf("wrapped errors out of order: %[3]w %[2]w %[1]w", errString("1"), errString("2"), errString("3")),
75 wantText: "wrapped errors out of order: 3 2 1",
76 wantSplit: []error{errString("1"), errString("2"), errString("3")},
78 err: fmt.Errorf("wrapped several times: %[1]w %[1]w %[2]w %[1]w", errString("1"), errString("2")),
79 wantText: "wrapped several times: 1 1 2 1",
80 wantSplit: []error{errString("1"), errString("2")},
82 err: fmt.Errorf("%w", nil),
83 wantText: "%!w(<nil>)",
84 wantUnwrap: nil, // still nil
86 if got, want := errors.Unwrap(test.err), test.wantUnwrap; got != want {
87 t.Errorf("Formatted error: %v\nerrors.Unwrap() = %v, want %v", test.err, got, want)
89 if got, want := splitErr(test.err), test.wantSplit; !reflect.DeepEqual(got, want) {
90 t.Errorf("Formatted error: %v\nUnwrap() []error = %v, want %v", test.err, got, want)
92 if got, want := test.err.Error(), test.wantText; got != want {
93 t.Errorf("err.Error() = %q, want %q", got, want)
98 func splitErr(err error) []error {
99 if e, ok := err.(interface{ Unwrap() []error }); ok {
105 type errString string
107 func (e errString) Error() string { return string(e) }