| 1 |
// Licensed under the MIT license, see LICENSE file for details. |
| 2 |
|
| 3 |
package quicktest_test |
| 4 |
|
| 5 |
import ( |
| 6 |
"bytes" |
| 7 |
"errors" |
| 8 |
"fmt" |
| 9 |
"strings" |
| 10 |
"testing" |
| 11 |
|
| 12 |
qt "github.com/frankban/quicktest" |
| 13 |
) |
| 14 |
|
| 15 |
var _ testing.TB = (*qt.C)(nil) |
| 16 |
|
| 17 |
var cTests = []struct { |
| 18 |
about string |
| 19 |
checker qt.Checker |
| 20 |
got interface{} |
| 21 |
args []interface{} |
| 22 |
format func(interface{}) string |
| 23 |
expectedFailure string |
| 24 |
}{{ |
| 25 |
about: "success", |
| 26 |
checker: qt.Equals, |
| 27 |
got: 42, |
| 28 |
args: []interface{}{42}, |
| 29 |
}, { |
| 30 |
about: "failure", |
| 31 |
checker: qt.Equals, |
| 32 |
got: "42", |
| 33 |
args: []interface{}{"47"}, |
| 34 |
expectedFailure: ` |
| 35 |
error: |
| 36 |
values are not equal |
| 37 |
got: |
| 38 |
"42" |
| 39 |
want: |
| 40 |
"47" |
| 41 |
`, |
| 42 |
}, { |
| 43 |
about: "failure with % signs", |
| 44 |
checker: qt.Equals, |
| 45 |
got: "42%x", |
| 46 |
args: []interface{}{"47%y"}, |
| 47 |
expectedFailure: ` |
| 48 |
error: |
| 49 |
values are not equal |
| 50 |
got: |
| 51 |
"42%x" |
| 52 |
want: |
| 53 |
"47%y" |
| 54 |
`, |
| 55 |
}, { |
| 56 |
about: "failure with comment", |
| 57 |
checker: qt.Equals, |
| 58 |
got: true, |
| 59 |
args: []interface{}{false, qt.Commentf("apparently %v != %v", true, false)}, |
| 60 |
expectedFailure: ` |
| 61 |
error: |
| 62 |
values are not equal |
| 63 |
comment: |
| 64 |
apparently true != false |
| 65 |
got: |
| 66 |
bool(true) |
| 67 |
want: |
| 68 |
bool(false) |
| 69 |
`, |
| 70 |
}, { |
| 71 |
about: "another failure with comment", |
| 72 |
checker: qt.IsNil, |
| 73 |
got: 42, |
| 74 |
args: []interface{}{qt.Commentf("bad wolf: %d", 42)}, |
| 75 |
expectedFailure: ` |
| 76 |
error: |
| 77 |
got non-nil value |
| 78 |
comment: |
| 79 |
bad wolf: 42 |
| 80 |
got: |
| 81 |
int(42) |
| 82 |
`, |
| 83 |
}, { |
| 84 |
about: "failure with constant comment", |
| 85 |
checker: qt.IsNil, |
| 86 |
got: "something", |
| 87 |
args: []interface{}{qt.Commentf("these are the voyages")}, |
| 88 |
expectedFailure: ` |
| 89 |
error: |
| 90 |
got non-nil value |
| 91 |
comment: |
| 92 |
these are the voyages |
| 93 |
got: |
| 94 |
"something" |
| 95 |
`, |
| 96 |
}, { |
| 97 |
about: "failure with empty comment", |
| 98 |
checker: qt.IsNil, |
| 99 |
got: 47, |
| 100 |
args: []interface{}{qt.Commentf("")}, |
| 101 |
expectedFailure: ` |
| 102 |
error: |
| 103 |
got non-nil value |
| 104 |
got: |
| 105 |
int(47) |
| 106 |
`, |
| 107 |
}, { |
| 108 |
about: "failure with multiple comments", |
| 109 |
checker: qt.IsNil, |
| 110 |
got: 42, |
| 111 |
args: []interface{}{ |
| 112 |
qt.Commentf("bad wolf: %d", 42), |
| 113 |
qt.Commentf("second comment"), |
| 114 |
}, |
| 115 |
expectedFailure: ` |
| 116 |
error: |
| 117 |
got non-nil value |
| 118 |
comment: |
| 119 |
bad wolf: 42 |
| 120 |
comment: |
| 121 |
second comment |
| 122 |
got: |
| 123 |
int(42) |
| 124 |
`, |
| 125 |
}, { |
| 126 |
about: "nil checker", |
| 127 |
expectedFailure: ` |
| 128 |
error: |
| 129 |
bad check: nil checker provided |
| 130 |
`, |
| 131 |
}, { |
| 132 |
about: "not enough arguments", |
| 133 |
checker: qt.Equals, |
| 134 |
got: 42, |
| 135 |
args: []interface{}{}, |
| 136 |
expectedFailure: ` |
| 137 |
error: |
| 138 |
bad check: not enough arguments provided to checker: got 0, want 1 |
| 139 |
want args: |
| 140 |
want |
| 141 |
`, |
| 142 |
}, { |
| 143 |
about: "not enough arguments with comment", |
| 144 |
checker: qt.DeepEquals, |
| 145 |
got: 42, |
| 146 |
args: []interface{}{qt.Commentf("test %d", 0)}, |
| 147 |
expectedFailure: ` |
| 148 |
error: |
| 149 |
bad check: not enough arguments provided to checker: got 0, want 1 |
| 150 |
comment: |
| 151 |
test 0 |
| 152 |
want args: |
| 153 |
want |
| 154 |
`, |
| 155 |
}, { |
| 156 |
about: "too many arguments", |
| 157 |
checker: qt.Matches, |
| 158 |
got: 42, |
| 159 |
args: []interface{}{42, 47}, |
| 160 |
expectedFailure: ` |
| 161 |
error: |
| 162 |
bad check: too many arguments provided to checker: got 2, want 1 |
| 163 |
got args: |
| 164 |
[]interface {}{ |
| 165 |
int(42), |
| 166 |
int(47), |
| 167 |
} |
| 168 |
want args: |
| 169 |
regexp |
| 170 |
`, |
| 171 |
}, { |
| 172 |
about: "really too many arguments", |
| 173 |
checker: qt.DeepEquals, |
| 174 |
got: 42, |
| 175 |
args: []interface{}{42, 47, nil, "stop"}, |
| 176 |
expectedFailure: ` |
| 177 |
error: |
| 178 |
bad check: too many arguments provided to checker: got 4, want 1 |
| 179 |
got args: |
| 180 |
[]interface {}{ |
| 181 |
int(42), |
| 182 |
int(47), |
| 183 |
nil, |
| 184 |
"stop", |
| 185 |
} |
| 186 |
want args: |
| 187 |
want |
| 188 |
`, |
| 189 |
}, { |
| 190 |
about: "too many arguments with comment", |
| 191 |
checker: qt.IsNil, |
| 192 |
got: 42, |
| 193 |
args: []interface{}{nil, qt.Commentf("these are the voyages")}, |
| 194 |
expectedFailure: ` |
| 195 |
error: |
| 196 |
bad check: too many arguments provided to checker: got 1, want 0 |
| 197 |
comment: |
| 198 |
these are the voyages |
| 199 |
got args: |
| 200 |
[]interface {}{ |
| 201 |
nil, |
| 202 |
} |
| 203 |
`, |
| 204 |
}, { |
| 205 |
about: "many arguments and notes", |
| 206 |
checker: &testingChecker{ |
| 207 |
argNames: []string{"arg1", "arg2", "arg3"}, |
| 208 |
addNotes: func(note func(key string, value interface{})) { |
| 209 |
note("note1", "these") |
| 210 |
note("note2", qt.Unquoted("are")) |
| 211 |
note("note3", "the") |
| 212 |
note("note4", "voyages") |
| 213 |
note("note5", true) |
| 214 |
}, |
| 215 |
err: errors.New("bad wolf"), |
| 216 |
}, |
| 217 |
got: 42, |
| 218 |
args: []interface{}{"val2", "val3"}, |
| 219 |
expectedFailure: ` |
| 220 |
error: |
| 221 |
bad wolf |
| 222 |
note1: |
| 223 |
"these" |
| 224 |
note2: |
| 225 |
are |
| 226 |
note3: |
| 227 |
"the" |
| 228 |
note4: |
| 229 |
"voyages" |
| 230 |
note5: |
| 231 |
bool(true) |
| 232 |
arg1: |
| 233 |
int(42) |
| 234 |
arg2: |
| 235 |
"val2" |
| 236 |
arg3: |
| 237 |
"val3" |
| 238 |
`, |
| 239 |
}, { |
| 240 |
about: "many arguments and notes with the same value", |
| 241 |
checker: &testingChecker{ |
| 242 |
argNames: []string{"arg1", "arg2", "arg3", "arg4"}, |
| 243 |
addNotes: func(note func(key string, value interface{})) { |
| 244 |
note("note1", "value1") |
| 245 |
note("note2", []int{42}) |
| 246 |
note("note3", "value1") |
| 247 |
note("note4", nil) |
| 248 |
}, |
| 249 |
err: errors.New("bad wolf"), |
| 250 |
}, |
| 251 |
got: "value1", |
| 252 |
args: []interface{}{"value1", []int{42}, nil}, |
| 253 |
expectedFailure: ` |
| 254 |
error: |
| 255 |
bad wolf |
| 256 |
note1: |
| 257 |
"value1" |
| 258 |
note2: |
| 259 |
[]int{42} |
| 260 |
note3: |
| 261 |
<same as "note1"> |
| 262 |
note4: |
| 263 |
nil |
| 264 |
arg1: |
| 265 |
<same as "note1"> |
| 266 |
arg2: |
| 267 |
<same as "note1"> |
| 268 |
arg3: |
| 269 |
<same as "note2"> |
| 270 |
arg4: |
| 271 |
<same as "note4"> |
| 272 |
`, |
| 273 |
}, { |
| 274 |
about: "many arguments and notes with custom format function", |
| 275 |
checker: &testingChecker{ |
| 276 |
argNames: []string{"arg1", "arg2", "arg3"}, |
| 277 |
addNotes: func(note func(key string, value interface{})) { |
| 278 |
note("note1", "these") |
| 279 |
note("note2", qt.Unquoted("are")) |
| 280 |
note("note3", "the") |
| 281 |
note("note4", "voyages") |
| 282 |
note("note5", true) |
| 283 |
}, |
| 284 |
err: errors.New("bad wolf"), |
| 285 |
}, |
| 286 |
got: 42, |
| 287 |
args: []interface{}{"val2", "val3"}, |
| 288 |
format: func(v interface{}) string { |
| 289 |
return fmt.Sprintf("bad wolf %v", v) |
| 290 |
}, |
| 291 |
expectedFailure: ` |
| 292 |
error: |
| 293 |
bad wolf |
| 294 |
note1: |
| 295 |
bad wolf these |
| 296 |
note2: |
| 297 |
are |
| 298 |
note3: |
| 299 |
bad wolf the |
| 300 |
note4: |
| 301 |
bad wolf voyages |
| 302 |
note5: |
| 303 |
bad wolf true |
| 304 |
arg1: |
| 305 |
bad wolf 42 |
| 306 |
arg2: |
| 307 |
bad wolf val2 |
| 308 |
arg3: |
| 309 |
bad wolf val3 |
| 310 |
`, |
| 311 |
}, { |
| 312 |
about: "bad check with notes", |
| 313 |
checker: &testingChecker{ |
| 314 |
argNames: []string{"got", "want"}, |
| 315 |
addNotes: func(note func(key string, value interface{})) { |
| 316 |
note("note", 42) |
| 317 |
}, |
| 318 |
err: qt.BadCheckf("bad wolf"), |
| 319 |
}, |
| 320 |
got: 42, |
| 321 |
args: []interface{}{"want"}, |
| 322 |
expectedFailure: ` |
| 323 |
error: |
| 324 |
bad check: bad wolf |
| 325 |
note: |
| 326 |
int(42) |
| 327 |
`, |
| 328 |
}, { |
| 329 |
about: "silent failure with notes", |
| 330 |
checker: &testingChecker{ |
| 331 |
argNames: []string{"got", "want"}, |
| 332 |
addNotes: func(note func(key string, value interface{})) { |
| 333 |
note("note1", "first note") |
| 334 |
note("note2", qt.Unquoted("second note")) |
| 335 |
}, |
| 336 |
err: qt.ErrSilent, |
| 337 |
}, |
| 338 |
got: 42, |
| 339 |
args: []interface{}{"want"}, |
| 340 |
expectedFailure: ` |
| 341 |
note1: |
| 342 |
"first note" |
| 343 |
note2: |
| 344 |
second note |
| 345 |
`, |
| 346 |
}} |
| 347 |
|
| 348 |
func TestCAssertCheck(t *testing.T) { |
| 349 |
for _, test := range cTests { |
| 350 |
t.Run("Assert: "+test.about, func(t *testing.T) { |
| 351 |
if test.format != nil { |
| 352 |
t.Skip("changing format not supported when using qt.Assert directly") |
| 353 |
} |
| 354 |
tt := &testingT{} |
| 355 |
ok := qt.Assert(tt, test.got, test.checker, test.args...) |
| 356 |
checkResult(t, ok, tt.fatalString(), test.expectedFailure) |
| 357 |
if tt.errorString() != "" { |
| 358 |
t.Fatalf("no error messages expected, but got %q", tt.errorString()) |
| 359 |
} |
| 360 |
}) |
| 361 |
t.Run("Check: "+test.about, func(t *testing.T) { |
| 362 |
if test.format != nil { |
| 363 |
t.Skip("changing format not supported when using qt.Check directly") |
| 364 |
} |
| 365 |
tt := &testingT{} |
| 366 |
ok := qt.Check(tt, test.got, test.checker, test.args...) |
| 367 |
checkResult(t, ok, tt.errorString(), test.expectedFailure) |
| 368 |
if tt.fatalString() != "" { |
| 369 |
t.Fatalf("no fatal messages expected, but got %q", tt.fatalString()) |
| 370 |
} |
| 371 |
}) |
| 372 |
t.Run("c.Assert: "+test.about, func(t *testing.T) { |
| 373 |
tt := &testingT{} |
| 374 |
c := qt.New(tt) |
| 375 |
if test.format != nil { |
| 376 |
c.SetFormat(test.format) |
| 377 |
} |
| 378 |
ok := c.Assert(test.got, test.checker, test.args...) |
| 379 |
checkResult(t, ok, tt.fatalString(), test.expectedFailure) |
| 380 |
if tt.errorString() != "" { |
| 381 |
t.Fatalf("no error messages expected, but got %q", tt.errorString()) |
| 382 |
} |
| 383 |
}) |
| 384 |
t.Run("c.Check: "+test.about, func(t *testing.T) { |
| 385 |
tt := &testingT{} |
| 386 |
c := qt.New(tt) |
| 387 |
if test.format != nil { |
| 388 |
c.SetFormat(test.format) |
| 389 |
} |
| 390 |
ok := c.Check(test.got, test.checker, test.args...) |
| 391 |
checkResult(t, ok, tt.errorString(), test.expectedFailure) |
| 392 |
if tt.fatalString() != "" { |
| 393 |
t.Fatalf("no fatal messages expected, but got %q", tt.fatalString()) |
| 394 |
} |
| 395 |
}) |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
func TestCRunSuccess(t *testing.T) { |
| 400 |
tt := &testingT{} |
| 401 |
c := qt.New(tt) |
| 402 |
var run bool |
| 403 |
subTestName := "my test" |
| 404 |
ok := c.Run(subTestName, func(innerC *qt.C) { |
| 405 |
run = true |
| 406 |
if innerC == c { |
| 407 |
t.Fatal("subtest C: same instance provided") |
| 408 |
} |
| 409 |
if innerC.TB != tt.subTestT { |
| 410 |
t.Fatalf("subtest testing object: got %p, want %p", innerC.TB, tt.subTestT) |
| 411 |
} |
| 412 |
if tt.subTestName != subTestName { |
| 413 |
t.Fatalf("subtest name: got %q, want %q", tt.subTestName, subTestName) |
| 414 |
} |
| 415 |
}) |
| 416 |
assertBool(t, run, true) |
| 417 |
assertBool(t, ok, false) |
| 418 |
|
| 419 |
// Simulate a test success. |
| 420 |
tt.subTestResult = true |
| 421 |
ok = c.Run(subTestName, func(innerC *qt.C) {}) |
| 422 |
assertBool(t, ok, true) |
| 423 |
} |
| 424 |
|
| 425 |
func TestCRunOnBenchmark(t *testing.T) { |
| 426 |
called := false |
| 427 |
testing.Benchmark(func(b *testing.B) { |
| 428 |
c := qt.New(b) |
| 429 |
c.Run("c", func(c *qt.C) { |
| 430 |
b1, ok := c.TB.(*testing.B) |
| 431 |
if !ok { |
| 432 |
t.Errorf("c.TB is type %T not *testing.B", c.TB) |
| 433 |
return |
| 434 |
} |
| 435 |
if b1 == b { |
| 436 |
t.Errorf("c.TB hasn't been given a new B value") |
| 437 |
return |
| 438 |
} |
| 439 |
called = true |
| 440 |
}) |
| 441 |
}) |
| 442 |
if !called { |
| 443 |
t.Fatalf("sub-benchmark was never called") |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
// wrongRun1 has Run method with wrong arg count. |
| 448 |
type wrongRun1 struct { |
| 449 |
testing.TB |
| 450 |
} |
| 451 |
|
| 452 |
func (wrongRun1) Run() {} |
| 453 |
|
| 454 |
// wrongRun2 has no Run method. |
| 455 |
type wrongRun2 struct { |
| 456 |
testing.TB |
| 457 |
} |
| 458 |
|
| 459 |
// wrongRun3 has Run method that takes a type not |
| 460 |
// assignable to testing.TB. |
| 461 |
type wrongRun3 struct { |
| 462 |
testing.TB |
| 463 |
} |
| 464 |
|
| 465 |
func (wrongRun3) Run(string, func(string)) bool { return false } |
| 466 |
|
| 467 |
// wrongRun4 has Run method that doesn't return bool. |
| 468 |
type wrongRun4 struct { |
| 469 |
testing.TB |
| 470 |
} |
| 471 |
|
| 472 |
func (wrongRun4) Run(string, func(*testing.T)) {} |
| 473 |
|
| 474 |
var CRunPanicTests = []struct { |
| 475 |
tb testing.TB |
| 476 |
expectPanic string |
| 477 |
}{{ |
| 478 |
tb: wrongRun1{}, |
| 479 |
expectPanic: "wrong argument count for Run method", |
| 480 |
}, { |
| 481 |
tb: wrongRun2{}, |
| 482 |
expectPanic: "no Run method", |
| 483 |
}, { |
| 484 |
tb: wrongRun3{}, |
| 485 |
expectPanic: "bad first argument type for Run method", |
| 486 |
}, { |
| 487 |
tb: wrongRun4{}, |
| 488 |
expectPanic: "wrong argument count for Run method", |
| 489 |
}} |
| 490 |
|
| 491 |
func TestCRunPanic(t *testing.T) { |
| 492 |
for _, test := range CRunPanicTests { |
| 493 |
t.Run(fmt.Sprintf("%T", test.tb), func(t *testing.T) { |
| 494 |
c := qt.New(test.tb) |
| 495 |
defer func() { |
| 496 |
got := recover() |
| 497 |
want := fmt.Sprintf( |
| 498 |
"cannot execute Run with underlying concrete type %T (%s)", |
| 499 |
test.tb, test.expectPanic, |
| 500 |
) |
| 501 |
if got != want { |
| 502 |
t.Fatalf("unexpected panic recover message; got %q want %q", got, want) |
| 503 |
} |
| 504 |
}() |
| 505 |
c.Run("panic", func(innerC *qt.C) {}) |
| 506 |
}) |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
func TestCRunFormat(t *testing.T) { |
| 511 |
tt, innerTT := &testingT{}, &testingT{} |
| 512 |
c := qt.New(tt) |
| 513 |
c.SetFormat(func(v interface{}) string { |
| 514 |
return fmt.Sprintf("myfmt(%v)", v) |
| 515 |
}) |
| 516 |
c.Run("my test", func(innerC *qt.C) { |
| 517 |
innerC.TB = innerTT |
| 518 |
innerC.Check(42, qt.Equals, nil) |
| 519 |
}) |
| 520 |
assertPrefix(t, innerTT.errorString(), ` |
| 521 |
error: |
| 522 |
values are not equal |
| 523 |
got: |
| 524 |
myfmt(42) |
| 525 |
want: |
| 526 |
myfmt(<nil>) |
| 527 |
`) |
| 528 |
} |
| 529 |
|
| 530 |
func TestHelper(t *testing.T) { |
| 531 |
tt := &testingT{} |
| 532 |
qt.Assert(tt, true, qt.IsFalse) |
| 533 |
if tt.helperCalls != 3 { |
| 534 |
t.Fatalf("want 3 calls (Assert, c.Assert, check), got %d", tt.helperCalls) |
| 535 |
} |
| 536 |
} |
| 537 |
|
| 538 |
func TestCHelper(t *testing.T) { |
| 539 |
tt := &testingT{} |
| 540 |
c := qt.New(tt) |
| 541 |
c.Assert(true, qt.IsFalse) |
| 542 |
if tt.helperCalls != 2 { |
| 543 |
t.Fatalf("want 2 calls (c.Assert, check), got %d", tt.helperCalls) |
| 544 |
} |
| 545 |
} |
| 546 |
|
| 547 |
func TestCParallel(t *testing.T) { |
| 548 |
tt := &testingT{} |
| 549 |
c := qt.New(tt) |
| 550 |
c.Parallel() |
| 551 |
if !tt.parallel { |
| 552 |
t.Fatalf("parallel not called") |
| 553 |
} |
| 554 |
} |
| 555 |
|
| 556 |
func TestCParallelPanic(t *testing.T) { |
| 557 |
c := qt.New(&testing.B{}) |
| 558 |
defer func() { |
| 559 |
r := recover() |
| 560 |
if r != "cannot execute Parallel with underlying concrete type *testing.B" { |
| 561 |
t.Fatalf("unexpected panic recover: %v", r) |
| 562 |
} |
| 563 |
}() |
| 564 |
c.Parallel() |
| 565 |
} |
| 566 |
|
| 567 |
func TestCDefer(t *testing.T) { |
| 568 |
c := qt.New(t) |
| 569 |
var defers []int |
| 570 |
c.Run("subtest", func(c *qt.C) { |
| 571 |
c.Defer(func() { defers = append(defers, 1) }) |
| 572 |
c.Defer(func() { defers = append(defers, 2) }) |
| 573 |
// Calling Done twice should not do anything more. |
| 574 |
c.Done() |
| 575 |
}) |
| 576 |
c.Assert(defers, qt.DeepEquals, []int{2, 1}) |
| 577 |
} |
| 578 |
|
| 579 |
func TestCDeferCalledEvenAfterGoexit(t *testing.T) { |
| 580 |
// The testing package uses runtime.Goexit on |
| 581 |
// assertion failure, so check that defers are still |
| 582 |
// called in that case. |
| 583 |
c := qt.New(t) |
| 584 |
defers := 0 |
| 585 |
c.Run("subtest", func(c *qt.C) { |
| 586 |
c.Defer(func() { |
| 587 |
defers++ |
| 588 |
}) |
| 589 |
c.Defer(func() { |
| 590 |
c.SkipNow() |
| 591 |
}) |
| 592 |
}) |
| 593 |
c.Assert(defers, qt.Equals, 1) |
| 594 |
} |
| 595 |
|
| 596 |
func TestCRunDefer(t *testing.T) { |
| 597 |
c := qt.New(t) |
| 598 |
defers := 0 |
| 599 |
c.Run("subtest", func(c *qt.C) { |
| 600 |
c.Run("x", func(c *qt.C) { |
| 601 |
c.Defer(func() { defers++ }) |
| 602 |
}) |
| 603 |
}) |
| 604 |
c.Assert(defers, qt.Equals, 1) |
| 605 |
} |
| 606 |
|
| 607 |
type customT struct { |
| 608 |
*testing.T |
| 609 |
data int |
| 610 |
} |
| 611 |
|
| 612 |
func (t *customT) Run(name string, f func(*customT)) bool { |
| 613 |
return t.T.Run(name, func(t1 *testing.T) { |
| 614 |
f(&customT{t1, t.data}) |
| 615 |
}) |
| 616 |
} |
| 617 |
|
| 618 |
func TestCRunCustomType(t *testing.T) { |
| 619 |
ct := &customT{t, 99} |
| 620 |
c := qt.New(ct) |
| 621 |
called := 0 |
| 622 |
c.Run("test", func(c *qt.C) { |
| 623 |
called++ |
| 624 |
ct1, ok := c.TB.(*customT) |
| 625 |
if !ok { |
| 626 |
t.Error("TB isn't expected type") |
| 627 |
} |
| 628 |
if ct1.data != ct.data { |
| 629 |
t.Errorf("data not copied correctly; got %v want %v", ct1.data, ct.data) |
| 630 |
} |
| 631 |
if ct1 == ct { |
| 632 |
t.Errorf("old instance passed, not new") |
| 633 |
} |
| 634 |
}) |
| 635 |
if called != 1 { |
| 636 |
t.Fatalf("subtest was called %d times, not once", called) |
| 637 |
} |
| 638 |
} |
| 639 |
|
| 640 |
func checkResult(t *testing.T, ok bool, got, want string) { |
| 641 |
t.Helper() |
| 642 |
if want != "" { |
| 643 |
assertPrefix(t, got, want+"stack:\n") |
| 644 |
assertBool(t, ok, false) |
| 645 |
return |
| 646 |
} |
| 647 |
if got != "" { |
| 648 |
t.Fatalf("output:\ngot %q\nwant empty", got) |
| 649 |
} |
| 650 |
assertBool(t, ok, true) |
| 651 |
} |
| 652 |
|
| 653 |
// testingT can be passed to qt.New for testing purposes. |
| 654 |
type testingT struct { |
| 655 |
testing.TB |
| 656 |
|
| 657 |
errorBuf bytes.Buffer |
| 658 |
fatalBuf bytes.Buffer |
| 659 |
|
| 660 |
subTestResult bool |
| 661 |
subTestName string |
| 662 |
subTestT *testing.T |
| 663 |
|
| 664 |
helperCalls int |
| 665 |
parallel bool |
| 666 |
} |
| 667 |
|
| 668 |
// Error overrides testing.TB.Error so that messages are collected. |
| 669 |
func (t *testingT) Error(a ...interface{}) { |
| 670 |
fmt.Fprint(&t.errorBuf, a...) |
| 671 |
} |
| 672 |
|
| 673 |
// Fatal overrides testing.TB.Fatal so that messages are collected and the |
| 674 |
// goroutine is not killed. |
| 675 |
func (t *testingT) Fatal(a ...interface{}) { |
| 676 |
fmt.Fprint(&t.fatalBuf, a...) |
| 677 |
} |
| 678 |
|
| 679 |
// Parallel overrides testing.TB.Parallel in order to record the call. |
| 680 |
func (t *testingT) Parallel() { |
| 681 |
t.parallel = true |
| 682 |
} |
| 683 |
|
| 684 |
// Helper overrides testing.TB.Helper in order to count calls. |
| 685 |
func (t *testingT) Helper() { |
| 686 |
t.helperCalls += 1 |
| 687 |
} |
| 688 |
|
| 689 |
// Fatal overrides *testing.T.Fatal so that messages are collected and the |
| 690 |
// goroutine is not killed. |
| 691 |
func (t *testingT) Run(name string, f func(t *testing.T)) bool { |
| 692 |
t.subTestName, t.subTestT = name, &testing.T{} |
| 693 |
f(t.subTestT) |
| 694 |
return t.subTestResult |
| 695 |
} |
| 696 |
|
| 697 |
// errorString returns the error message. |
| 698 |
func (t *testingT) errorString() string { |
| 699 |
return t.errorBuf.String() |
| 700 |
} |
| 701 |
|
| 702 |
// fatalString returns the fatal error message. |
| 703 |
func (t *testingT) fatalString() string { |
| 704 |
return t.fatalBuf.String() |
| 705 |
} |
| 706 |
|
| 707 |
// assertPrefix fails if the got value does not have the given prefix. |
| 708 |
func assertPrefix(t testing.TB, got, prefix string) { |
| 709 |
t.Helper() |
| 710 |
if prefix == "" { |
| 711 |
t.Fatal("prefix: empty value provided") |
| 712 |
} |
| 713 |
if !strings.HasPrefix(got, prefix) { |
| 714 |
t.Fatalf(`prefix: |
| 715 |
got %q |
| 716 |
want %q |
| 717 |
-------------------- got -------------------- |
| 718 |
%s |
| 719 |
-------------------- want ------------------- |
| 720 |
%s |
| 721 |
---------------------------------------------`, got, prefix, got, prefix) |
| 722 |
} |
| 723 |
} |
| 724 |
|
| 725 |
// assertBool fails if the given boolean values don't match. |
| 726 |
func assertBool(t testing.TB, got, want bool) { |
| 727 |
t.Helper() |
| 728 |
if got != want { |
| 729 |
t.Fatalf("bool:\ngot %v\nwant %v", got, want) |
| 730 |
} |
| 731 |
} |
| 732 |
|
| 733 |
// testingChecker is a quicktest.Checker used in tests. It receives the |
| 734 |
// provided argNames, adds notes via the provided addNotes function, and when |
| 735 |
// the check is run the provided error is returned. |
| 736 |
type testingChecker struct { |
| 737 |
argNames []string |
| 738 |
addNotes func(note func(key string, value interface{})) |
| 739 |
err error |
| 740 |
} |
| 741 |
|
| 742 |
// Check implements quicktest.Checker by returning the stored error. |
| 743 |
func (c *testingChecker) Check(got interface{}, args []interface{}, note func(key string, value interface{})) error { |
| 744 |
if c.addNotes != nil { |
| 745 |
c.addNotes(note) |
| 746 |
} |
| 747 |
return c.err |
| 748 |
} |
| 749 |
|
| 750 |
// Info implements quicktest.Checker by returning the stored args. |
| 751 |
func (c *testingChecker) ArgNames() []string { |
| 752 |
return c.argNames |
| 753 |
} |