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. 4 5package fmt_test 6 7import ( 8 "errors" 9 "fmt" 10 "reflect" 11 "testing" 12) 13 14func TestErrorf(t *testing.T) { 15 // noVetErrorf is an alias for fmt.Errorf that does not trigger vet warnings for 16 // %w format strings. 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: noVetErrorf("wrapped two errors: %w %w", errString("1"), errString("2")), 59 wantText: "wrapped two errors: 1 2", 60 wantSplit: []error{errString("1"), errString("2")}, 61 }, { 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")}, 65 }, { 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")}, 69 }, { 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")}, 73 }, { 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")}, 77 }, { 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")}, 81 }, { 82 err: fmt.Errorf("%w", nil), 83 wantText: "%!w(<nil>)", 84 wantUnwrap: nil, // still nil 85 }} { 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) 88 } 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) 91 } 92 if got, want := test.err.Error(), test.wantText; got != want { 93 t.Errorf("err.Error() = %q, want %q", got, want) 94 } 95 } 96} 97 98func splitErr(err error) []error { 99 if e, ok := err.(interface{ Unwrap() []error }); ok { 100 return e.Unwrap() 101 } 102 return nil 103} 104 105type errString string 106 107func (e errString) Error() string { return string(e) } 108