| 1 |
// Licensed under the MIT license, see LICENSE file for details. |
| 2 |
|
| 3 |
package quicktest_test |
| 4 |
|
| 5 |
import ( |
| 6 |
"errors" |
| 7 |
"fmt" |
| 8 |
|
| 9 |
qt "github.com/frankban/quicktest" |
| 10 |
) |
| 11 |
|
| 12 |
func init() { |
| 13 |
checkerTests = append(checkerTests, errorCheckerTests...) |
| 14 |
} |
| 15 |
|
| 16 |
type errTarget struct { |
| 17 |
msg string |
| 18 |
} |
| 19 |
|
| 20 |
func (e *errTarget) Error() string { |
| 21 |
return "ptr: " + e.msg |
| 22 |
} |
| 23 |
|
| 24 |
type errTargetNonPtr struct { |
| 25 |
msg string |
| 26 |
} |
| 27 |
|
| 28 |
func (e errTargetNonPtr) Error() string { |
| 29 |
return "non ptr: " + e.msg |
| 30 |
} |
| 31 |
|
| 32 |
var targetErr = &errTarget{msg: "target"} |
| 33 |
|
| 34 |
var errorCheckerTests = []struct { |
| 35 |
about string |
| 36 |
checker qt.Checker |
| 37 |
got interface{} |
| 38 |
args []interface{} |
| 39 |
verbose bool |
| 40 |
expectedCheckFailure string |
| 41 |
expectedNegateFailure string |
| 42 |
}{{ |
| 43 |
about: "ErrorAs: exact match", |
| 44 |
checker: qt.ErrorAs, |
| 45 |
got: targetErr, |
| 46 |
args: []interface{}{new(*errTarget)}, |
| 47 |
expectedNegateFailure: ` |
| 48 |
error: |
| 49 |
unexpected success |
| 50 |
got: |
| 51 |
e"ptr: target" |
| 52 |
as: |
| 53 |
&&quicktest_test.errTarget{msg:"target"} |
| 54 |
`, |
| 55 |
}, { |
| 56 |
about: "ErrorAs: wrapped match", |
| 57 |
checker: qt.ErrorAs, |
| 58 |
got: fmt.Errorf("wrapped: %w", targetErr), |
| 59 |
args: []interface{}{new(*errTarget)}, |
| 60 |
expectedNegateFailure: ` |
| 61 |
error: |
| 62 |
unexpected success |
| 63 |
got: |
| 64 |
e"wrapped: ptr: target" |
| 65 |
as: |
| 66 |
&&quicktest_test.errTarget{msg:"target"} |
| 67 |
`, |
| 68 |
}, { |
| 69 |
about: "ErrorAs: fails if nil error", |
| 70 |
checker: qt.ErrorAs, |
| 71 |
got: nil, |
| 72 |
args: []interface{}{new(*errTarget)}, |
| 73 |
expectedCheckFailure: ` |
| 74 |
error: |
| 75 |
got nil error but want non-nil |
| 76 |
got: |
| 77 |
nil |
| 78 |
as: |
| 79 |
&(*quicktest_test.errTarget)(nil) |
| 80 |
`, |
| 81 |
}, { |
| 82 |
about: "ErrorAs: fails if mismatch", |
| 83 |
checker: qt.ErrorAs, |
| 84 |
got: errors.New("other error"), |
| 85 |
args: []interface{}{new(*errTarget)}, |
| 86 |
expectedCheckFailure: ` |
| 87 |
error: |
| 88 |
wanted type is not found in error chain |
| 89 |
got: |
| 90 |
e"other error" |
| 91 |
as: |
| 92 |
**quicktest_test.errTarget |
| 93 |
`, |
| 94 |
}, { |
| 95 |
about: "ErrorAs: fails if mismatch with a non-pointer error implementation", |
| 96 |
checker: qt.ErrorAs, |
| 97 |
got: errors.New("other error"), |
| 98 |
args: []interface{}{new(errTargetNonPtr)}, |
| 99 |
expectedCheckFailure: ` |
| 100 |
error: |
| 101 |
wanted type is not found in error chain |
| 102 |
got: |
| 103 |
e"other error" |
| 104 |
as: |
| 105 |
*quicktest_test.errTargetNonPtr |
| 106 |
`, |
| 107 |
}, { |
| 108 |
about: "ErrorAs: bad check if invalid error", |
| 109 |
checker: qt.ErrorAs, |
| 110 |
got: "not an error", |
| 111 |
args: []interface{}{new(*errTarget)}, |
| 112 |
expectedCheckFailure: ` |
| 113 |
error: |
| 114 |
bad check: first argument is not an error |
| 115 |
got: |
| 116 |
"not an error" |
| 117 |
`, |
| 118 |
expectedNegateFailure: ` |
| 119 |
error: |
| 120 |
bad check: first argument is not an error |
| 121 |
got: |
| 122 |
"not an error" |
| 123 |
`, |
| 124 |
}, { |
| 125 |
about: "ErrorAs: bad check if invalid as", |
| 126 |
checker: qt.ErrorAs, |
| 127 |
got: targetErr, |
| 128 |
args: []interface{}{&struct{}{}}, |
| 129 |
expectedCheckFailure: ` |
| 130 |
error: |
| 131 |
bad check: errors: *target must be interface or implement error |
| 132 |
`, |
| 133 |
expectedNegateFailure: ` |
| 134 |
error: |
| 135 |
bad check: errors: *target must be interface or implement error |
| 136 |
`, |
| 137 |
}, { |
| 138 |
about: "ErrorIs: nil to nil match", |
| 139 |
checker: qt.ErrorIs, |
| 140 |
got: nil, |
| 141 |
args: []interface{}{nil}, |
| 142 |
expectedNegateFailure: ` |
| 143 |
error: |
| 144 |
unexpected success |
| 145 |
got: |
| 146 |
nil |
| 147 |
want: |
| 148 |
<same as "got"> |
| 149 |
`, |
| 150 |
}, { |
| 151 |
about: "ErrorIs: non-nil to nil mismatch", |
| 152 |
checker: qt.ErrorIs, |
| 153 |
got: targetErr, |
| 154 |
args: []interface{}{nil}, |
| 155 |
expectedCheckFailure: ` |
| 156 |
error: |
| 157 |
wanted error is not found in error chain |
| 158 |
got: |
| 159 |
e"ptr: target" |
| 160 |
want: |
| 161 |
nil |
| 162 |
`, |
| 163 |
}, { |
| 164 |
about: "ErrorIs: exact match", |
| 165 |
checker: qt.ErrorIs, |
| 166 |
got: targetErr, |
| 167 |
args: []interface{}{targetErr}, |
| 168 |
expectedNegateFailure: ` |
| 169 |
error: |
| 170 |
unexpected success |
| 171 |
got: |
| 172 |
e"ptr: target" |
| 173 |
want: |
| 174 |
<same as "got"> |
| 175 |
`, |
| 176 |
}, { |
| 177 |
about: "ErrorIs: wrapped match", |
| 178 |
checker: qt.ErrorIs, |
| 179 |
got: fmt.Errorf("wrapped: %w", targetErr), |
| 180 |
args: []interface{}{targetErr}, |
| 181 |
expectedNegateFailure: ` |
| 182 |
error: |
| 183 |
unexpected success |
| 184 |
got: |
| 185 |
e"wrapped: ptr: target" |
| 186 |
want: |
| 187 |
e"ptr: target" |
| 188 |
`, |
| 189 |
}, { |
| 190 |
about: "ErrorIs: fails if nil error", |
| 191 |
checker: qt.ErrorIs, |
| 192 |
got: nil, |
| 193 |
args: []interface{}{targetErr}, |
| 194 |
expectedCheckFailure: ` |
| 195 |
error: |
| 196 |
got nil error but want non-nil |
| 197 |
got: |
| 198 |
nil |
| 199 |
want: |
| 200 |
e"ptr: target" |
| 201 |
`, |
| 202 |
}, { |
| 203 |
about: "ErrorIs: fails if mismatch", |
| 204 |
checker: qt.ErrorIs, |
| 205 |
got: errors.New("other error"), |
| 206 |
args: []interface{}{targetErr}, |
| 207 |
expectedCheckFailure: ` |
| 208 |
error: |
| 209 |
wanted error is not found in error chain |
| 210 |
got: |
| 211 |
e"other error" |
| 212 |
want: |
| 213 |
e"ptr: target" |
| 214 |
`, |
| 215 |
}, { |
| 216 |
about: "ErrorIs: bad check if invalid error", |
| 217 |
checker: qt.ErrorIs, |
| 218 |
got: "not an error", |
| 219 |
args: []interface{}{targetErr}, |
| 220 |
expectedCheckFailure: ` |
| 221 |
error: |
| 222 |
bad check: first argument is not an error |
| 223 |
got: |
| 224 |
"not an error" |
| 225 |
`, |
| 226 |
expectedNegateFailure: ` |
| 227 |
error: |
| 228 |
bad check: first argument is not an error |
| 229 |
got: |
| 230 |
"not an error" |
| 231 |
`, |
| 232 |
}, { |
| 233 |
about: "ErrorIs: bad check if invalid error value", |
| 234 |
checker: qt.ErrorIs, |
| 235 |
got: targetErr, |
| 236 |
args: []interface{}{"not an error"}, |
| 237 |
expectedCheckFailure: ` |
| 238 |
error: |
| 239 |
bad check: second argument is not an error |
| 240 |
want: |
| 241 |
"not an error" |
| 242 |
`, |
| 243 |
expectedNegateFailure: ` |
| 244 |
error: |
| 245 |
bad check: second argument is not an error |
| 246 |
want: |
| 247 |
"not an error" |
| 248 |
`, |
| 249 |
}} |