|
| 1 | +// Copyright 2024 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 | + |
| 5 | +package message_test |
| 6 | + |
| 7 | +import ( |
| 8 | + "errors" |
| 9 | + "reflect" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "golang.org/x/text/language" |
| 13 | + "golang.org/x/text/message" |
| 14 | +) |
| 15 | + |
| 16 | +func TestErorrf(t *testing.T) { |
| 17 | + wrapped := errors.New("inner error") |
| 18 | + p := message.NewPrinter(language.Und) |
| 19 | + for _, test := range []struct { |
| 20 | + err error |
| 21 | + wantText string |
| 22 | + wantUnwrap error |
| 23 | + wantSplit []error |
| 24 | + }{{ |
| 25 | + err: p.Errorf("%w", wrapped), |
| 26 | + wantText: "inner error", |
| 27 | + wantUnwrap: wrapped, |
| 28 | + }, { |
| 29 | + err: p.Errorf("added context: %w", wrapped), |
| 30 | + wantText: "added context: inner error", |
| 31 | + wantUnwrap: wrapped, |
| 32 | + }, { |
| 33 | + err: p.Errorf("%w with added context", wrapped), |
| 34 | + wantText: "inner error with added context", |
| 35 | + wantUnwrap: wrapped, |
| 36 | + }, { |
| 37 | + err: p.Errorf("%s %w %v", "prefix", wrapped, "suffix"), |
| 38 | + wantText: "prefix inner error suffix", |
| 39 | + wantUnwrap: wrapped, |
| 40 | + }, { |
| 41 | + err: p.Errorf("%[2]s: %[1]w", wrapped, "positional verb"), |
| 42 | + wantText: "positional verb: inner error", |
| 43 | + wantUnwrap: wrapped, |
| 44 | + }, { |
| 45 | + err: p.Errorf("%v", wrapped), |
| 46 | + wantText: "inner error", |
| 47 | + }, { |
| 48 | + err: p.Errorf("added context: %v", wrapped), |
| 49 | + wantText: "added context: inner error", |
| 50 | + }, { |
| 51 | + err: p.Errorf("%v with added context", wrapped), |
| 52 | + wantText: "inner error with added context", |
| 53 | + }, { |
| 54 | + err: p.Errorf("%w is not an error", "not-an-error"), |
| 55 | + wantText: "%!w(string=not-an-error) is not an error", |
| 56 | + }, { |
| 57 | + err: p.Errorf("wrapped two errors: %w %w", errString("1"), errString("2")), |
| 58 | + wantText: "wrapped two errors: 1 2", |
| 59 | + wantSplit: []error{errString("1"), errString("2")}, |
| 60 | + }, { |
| 61 | + err: p.Errorf("wrapped three errors: %w %w %w", errString("1"), errString("2"), errString("3")), |
| 62 | + wantText: "wrapped three errors: 1 2 3", |
| 63 | + wantSplit: []error{errString("1"), errString("2"), errString("3")}, |
| 64 | + }, { |
| 65 | + err: p.Errorf("wrapped nil error: %w %w %w", errString("1"), nil, errString("2")), |
| 66 | + wantText: "wrapped nil error: 1 %!w(<nil>) 2", |
| 67 | + wantSplit: []error{errString("1"), errString("2")}, |
| 68 | + }, { |
| 69 | + err: p.Errorf("wrapped one non-error: %w %w %w", errString("1"), "not-an-error", errString("3")), |
| 70 | + wantText: "wrapped one non-error: 1 %!w(string=not-an-error) 3", |
| 71 | + wantSplit: []error{errString("1"), errString("3")}, |
| 72 | + }, { |
| 73 | + err: p.Errorf("wrapped errors out of order: %[3]w %[2]w %[1]w", errString("1"), errString("2"), errString("3")), |
| 74 | + wantText: "wrapped errors out of order: 3 2 1", |
| 75 | + wantSplit: []error{errString("1"), errString("2"), errString("3")}, |
| 76 | + }, { |
| 77 | + err: p.Errorf("wrapped several times: %[1]w %[1]w %[2]w %[1]w", errString("1"), errString("2")), |
| 78 | + wantText: "wrapped several times: 1 1 2 1", |
| 79 | + wantSplit: []error{errString("1"), errString("2")}, |
| 80 | + }, { |
| 81 | + err: p.Errorf("%w", nil), |
| 82 | + wantText: "%!w(<nil>)", |
| 83 | + wantUnwrap: nil, // still nil |
| 84 | + }} { |
| 85 | + if got, want := errors.Unwrap(test.err), test.wantUnwrap; got != want { |
| 86 | + t.Errorf("Formatted error: %v\nerrors.Unwrap() = %v, want %v", test.err, got, want) |
| 87 | + } |
| 88 | + if got, want := splitErr(test.err), test.wantSplit; !reflect.DeepEqual(got, want) { |
| 89 | + t.Errorf("Formatted error: %v\nUnwrap() []error = %v, want %v", test.err, got, want) |
| 90 | + } |
| 91 | + if got, want := test.err.Error(), test.wantText; got != want { |
| 92 | + t.Errorf("err.Error() = %q, want %q", got, want) |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func splitErr(err error) []error { |
| 98 | + if e, ok := err.(interface{ Unwrap() []error }); ok { |
| 99 | + return e.Unwrap() |
| 100 | + } |
| 101 | + return nil |
| 102 | +} |
| 103 | + |
| 104 | +type errString string |
| 105 | + |
| 106 | +func (e errString) Error() string { return string(e) } |
0 commit comments