|
| 1 | +// Copyright 2024 FishGoddess. All rights reserved. |
| 2 | +// Use of this source code is governed by a MIT style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package x |
| 6 | + |
| 7 | +import ( |
| 8 | + "os" |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +type testError struct { |
| 13 | + reason string |
| 14 | +} |
| 15 | + |
| 16 | +func (te *testError) Code() int32 { |
| 17 | + return 500 |
| 18 | +} |
| 19 | + |
| 20 | +func (te *testError) Error() string { |
| 21 | + return te.reason |
| 22 | +} |
| 23 | + |
| 24 | +// go test -v -cover -count=1 -test.cpu=1 -run=^TestIs$ |
| 25 | +func TestIs(t *testing.T) { |
| 26 | + testErr := &testError{reason: "test error"} |
| 27 | + wrapErr := Wrap(testErr, -1000, "wrap test error") |
| 28 | + |
| 29 | + testCases := []struct { |
| 30 | + err error |
| 31 | + cause error |
| 32 | + }{ |
| 33 | + { |
| 34 | + err: Wrap(testErr, 1000, "wow"), |
| 35 | + cause: testErr, |
| 36 | + }, |
| 37 | + { |
| 38 | + err: Wrap(wrapErr, 1000, "wow too"), |
| 39 | + cause: testErr, |
| 40 | + }, |
| 41 | + } |
| 42 | + |
| 43 | + for _, testCase := range testCases { |
| 44 | + if ok := Is(testCase.err, testCase.cause); !ok { |
| 45 | + t.Errorf("testCase.err %+v isn't testCase.cause %+v", testCase.err, testCase.cause) |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +// go test -v -cover -count=1 -test.cpu=1 -run=^TestAs$ |
| 51 | +func TestAs(t *testing.T) { |
| 52 | + testErr := &testError{reason: "test error"} |
| 53 | + wrapErr := Wrap(testErr, -1000, "wrap test error") |
| 54 | + |
| 55 | + targetTestErr := &testError{} |
| 56 | + targetTestErr2 := &testError{} |
| 57 | + targetPathErr := &os.PathError{} |
| 58 | + |
| 59 | + testCases := []struct { |
| 60 | + err error |
| 61 | + target any |
| 62 | + ok bool |
| 63 | + want any |
| 64 | + }{ |
| 65 | + { |
| 66 | + err: Wrap(testErr, 1000, "wow"), |
| 67 | + target: &targetTestErr, |
| 68 | + ok: true, |
| 69 | + want: &testErr, |
| 70 | + }, |
| 71 | + { |
| 72 | + err: Wrap(wrapErr, 1000, "wow too"), |
| 73 | + target: &targetTestErr2, |
| 74 | + ok: true, |
| 75 | + want: &testErr, |
| 76 | + }, |
| 77 | + { |
| 78 | + err: New(1000, "no"), |
| 79 | + target: &targetPathErr, |
| 80 | + ok: false, |
| 81 | + want: nil, |
| 82 | + }, |
| 83 | + } |
| 84 | + |
| 85 | + for _, testCase := range testCases { |
| 86 | + ok := As(testCase.err, testCase.target) |
| 87 | + if ok != testCase.ok { |
| 88 | + t.Errorf("err %+v ok %+v != err %+v testCase.ok %+v", testCase.err, testCase.target, ok, testCase.ok) |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +// go test -v -cover -count=1 -test.cpu=1 -run=^TestUnwrap$ |
| 94 | +func TestUnwrap(t *testing.T) { |
| 95 | + testErr := &testError{reason: "test error"} |
| 96 | + wrapErr := Wrap(testErr, -1000, "wrap test error") |
| 97 | + |
| 98 | + testCases := []struct { |
| 99 | + err error |
| 100 | + cause error |
| 101 | + }{ |
| 102 | + { |
| 103 | + err: testErr, |
| 104 | + cause: nil, |
| 105 | + }, |
| 106 | + { |
| 107 | + err: Wrap(testErr, 1000, "wow"), |
| 108 | + cause: testErr, |
| 109 | + }, |
| 110 | + { |
| 111 | + err: Wrap(wrapErr, 1000, "wow too"), |
| 112 | + cause: wrapErr, |
| 113 | + }, |
| 114 | + } |
| 115 | + |
| 116 | + for _, testCase := range testCases { |
| 117 | + if Unwrap(testCase.err) != testCase.cause { |
| 118 | + t.Errorf("Unwrap(testCase.err) %+v != testCase.cause %+v", Unwrap(testCase.err), testCase.cause) |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments