|
5 | 5 | package errors |
6 | 6 |
|
7 | 7 | import ( |
| 8 | + "fmt" |
8 | 9 | "io" |
9 | 10 | "testing" |
10 | 11 | ) |
11 | 12 |
|
| 13 | +func compareMaps(m1 map[string]any, m2 map[string]any) error { |
| 14 | + for k1, v1 := range m1 { |
| 15 | + v2, ok := m2[k1] |
| 16 | + if !ok { |
| 17 | + return fmt.Errorf("key %s not found in m2 %+v", k1, m2) |
| 18 | + } |
| 19 | + |
| 20 | + if v1 != v2 { |
| 21 | + return fmt.Errorf("v1 %+v != v2 %+v", v1, v2) |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + for k2, v2 := range m2 { |
| 26 | + v1, ok := m1[k2] |
| 27 | + if !ok { |
| 28 | + return fmt.Errorf("key %s not found in m1 %+v", k2, m1) |
| 29 | + } |
| 30 | + |
| 31 | + if v1 != v2 { |
| 32 | + return fmt.Errorf("v1 %+v != v2 %+v", v1, v2) |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + return nil |
| 37 | +} |
| 38 | + |
12 | 39 | // go test -v -cover -count=1 -test.cpu=1 -run=^TestWrap$ |
13 | 40 | func TestWrap(t *testing.T) { |
14 | 41 | testCases := []struct { |
@@ -66,6 +93,44 @@ func TestWrapWith(t *testing.T) { |
66 | 93 | } |
67 | 94 | } |
68 | 95 |
|
| 96 | +// go test -v -cover -count=1 -test.cpu=1 -run=^TestWrapWithArgs$ |
| 97 | +func TestWrapWithArgs(t *testing.T) { |
| 98 | + testCases := []struct { |
| 99 | + code int32 |
| 100 | + message string |
| 101 | + cause error |
| 102 | + args []any |
| 103 | + wantArgs map[string]any |
| 104 | + }{ |
| 105 | + {code: -1000, message: "eof", cause: io.EOF, args: nil, wantArgs: nil}, |
| 106 | + {code: 1000, message: "need login", cause: nil, args: nil, wantArgs: nil}, |
| 107 | + {code: 1000, message: "need login", cause: nil, args: []any{io.EOF}, wantArgs: map[string]any{keyBad + "_1": io.EOF}}, |
| 108 | + {code: -1000, message: "eof", cause: io.EOF, args: []any{1, true, 3.14, io.EOF}, wantArgs: map[string]any{keyBad + "_1": 1, keyBad + "_2": true, keyBad + "_3": 3.14, keyBad + "_4": io.EOF}}, |
| 109 | + {code: -1000, message: "eof", cause: io.EOF, args: []any{1, true, 3.14, "abc"}, wantArgs: map[string]any{keyBad + "_1": 1, keyBad + "_2": true, keyBad + "_3": 3.14, keyBad + "_4": "abc"}}, |
| 110 | + {code: 1000, message: "need login", cause: io.EOF, args: []any{"k1", 1, "k2", true, "k3", 3.14, "key", "abc"}, wantArgs: map[string]any{"k1": 1, "k2": true, "k3": 3.14, "key": "abc"}}, |
| 111 | + } |
| 112 | + |
| 113 | + for _, testCase := range testCases { |
| 114 | + err := Wrap(testCase.code, testCase.message).With(testCase.cause).WithArgs(testCase.args...) |
| 115 | + if err.Code() != testCase.code { |
| 116 | + t.Errorf("err.Code() %d != testCase.code %d", err.Code(), testCase.code) |
| 117 | + } |
| 118 | + |
| 119 | + if err.Message() != testCase.message { |
| 120 | + t.Errorf("err.Message() %s != testCase.code %s", err.Message(), testCase.message) |
| 121 | + } |
| 122 | + |
| 123 | + if err.Unwrap() != testCase.cause { |
| 124 | + t.Errorf("err.Unwrap() %+v != testCase.code %+v", err.Unwrap(), testCase.cause) |
| 125 | + } |
| 126 | + |
| 127 | + args := err.Args() |
| 128 | + if merr := compareMaps(args, testCase.wantArgs); merr != nil { |
| 129 | + t.Error(merr) |
| 130 | + } |
| 131 | + } |
| 132 | +} |
| 133 | + |
69 | 134 | // go test -v -cover -count=1 -test.cpu=1 -run=^TestErrorError$ |
70 | 135 | func TestErrorError(t *testing.T) { |
71 | 136 | testCases := []struct { |
|
0 commit comments