| 1 |
// Licensed under the MIT license, see LICENSE file for details. |
| 2 |
|
| 3 |
package qt_test |
| 4 |
|
| 5 |
import ( |
| 6 |
"encoding/json" |
| 7 |
"errors" |
| 8 |
"fmt" |
| 9 |
"regexp" |
| 10 |
"strings" |
| 11 |
"testing" |
| 12 |
"time" |
| 13 |
|
| 14 |
"github.com/google/go-cmp/cmp" |
| 15 |
"github.com/google/go-cmp/cmp/cmpopts" |
| 16 |
|
| 17 |
"github.com/go-quicktest/qt" |
| 18 |
) |
| 19 |
|
| 20 |
// errTarget is an error implemented as a pointer. |
| 21 |
type errTarget struct { |
| 22 |
msg string |
| 23 |
} |
| 24 |
|
| 25 |
func (e *errTarget) Error() string { |
| 26 |
return "ptr: " + e.msg |
| 27 |
} |
| 28 |
|
| 29 |
// errTargetNonPtr is an error implemented as a non-pointer. |
| 30 |
type errTargetNonPtr struct { |
| 31 |
msg string |
| 32 |
} |
| 33 |
|
| 34 |
func (e errTargetNonPtr) Error() string { |
| 35 |
return "non ptr: " + e.msg |
| 36 |
} |
| 37 |
|
| 38 |
// Fooer is an interface for testing. |
| 39 |
type Fooer interface { |
| 40 |
Foo() |
| 41 |
} |
| 42 |
|
| 43 |
type cmpType struct { |
| 44 |
Strings []any |
| 45 |
Ints []int |
| 46 |
} |
| 47 |
|
| 48 |
type InnerJSON struct { |
| 49 |
First string |
| 50 |
Second int `json:",omitempty" yaml:",omitempty"` |
| 51 |
Third map[string]bool `json:",omitempty" yaml:",omitempty"` |
| 52 |
} |
| 53 |
|
| 54 |
type OuterJSON struct { |
| 55 |
First float64 |
| 56 |
Second []*InnerJSON `json:"Last,omitempty" yaml:"last,omitempty"` |
| 57 |
} |
| 58 |
|
| 59 |
type boolean bool |
| 60 |
|
| 61 |
var ( |
| 62 |
targetErr = &errTarget{msg: "target"} |
| 63 |
|
| 64 |
goTime = time.Date(2012, 3, 28, 0, 0, 0, 0, time.UTC) |
| 65 |
chInt = func() chan int { |
| 66 |
ch := make(chan int, 4) |
| 67 |
ch <- 42 |
| 68 |
ch <- 47 |
| 69 |
return ch |
| 70 |
}() |
| 71 |
sameInts = cmpopts.SortSlices(func(x, y int) bool { |
| 72 |
return x < y |
| 73 |
}) |
| 74 |
|
| 75 |
cmpEqualsGot = cmpType{ |
| 76 |
Strings: []any{"who", "dalek"}, |
| 77 |
Ints: []int{42, 47}, |
| 78 |
} |
| 79 |
cmpEqualsWant = cmpType{ |
| 80 |
Strings: []any{"who", "dalek"}, |
| 81 |
Ints: []int{42}, |
| 82 |
} |
| 83 |
) |
| 84 |
|
| 85 |
var checkerTests = []struct { |
| 86 |
about string |
| 87 |
checker qt.Checker |
| 88 |
verbose bool |
| 89 |
expectedCheckFailure string |
| 90 |
expectedNegateFailure string |
| 91 |
}{{ |
| 92 |
about: "Equals: same values", |
| 93 |
checker: qt.Equals(42, 42), |
| 94 |
expectedNegateFailure: ` |
| 95 |
error: |
| 96 |
unexpected success |
| 97 |
got: |
| 98 |
int(42) |
| 99 |
want: |
| 100 |
<same as "got"> |
| 101 |
`, |
| 102 |
}, { |
| 103 |
about: "Equals: different values", |
| 104 |
checker: qt.Equals("42", "47"), |
| 105 |
expectedCheckFailure: ` |
| 106 |
error: |
| 107 |
values are not equal |
| 108 |
got: |
| 109 |
"42" |
| 110 |
want: |
| 111 |
"47" |
| 112 |
`, |
| 113 |
}, { |
| 114 |
about: "Equals: different strings with quotes", |
| 115 |
checker: qt.Equals(`string "foo"`, `string "bar"`), |
| 116 |
expectedCheckFailure: tilde2bq(` |
| 117 |
error: |
| 118 |
values are not equal |
| 119 |
got: |
| 120 |
~string "foo"~ |
| 121 |
want: |
| 122 |
~string "bar"~ |
| 123 |
`), |
| 124 |
}, { |
| 125 |
about: "Equals: same multiline strings", |
| 126 |
checker: qt.Equals("a\nmultiline\nstring", "a\nmultiline\nstring"), |
| 127 |
expectedNegateFailure: ` |
| 128 |
error: |
| 129 |
unexpected success |
| 130 |
got: |
| 131 |
"a\nmultiline\nstring" |
| 132 |
want: |
| 133 |
<same as "got"> |
| 134 |
`}, { |
| 135 |
about: "Equals: different multi-line strings", |
| 136 |
checker: qt.Equals("a\nlong\nmultiline\nstring", "just\na\nlong\nmulti-line\nstring\n"), |
| 137 |
expectedCheckFailure: fmt.Sprintf(` |
| 138 |
error: |
| 139 |
values are not equal |
| 140 |
line diff (-want +got): |
| 141 |
%s |
| 142 |
got: |
| 143 |
"a\nlong\nmultiline\nstring" |
| 144 |
want: |
| 145 |
"just\na\nlong\nmulti-line\nstring\n" |
| 146 |
`, diff([]string{"a\n", "long\n", "multiline\n", "string"}, []string{"just\n", "a\n", "long\n", "multi-line\n", "string\n", ""})), |
| 147 |
}, { |
| 148 |
about: "Equals: different single-line strings ending with newline", |
| 149 |
checker: qt.Equals("foo\n", "bar\n"), |
| 150 |
expectedCheckFailure: ` |
| 151 |
error: |
| 152 |
values are not equal |
| 153 |
got: |
| 154 |
"foo\n" |
| 155 |
want: |
| 156 |
"bar\n" |
| 157 |
`, |
| 158 |
}, { |
| 159 |
about: "Equals: different strings starting with newline", |
| 160 |
checker: qt.Equals("\nfoo", "\nbar"), |
| 161 |
expectedCheckFailure: fmt.Sprintf(` |
| 162 |
error: |
| 163 |
values are not equal |
| 164 |
line diff (-want +got): |
| 165 |
%s |
| 166 |
got: |
| 167 |
"\nfoo" |
| 168 |
want: |
| 169 |
"\nbar" |
| 170 |
`, diff([]string{"\n", "foo"}, []string{"\n", "bar"})), |
| 171 |
}, { |
| 172 |
about: "Equals: different types", |
| 173 |
checker: qt.Equals(42, any("42")), |
| 174 |
expectedCheckFailure: ` |
| 175 |
error: |
| 176 |
values are not equal |
| 177 |
got: |
| 178 |
int(42) |
| 179 |
want: |
| 180 |
"42" |
| 181 |
`}, { |
| 182 |
about: "Equals: nil and nil", |
| 183 |
checker: qt.Equals(nil, any(nil)), |
| 184 |
expectedNegateFailure: ` |
| 185 |
error: |
| 186 |
unexpected success |
| 187 |
got: |
| 188 |
nil |
| 189 |
want: |
| 190 |
<same as "got"> |
| 191 |
`, |
| 192 |
}, { |
| 193 |
about: "Equals: error is not nil", |
| 194 |
checker: qt.Equals(error(errBadWolf), error(nil)), |
| 195 |
expectedCheckFailure: ` |
| 196 |
error: |
| 197 |
got non-nil error |
| 198 |
got: |
| 199 |
bad wolf |
| 200 |
file:line |
| 201 |
want: |
| 202 |
nil |
| 203 |
`}, { |
| 204 |
about: "Equals: error is not nil: not formatted", |
| 205 |
checker: qt.Equals[error](&errTest{ |
| 206 |
msg: "bad wolf", |
| 207 |
}, nil), |
| 208 |
expectedCheckFailure: ` |
| 209 |
error: |
| 210 |
got non-nil error |
| 211 |
got: |
| 212 |
e"bad wolf" |
| 213 |
want: |
| 214 |
nil |
| 215 |
`, |
| 216 |
}, { |
| 217 |
about: "Equals: error does not guard against nil", |
| 218 |
checker: qt.Equals[error]((*errTest)(nil), nil), |
| 219 |
expectedCheckFailure: ` |
| 220 |
error: |
| 221 |
got non-nil error |
| 222 |
got: |
| 223 |
e<nil> |
| 224 |
want: |
| 225 |
nil |
| 226 |
`, |
| 227 |
}, { |
| 228 |
about: "Equals: error is not nil: not formatted and with quotes", |
| 229 |
checker: qt.Equals[error](&errTest{ |
| 230 |
msg: `failure: "bad wolf"`, |
| 231 |
}, nil), |
| 232 |
expectedCheckFailure: tilde2bq(` |
| 233 |
error: |
| 234 |
got non-nil error |
| 235 |
got: |
| 236 |
e~failure: "bad wolf"~ |
| 237 |
want: |
| 238 |
nil |
| 239 |
`), |
| 240 |
}, { |
| 241 |
about: "Equals: different errors with same message", |
| 242 |
checker: qt.Equals[error](&errTest{ |
| 243 |
msg: "bad wolf", |
| 244 |
}, errors.New("bad wolf")), |
| 245 |
expectedCheckFailure: ` |
| 246 |
error: |
| 247 |
values are not equal |
| 248 |
got type: |
| 249 |
*qt_test.errTest |
| 250 |
want type: |
| 251 |
*errors.errorString |
| 252 |
got: |
| 253 |
e"bad wolf" |
| 254 |
want: |
| 255 |
<same as "got" but different pointer value> |
| 256 |
`, |
| 257 |
}, { |
| 258 |
about: "Equals: different pointer errors with the same message", |
| 259 |
checker: qt.Equals(targetErr, &errTarget{msg: "target"}), |
| 260 |
expectedCheckFailure: ` |
| 261 |
error: |
| 262 |
values are not equal |
| 263 |
got: |
| 264 |
e"ptr: target" |
| 265 |
want: |
| 266 |
<same as "got" but different pointer value> |
| 267 |
`, |
| 268 |
}, { |
| 269 |
about: "Equals: different pointers with the same formatted output", |
| 270 |
checker: qt.Equals(new(int), new(int)), |
| 271 |
expectedCheckFailure: ` |
| 272 |
error: |
| 273 |
values are not equal |
| 274 |
got: |
| 275 |
&int(0) |
| 276 |
want: |
| 277 |
<same as "got" but different pointer value> |
| 278 |
`, |
| 279 |
}, { |
| 280 |
about: "Equals: nil struct", |
| 281 |
checker: qt.Equals[any]((*struct{})(nil), nil), |
| 282 |
expectedCheckFailure: ` |
| 283 |
error: |
| 284 |
values are not equal |
| 285 |
got: |
| 286 |
(*struct {})(nil) |
| 287 |
want: |
| 288 |
nil |
| 289 |
`, |
| 290 |
}, { |
| 291 |
about: "Equals: different booleans", |
| 292 |
checker: qt.Equals(true, false), |
| 293 |
expectedCheckFailure: ` |
| 294 |
error: |
| 295 |
values are not equal |
| 296 |
got: |
| 297 |
bool(true) |
| 298 |
want: |
| 299 |
bool(false) |
| 300 |
`, |
| 301 |
}, { |
| 302 |
about: "Equals: uncomparable types", |
| 303 |
checker: qt.Equals[any](struct { |
| 304 |
Ints []int |
| 305 |
}{ |
| 306 |
Ints: []int{42, 47}, |
| 307 |
}, struct { |
| 308 |
Ints []int |
| 309 |
}{ |
| 310 |
Ints: []int{42, 47}, |
| 311 |
}), |
| 312 |
expectedCheckFailure: ` |
| 313 |
error: |
| 314 |
runtime error: comparing uncomparable type struct { Ints []int } |
| 315 |
got: |
| 316 |
struct { Ints []int }{ |
| 317 |
Ints: {42, 47}, |
| 318 |
} |
| 319 |
want: |
| 320 |
<same as "got"> |
| 321 |
`}, { |
| 322 |
about: "DeepEquals: same values", |
| 323 |
checker: qt.DeepEquals(cmpEqualsGot, cmpEqualsGot), |
| 324 |
expectedNegateFailure: ` |
| 325 |
error: |
| 326 |
unexpected success |
| 327 |
got: |
| 328 |
qt_test.cmpType{ |
| 329 |
Strings: { |
| 330 |
"who", |
| 331 |
"dalek", |
| 332 |
}, |
| 333 |
Ints: {42, 47}, |
| 334 |
} |
| 335 |
want: |
| 336 |
<same as "got"> |
| 337 |
`, |
| 338 |
}, { |
| 339 |
about: "DeepEquals: different values", |
| 340 |
checker: qt.DeepEquals(cmpEqualsGot, cmpEqualsWant), |
| 341 |
expectedCheckFailure: fmt.Sprintf(` |
| 342 |
error: |
| 343 |
values are not deep equal |
| 344 |
diff (-want +got): |
| 345 |
%s |
| 346 |
got: |
| 347 |
qt_test.cmpType{ |
| 348 |
Strings: { |
| 349 |
"who", |
| 350 |
"dalek", |
| 351 |
}, |
| 352 |
Ints: {42, 47}, |
| 353 |
} |
| 354 |
want: |
| 355 |
qt_test.cmpType{ |
| 356 |
Strings: { |
| 357 |
"who", |
| 358 |
"dalek", |
| 359 |
}, |
| 360 |
Ints: {42}, |
| 361 |
} |
| 362 |
`, diff(cmpEqualsGot, cmpEqualsWant)), |
| 363 |
}, { |
| 364 |
about: "DeepEquals: different values: long output", |
| 365 |
checker: qt.DeepEquals([]any{cmpEqualsWant, cmpEqualsWant}, []any{cmpEqualsWant, cmpEqualsWant, 42}), |
| 366 |
expectedCheckFailure: fmt.Sprintf(` |
| 367 |
error: |
| 368 |
values are not deep equal |
| 369 |
diff (-want +got): |
| 370 |
%s |
| 371 |
got: |
| 372 |
<suppressed due to length (15 lines), use -v for full output> |
| 373 |
want: |
| 374 |
<suppressed due to length (16 lines), use -v for full output> |
| 375 |
`, diff([]any{cmpEqualsWant, cmpEqualsWant}, []any{cmpEqualsWant, cmpEqualsWant, 42})), |
| 376 |
}, { |
| 377 |
about: "DeepEquals: different values: long output and verbose", |
| 378 |
checker: qt.DeepEquals([]any{cmpEqualsWant, cmpEqualsWant}, []any{cmpEqualsWant, cmpEqualsWant, 42}), |
| 379 |
verbose: true, |
| 380 |
expectedCheckFailure: fmt.Sprintf(` |
| 381 |
error: |
| 382 |
values are not deep equal |
| 383 |
diff (-want +got): |
| 384 |
%s |
| 385 |
got: |
| 386 |
[]interface {}{ |
| 387 |
qt_test.cmpType{ |
| 388 |
Strings: { |
| 389 |
"who", |
| 390 |
"dalek", |
| 391 |
}, |
| 392 |
Ints: {42}, |
| 393 |
}, |
| 394 |
qt_test.cmpType{ |
| 395 |
Strings: { |
| 396 |
"who", |
| 397 |
"dalek", |
| 398 |
}, |
| 399 |
Ints: {42}, |
| 400 |
}, |
| 401 |
} |
| 402 |
want: |
| 403 |
[]interface {}{ |
| 404 |
qt_test.cmpType{ |
| 405 |
Strings: { |
| 406 |
"who", |
| 407 |
"dalek", |
| 408 |
}, |
| 409 |
Ints: {42}, |
| 410 |
}, |
| 411 |
qt_test.cmpType{ |
| 412 |
Strings: { |
| 413 |
"who", |
| 414 |
"dalek", |
| 415 |
}, |
| 416 |
Ints: {42}, |
| 417 |
}, |
| 418 |
int(42), |
| 419 |
} |
| 420 |
`, diff([]any{cmpEqualsWant, cmpEqualsWant}, []any{cmpEqualsWant, cmpEqualsWant, 42})), |
| 421 |
}, { |
| 422 |
about: "CmpEquals: different values, long output", |
| 423 |
checker: qt.CmpEquals([]any{cmpEqualsWant, "extra line 1", "extra line 2", "extra line 3"}, []any{cmpEqualsWant, "extra line 1"}), |
| 424 |
expectedCheckFailure: fmt.Sprintf(` |
| 425 |
error: |
| 426 |
values are not deep equal |
| 427 |
diff (-want +got): |
| 428 |
%s |
| 429 |
got: |
| 430 |
<suppressed due to length (11 lines), use -v for full output> |
| 431 |
want: |
| 432 |
[]interface {}{ |
| 433 |
qt_test.cmpType{ |
| 434 |
Strings: { |
| 435 |
"who", |
| 436 |
"dalek", |
| 437 |
}, |
| 438 |
Ints: {42}, |
| 439 |
}, |
| 440 |
"extra line 1", |
| 441 |
} |
| 442 |
`, diff([]any{cmpEqualsWant, "extra line 1", "extra line 2", "extra line 3"}, []any{cmpEqualsWant, "extra line 1"})), |
| 443 |
}, { |
| 444 |
about: "CmpEquals: different values: long output and verbose", |
| 445 |
checker: qt.CmpEquals([]any{cmpEqualsWant, "extra line 1", "extra line 2"}, []any{cmpEqualsWant, "extra line 1"}), |
| 446 |
verbose: true, |
| 447 |
expectedCheckFailure: fmt.Sprintf(` |
| 448 |
error: |
| 449 |
values are not deep equal |
| 450 |
diff (-want +got): |
| 451 |
%s |
| 452 |
got: |
| 453 |
[]interface {}{ |
| 454 |
qt_test.cmpType{ |
| 455 |
Strings: { |
| 456 |
"who", |
| 457 |
"dalek", |
| 458 |
}, |
| 459 |
Ints: {42}, |
| 460 |
}, |
| 461 |
"extra line 1", |
| 462 |
"extra line 2", |
| 463 |
} |
| 464 |
want: |
| 465 |
[]interface {}{ |
| 466 |
qt_test.cmpType{ |
| 467 |
Strings: { |
| 468 |
"who", |
| 469 |
"dalek", |
| 470 |
}, |
| 471 |
Ints: {42}, |
| 472 |
}, |
| 473 |
"extra line 1", |
| 474 |
} |
| 475 |
`, diff([]any{cmpEqualsWant, "extra line 1", "extra line 2"}, []any{cmpEqualsWant, "extra line 1"})), |
| 476 |
}, { |
| 477 |
about: "CmpEquals: different values, long output, same number of lines", |
| 478 |
checker: qt.CmpEquals([]any{cmpEqualsWant, "extra line 1", "extra line 2", "extra line 3"}, []any{cmpEqualsWant, "extra line 1", "extra line 2", "extra line three"}), |
| 479 |
expectedCheckFailure: fmt.Sprintf(` |
| 480 |
error: |
| 481 |
values are not deep equal |
| 482 |
diff (-want +got): |
| 483 |
%s |
| 484 |
got: |
| 485 |
<suppressed due to length (11 lines), use -v for full output> |
| 486 |
want: |
| 487 |
<suppressed due to length (11 lines), use -v for full output> |
| 488 |
`, diff([]any{cmpEqualsWant, "extra line 1", "extra line 2", "extra line 3"}, []any{cmpEqualsWant, "extra line 1", "extra line 2", "extra line three"})), |
| 489 |
}, { |
| 490 |
about: "CmpEquals: same values with options", |
| 491 |
checker: qt.CmpEquals([]int{1, 2, 3}, []int{3, 2, 1}, sameInts), |
| 492 |
expectedNegateFailure: ` |
| 493 |
error: |
| 494 |
unexpected success |
| 495 |
got: |
| 496 |
[]int{1, 2, 3} |
| 497 |
want: |
| 498 |
[]int{3, 2, 1} |
| 499 |
`, |
| 500 |
}, { |
| 501 |
about: "CmpEquals: different values with options", |
| 502 |
checker: qt.CmpEquals([]int{1, 2, 4}, []int{3, 2, 1}, sameInts), |
| 503 |
expectedCheckFailure: fmt.Sprintf(` |
| 504 |
error: |
| 505 |
values are not deep equal |
| 506 |
diff (-want +got): |
| 507 |
%s |
| 508 |
got: |
| 509 |
[]int{1, 2, 4} |
| 510 |
want: |
| 511 |
[]int{3, 2, 1} |
| 512 |
`, diff([]int{1, 2, 4}, []int{3, 2, 1}, sameInts)), |
| 513 |
}, { |
| 514 |
about: "DeepEquals: structs with unexported fields not allowed", |
| 515 |
checker: qt.DeepEquals( |
| 516 |
struct{ answer int }{ |
| 517 |
answer: 42, |
| 518 |
}, |
| 519 |
struct{ answer int }{ |
| 520 |
answer: 42, |
| 521 |
}, |
| 522 |
), |
| 523 |
expectedCheckFailure: ` |
| 524 |
error: |
| 525 |
bad check: cannot handle unexported field at root.answer: |
| 526 |
"github.com/go-quicktest/qt_test".(struct { answer int }) |
| 527 |
consider using a custom Comparer; if you control the implementation of type, you can also consider using an Exporter, AllowUnexported, or cmpopts.IgnoreUnexported |
| 528 |
`, |
| 529 |
expectedNegateFailure: ` |
| 530 |
error: |
| 531 |
bad check: cannot handle unexported field at root.answer: |
| 532 |
"github.com/go-quicktest/qt_test".(struct { answer int }) |
| 533 |
consider using a custom Comparer; if you control the implementation of type, you can also consider using an Exporter, AllowUnexported, or cmpopts.IgnoreUnexported |
| 534 |
`, |
| 535 |
}, { |
| 536 |
about: "CmpEquals: structs with unexported fields ignored", |
| 537 |
checker: qt.CmpEquals( |
| 538 |
struct{ answer int }{ |
| 539 |
answer: 42, |
| 540 |
}, |
| 541 |
struct{ answer int }{ |
| 542 |
answer: 42, |
| 543 |
}, cmpopts.IgnoreUnexported(struct{ answer int }{})), |
| 544 |
expectedNegateFailure: ` |
| 545 |
error: |
| 546 |
unexpected success |
| 547 |
got: |
| 548 |
struct { answer int }{answer:42} |
| 549 |
want: |
| 550 |
<same as "got"> |
| 551 |
`, |
| 552 |
}, { |
| 553 |
about: "DeepEquals: same times", |
| 554 |
checker: qt.DeepEquals(goTime, goTime), |
| 555 |
expectedNegateFailure: ` |
| 556 |
error: |
| 557 |
unexpected success |
| 558 |
got: |
| 559 |
s"2012-03-28 00:00:00 +0000 UTC" |
| 560 |
want: |
| 561 |
<same as "got"> |
| 562 |
`, |
| 563 |
}, { |
| 564 |
about: "DeepEquals: different times: verbose", |
| 565 |
checker: qt.DeepEquals(goTime.Add(24*time.Hour), goTime), |
| 566 |
verbose: true, |
| 567 |
expectedCheckFailure: fmt.Sprintf(` |
| 568 |
error: |
| 569 |
values are not deep equal |
| 570 |
diff (-want +got): |
| 571 |
%s |
| 572 |
got: |
| 573 |
s"2012-03-29 00:00:00 +0000 UTC" |
| 574 |
want: |
| 575 |
s"2012-03-28 00:00:00 +0000 UTC" |
| 576 |
`, diff(goTime.Add(24*time.Hour), goTime)), |
| 577 |
}, { |
| 578 |
about: "ContentEquals: same values", |
| 579 |
checker: qt.ContentEquals([]string{"these", "are", "the", "voyages"}, []string{"these", "are", "the", "voyages"}), |
| 580 |
expectedNegateFailure: ` |
| 581 |
error: |
| 582 |
unexpected success |
| 583 |
got: |
| 584 |
[]string{"these", "are", "the", "voyages"} |
| 585 |
want: |
| 586 |
<same as "got"> |
| 587 |
`, |
| 588 |
}, { |
| 589 |
about: "ContentEquals: same contents", |
| 590 |
checker: qt.ContentEquals([]int{1, 2, 3}, []int{3, 2, 1}), |
| 591 |
expectedNegateFailure: ` |
| 592 |
error: |
| 593 |
unexpected success |
| 594 |
got: |
| 595 |
[]int{1, 2, 3} |
| 596 |
want: |
| 597 |
[]int{3, 2, 1} |
| 598 |
`, |
| 599 |
}, { |
| 600 |
about: "ContentEquals: same contents on complex slice", |
| 601 |
checker: qt.ContentEquals( |
| 602 |
[]struct { |
| 603 |
Strings []any |
| 604 |
Ints []int |
| 605 |
}{cmpEqualsGot, cmpEqualsGot, cmpEqualsWant}, |
| 606 |
[]struct { |
| 607 |
Strings []any |
| 608 |
Ints []int |
| 609 |
}{cmpEqualsWant, cmpEqualsGot, cmpEqualsGot}, |
| 610 |
), |
| 611 |
expectedNegateFailure: ` |
| 612 |
error: |
| 613 |
unexpected success |
| 614 |
got: |
| 615 |
[]struct { Strings []interface {}; Ints []int }{ |
| 616 |
{ |
| 617 |
Strings: { |
| 618 |
"who", |
| 619 |
"dalek", |
| 620 |
}, |
| 621 |
Ints: {42, 47}, |
| 622 |
}, |
| 623 |
{ |
| 624 |
Strings: { |
| 625 |
"who", |
| 626 |
"dalek", |
| 627 |
}, |
| 628 |
Ints: {42, 47}, |
| 629 |
}, |
| 630 |
{ |
| 631 |
Strings: { |
| 632 |
"who", |
| 633 |
"dalek", |
| 634 |
}, |
| 635 |
Ints: {42}, |
| 636 |
}, |
| 637 |
} |
| 638 |
want: |
| 639 |
[]struct { Strings []interface {}; Ints []int }{ |
| 640 |
{ |
| 641 |
Strings: { |
| 642 |
"who", |
| 643 |
"dalek", |
| 644 |
}, |
| 645 |
Ints: {42}, |
| 646 |
}, |
| 647 |
{ |
| 648 |
Strings: { |
| 649 |
"who", |
| 650 |
"dalek", |
| 651 |
}, |
| 652 |
Ints: {42, 47}, |
| 653 |
}, |
| 654 |
{ |
| 655 |
Strings: { |
| 656 |
"who", |
| 657 |
"dalek", |
| 658 |
}, |
| 659 |
Ints: {42, 47}, |
| 660 |
}, |
| 661 |
} |
| 662 |
`}, { |
| 663 |
about: "ContentEquals: same contents on a nested slice", |
| 664 |
checker: qt.ContentEquals( |
| 665 |
struct { |
| 666 |
Nums []int |
| 667 |
}{ |
| 668 |
Nums: []int{1, 2, 3, 4}, |
| 669 |
}, |
| 670 |
struct { |
| 671 |
Nums []int |
| 672 |
}{ |
| 673 |
Nums: []int{4, 3, 2, 1}, |
| 674 |
}, |
| 675 |
), |
| 676 |
expectedNegateFailure: ` |
| 677 |
error: |
| 678 |
unexpected success |
| 679 |
got: |
| 680 |
struct { Nums []int }{ |
| 681 |
Nums: {1, 2, 3, 4}, |
| 682 |
} |
| 683 |
want: |
| 684 |
struct { Nums []int }{ |
| 685 |
Nums: {4, 3, 2, 1}, |
| 686 |
} |
| 687 |
`, |
| 688 |
}, { |
| 689 |
about: "ContentEquals: slices of different type", |
| 690 |
checker: qt.ContentEquals[any]([]string{"bad", "wolf"}, []any{"bad", "wolf"}), |
| 691 |
expectedCheckFailure: fmt.Sprintf(` |
| 692 |
error: |
| 693 |
values are not deep equal |
| 694 |
diff (-want +got): |
| 695 |
%s |
| 696 |
got: |
| 697 |
[]string{"bad", "wolf"} |
| 698 |
want: |
| 699 |
[]interface {}{ |
| 700 |
"bad", |
| 701 |
"wolf", |
| 702 |
} |
| 703 |
`, diff([]string{"bad", "wolf"}, []any{"bad", "wolf"})), |
| 704 |
}, { |
| 705 |
about: "Matches: perfect match", |
| 706 |
checker: qt.Matches("exterminate", "exterminate"), |
| 707 |
expectedNegateFailure: ` |
| 708 |
error: |
| 709 |
unexpected success |
| 710 |
got value: |
| 711 |
"exterminate" |
| 712 |
regexp: |
| 713 |
<same as "got value"> |
| 714 |
`, |
| 715 |
}, { |
| 716 |
about: "Matches: match", |
| 717 |
checker: qt.Matches("these are the voyages", "these are the .*"), |
| 718 |
expectedNegateFailure: ` |
| 719 |
error: |
| 720 |
unexpected success |
| 721 |
got value: |
| 722 |
"these are the voyages" |
| 723 |
regexp: |
| 724 |
"these are the .*" |
| 725 |
`, |
| 726 |
}, { |
| 727 |
about: "Matches: mismatch", |
| 728 |
checker: qt.Matches("voyages", "these are the voyages"), |
| 729 |
expectedCheckFailure: ` |
| 730 |
error: |
| 731 |
value does not match regexp |
| 732 |
got value: |
| 733 |
"voyages" |
| 734 |
regexp: |
| 735 |
"these are the voyages" |
| 736 |
`, |
| 737 |
}, { |
| 738 |
about: "Matches: empty pattern", |
| 739 |
checker: qt.Matches("these are the voyages", ""), |
| 740 |
expectedCheckFailure: ` |
| 741 |
error: |
| 742 |
value does not match regexp |
| 743 |
got value: |
| 744 |
"these are the voyages" |
| 745 |
regexp: |
| 746 |
"" |
| 747 |
`, |
| 748 |
}, { |
| 749 |
about: "Matches: complex pattern", |
| 750 |
checker: qt.Matches("end of the universe", "bad wolf|end of the .*"), |
| 751 |
expectedNegateFailure: ` |
| 752 |
error: |
| 753 |
unexpected success |
| 754 |
got value: |
| 755 |
"end of the universe" |
| 756 |
regexp: |
| 757 |
"bad wolf|end of the .*" |
| 758 |
`, |
| 759 |
}, { |
| 760 |
about: "Matches: invalid pattern", |
| 761 |
checker: qt.Matches("voyages", "("), |
| 762 |
expectedCheckFailure: ` |
| 763 |
error: |
| 764 |
bad check: cannot compile regexp: error parsing regexp: missing closing ): ` + "`^(()$`" + ` |
| 765 |
regexp: |
| 766 |
"(" |
| 767 |
`, |
| 768 |
expectedNegateFailure: ` |
| 769 |
error: |
| 770 |
bad check: cannot compile regexp: error parsing regexp: missing closing ): ` + "`^(()$`" + ` |
| 771 |
regexp: |
| 772 |
"(" |
| 773 |
`, |
| 774 |
}, { |
| 775 |
about: "Matches: match with pre-compiled regexp", |
| 776 |
checker: qt.Matches("resistance is futile", regexp.MustCompile("resistance is (futile|useful)")), |
| 777 |
expectedNegateFailure: ` |
| 778 |
error: |
| 779 |
unexpected success |
| 780 |
got value: |
| 781 |
"resistance is futile" |
| 782 |
regexp: |
| 783 |
s"resistance is (futile|useful)" |
| 784 |
`, |
| 785 |
}, { |
| 786 |
about: "Matches: mismatch with pre-compiled regexp", |
| 787 |
checker: qt.Matches("resistance is cool", regexp.MustCompile("resistance is (futile|useful)")), |
| 788 |
expectedCheckFailure: ` |
| 789 |
error: |
| 790 |
value does not match regexp |
| 791 |
got value: |
| 792 |
"resistance is cool" |
| 793 |
regexp: |
| 794 |
s"resistance is (futile|useful)" |
| 795 |
`, |
| 796 |
}, { |
| 797 |
about: "Matches: match with pre-compiled multi-line regexp", |
| 798 |
checker: qt.Matches("line 1\nline 2", regexp.MustCompile(`line \d\nline \d`)), |
| 799 |
expectedNegateFailure: ` |
| 800 |
error: |
| 801 |
unexpected success |
| 802 |
got value: |
| 803 |
"line 1\nline 2" |
| 804 |
regexp: |
| 805 |
s"line \\d\\nline \\d" |
| 806 |
`, |
| 807 |
}, { |
| 808 |
about: "ErrorMatches: perfect match", |
| 809 |
checker: qt.ErrorMatches(errBadWolf, "bad wolf"), |
| 810 |
expectedNegateFailure: ` |
| 811 |
error: |
| 812 |
unexpected success |
| 813 |
got error: |
| 814 |
bad wolf |
| 815 |
file:line |
| 816 |
regexp: |
| 817 |
"bad wolf" |
| 818 |
`, |
| 819 |
}, { |
| 820 |
about: "ErrorMatches: match", |
| 821 |
checker: qt.ErrorMatches(errBadWolf, "bad .*"), |
| 822 |
expectedNegateFailure: ` |
| 823 |
error: |
| 824 |
unexpected success |
| 825 |
got error: |
| 826 |
bad wolf |
| 827 |
file:line |
| 828 |
regexp: |
| 829 |
"bad .*" |
| 830 |
`, |
| 831 |
}, { |
| 832 |
about: "ErrorMatches: mismatch", |
| 833 |
checker: qt.ErrorMatches(errBadWolf, "exterminate"), |
| 834 |
expectedCheckFailure: ` |
| 835 |
error: |
| 836 |
error does not match regexp |
| 837 |
got error: |
| 838 |
bad wolf |
| 839 |
file:line |
| 840 |
regexp: |
| 841 |
"exterminate" |
| 842 |
`, |
| 843 |
}, { |
| 844 |
about: "ErrorMatches: empty pattern", |
| 845 |
checker: qt.ErrorMatches(errBadWolf, ""), |
| 846 |
expectedCheckFailure: ` |
| 847 |
error: |
| 848 |
error does not match regexp |
| 849 |
got error: |
| 850 |
bad wolf |
| 851 |
file:line |
| 852 |
regexp: |
| 853 |
"" |
| 854 |
`, |
| 855 |
}, { |
| 856 |
about: "ErrorMatches: complex pattern", |
| 857 |
checker: qt.ErrorMatches(errBadWolf, "bad wolf|end of the universe"), |
| 858 |
expectedNegateFailure: ` |
| 859 |
error: |
| 860 |
unexpected success |
| 861 |
got error: |
| 862 |
bad wolf |
| 863 |
file:line |
| 864 |
regexp: |
| 865 |
"bad wolf|end of the universe" |
| 866 |
`, |
| 867 |
}, { |
| 868 |
about: "ErrorMatches: invalid pattern", |
| 869 |
checker: qt.ErrorMatches(errBadWolf, "("), |
| 870 |
expectedCheckFailure: ` |
| 871 |
error: |
| 872 |
bad check: cannot compile regexp: error parsing regexp: missing closing ): ` + "`^(()$`" + ` |
| 873 |
regexp: |
| 874 |
"(" |
| 875 |
`, |
| 876 |
expectedNegateFailure: ` |
| 877 |
error: |
| 878 |
bad check: cannot compile regexp: error parsing regexp: missing closing ): ` + "`^(()$`" + ` |
| 879 |
regexp: |
| 880 |
"(" |
| 881 |
`, |
| 882 |
}, { |
| 883 |
about: "ErrorMatches: nil error", |
| 884 |
checker: qt.ErrorMatches(nil, "some pattern"), |
| 885 |
expectedCheckFailure: ` |
| 886 |
error: |
| 887 |
got nil error but want non-nil |
| 888 |
got error: |
| 889 |
nil |
| 890 |
regexp: |
| 891 |
"some pattern" |
| 892 |
`, |
| 893 |
}, { |
| 894 |
about: "ErrorMatches: match with pre-compiled regexp", |
| 895 |
checker: qt.ErrorMatches(errBadWolf, regexp.MustCompile("bad (wolf|dog)")), |
| 896 |
expectedNegateFailure: ` |
| 897 |
error: |
| 898 |
unexpected success |
| 899 |
got error: |
| 900 |
bad wolf |
| 901 |
file:line |
| 902 |
regexp: |
| 903 |
s"bad (wolf|dog)" |
| 904 |
`, |
| 905 |
}, { |
| 906 |
about: "ErrorMatches: match with pre-compiled multi-line regexp", |
| 907 |
checker: qt.ErrorMatches(errBadWolfMultiLine, regexp.MustCompile(`bad (wolf|dog)\nfaulty (logic|statement)`)), |
| 908 |
expectedNegateFailure: ` |
| 909 |
error: |
| 910 |
unexpected success |
| 911 |
got error: |
| 912 |
bad wolf |
| 913 |
faulty logic |
| 914 |
file:line |
| 915 |
regexp: |
| 916 |
s"bad (wolf|dog)\\nfaulty (logic|statement)" |
| 917 |
`, |
| 918 |
}, { |
| 919 |
about: "ErrorMatches: mismatch with pre-compiled regexp", |
| 920 |
checker: qt.ErrorMatches(errBadWolf, regexp.MustCompile("good (wolf|dog)")), |
| 921 |
expectedCheckFailure: ` |
| 922 |
error: |
| 923 |
error does not match regexp |
| 924 |
got error: |
| 925 |
bad wolf |
| 926 |
file:line |
| 927 |
regexp: |
| 928 |
s"good (wolf|dog)" |
| 929 |
`, |
| 930 |
}, { |
| 931 |
about: "PanicMatches: perfect match", |
| 932 |
checker: qt.PanicMatches(func() { panic("error: bad wolf") }, "error: bad wolf"), |
| 933 |
expectedNegateFailure: ` |
| 934 |
error: |
| 935 |
unexpected success |
| 936 |
panic value: |
| 937 |
"error: bad wolf" |
| 938 |
function: |
| 939 |
func() {...} |
| 940 |
regexp: |
| 941 |
<same as "panic value"> |
| 942 |
`, |
| 943 |
}, { |
| 944 |
about: "PanicMatches: match", |
| 945 |
checker: qt.PanicMatches(func() { panic("error: bad wolf") }, "error: .*"), |
| 946 |
expectedNegateFailure: ` |
| 947 |
error: |
| 948 |
unexpected success |
| 949 |
panic value: |
| 950 |
"error: bad wolf" |
| 951 |
function: |
| 952 |
func() {...} |
| 953 |
regexp: |
| 954 |
"error: .*" |
| 955 |
`, |
| 956 |
}, { |
| 957 |
about: "PanicMatches: mismatch", |
| 958 |
checker: qt.PanicMatches(func() { panic("error: bad wolf") }, "error: exterminate"), |
| 959 |
expectedCheckFailure: ` |
| 960 |
error: |
| 961 |
panic value does not match regexp |
| 962 |
panic value: |
| 963 |
"error: bad wolf" |
| 964 |
function: |
| 965 |
func() {...} |
| 966 |
regexp: |
| 967 |
"error: exterminate" |
| 968 |
`, |
| 969 |
}, { |
| 970 |
about: "PanicMatches: empty pattern", |
| 971 |
checker: qt.PanicMatches(func() { panic("error: bad wolf") }, ""), |
| 972 |
expectedCheckFailure: ` |
| 973 |
error: |
| 974 |
panic value does not match regexp |
| 975 |
panic value: |
| 976 |
"error: bad wolf" |
| 977 |
function: |
| 978 |
func() {...} |
| 979 |
regexp: |
| 980 |
"" |
| 981 |
`, |
| 982 |
}, { |
| 983 |
about: "PanicMatches: complex pattern", |
| 984 |
checker: qt.PanicMatches(func() { panic("bad wolf") }, "bad wolf|end of the universe"), |
| 985 |
expectedNegateFailure: ` |
| 986 |
error: |
| 987 |
unexpected success |
| 988 |
panic value: |
| 989 |
"bad wolf" |
| 990 |
function: |
| 991 |
func() {...} |
| 992 |
regexp: |
| 993 |
"bad wolf|end of the universe" |
| 994 |
`, |
| 995 |
}, { |
| 996 |
about: "PanicMatches: invalid pattern", |
| 997 |
checker: qt.PanicMatches(func() { panic("error: bad wolf") }, "("), |
| 998 |
expectedCheckFailure: ` |
| 999 |
error: |
| 1000 |
bad check: cannot compile regexp: error parsing regexp: missing closing ): ` + "`^(()$`" + ` |
| 1001 |
panic value: |
| 1002 |
"error: bad wolf" |
| 1003 |
regexp: |
| 1004 |
"(" |
| 1005 |
`, |
| 1006 |
expectedNegateFailure: ` |
| 1007 |
error: |
| 1008 |
bad check: cannot compile regexp: error parsing regexp: missing closing ): ` + "`^(()$`" + ` |
| 1009 |
panic value: |
| 1010 |
"error: bad wolf" |
| 1011 |
regexp: |
| 1012 |
"(" |
| 1013 |
`, |
| 1014 |
}, { |
| 1015 |
about: "PanicMatches: no panic", |
| 1016 |
checker: qt.PanicMatches(func() {}, ".*"), |
| 1017 |
expectedCheckFailure: ` |
| 1018 |
error: |
| 1019 |
function did not panic |
| 1020 |
function: |
| 1021 |
func() {...} |
| 1022 |
regexp: |
| 1023 |
".*" |
| 1024 |
`, |
| 1025 |
}, { |
| 1026 |
about: "PanicMatches: match with pre-compiled regexp", |
| 1027 |
checker: qt.PanicMatches(func() { panic("error: bad wolf") }, regexp.MustCompile("error: bad (wolf|dog)")), |
| 1028 |
expectedNegateFailure: ` |
| 1029 |
error: |
| 1030 |
unexpected success |
| 1031 |
panic value: |
| 1032 |
"error: bad wolf" |
| 1033 |
function: |
| 1034 |
func() {...} |
| 1035 |
regexp: |
| 1036 |
s"error: bad (wolf|dog)" |
| 1037 |
`, |
| 1038 |
}, { |
| 1039 |
about: "PanicMatches: match with pre-compiled multi-line regexp", |
| 1040 |
checker: qt.PanicMatches(func() { panic("error: bad wolf\nfaulty logic") }, regexp.MustCompile(`error: bad (wolf|dog)\nfaulty (logic|statement)`)), |
| 1041 |
expectedNegateFailure: ` |
| 1042 |
error: |
| 1043 |
unexpected success |
| 1044 |
panic value: |
| 1045 |
"error: bad wolf\nfaulty logic" |
| 1046 |
function: |
| 1047 |
func() {...} |
| 1048 |
regexp: |
| 1049 |
s"error: bad (wolf|dog)\\nfaulty (logic|statement)" |
| 1050 |
`, |
| 1051 |
}, { |
| 1052 |
about: "PanicMatches: mismatch with pre-compiled regexp", |
| 1053 |
checker: qt.PanicMatches(func() { panic("error: bad wolf") }, regexp.MustCompile("good (wolf|dog)")), |
| 1054 |
expectedCheckFailure: ` |
| 1055 |
error: |
| 1056 |
panic value does not match regexp |
| 1057 |
panic value: |
| 1058 |
"error: bad wolf" |
| 1059 |
function: |
| 1060 |
func() {...} |
| 1061 |
regexp: |
| 1062 |
s"good (wolf|dog)" |
| 1063 |
`, |
| 1064 |
}, { |
| 1065 |
about: "IsNil: nil", |
| 1066 |
checker: qt.IsNil(any(nil)), |
| 1067 |
expectedNegateFailure: ` |
| 1068 |
error: |
| 1069 |
got <nil> but want non-nil |
| 1070 |
got: |
| 1071 |
nil |
| 1072 |
`, |
| 1073 |
}, { |
| 1074 |
about: "IsNil: nil pointer to struct", |
| 1075 |
checker: qt.IsNil((*struct{})(nil)), |
| 1076 |
expectedNegateFailure: ` |
| 1077 |
error: |
| 1078 |
got nil ptr but want non-nil |
| 1079 |
got: |
| 1080 |
(*struct {})(nil) |
| 1081 |
`, |
| 1082 |
}, { |
| 1083 |
about: "IsNil: nil func", |
| 1084 |
checker: qt.IsNil((func())(nil)), |
| 1085 |
expectedNegateFailure: ` |
| 1086 |
error: |
| 1087 |
got nil func but want non-nil |
| 1088 |
got: |
| 1089 |
func() {...} |
| 1090 |
`, |
| 1091 |
}, { |
| 1092 |
about: "IsNil: nil map", |
| 1093 |
checker: qt.IsNil((map[string]string)(nil)), |
| 1094 |
expectedNegateFailure: ` |
| 1095 |
error: |
| 1096 |
got nil map but want non-nil |
| 1097 |
got: |
| 1098 |
map[string]string{} |
| 1099 |
`, |
| 1100 |
}, { |
| 1101 |
about: "IsNil: nil slice", |
| 1102 |
checker: qt.IsNil(([]int)(nil)), |
| 1103 |
expectedNegateFailure: ` |
| 1104 |
error: |
| 1105 |
got nil slice but want non-nil |
| 1106 |
got: |
| 1107 |
[]int(nil) |
| 1108 |
`, |
| 1109 |
}, { |
| 1110 |
about: "IsNil: nil error-implementing type", |
| 1111 |
checker: qt.IsNil(error((*errTest)(nil))), |
| 1112 |
// TODO e<nil> isn't great here - perhaps we should |
| 1113 |
// mention the type too. |
| 1114 |
expectedCheckFailure: ` |
| 1115 |
error: |
| 1116 |
got non-nil value |
| 1117 |
got: |
| 1118 |
e<nil> |
| 1119 |
`, |
| 1120 |
}, { |
| 1121 |
about: "IsNil: not nil", |
| 1122 |
checker: qt.IsNil([]int{}), |
| 1123 |
expectedCheckFailure: ` |
| 1124 |
error: |
| 1125 |
got non-nil value |
| 1126 |
got: |
| 1127 |
[]int{} |
| 1128 |
`, |
| 1129 |
}, { |
| 1130 |
about: "IsNil: error is not nil", |
| 1131 |
checker: qt.IsNil(error(errBadWolf)), |
| 1132 |
expectedCheckFailure: ` |
| 1133 |
error: |
| 1134 |
got non-nil value |
| 1135 |
got: |
| 1136 |
bad wolf |
| 1137 |
file:line |
| 1138 |
`, |
| 1139 |
}, { |
| 1140 |
about: "IsNotNil: success", |
| 1141 |
checker: qt.IsNotNil(any(42)), |
| 1142 |
expectedNegateFailure: ` |
| 1143 |
error: |
| 1144 |
got non-nil value |
| 1145 |
got: |
| 1146 |
int(42) |
| 1147 |
`, |
| 1148 |
}, { |
| 1149 |
about: "IsNotNil: failure", |
| 1150 |
checker: qt.IsNotNil[any](nil), |
| 1151 |
expectedCheckFailure: ` |
| 1152 |
error: |
| 1153 |
got <nil> but want non-nil |
| 1154 |
got: |
| 1155 |
nil |
| 1156 |
`, |
| 1157 |
}, { |
| 1158 |
about: "HasLen: arrays with the same length", |
| 1159 |
checker: qt.HasLen([4]string{"these", "are", "the", "voyages"}, 4), |
| 1160 |
expectedNegateFailure: ` |
| 1161 |
error: |
| 1162 |
unexpected success |
| 1163 |
len(got): |
| 1164 |
int(4) |
| 1165 |
got: |
| 1166 |
[4]string{"these", "are", "the", "voyages"} |
| 1167 |
want length: |
| 1168 |
<same as "len(got)"> |
| 1169 |
`, |
| 1170 |
}, { |
| 1171 |
about: "HasLen: pointers to array with the same length", |
| 1172 |
checker: qt.HasLen(&[4]string{"these", "are", "the", "voyages"}, 4), |
| 1173 |
expectedNegateFailure: ` |
| 1174 |
error: |
| 1175 |
unexpected success |
| 1176 |
len(got): |
| 1177 |
int(4) |
| 1178 |
got: |
| 1179 |
&[4]string{"these", "are", "the", "voyages"} |
| 1180 |
want length: |
| 1181 |
<same as "len(got)"> |
| 1182 |
`, |
| 1183 |
}, { |
| 1184 |
about: "HasLen: channels with the same length", |
| 1185 |
checker: qt.HasLen(chInt, 2), |
| 1186 |
expectedNegateFailure: fmt.Sprintf(` |
| 1187 |
error: |
| 1188 |
unexpected success |
| 1189 |
len(got): |
| 1190 |
int(2) |
| 1191 |
got: |
| 1192 |
(chan int)(%v) |
| 1193 |
want length: |
| 1194 |
<same as "len(got)"> |
| 1195 |
`, chInt), |
| 1196 |
}, { |
| 1197 |
about: "HasLen: maps with the same length", |
| 1198 |
checker: qt.HasLen(map[string]bool{"true": true}, 1), |
| 1199 |
expectedNegateFailure: ` |
| 1200 |
error: |
| 1201 |
unexpected success |
| 1202 |
len(got): |
| 1203 |
int(1) |
| 1204 |
got: |
| 1205 |
map[string]bool{"true":true} |
| 1206 |
want length: |
| 1207 |
<same as "len(got)"> |
| 1208 |
`, |
| 1209 |
}, { |
| 1210 |
about: "HasLen: slices with the same length", |
| 1211 |
checker: qt.HasLen([]int{}, 0), |
| 1212 |
expectedNegateFailure: ` |
| 1213 |
error: |
| 1214 |
unexpected success |
| 1215 |
len(got): |
| 1216 |
int(0) |
| 1217 |
got: |
| 1218 |
[]int{} |
| 1219 |
want length: |
| 1220 |
<same as "len(got)"> |
| 1221 |
`, |
| 1222 |
}, { |
| 1223 |
about: "HasLen: strings with the same length", |
| 1224 |
checker: qt.HasLen("these are the voyages", 21), |
| 1225 |
expectedNegateFailure: ` |
| 1226 |
error: |
| 1227 |
unexpected success |
| 1228 |
len(got): |
| 1229 |
int(21) |
| 1230 |
got: |
| 1231 |
"these are the voyages" |
| 1232 |
want length: |
| 1233 |
<same as "len(got)"> |
| 1234 |
`, |
| 1235 |
}, { |
| 1236 |
about: "HasLen: arrays with different lengths", |
| 1237 |
checker: qt.HasLen([4]string{"these", "are", "the", "voyages"}, 0), |
| 1238 |
expectedCheckFailure: ` |
| 1239 |
error: |
| 1240 |
unexpected length |
| 1241 |
len(got): |
| 1242 |
int(4) |
| 1243 |
got: |
| 1244 |
[4]string{"these", "are", "the", "voyages"} |
| 1245 |
want length: |
| 1246 |
int(0) |
| 1247 |
`, |
| 1248 |
}, { |
| 1249 |
about: "HasLen: channels with different lengths", |
| 1250 |
checker: qt.HasLen(chInt, 4), |
| 1251 |
expectedCheckFailure: fmt.Sprintf(` |
| 1252 |
error: |
| 1253 |
unexpected length |
| 1254 |
len(got): |
| 1255 |
int(2) |
| 1256 |
got: |
| 1257 |
(chan int)(%v) |
| 1258 |
want length: |
| 1259 |
int(4) |
| 1260 |
`, chInt), |
| 1261 |
}, { |
| 1262 |
about: "HasLen: maps with different lengths", |
| 1263 |
checker: qt.HasLen(map[string]bool{"true": true}, 42), |
| 1264 |
expectedCheckFailure: ` |
| 1265 |
error: |
| 1266 |
unexpected length |
| 1267 |
len(got): |
| 1268 |
int(1) |
| 1269 |
got: |
| 1270 |
map[string]bool{"true":true} |
| 1271 |
want length: |
| 1272 |
int(42) |
| 1273 |
`, |
| 1274 |
}, { |
| 1275 |
about: "HasLen: slices with different lengths", |
| 1276 |
checker: qt.HasLen([]int{42, 47}, 1), |
| 1277 |
expectedCheckFailure: ` |
| 1278 |
error: |
| 1279 |
unexpected length |
| 1280 |
len(got): |
| 1281 |
int(2) |
| 1282 |
got: |
| 1283 |
[]int{42, 47} |
| 1284 |
want length: |
| 1285 |
int(1) |
| 1286 |
`, |
| 1287 |
}, { |
| 1288 |
about: "HasLen: strings with different lengths", |
| 1289 |
checker: qt.HasLen("these are the voyages", 42), |
| 1290 |
expectedCheckFailure: ` |
| 1291 |
error: |
| 1292 |
unexpected length |
| 1293 |
len(got): |
| 1294 |
int(21) |
| 1295 |
got: |
| 1296 |
"these are the voyages" |
| 1297 |
want length: |
| 1298 |
int(42) |
| 1299 |
`, |
| 1300 |
}, { |
| 1301 |
about: "HasLen: value without a length", |
| 1302 |
checker: qt.HasLen(42, 42), |
| 1303 |
expectedCheckFailure: ` |
| 1304 |
error: |
| 1305 |
bad check: first argument of type int has no length |
| 1306 |
got: |
| 1307 |
int(42) |
| 1308 |
`, |
| 1309 |
expectedNegateFailure: ` |
| 1310 |
error: |
| 1311 |
bad check: first argument of type int has no length |
| 1312 |
got: |
| 1313 |
int(42) |
| 1314 |
`, |
| 1315 |
}, { |
| 1316 |
about: "HasLen: pointer value without a length", |
| 1317 |
checker: qt.HasLen(&[]string{"arrays", "are", "fine", "but", "not", "slices"}, 1), |
| 1318 |
expectedCheckFailure: ` |
| 1319 |
error: |
| 1320 |
bad check: first argument of type *[]string has no length |
| 1321 |
got: |
| 1322 |
&[]string{"arrays", "are", "fine", "but", "not", "slices"} |
| 1323 |
`, |
| 1324 |
expectedNegateFailure: ` |
| 1325 |
error: |
| 1326 |
bad check: first argument of type *[]string has no length |
| 1327 |
got: |
| 1328 |
&[]string{"arrays", "are", "fine", "but", "not", "slices"} |
| 1329 |
`, |
| 1330 |
}, { |
| 1331 |
about: "Implements: implements interface", |
| 1332 |
checker: qt.Implements[error](errBadWolf), |
| 1333 |
expectedNegateFailure: ` |
| 1334 |
error: |
| 1335 |
unexpected success |
| 1336 |
got: |
| 1337 |
bad wolf |
| 1338 |
file:line |
| 1339 |
want interface: |
| 1340 |
error |
| 1341 |
`, |
| 1342 |
}, { |
| 1343 |
about: "Implements: does not implement interface", |
| 1344 |
checker: qt.Implements[Fooer](errBadWolf), |
| 1345 |
expectedCheckFailure: ` |
| 1346 |
error: |
| 1347 |
got value does not implement wanted interface |
| 1348 |
got: |
| 1349 |
bad wolf |
| 1350 |
file:line |
| 1351 |
want interface: |
| 1352 |
qt_test.Fooer |
| 1353 |
`, |
| 1354 |
}, { |
| 1355 |
about: "Implements: fails if got nil", |
| 1356 |
checker: qt.Implements[Fooer](nil), |
| 1357 |
expectedCheckFailure: ` |
| 1358 |
error: |
| 1359 |
got nil value but want non-nil |
| 1360 |
got: |
| 1361 |
nil |
| 1362 |
`, |
| 1363 |
}, { |
| 1364 |
about: "Satisfies: success with an error", |
| 1365 |
checker: qt.Satisfies(qt.BadCheckf("bad wolf"), qt.IsBadCheck), |
| 1366 |
expectedNegateFailure: ` |
| 1367 |
error: |
| 1368 |
unexpected success |
| 1369 |
got: |
| 1370 |
e"bad check: bad wolf" |
| 1371 |
predicate: |
| 1372 |
func(error) bool {...} |
| 1373 |
`, |
| 1374 |
}, { |
| 1375 |
about: "Satisfies: success with an int", |
| 1376 |
checker: qt.Satisfies(42, func(v int) bool { return v == 42 }), |
| 1377 |
expectedNegateFailure: ` |
| 1378 |
error: |
| 1379 |
unexpected success |
| 1380 |
got: |
| 1381 |
int(42) |
| 1382 |
predicate: |
| 1383 |
func(int) bool {...} |
| 1384 |
`, |
| 1385 |
}, { |
| 1386 |
about: "Satisfies: success with nil", |
| 1387 |
checker: qt.Satisfies([]int(nil), func(v []int) bool { return true }), |
| 1388 |
expectedNegateFailure: ` |
| 1389 |
error: |
| 1390 |
unexpected success |
| 1391 |
got: |
| 1392 |
[]int(nil) |
| 1393 |
predicate: |
| 1394 |
func([]int) bool {...} |
| 1395 |
`, |
| 1396 |
}, { |
| 1397 |
about: "Satisfies: failure with an error", |
| 1398 |
checker: qt.Satisfies(nil, qt.IsBadCheck), |
| 1399 |
expectedCheckFailure: ` |
| 1400 |
error: |
| 1401 |
value does not satisfy predicate function |
| 1402 |
got: |
| 1403 |
nil |
| 1404 |
predicate: |
| 1405 |
func(error) bool {...} |
| 1406 |
`, |
| 1407 |
}, { |
| 1408 |
about: "Satisfies: failure with a string", |
| 1409 |
checker: qt.Satisfies("bad wolf", func(string) bool { return false }), |
| 1410 |
expectedCheckFailure: ` |
| 1411 |
error: |
| 1412 |
value does not satisfy predicate function |
| 1413 |
got: |
| 1414 |
"bad wolf" |
| 1415 |
predicate: |
| 1416 |
func(string) bool {...} |
| 1417 |
`, |
| 1418 |
}, { |
| 1419 |
about: "IsTrue: success", |
| 1420 |
checker: qt.IsTrue(true), |
| 1421 |
expectedNegateFailure: ` |
| 1422 |
error: |
| 1423 |
unexpected success |
| 1424 |
got: |
| 1425 |
bool(true) |
| 1426 |
want: |
| 1427 |
<same as "got"> |
| 1428 |
`, |
| 1429 |
}, { |
| 1430 |
about: "IsTrue: failure", |
| 1431 |
checker: qt.IsTrue(false), |
| 1432 |
expectedCheckFailure: ` |
| 1433 |
error: |
| 1434 |
values are not equal |
| 1435 |
got: |
| 1436 |
bool(false) |
| 1437 |
want: |
| 1438 |
bool(true) |
| 1439 |
`, |
| 1440 |
}, { |
| 1441 |
about: "IsTrue: success with subtype", |
| 1442 |
checker: qt.IsTrue(boolean(true)), |
| 1443 |
expectedNegateFailure: ` |
| 1444 |
error: |
| 1445 |
unexpected success |
| 1446 |
got: |
| 1447 |
qt_test.boolean(true) |
| 1448 |
want: |
| 1449 |
<same as "got"> |
| 1450 |
`, |
| 1451 |
}, { |
| 1452 |
about: "IsTrue: failure with subtype", |
| 1453 |
checker: qt.IsTrue(boolean(false)), |
| 1454 |
expectedCheckFailure: ` |
| 1455 |
error: |
| 1456 |
values are not equal |
| 1457 |
got: |
| 1458 |
qt_test.boolean(false) |
| 1459 |
want: |
| 1460 |
qt_test.boolean(true) |
| 1461 |
`, |
| 1462 |
}, { |
| 1463 |
about: "IsFalse: success", |
| 1464 |
checker: qt.IsFalse(false), |
| 1465 |
expectedNegateFailure: ` |
| 1466 |
error: |
| 1467 |
unexpected success |
| 1468 |
got: |
| 1469 |
bool(false) |
| 1470 |
want: |
| 1471 |
<same as "got"> |
| 1472 |
`, |
| 1473 |
}, { |
| 1474 |
about: "IsFalse: failure", |
| 1475 |
checker: qt.IsFalse(true), |
| 1476 |
expectedCheckFailure: ` |
| 1477 |
error: |
| 1478 |
values are not equal |
| 1479 |
got: |
| 1480 |
bool(true) |
| 1481 |
want: |
| 1482 |
bool(false) |
| 1483 |
`, |
| 1484 |
}, { |
| 1485 |
about: "StringContains match", |
| 1486 |
checker: qt.StringContains("hello, world", "world"), |
| 1487 |
expectedNegateFailure: ` |
| 1488 |
error: |
| 1489 |
unexpected success |
| 1490 |
got: |
| 1491 |
"hello, world" |
| 1492 |
substr: |
| 1493 |
"world" |
| 1494 |
`, |
| 1495 |
}, { |
| 1496 |
about: "StringContains no match", |
| 1497 |
checker: qt.StringContains("hello, world", "worlds"), |
| 1498 |
expectedCheckFailure: ` |
| 1499 |
error: |
| 1500 |
no substring match found |
| 1501 |
got: |
| 1502 |
"hello, world" |
| 1503 |
substr: |
| 1504 |
"worlds" |
| 1505 |
`}, { |
| 1506 |
about: "SliceContains match", |
| 1507 |
checker: qt.SliceContains([]string{"a", "b", "c"}, "a"), |
| 1508 |
expectedNegateFailure: ` |
| 1509 |
error: |
| 1510 |
unexpected success |
| 1511 |
container: |
| 1512 |
[]string{"a", "b", "c"} |
| 1513 |
want: |
| 1514 |
"a" |
| 1515 |
`, |
| 1516 |
}, { |
| 1517 |
about: "SliceContains mismatch", |
| 1518 |
checker: qt.SliceContains([]string{"a", "b", "c"}, "d"), |
| 1519 |
expectedCheckFailure: ` |
| 1520 |
error: |
| 1521 |
no matching element found |
| 1522 |
container: |
| 1523 |
[]string{"a", "b", "c"} |
| 1524 |
want: |
| 1525 |
"d" |
| 1526 |
`, |
| 1527 |
}, { |
| 1528 |
about: "Contains with map", |
| 1529 |
checker: qt.MapContains(map[string]string{ |
| 1530 |
"a": "d", |
| 1531 |
"b": "a", |
| 1532 |
}, "d"), |
| 1533 |
expectedNegateFailure: ` |
| 1534 |
error: |
| 1535 |
unexpected success |
| 1536 |
container: |
| 1537 |
map[string]string{"a":"d", "b":"a"} |
| 1538 |
want: |
| 1539 |
"d" |
| 1540 |
`, |
| 1541 |
}, { |
| 1542 |
about: "Contains with map and interface value", |
| 1543 |
checker: qt.MapContains(map[string]any{ |
| 1544 |
"a": "d", |
| 1545 |
"b": "a", |
| 1546 |
}, "d"), |
| 1547 |
expectedNegateFailure: ` |
| 1548 |
error: |
| 1549 |
unexpected success |
| 1550 |
container: |
| 1551 |
map[string]interface {}{ |
| 1552 |
"a": "d", |
| 1553 |
"b": "a", |
| 1554 |
} |
| 1555 |
want: |
| 1556 |
"d" |
| 1557 |
`, |
| 1558 |
}, { |
| 1559 |
about: "All slice equals", |
| 1560 |
checker: qt.SliceAll([]string{"a", "a"}, qt.F2(qt.Equals[string], "a")), |
| 1561 |
expectedNegateFailure: ` |
| 1562 |
error: |
| 1563 |
unexpected success |
| 1564 |
container: |
| 1565 |
[]string{"a", "a"} |
| 1566 |
want: |
| 1567 |
"a" |
| 1568 |
`, |
| 1569 |
}, { |
| 1570 |
about: "All slice match", |
| 1571 |
checker: qt.SliceAll([]string{"red", "blue", "green"}, qt.F2(qt.Matches[string], ".*e.*")), |
| 1572 |
expectedNegateFailure: ` |
| 1573 |
error: |
| 1574 |
unexpected success |
| 1575 |
container: |
| 1576 |
[]string{"red", "blue", "green"} |
| 1577 |
regexp: |
| 1578 |
".*e.*" |
| 1579 |
`, |
| 1580 |
}, { |
| 1581 |
about: "All nested match", |
| 1582 |
// TODO this is a bit awkward. Is there something we could do to improve it? |
| 1583 |
checker: qt.SliceAll([][]string{{"hello", "goodbye"}, {"red", "blue"}, {}}, func(elem []string) qt.Checker { |
| 1584 |
return qt.SliceAll(elem, qt.F2(qt.Matches[string], ".*e.*")) |
| 1585 |
}), |
| 1586 |
expectedNegateFailure: ` |
| 1587 |
error: |
| 1588 |
unexpected success |
| 1589 |
container: |
| 1590 |
[][]string{ |
| 1591 |
{"hello", "goodbye"}, |
| 1592 |
{"red", "blue"}, |
| 1593 |
{}, |
| 1594 |
} |
| 1595 |
regexp: |
| 1596 |
".*e.*" |
| 1597 |
`, |
| 1598 |
}, { |
| 1599 |
about: "All nested mismatch", |
| 1600 |
checker: qt.SliceAll([][]string{{"hello", "goodbye"}, {"black", "blue"}, {}}, func(elem []string) qt.Checker { |
| 1601 |
return qt.SliceAll(elem, qt.F2(qt.Matches[string], ".*e.*")) |
| 1602 |
}), |
| 1603 |
expectedCheckFailure: ` |
| 1604 |
error: |
| 1605 |
mismatch at index 1 |
| 1606 |
error: |
| 1607 |
mismatch at index 0 |
| 1608 |
error: |
| 1609 |
value does not match regexp |
| 1610 |
first mismatched element: |
| 1611 |
"black" |
| 1612 |
`, |
| 1613 |
}, { |
| 1614 |
about: "All slice mismatch", |
| 1615 |
checker: qt.SliceAll([]string{"red", "black"}, qt.F2(qt.Matches[string], ".*e.*")), |
| 1616 |
expectedCheckFailure: ` |
| 1617 |
error: |
| 1618 |
mismatch at index 1 |
| 1619 |
error: |
| 1620 |
value does not match regexp |
| 1621 |
first mismatched element: |
| 1622 |
"black" |
| 1623 |
`, |
| 1624 |
}, { |
| 1625 |
about: "All slice mismatch with DeepEqual", |
| 1626 |
checker: qt.SliceAll([][]string{{"a", "b"}, {"a", "c"}}, qt.F2(qt.DeepEquals[[]string], []string{"a", "b"})), |
| 1627 |
expectedCheckFailure: fmt.Sprintf(` |
| 1628 |
error: |
| 1629 |
mismatch at index 1 |
| 1630 |
error: |
| 1631 |
values are not deep equal |
| 1632 |
diff (-want +got): |
| 1633 |
%s |
| 1634 |
got: |
| 1635 |
[]string{"a", "c"} |
| 1636 |
want: |
| 1637 |
[]string{"a", "b"} |
| 1638 |
`, diff([]string{"a", "c"}, []string{"a", "b"})), |
| 1639 |
}, { |
| 1640 |
about: "All mismatch with map", |
| 1641 |
checker: qt.MapAll(map[string]string{"a": "red", "b": "black"}, qt.F2(qt.Matches[string], ".*e.*")), |
| 1642 |
expectedCheckFailure: ` |
| 1643 |
error: |
| 1644 |
mismatch at key "b" |
| 1645 |
error: |
| 1646 |
value does not match regexp |
| 1647 |
first mismatched element: |
| 1648 |
"black" |
| 1649 |
`}, { |
| 1650 |
about: "Any no match", |
| 1651 |
checker: qt.SliceAny([]int{}, qt.F2(qt.Equals[int], 5)), |
| 1652 |
expectedCheckFailure: ` |
| 1653 |
error: |
| 1654 |
no matching element found |
| 1655 |
container: |
| 1656 |
[]int{} |
| 1657 |
want: |
| 1658 |
int(5) |
| 1659 |
`, |
| 1660 |
}, { |
| 1661 |
about: "JSONEquals simple", |
| 1662 |
checker: qt.JSONEquals( |
| 1663 |
[]byte(`{"First": 47.11}`), |
| 1664 |
&OuterJSON{ |
| 1665 |
First: 47.11, |
| 1666 |
}, |
| 1667 |
), |
| 1668 |
expectedNegateFailure: tilde2bq(` |
| 1669 |
error: |
| 1670 |
unexpected success |
| 1671 |
got: |
| 1672 |
[]uint8(~{"First": 47.11}~) |
| 1673 |
want: |
| 1674 |
&qt_test.OuterJSON{ |
| 1675 |
First: 47.11, |
| 1676 |
Second: nil, |
| 1677 |
} |
| 1678 |
`), |
| 1679 |
}, { |
| 1680 |
about: "JSONEquals nested", |
| 1681 |
checker: qt.JSONEquals( |
| 1682 |
`{"First": 47.11, "Last": [{"First": "Hello", "Second": 42}]}`, |
| 1683 |
&OuterJSON{ |
| 1684 |
First: 47.11, |
| 1685 |
Second: []*InnerJSON{ |
| 1686 |
{First: "Hello", Second: 42}, |
| 1687 |
}, |
| 1688 |
}, |
| 1689 |
), |
| 1690 |
expectedNegateFailure: tilde2bq(` |
| 1691 |
error: |
| 1692 |
unexpected success |
| 1693 |
got: |
| 1694 |
~{"First": 47.11, "Last": [{"First": "Hello", "Second": 42}]}~ |
| 1695 |
want: |
| 1696 |
&qt_test.OuterJSON{ |
| 1697 |
First: 47.11, |
| 1698 |
Second: { |
| 1699 |
&qt_test.InnerJSON{ |
| 1700 |
First: "Hello", |
| 1701 |
Second: 42, |
| 1702 |
Third: {}, |
| 1703 |
}, |
| 1704 |
}, |
| 1705 |
} |
| 1706 |
`), |
| 1707 |
}, { |
| 1708 |
about: "JSONEquals nested with newline", |
| 1709 |
checker: qt.JSONEquals( |
| 1710 |
`{"First": 47.11, "Last": [{"First": "Hello", "Second": 42}, |
| 1711 |
{"First": "World", "Third": {"F": false}}]}`, |
| 1712 |
&OuterJSON{ |
| 1713 |
First: 47.11, |
| 1714 |
Second: []*InnerJSON{ |
| 1715 |
{First: "Hello", Second: 42}, |
| 1716 |
{First: "World", Third: map[string]bool{ |
| 1717 |
"F": false, |
| 1718 |
}}, |
| 1719 |
}, |
| 1720 |
}, |
| 1721 |
), |
| 1722 |
expectedNegateFailure: ` |
| 1723 |
error: |
| 1724 |
unexpected success |
| 1725 |
got: |
| 1726 |
"{\"First\": 47.11, \"Last\": [{\"First\": \"Hello\", \"Second\": 42},\n\t\t\t{\"First\": \"World\", \"Third\": {\"F\": false}}]}" |
| 1727 |
want: |
| 1728 |
&qt_test.OuterJSON{ |
| 1729 |
First: 47.11, |
| 1730 |
Second: { |
| 1731 |
&qt_test.InnerJSON{ |
| 1732 |
First: "Hello", |
| 1733 |
Second: 42, |
| 1734 |
Third: {}, |
| 1735 |
}, |
| 1736 |
&qt_test.InnerJSON{ |
| 1737 |
First: "World", |
| 1738 |
Second: 0, |
| 1739 |
Third: {"F":false}, |
| 1740 |
}, |
| 1741 |
}, |
| 1742 |
} |
| 1743 |
`, |
| 1744 |
}, { |
| 1745 |
about: "JSONEquals extra field", |
| 1746 |
checker: qt.JSONEquals( |
| 1747 |
`{"NotThere": 1}`, |
| 1748 |
&OuterJSON{ |
| 1749 |
First: 2, |
| 1750 |
}, |
| 1751 |
), |
| 1752 |
expectedCheckFailure: fmt.Sprintf(` |
| 1753 |
error: |
| 1754 |
values are not deep equal |
| 1755 |
diff (-want +got): |
| 1756 |
%s |
| 1757 |
got: |
| 1758 |
map[string]interface {}{ |
| 1759 |
"NotThere": float64(1), |
| 1760 |
} |
| 1761 |
want: |
| 1762 |
map[string]interface {}{ |
| 1763 |
"First": float64(2), |
| 1764 |
} |
| 1765 |
`, diff(map[string]any{"NotThere": 1.0}, map[string]any{"First": 2.0})), |
| 1766 |
}, { |
| 1767 |
about: "JSONEquals cannot unmarshal obtained value", |
| 1768 |
checker: qt.JSONEquals([]byte(`{"NotThere": `), nil), |
| 1769 |
expectedCheckFailure: fmt.Sprintf(tilde2bq(` |
| 1770 |
error: |
| 1771 |
cannot unmarshal obtained contents: %s; "{\"NotThere\": " |
| 1772 |
got: |
| 1773 |
[]uint8(~{"NotThere": ~) |
| 1774 |
want: |
| 1775 |
nil |
| 1776 |
`), mustJSONUnmarshalErr(`{"NotThere": `)), |
| 1777 |
}, { |
| 1778 |
about: "JSONEquals cannot marshal expected value", |
| 1779 |
checker: qt.JSONEquals([]byte(`null`), jsonErrorMarshaler{}), |
| •1780 |
expectedCheckFailure: fmt.Sprintf(` |
| 1781 |
error: |
| •1782 |
bad check: cannot marshal expected contents: %s |
| 1783 |
`, mustJSONMarshalErr(jsonErrorMarshaler{})), |
| 1784 |
expectedNegateFailure: fmt.Sprintf(` |
| 1785 |
error: |
| •1786 |
bad check: cannot marshal expected contents: %s |
| 1787 |
`, mustJSONMarshalErr(jsonErrorMarshaler{})), |
| 1788 |
}, { |
| 1789 |
about: "JSONEquals with []byte", |
| 1790 |
checker: qt.JSONEquals([]byte("null"), nil), |
| 1791 |
expectedNegateFailure: ` |
| 1792 |
error: |
| 1793 |
unexpected success |
| 1794 |
got: |
| 1795 |
[]uint8("null") |
| 1796 |
want: |
| 1797 |
nil |
| 1798 |
`, |
| 1799 |
}, { |
| 1800 |
about: "JSONEquals with RawMessage", |
| 1801 |
checker: qt.JSONEquals([]byte("null"), json.RawMessage("null")), |
| •1802 |
expectedNegateFailure: fmt.Sprintf(` |
| 1803 |
error: |
| 1804 |
unexpected success |
| 1805 |
got: |
| 1806 |
[]uint8("null") |
| 1807 |
want: |
| •1808 |
%s |
| 1809 |
`, qt.Format(json.RawMessage("null"))), |
| 1810 |
}, { |
| 1811 |
about: "CodecEquals with bad marshal", |
| 1812 |
checker: qt.CodecEquals( |
| 1813 |
"null", |
| 1814 |
nil, |
| 1815 |
func(x any) ([]byte, error) { return []byte("bad json"), nil }, |
| 1816 |
json.Unmarshal, |
| 1817 |
), |
| 1818 |
expectedCheckFailure: fmt.Sprintf(` |
| 1819 |
error: |
| 1820 |
bad check: cannot unmarshal expected contents: %s |
| 1821 |
`, mustJSONUnmarshalErr("bad json")), |
| 1822 |
expectedNegateFailure: fmt.Sprintf(` |
| 1823 |
error: |
| 1824 |
bad check: cannot unmarshal expected contents: %s |
| 1825 |
`, mustJSONUnmarshalErr("bad json")), |
| 1826 |
}, { |
| 1827 |
about: "CodecEquals with options", |
| 1828 |
checker: qt.CodecEquals( |
| 1829 |
`["b", "z", "c", "a"]`, |
| 1830 |
[]string{"a", "c", "z", "b"}, |
| 1831 |
json.Marshal, |
| 1832 |
json.Unmarshal, |
| 1833 |
cmpopts.SortSlices(func(x, y any) bool { return x.(string) < y.(string) }), |
| 1834 |
), |
| 1835 |
expectedNegateFailure: tilde2bq(` |
| 1836 |
error: |
| 1837 |
unexpected success |
| 1838 |
got: |
| 1839 |
~["b", "z", "c", "a"]~ |
| 1840 |
want: |
| 1841 |
[]string{"a", "c", "z", "b"} |
| 1842 |
`), |
| 1843 |
}, { |
| 1844 |
about: "ErrorAs: exact match", |
| 1845 |
checker: qt.ErrorAs(targetErr, new(*errTarget)), |
| 1846 |
expectedNegateFailure: ` |
| 1847 |
error: |
| 1848 |
unexpected success |
| 1849 |
got: |
| 1850 |
e"ptr: target" |
| 1851 |
as type: |
| 1852 |
*qt_test.errTarget |
| 1853 |
`, |
| 1854 |
}, { |
| 1855 |
about: "ErrorAs: wrapped match", |
| 1856 |
checker: qt.ErrorAs(fmt.Errorf("wrapped: %w", targetErr), new(*errTarget)), |
| 1857 |
expectedNegateFailure: ` |
| 1858 |
error: |
| 1859 |
unexpected success |
| 1860 |
got: |
| 1861 |
e"wrapped: ptr: target" |
| 1862 |
as type: |
| 1863 |
*qt_test.errTarget |
| 1864 |
`, |
| 1865 |
}, { |
| 1866 |
about: "ErrorAs: fails if nil error", |
| 1867 |
checker: qt.ErrorAs(nil, new(*errTarget)), |
| 1868 |
expectedCheckFailure: ` |
| 1869 |
error: |
| 1870 |
got nil error but want non-nil |
| 1871 |
got: |
| 1872 |
nil |
| 1873 |
as type: |
| 1874 |
*qt_test.errTarget |
| 1875 |
`, |
| 1876 |
}, { |
| 1877 |
about: "ErrorAs: fails if mismatch", |
| 1878 |
checker: qt.ErrorAs(errors.New("other error"), new(*errTarget)), |
| 1879 |
expectedCheckFailure: ` |
| 1880 |
error: |
| 1881 |
wanted type is not found in error chain |
| 1882 |
got: |
| 1883 |
e"other error" |
| 1884 |
as type: |
| 1885 |
*qt_test.errTarget |
| 1886 |
`, |
| 1887 |
}, { |
| 1888 |
about: "ErrorAs: fails if mismatch with a non-pointer error implementation", |
| 1889 |
checker: qt.ErrorAs(errors.New("other error"), new(errTargetNonPtr)), |
| 1890 |
expectedCheckFailure: ` |
| 1891 |
error: |
| 1892 |
wanted type is not found in error chain |
| 1893 |
got: |
| 1894 |
e"other error" |
| 1895 |
as type: |
| 1896 |
qt_test.errTargetNonPtr |
| 1897 |
`, |
| 1898 |
}, { |
| 1899 |
about: "ErrorAs: bad check if invalid as", |
| 1900 |
checker: qt.ErrorAs(targetErr, &struct{}{}), |
| 1901 |
expectedCheckFailure: ` |
| 1902 |
error: |
| 1903 |
bad check: errors: *target must be interface or implement error |
| 1904 |
`, |
| 1905 |
expectedNegateFailure: ` |
| 1906 |
error: |
| 1907 |
bad check: errors: *target must be interface or implement error |
| 1908 |
`, |
| 1909 |
}, { |
| 1910 |
about: "ErrorIs: exact match", |
| 1911 |
checker: qt.ErrorIs(targetErr, targetErr), |
| 1912 |
expectedNegateFailure: ` |
| 1913 |
error: |
| 1914 |
unexpected success |
| 1915 |
got: |
| 1916 |
e"ptr: target" |
| 1917 |
want: |
| 1918 |
<same as "got"> |
| 1919 |
`, |
| 1920 |
}, { |
| 1921 |
about: "ErrorIs: wrapped match", |
| 1922 |
checker: qt.ErrorIs(fmt.Errorf("wrapped: %w", targetErr), targetErr), |
| 1923 |
expectedNegateFailure: ` |
| 1924 |
error: |
| 1925 |
unexpected success |
| 1926 |
got: |
| 1927 |
e"wrapped: ptr: target" |
| 1928 |
want: |
| 1929 |
e"ptr: target" |
| 1930 |
`, |
| 1931 |
}, { |
| 1932 |
about: "ErrorIs: fails if nil error", |
| 1933 |
checker: qt.ErrorIs(nil, targetErr), |
| 1934 |
expectedCheckFailure: ` |
| 1935 |
error: |
| 1936 |
got nil error but want non-nil |
| 1937 |
got: |
| 1938 |
nil |
| 1939 |
want: |
| 1940 |
e"ptr: target" |
| 1941 |
`, |
| 1942 |
}, { |
| 1943 |
about: "ErrorIs: fails if mismatch", |
| 1944 |
checker: qt.ErrorIs(errors.New("other error"), targetErr), |
| 1945 |
expectedCheckFailure: ` |
| 1946 |
error: |
| 1947 |
wanted error is not found in error chain |
| 1948 |
got: |
| 1949 |
e"other error" |
| 1950 |
want: |
| 1951 |
e"ptr: target" |
| 1952 |
`, |
| 1953 |
}, { |
| 1954 |
about: "ErrorIs: nil to nil match", |
| 1955 |
checker: qt.ErrorIs(nil, nil), |
| 1956 |
expectedNegateFailure: ` |
| 1957 |
error: |
| 1958 |
unexpected success |
| 1959 |
got: |
| 1960 |
nil |
| 1961 |
want: |
| 1962 |
<same as "got"> |
| 1963 |
`, |
| 1964 |
}, { |
| 1965 |
about: "ErrorIs: non-nil to nil mismatch", |
| 1966 |
checker: qt.ErrorIs(targetErr, nil), |
| 1967 |
expectedCheckFailure: ` |
| 1968 |
error: |
| 1969 |
wanted error is not found in error chain |
| 1970 |
got: |
| 1971 |
e"ptr: target" |
| 1972 |
want: |
| 1973 |
nil |
| 1974 |
`, |
| 1975 |
}, { |
| 1976 |
about: "Not: failure", |
| 1977 |
checker: qt.Not(qt.Equals(42, 42)), |
| 1978 |
expectedCheckFailure: ` |
| 1979 |
error: |
| 1980 |
unexpected success |
| 1981 |
got: |
| 1982 |
int(42) |
| 1983 |
want: |
| 1984 |
<same as "got"> |
| 1985 |
`, |
| 1986 |
}, { |
| 1987 |
about: "Not: IsNil failure", |
| 1988 |
checker: qt.Not(qt.IsNil[*int](nil)), |
| 1989 |
expectedCheckFailure: ` |
| 1990 |
error: |
| 1991 |
got nil ptr but want non-nil |
| 1992 |
got: |
| 1993 |
(*int)(nil) |
| 1994 |
`, |
| 1995 |
}} |
| 1996 |
|
| 1997 |
func TestCheckers(t *testing.T) { |
| 1998 |
original := qt.TestingVerbose |
| 1999 |
defer func() { |
| 2000 |
qt.TestingVerbose = original |
| 2001 |
}() |
| 2002 |
for _, test := range checkerTests { |
| 2003 |
*qt.TestingVerbose = func() bool { |
| 2004 |
return test.verbose |
| 2005 |
} |
| 2006 |
t.Run(test.about, func(t *testing.T) { |
| 2007 |
tt := &testingT{} |
| 2008 |
ok := qt.Check(tt, test.checker) |
| 2009 |
checkResult(t, ok, tt.errorString(), test.expectedCheckFailure) |
| 2010 |
}) |
| 2011 |
t.Run("Not "+test.about, func(t *testing.T) { |
| 2012 |
tt := &testingT{} |
| 2013 |
ok := qt.Check(tt, qt.Not(test.checker)) |
| 2014 |
checkResult(t, ok, tt.errorString(), test.expectedNegateFailure) |
| 2015 |
}) |
| 2016 |
} |
| 2017 |
} |
| 2018 |
|
| 2019 |
func diff(got, want any, opts ...cmp.Option) string { |
| 2020 |
d := cmp.Diff(want, got, opts...) |
| 2021 |
return strings.TrimSuffix(qt.Prefixf(" ", "%s", d), "\n") |
| 2022 |
} |
| 2023 |
|
| 2024 |
type jsonErrorMarshaler struct{} |
| 2025 |
|
| 2026 |
func (jsonErrorMarshaler) MarshalJSON() ([]byte, error) { |
| 2027 |
return nil, fmt.Errorf("qt json marshal error") |
| 2028 |
} |
| 2029 |
|
| 2030 |
func mustJSONUnmarshalErr(s string) error { |
| 2031 |
var v any |
| 2032 |
err := json.Unmarshal([]byte(s), &v) |
| 2033 |
if err == nil { |
| 2034 |
panic("want JSON error, got nil") |
| 2035 |
} |
| 2036 |
return err |
| 2037 |
} |
| 2038 |
|
| •2039 |
func mustJSONMarshalErr(v any) error { |
| 2040 |
_, err := json.Marshal(v) |
| 2041 |
if err == nil { |
| 2042 |
panic("want JSON marshal error, got nil") |
| 2043 |
} |
| 2044 |
return err |
| 2045 |
} |
| 2046 |
|
| 2047 |
func tilde2bq(s string) string { |
| 2048 |
return strings.Replace(s, "~", "`", -1) |
| 2049 |
} |