|
| 1 | +package tst |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "reflect" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +const expectedError = "Expected error but none received" |
| 14 | + |
| 15 | +type TestingT interface { |
| 16 | + Errorf(format string, args ...interface{}) |
| 17 | + FailNow() |
| 18 | +} |
| 19 | + |
| 20 | +type ErrorAssertionFunc func(t TestingT, err error) bool |
| 21 | + |
| 22 | +func (e ErrorAssertionFunc) AsRequire() require.ErrorAssertionFunc { |
| 23 | + return func(tt require.TestingT, err error, _ ...any) { |
| 24 | + if suc := e(tt, err); !suc { |
| 25 | + tt.FailNow() |
| 26 | + } |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +func (e ErrorAssertionFunc) AsAssert() assert.ErrorAssertionFunc { |
| 31 | + return func(tt assert.TestingT, err error, _ ...any) bool { |
| 32 | + t, is := tt.(TestingT) |
| 33 | + if is { |
| 34 | + return e(t, err) |
| 35 | + } |
| 36 | + |
| 37 | + // not possible |
| 38 | + tt.Errorf("Wrong TestingT type %T", tt) |
| 39 | + return false |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func NoError() ErrorAssertionFunc { |
| 44 | + return func(t TestingT, err error) bool { |
| 45 | + if h, ok := t.(interface{ Helper() }); ok { |
| 46 | + h.Helper() |
| 47 | + } |
| 48 | + |
| 49 | + if err != nil { |
| 50 | + t.Errorf("Expected nil error but received : %T(%s)", err, err.Error()) |
| 51 | + return false |
| 52 | + } |
| 53 | + |
| 54 | + return true |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +func Error() ErrorAssertionFunc { |
| 59 | + return func(t TestingT, err error) bool { |
| 60 | + if h, ok := t.(interface{ Helper() }); ok { |
| 61 | + h.Helper() |
| 62 | + } |
| 63 | + |
| 64 | + if err == nil { |
| 65 | + t.Errorf(expectedError) |
| 66 | + return false |
| 67 | + } |
| 68 | + |
| 69 | + return true |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +// ErrorIs returns an ErrorAssertionFunc that checks if the given error matches |
| 74 | +// any of the expected errors using errors.Is. |
| 75 | +// If no expected errors are provided, it simply checks that an error is present (similar to Error()). |
| 76 | +// Returns false if the error is nil or doesn't match any expected errors. |
| 77 | +func ErrorIs(allExpectedErrors ...error) ErrorAssertionFunc { |
| 78 | + return func(t TestingT, err error) bool { |
| 79 | + if h, ok := t.(interface{ Helper() }); ok { |
| 80 | + h.Helper() |
| 81 | + } |
| 82 | + if err == nil { |
| 83 | + t.Errorf(expectedError) |
| 84 | + return false |
| 85 | + } |
| 86 | + |
| 87 | + if len(allExpectedErrors) == 0 { |
| 88 | + return true |
| 89 | + } |
| 90 | + |
| 91 | + suc := true |
| 92 | + notMatched := make([]error, 0, len(allExpectedErrors)) |
| 93 | + for _, expected := range allExpectedErrors { |
| 94 | + if !errors.Is(err, expected) { |
| 95 | + notMatched = append(notMatched, expected) |
| 96 | + suc = false |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + if !suc { |
| 101 | + sb := strings.Builder{} |
| 102 | + sb.WriteString("Error is unexpected.\n") |
| 103 | + sb.WriteString(fmt.Sprintf("Got error : %T(%s)\n", err, err.Error())) |
| 104 | + |
| 105 | + if len(notMatched) == 1 { |
| 106 | + sb.WriteString(fmt.Sprintf("Expected error : %T(%s)\n", notMatched[0], notMatched[0].Error())) |
| 107 | + t.Errorf(sb.String()) |
| 108 | + return suc |
| 109 | + } |
| 110 | + |
| 111 | + sb.WriteString("Expected errors:\n") |
| 112 | + for _, e := range notMatched { |
| 113 | + sb.WriteString(fmt.Sprintf(" -> %T(%s)\n", e, e.Error())) |
| 114 | + } |
| 115 | + t.Errorf(sb.String()) |
| 116 | + } |
| 117 | + |
| 118 | + return suc |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +func ErrorOfType[T error](typedAsserts ...func(TestingT, T)) ErrorAssertionFunc { |
| 123 | + return func(t TestingT, err error) bool { |
| 124 | + if h, ok := t.(interface{ Helper() }); ok { |
| 125 | + h.Helper() |
| 126 | + } |
| 127 | + |
| 128 | + if err == nil { |
| 129 | + t.Errorf(expectedError) |
| 130 | + return false |
| 131 | + } |
| 132 | + |
| 133 | + var wantErr T |
| 134 | + if !errors.As(err, &wantErr) { |
| 135 | + var tErr T |
| 136 | + t.Errorf("Error type check failed.\nExpected error type: %T\nGot : %T(%s)", tErr, err, err) |
| 137 | + return false |
| 138 | + } |
| 139 | + |
| 140 | + if v := reflect.ValueOf(wantErr); v.Kind() == reflect.Pointer && v.IsNil() { |
| 141 | + t.Errorf("Error check failed.\nExpected not nill error value: %T\nGot : %T(nil)", wantErr, wantErr) |
| 142 | + return false |
| 143 | + } |
| 144 | + |
| 145 | + for _, e := range typedAsserts { |
| 146 | + e(t, wantErr) |
| 147 | + } |
| 148 | + |
| 149 | + return true |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +func ErrorStringContains(s string) ErrorAssertionFunc { |
| 154 | + return func(t TestingT, err error) bool { |
| 155 | + if h, ok := t.(interface{ Helper() }); ok { |
| 156 | + h.Helper() |
| 157 | + } |
| 158 | + |
| 159 | + if err == nil { |
| 160 | + t.Errorf(expectedError) |
| 161 | + return false |
| 162 | + } |
| 163 | + |
| 164 | + // consider case insensitive? |
| 165 | + if !strings.Contains(err.Error(), s) { |
| 166 | + t.Errorf("Error string check failed. \nExpected to contain: %s\nGot : %s\n", s, err.Error()) |
| 167 | + return false |
| 168 | + } |
| 169 | + |
| 170 | + return true |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +func All(expected ...ErrorAssertionFunc) ErrorAssertionFunc { |
| 175 | + return func(t TestingT, err error) bool { |
| 176 | + if h, ok := t.(interface{ Helper() }); ok { |
| 177 | + h.Helper() |
| 178 | + } |
| 179 | + |
| 180 | + ret := true |
| 181 | + for _, fn := range expected { |
| 182 | + ok := fn(t, err) |
| 183 | + if !ok { |
| 184 | + ret = ok |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + return ret |
| 189 | + } |
| 190 | +} |
0 commit comments