Debian Codegraph @

helpers/tdsuite/suite_test.go

raw
1 // Copyright (c) 2021, Maxime Soulé
2 // All rights reserved.
3 //
4 // This source code is licensed under the BSD-style license found in the
5 // LICENSE file in the root directory of this source tree.
6
7 package tdsuite_test
8
9 import (
10 "errors"
11 "runtime"
12 "strings"
13 "testing"
14
15 "github.com/maxatome/go-testdeep/helpers/tdsuite"
16 "github.com/maxatome/go-testdeep/internal/test"
17 "github.com/maxatome/go-testdeep/td"
18 )
19
20 type base struct {
21 calls []string
22 }
23
24 func (b *base) rec(plus ...string) {
25 pc, _, _, _ := runtime.Caller(1)
26 name := runtime.FuncForPC(pc).Name()
27 pos := strings.LastIndexByte(name, '.')
28 if name[pos+1:] == "func1" { // Cleanup()
29 npos := strings.LastIndexByte(name[:pos], '.')
30 name = name[npos+1:pos] + ".Cleanup"
31 } else {
32 name = name[pos+1:]
33 }
34 if len(plus) > 0 {
35 name += "+" + strings.Join(plus, "+")
36 }
37 b.calls = append(b.calls, name)
38 }
39
40 func (b *base) clean() {
41 b.calls = b.calls[:0]
42 }
43
44 // Mini has only tests, no hooks.
45 type Mini struct{ base }
46
47 func (m *Mini) Test1(t *td.T) { m.rec() }
48 func (m *Mini) Test2(assert *td.T, require *td.T) { m.rec() }
49
50 // Full has tests and all possible hooks.
51 type Full struct{ base }
52
53 func (f *Full) Setup(t *td.T) error { f.rec(); return nil }
54 func (f *Full) PreTest(t *td.T, tn string) error { f.rec(tn); return nil }
55 func (f *Full) PostTest(t *td.T, tn string) error { f.rec(tn); return nil }
56 func (f *Full) BetweenTests(t *td.T, prev, next string) error {
57 f.rec(prev, next)
58 return nil
59 }
60 func (f *Full) Destroy(t *td.T) error { f.rec(); return nil }
61
62 func (f *Full) Test1(t *td.T) { f.rec() }
63 func (f *Full) Test2(assert *td.T, require *td.T) { f.rec() }
64 func (f *Full) Test3(t *td.T) { f.rec() }
65 func (f *Full) Testimony(t *td.T) { f.rec() } // not a test method
66
67 var (
68 _ tdsuite.Setup = (*Full)(nil)
69 _ tdsuite.PreTest = (*Full)(nil)
70 _ tdsuite.PostTest = (*Full)(nil)
71 _ tdsuite.BetweenTests = (*Full)(nil)
72 _ tdsuite.Destroy = (*Full)(nil)
73 )
74
75 // FullBrokenHooks has all possible hooks, but wrongly defined, as they don't
76 // match the hook interfaces.
77 type FullBrokenHooks struct{}
78
79 func (*FullBrokenHooks) Setup() error { return nil }
80 func (*FullBrokenHooks) PreTest(t *td.T, testName *string) error { return nil }
81 func (*FullBrokenHooks) PostTest(t *td.T, testName string) {}
82 func (*FullBrokenHooks) BetweenTests(t *td.T, prev, next *string) error { return nil }
83 func (*FullBrokenHooks) Destroy(t *td.T) {}
84
85 func (*FullBrokenHooks) Test1(_ *td.T) {}
86
87 // FullNoPtr has hooks & tests as non-pointer & pointer methods.
88 type FullNoPtr struct{}
89
90 var traceFullNoPtr base
91
92 func (f FullNoPtr) Setup(t *td.T) error { traceFullNoPtr.rec(); return nil }
93 func (f FullNoPtr) PreTest(t *td.T, tn string) error { traceFullNoPtr.rec(tn); return nil }
94 func (f *FullNoPtr) PostTest(t *td.T, tn string) error { traceFullNoPtr.rec(tn); return nil }
95 func (f FullNoPtr) BetweenTests(t *td.T, prev, next string) error {
96 traceFullNoPtr.rec(prev, next)
97 return nil
98 }
99 func (f FullNoPtr) Destroy(t *td.T) error { traceFullNoPtr.rec(); return nil }
100
101 func (f FullNoPtr) Test1(t *td.T) { traceFullNoPtr.rec() }
102 func (f *FullNoPtr) Test2(assert *td.T, require *td.T) { traceFullNoPtr.rec() }
103 func (f FullNoPtr) Test3(t *td.T) { traceFullNoPtr.rec() }
104 func (f *FullNoPtr) Testimony(t *td.T) { traceFullNoPtr.rec() } // not a test method
105
106 // ErrNone has no tests.
107 type ErrNone struct{}
108
109 // ErrOut1 has a Test method with bad return type.
110 type ErrOut1 struct{}
111
112 func (f ErrOut1) Test(t *td.T) int { return 0 }
113
114 // ErrOut2a has a Test method with bad return types.
115 type ErrOut2a struct{}
116
117 func (f ErrOut2a) Test(t *td.T) (bool, int) { return false, 0 }
118
119 // ErrOut2b has a Test method with bad return types.
120 type ErrOut2b struct{}
121
122 func (f ErrOut2b) Test(t *td.T) (int, error) { return 0, nil }
123
124 // ErrOut has a Test method with bad return types.
125 type ErrOut struct{}
126
127 func (f ErrOut) Test(t *td.T) (int, int, int) { return 1, 2, 3 }
128
129 // Skip has several skipped Test methods.
130 type Skip struct{ base }
131
132 func (s *Skip) Test1Param(i int) {}
133 func (s *Skip) Test2ParamsA(i, j int) {}
134 func (s *Skip) Test2ParamsB(i int, require *td.T) {}
135 func (s *Skip) Test2ParamsC(assert *td.T, i int) {}
136 func (s *Skip) Test3Params(t *td.T, i, j int) {}
137 func (s *Skip) TestNoParams() {}
138 func (s *Skip) TestOK(t *td.T) { s.rec() }
139 func (s *Skip) TestVariadic(t ...*td.T) {}
140
141 func TestRun(t *testing.T) {
142 t.Run("Mini", func(t *testing.T) {
143 suite := Mini{}
144 td.CmpTrue(t, tdsuite.Run(t, &suite))
145 td.Cmp(t, suite.calls, []string{"Test1", "Test2"})
146 })
147
148 t.Run("Full ptr", func(t *testing.T) {
149 suite := Full{}
150 td.CmpTrue(t, tdsuite.Run(t, &suite))
151 ok := td.Cmp(t, suite.calls, []string{
152 "Setup",
153 /**/ "PreTest+Test1",
154 /**/ "Test1",
155 /**/ "PostTest+Test1",
156 "BetweenTests+Test1+Test2",
157 /**/ "PreTest+Test2",
158 /**/ "Test2",
159 /**/ "PostTest+Test2",
160 "BetweenTests+Test2+Test3",
161 /**/ "PreTest+Test3",
162 /**/ "Test3",
163 /**/ "PostTest+Test3",
164 "Destroy",
165 })
166 if !ok {
167 for _, c := range suite.calls {
168 switch c[0] {
169 case 'S', 'B', 'D':
170 t.Log(c)
171 default:
172 t.Log(" ", c)
173 }
174 }
175 }
176 })
177
178 t.Run("Without ptr: only non-ptr methods", func(t *testing.T) {
179 defer traceFullNoPtr.clean()
180 suite := FullNoPtr{}
181 tb := test.NewTestingTB("TestWithoutPtr")
182 td.CmpTrue(t, tdsuite.Run(tb, suite)) // non-ptr
183 ok := td.Cmp(t, traceFullNoPtr.calls, []string{
184 "Setup",
185 /**/ "PreTest+Test1",
186 /**/ "Test1",
187 // /**/ "PostTest+Test1", // PostTest is a ptr method
188 // Test2 is a ptr method
189 // "BetweenTests+Test1+Test2",
190 // /**/ "PreTest+Test2",
191 // /**/ "Test2",
192 // /**/ "PostTest+Test2",
193 // "BetweenTests+Test2+Test3",
194 "BetweenTests+Test1+Test3",
195 /**/ "PreTest+Test3",
196 /**/ "Test3",
197 // /**/ "PostTest+Test3", // PostTest is a ptr method
198 "Destroy",
199 })
200 if !ok {
201 for _, c := range traceFullNoPtr.calls {
202 switch c[0] {
203 case 'S', 'B', 'D':
204 t.Log(c)
205 default:
206 t.Log(" ", c)
207 }
208 }
209 }
210 td.CmpEmpty(t, tb.ContainsMessages("Run(): several methods are not accessible as suite is not a pointer but tdsuite_test.FullNoPtr: PostTest, Test2"))
211 })
212
213 t.Run("With ptr: all ptr & non-ptr methods", func(t *testing.T) {
214 defer traceFullNoPtr.clean()
215 suite := FullNoPtr{}
216 td.CmpTrue(t, tdsuite.Run(t, &suite)) // ptr
217 ok := td.Cmp(t, traceFullNoPtr.calls, []string{
218 "Setup",
219 /**/ "PreTest+Test1",
220 /**/ "Test1",
221 /**/ "PostTest+Test1",
222 "BetweenTests+Test1+Test2",
223 /**/ "PreTest+Test2",
224 /**/ "Test2",
225 /**/ "PostTest+Test2",
226 "BetweenTests+Test2+Test3",
227 /**/ "PreTest+Test3",
228 /**/ "Test3",
229 /**/ "PostTest+Test3",
230 "Destroy",
231 })
232 if !ok {
233 for _, c := range traceFullNoPtr.calls {
234 switch c[0] {
235 case 'S', 'B', 'D':
236 t.Log(c)
237 default:
238 t.Log(" ", c)
239 }
240 }
241 }
242 })
243
244 t.Run("ErrNil", func(t *testing.T) {
245 tb := test.NewTestingTB("TestNil")
246 tb.CatchFatal(func() { tdsuite.Run(tb, nil) })
247 td.CmpTrue(t, tb.IsFatal)
248 td.Cmp(t, tb.LastMessage(), "Run(): suite parameter cannot be nil")
249 })
250
251 t.Run("ErrNone", func(t *testing.T) {
252 suite := ErrNone{}
253 tb := test.NewTestingTB("TestErrNone")
254 tb.CatchFatal(func() { tdsuite.Run(tb, suite) })
255 td.CmpTrue(t, tb.IsFatal)
256 td.Cmp(t, tb.LastMessage(), "Run(): no test methods found for type tdsuite_test.ErrNone")
257 })
258
259 t.Run("Full-no-ptr", func(t *testing.T) {
260 suite := Full{}
261 tb := test.NewTestingTB("Full-no-ptr")
262 tb.CatchFatal(func() { tdsuite.Run(tb, suite) })
263 td.CmpTrue(t, tb.IsFatal)
264 td.Cmp(t, tb.Messages, []string{
265 "Run(): several methods are not accessible as suite is not a pointer but tdsuite_test.Full: BetweenTests, Destroy, PostTest, PreTest, Setup, Test1, Test2, Test3",
266 "Run(): no test methods found for type tdsuite_test.Full",
267 })
268 })
269
270 t.Run("ErrOut1", func(t *testing.T) {
271 suite := ErrOut1{}
272 tb := test.NewTestingTB("TestErrOut1")
273 tb.CatchFatal(func() { tdsuite.Run(tb, suite) })
274 td.CmpTrue(t, tb.IsFatal)
275 td.Cmp(t, tb.LastMessage(), "Run(): method tdsuite_test.ErrOut1.Test returns int value. Only bool or error are allowed")
276 })
277
278 t.Run("ErrOut2a", func(t *testing.T) {
279 suite := ErrOut2a{}
280 tb := test.NewTestingTB("TestErrOut2a")
281 tb.CatchFatal(func() { tdsuite.Run(tb, suite) })
282 td.CmpTrue(t, tb.IsFatal)
283 td.Cmp(t, tb.LastMessage(), "Run(): method tdsuite_test.ErrOut2a.Test returns (bool, int) values. Only (bool, error) is allowed")
284 })
285
286 t.Run("ErrOut2b", func(t *testing.T) {
287 suite := ErrOut2b{}
288 tb := test.NewTestingTB("TestErrOut2b")
289 tb.CatchFatal(func() { tdsuite.Run(tb, suite) })
290 td.CmpTrue(t, tb.IsFatal)
291 td.Cmp(t, tb.LastMessage(), "Run(): method tdsuite_test.ErrOut2b.Test returns (int, error) values. Only (bool, error) is allowed")
292 })
293
294 t.Run("ErrOut", func(t *testing.T) {
295 suite := ErrOut{}
296 tb := test.NewTestingTB("TestErrOut")
297 tb.CatchFatal(func() { tdsuite.Run(tb, suite) })
298 td.CmpTrue(t, tb.IsFatal)
299 td.Cmp(t, tb.LastMessage(), "Run(): method tdsuite_test.ErrOut.Test returns 3 values. Only 0, 1 (bool or error) or 2 (bool, error) values are allowed")
300 })
301
302 t.Run("Skip", func(t *testing.T) {
303 suite := Skip{}
304 tb := test.NewTestingTB("TestSkip")
305 tdsuite.Run(tb, &suite)
306 test.IsFalse(t, tb.IsFatal)
307 td.Cmp(t, suite.calls, []string{"TestOK"})
308
309 const p = "Run(): method *tdsuite_test.Skip."
310 td.Cmp(t, tb.Messages, []string{
311 p + "Test1Param skipped, unrecognized parameter type int. Only *td.T allowed",
312 p + "Test2ParamsA skipped, unrecognized parameters types (int, int). Only (*td.T, *td.T) allowed",
313 p + "Test2ParamsB skipped, unrecognized first parameter type int. Only (*td.T, *td.T) allowed",
314 p + "Test2ParamsC skipped, unrecognized second parameter type int. Only (*td.T, *td.T) allowed",
315 p + "Test3Params skipped, too many parameters",
316 p + "TestNoParams skipped, no input parameters",
317 p + "TestVariadic skipped, variadic parameters not supported",
318 "++++ TestOK", // (*T).Run() log as test.TestingTB has no Run() method
319 })
320 })
321 }
322
323 // Error allows to raise errors.
324 type Error struct {
325 base
326 setup bool
327 destroy bool
328 betweenTests bool
329 preTest int
330 postTest int
331
332 testBool [2]bool
333 testError [2]bool
334 testBoolErrorBool [2]bool
335 testBoolErrorErr [2]bool
336 }
337
338 func (e *Error) Setup(t *td.T) error {
339 if e.setup {
340 return errors.New("Setup error")
341 }
342 return nil
343 }
344
345 func (e *Error) PreTest(t *td.T, tn string) error {
346 if e.preTest > 0 {
347 e.preTest--
348 if e.preTest == 0 {
349 return errors.New("PreTest error")
350 }
351 }
352 return nil
353 }
354
355 func (e *Error) PostTest(t *td.T, tn string) error {
356 if e.postTest > 0 {
357 e.postTest--
358 if e.postTest == 0 {
359 return errors.New("PostTest error")
360 }
361 }
362 return nil
363 }
364
365 func (e *Error) BetweenTests(t *td.T, prev, next string) error {
366 if e.betweenTests {
367 return errors.New("BetweenTests error")
368 }
369 return nil
370 }
371
372 func (e *Error) Destroy(t *td.T) error {
373 if e.destroy {
374 return errors.New("Destroy error")
375 }
376 return nil
377 }
378
379 // 1 param methods.
380 func (e *Error) Test1Bool(t *td.T) bool {
381 e.rec()
382 return !e.testBool[0]
383 }
384
385 func (e *Error) Test1Error(t *td.T) error {
386 e.rec()
387 if e.testError[0] {
388 return errors.New("Test1Error error")
389 }
390 return nil
391 }
392
393 func (e *Error) Test1BoolError(t *td.T) (b bool, err error) {
394 e.rec()
395 b = !e.testBoolErrorBool[0]
396 if e.testBoolErrorErr[0] {
397 err = errors.New("Test1BoolError error")
398 }
399 return
400 }
401 func (e *Error) Test1Z(t *td.T) { e.rec() }
402
403 // 2 params methods.
404 func (e *Error) Test2Bool(assert, require *td.T) bool {
405 e.rec()
406 return !e.testBool[1]
407 }
408
409 func (e *Error) Test2Error(assert, require *td.T) error {
410 e.rec()
411 if e.testError[1] {
412 return errors.New("Test2Error error")
413 }
414 return nil
415 }
416
417 func (e *Error) Test2BoolError(assert, require *td.T) (b bool, err error) {
418 e.rec()
419 b = !e.testBoolErrorBool[1]
420 if e.testBoolErrorErr[1] {
421 err = errors.New("Test2BoolError error")
422 }
423 return
424 }
425 func (e *Error) Test2Z(assert, require *td.T) { e.rec() }
426
427 func TestRunErrors(t *testing.T) {
428 t.Run("Setup", func(t *testing.T) {
429 suite := Error{setup: true}
430 tb := test.NewTestingTB("TestError")
431 td.CmpFalse(t, tdsuite.Run(tb, &suite))
432 td.CmpFalse(t, tb.IsFatal)
433 td.Cmp(t, tb.Messages, []string{
434 "*tdsuite_test.Error suite setup error: Setup error",
435 })
436 })
437
438 t.Run("Destroy", func(t *testing.T) {
439 suite := Error{destroy: true}
440 tb := test.NewTestingTB("TestError")
441 td.CmpFalse(t, tdsuite.Run(tb, &suite))
442 td.CmpFalse(t, tb.IsFatal)
443 td.Cmp(t, tb.Messages, []string{
444 "++++ Test1Bool",
445 "++++ Test1BoolError",
446 "++++ Test1Error",
447 "++++ Test1Z",
448 //
449 "++++ Test2Bool",
450 "++++ Test2BoolError",
451 "++++ Test2Error",
452 "++++ Test2Z",
453 "*tdsuite_test.Error suite destroy error: Destroy error",
454 })
455 })
456
457 t.Run("PreTest", func(t *testing.T) {
458 t.Run("1 param", func(t *testing.T) {
459 suite := Error{preTest: 2}
460 tb := test.NewTestingTB("TestError")
461 td.CmpFalse(t, tdsuite.Run(tb, &suite))
462 td.CmpFalse(t, tb.IsFatal)
463 td.Cmp(t, tb.Messages, []string{
464 "++++ Test1Bool",
465 "++++ Test1BoolError",
466 "Test1BoolError pre-test error: PreTest error",
467 "++++ Test1Error",
468 "++++ Test1Z",
469 //
470 "++++ Test2Bool",
471 "++++ Test2BoolError",
472 "++++ Test2Error",
473 "++++ Test2Z",
474 })
475 })
476
477 t.Run("2 params", func(t *testing.T) {
478 suite := Error{preTest: 6}
479 tb := test.NewTestingTB("TestError")
480 td.CmpFalse(t, tdsuite.Run(tb, &suite))
481 td.CmpFalse(t, tb.IsFatal)
482 td.Cmp(t, tb.Messages, []string{
483 "++++ Test1Bool",
484 "++++ Test1BoolError",
485 "++++ Test1Error",
486 "++++ Test1Z",
487 //
488 "++++ Test2Bool",
489 "++++ Test2BoolError",
490 "Test2BoolError pre-test error: PreTest error",
491 "++++ Test2Error",
492 "++++ Test2Z",
493 })
494 })
495 })
496
497 t.Run("PostTest", func(t *testing.T) {
498 t.Run("1 param", func(t *testing.T) {
499 suite := Error{postTest: 3}
500 tb := test.NewTestingTB("TestError")
501 td.CmpFalse(t, tdsuite.Run(tb, &suite))
502 td.CmpFalse(t, tb.IsFatal)
503 td.Cmp(t, tb.Messages, []string{
504 "++++ Test1Bool",
505 "++++ Test1BoolError",
506 "++++ Test1Error",
507 "Test1Error post-test error: PostTest error",
508 "++++ Test1Z",
509 //
510 "++++ Test2Bool",
511 "++++ Test2BoolError",
512 "++++ Test2Error",
513 "++++ Test2Z",
514 })
515 })
516
517 t.Run("2 params", func(t *testing.T) {
518 suite := Error{postTest: 7}
519 tb := test.NewTestingTB("TestError")
520 td.CmpFalse(t, tdsuite.Run(tb, &suite))
521 td.CmpFalse(t, tb.IsFatal)
522 td.Cmp(t, tb.Messages, []string{
523 "++++ Test1Bool",
524 "++++ Test1BoolError",
525 "++++ Test1Error",
526 "++++ Test1Z",
527 //
528 "++++ Test2Bool",
529 "++++ Test2BoolError",
530 "++++ Test2Error",
531 "Test2Error post-test error: PostTest error",
532 "++++ Test2Z",
533 })
534 })
535 })
536
537 t.Run("BetweenTests", func(t *testing.T) {
538 suite := Error{betweenTests: true}
539 tb := test.NewTestingTB("TestError")
540 td.CmpFalse(t, tdsuite.Run(tb, &suite))
541 td.CmpFalse(t, tb.IsFatal)
542 td.Cmp(t, tb.Messages, []string{
543 "++++ Test1Bool",
544 "Test1Bool / Test1BoolError between-tests error: BetweenTests error",
545 })
546 })
547
548 t.Run("InvalidHooks", func(t *testing.T) {
549 tb := test.NewTestingTB("TestError")
550 td.CmpFalse(t, tdsuite.Run(tb, &FullBrokenHooks{}))
551 td.CmpFalse(t, tb.IsFatal)
552 name := "*tdsuite_test.FullBrokenHooks"
553 td.Cmp(t, tb.Messages, []string{
554 name + " suite has a Setup method but it does not match Setup(t *td.T) error",
555 name + " suite has a Destroy method but it does not match Destroy(t *td.T) error",
556 name + " suite has a PreTest method but it does not match PreTest(t *td.T, testName string) error",
557 name + " suite has a PostTest method but it does not match PostTest(t *td.T, testName string) error",
558 name + " suite has a BetweenTests method but it does not match BetweenTests(t *td.T, previousTestName, nextTestName string) error",
559 "++++ Test1",
560 })
561 })
562
563 t.Run("Stop_after_TestBool", func(t *testing.T) {
564 t.Run("1 param", func(t *testing.T) {
565 suite := Error{testBool: [2]bool{true, false}}
566 tb := test.NewTestingTB("TestError")
567 td.CmpTrue(t, tdsuite.Run(tb, &suite)) // returning false is not an error
568 td.CmpFalse(t, tb.IsFatal)
569 td.Cmp(t, tb.Messages, []string{
570 "++++ Test1Bool",
571 "Test1Bool required discontinuing suite tests",
572 })
573 })
574
575 t.Run("2 params", func(t *testing.T) {
576 suite := Error{testBool: [2]bool{false, true}}
577 tb := test.NewTestingTB("TestError")
578 td.CmpTrue(t, tdsuite.Run(tb, &suite)) // returning false is not an error
579 td.CmpFalse(t, tb.IsFatal)
580 td.Cmp(t, tb.Messages, []string{
581 "++++ Test1Bool",
582 "++++ Test1BoolError",
583 "++++ Test1Error",
584 "++++ Test1Z",
585 //
586 "++++ Test2Bool",
587 "Test2Bool required discontinuing suite tests",
588 })
589 })
590 })
591
592 t.Run("TestBoolError", func(t *testing.T) {
593 t.Run("Stop after", func(t *testing.T) {
594 t.Run("1 param", func(t *testing.T) {
595 suite := Error{testBoolErrorBool: [2]bool{true, false}}
596 tb := test.NewTestingTB("TestError")
597 td.CmpTrue(t, tdsuite.Run(tb, &suite)) // returning false is not an error
598 td.CmpFalse(t, tb.IsFatal)
599 td.Cmp(t, tb.Messages, []string{
600 "++++ Test1Bool",
601 "++++ Test1BoolError",
602 "Test1BoolError required discontinuing suite tests",
603 })
604 })
605
606 t.Run("2 params", func(t *testing.T) {
607 suite := Error{testBoolErrorBool: [2]bool{false, true}}
608 tb := test.NewTestingTB("TestError")
609 td.CmpTrue(t, tdsuite.Run(tb, &suite)) // returning false is not an error
610 td.CmpFalse(t, tb.IsFatal)
611 td.Cmp(t, tb.Messages, []string{
612 "++++ Test1Bool",
613 "++++ Test1BoolError",
614 "++++ Test1Error",
615 "++++ Test1Z",
616 //
617 "++++ Test2Bool",
618 "++++ Test2BoolError",
619 "Test2BoolError required discontinuing suite tests",
620 })
621 })
622 })
623
624 t.Run("Error but continue", func(t *testing.T) {
625 t.Run("1 param", func(t *testing.T) {
626 suite := Error{testBoolErrorErr: [2]bool{true, false}}
627 tb := test.NewTestingTB("TestError")
628 td.CmpFalse(t, tdsuite.Run(tb, &suite))
629 td.CmpFalse(t, tb.IsFatal)
630 td.Cmp(t, tb.Messages, []string{
631 "++++ Test1Bool",
632 "++++ Test1BoolError",
633 "Test1BoolError error: Test1BoolError error",
634 "++++ Test1Error",
635 "++++ Test1Z",
636 //
637 "++++ Test2Bool",
638 "++++ Test2BoolError",
639 "++++ Test2Error",
640 "++++ Test2Z",
641 })
642 })
643
644 t.Run("2 params", func(t *testing.T) {
645 suite := Error{testBoolErrorErr: [2]bool{false, true}}
646 tb := test.NewTestingTB("TestError")
647 td.CmpFalse(t, tdsuite.Run(tb, &suite))
648 td.CmpFalse(t, tb.IsFatal)
649 td.Cmp(t, tb.Messages, []string{
650 "++++ Test1Bool",
651 "++++ Test1BoolError",
652 "++++ Test1Error",
653 "++++ Test1Z",
654 //
655 "++++ Test2Bool",
656 "++++ Test2BoolError",
657 "Test2BoolError error: Test2BoolError error",
658 "++++ Test2Error",
659 "++++ Test2Z",
660 })
661 })
662 })
663
664 t.Run("Error and stop after", func(t *testing.T) {
665 t.Run("1 param", func(t *testing.T) {
666 suite := Error{
667 testBoolErrorBool: [2]bool{true, false},
668 testBoolErrorErr: [2]bool{true, false},
669 }
670 tb := test.NewTestingTB("TestError")
671 td.CmpFalse(t, tdsuite.Run(tb, &suite))
672 td.CmpFalse(t, tb.IsFatal)
673 td.Cmp(t, tb.Messages, []string{
674 "++++ Test1Bool",
675 "++++ Test1BoolError",
676 "Test1BoolError error: Test1BoolError error",
677 "Test1BoolError required discontinuing suite tests",
678 })
679 })
680
681 t.Run("2 params", func(t *testing.T) {
682 suite := Error{
683 testBoolErrorBool: [2]bool{false, true},
684 testBoolErrorErr: [2]bool{false, true},
685 }
686 tb := test.NewTestingTB("TestError")
687 td.CmpFalse(t, tdsuite.Run(tb, &suite))
688 td.CmpFalse(t, tb.IsFatal)
689 td.Cmp(t, tb.Messages, []string{
690 "++++ Test1Bool",
691 "++++ Test1BoolError",
692 "++++ Test1Error",
693 "++++ Test1Z",
694 //
695 "++++ Test2Bool",
696 "++++ Test2BoolError",
697 "Test2BoolError error: Test2BoolError error",
698 "Test2BoolError required discontinuing suite tests",
699 })
700 })
701 })
702 })
703
704 t.Run("Error_for_TestError", func(t *testing.T) {
705 t.Run("1 param", func(t *testing.T) {
706 suite := Error{testError: [2]bool{true, false}}
707 tb := test.NewTestingTB("TestError")
708 td.CmpFalse(t, tdsuite.Run(tb, &suite))
709 td.CmpFalse(t, tb.IsFatal)
710 td.Cmp(t, tb.Messages, []string{
711 "++++ Test1Bool",
712 "++++ Test1BoolError",
713 "++++ Test1Error",
714 "Test1Error error: Test1Error error",
715 "Test1Error required discontinuing suite tests",
716 })
717 })
718
719 t.Run("2 params", func(t *testing.T) {
720 suite := Error{testError: [2]bool{false, true}}
721 tb := test.NewTestingTB("TestError")
722 td.CmpFalse(t, tdsuite.Run(tb, &suite))
723 td.CmpFalse(t, tb.IsFatal)
724 td.Cmp(t, tb.Messages, []string{
725 "++++ Test1Bool",
726 "++++ Test1BoolError",
727 "++++ Test1Error",
728 "++++ Test1Z",
729 //
730 "++++ Test2Bool",
731 "++++ Test2BoolError",
732 "++++ Test2Error",
733 "Test2Error error: Test2Error error",
734 "Test2Error required discontinuing suite tests",
735 })
736 })
737 })
738 }
739
740 // FullCleanup has tests and all possible hooks.
741 type FullCleanup struct{ base }
742
743 func (f *FullCleanup) Setup(t *td.T) error { f.rec(); return nil }
744 func (f *FullCleanup) PreTest(t *td.T, tn string) error {
745 f.rec(tn)
746 t.Cleanup(func() { f.rec(tn) })
747 return nil
748 }
749
750 func (f *FullCleanup) PostTest(t *td.T, tn string) error {
751 f.rec(tn)
752 t.Cleanup(func() { f.rec(tn) })
753 return nil
754 }
755
756 func (f *FullCleanup) BetweenTests(t *td.T, prev, next string) error {
757 f.rec(prev, next)
758 return nil
759 }
760 func (f *FullCleanup) Destroy(t *td.T) error { f.rec(); return nil }
761
762 func (f *FullCleanup) Test1(t *td.T) {
763 f.rec()
764 t.Cleanup(func() { f.rec() })
765 }
766
767 func (f *FullCleanup) Test2(assert *td.T, require *td.T) {
768 f.rec()
769 assert.Cleanup(func() { f.rec() })
770 }
771
772 func (f *FullCleanup) Test3(t *td.T) {
773 f.rec()
774 t.Cleanup(func() { f.rec() })
775 }
776 func (f *FullCleanup) Testimony(t *td.T) {} // not a test method
777
778 var (
779 _ tdsuite.Setup = (*FullCleanup)(nil)
780 _ tdsuite.PreTest = (*FullCleanup)(nil)
781 _ tdsuite.PostTest = (*FullCleanup)(nil)
782 _ tdsuite.BetweenTests = (*FullCleanup)(nil)
783 _ tdsuite.Destroy = (*FullCleanup)(nil)
784 )
785
786 func TestRunCleanup(t *testing.T) {
787 t.Run("Full", func(t *testing.T) {
788 suite := FullCleanup{}
789 td.CmpTrue(t, tdsuite.Run(t, &suite))
790 ok := td.Cmp(t, suite.calls, []string{
791 "Setup",
792 /**/ "PreTest+Test1",
793 /**/ "Test1",
794 /**/ "PostTest+Test1",
795 /**/ "PostTest.Cleanup+Test1",
796 /**/ "Test1.Cleanup",
797 /**/ "PreTest.Cleanup+Test1",
798 "BetweenTests+Test1+Test2",
799 /**/ "PreTest+Test2",
800 /**/ "Test2",
801 /**/ "PostTest+Test2",
802 /**/ "PostTest.Cleanup+Test2",
803 /**/ "Test2.Cleanup",
804 /**/ "PreTest.Cleanup+Test2",
805 "BetweenTests+Test2+Test3",
806 /**/ "PreTest+Test3",
807 /**/ "Test3",
808 /**/ "PostTest+Test3",
809 /**/ "PostTest.Cleanup+Test3",
810 /**/ "Test3.Cleanup",
811 /**/ "PreTest.Cleanup+Test3",
812 "Destroy",
813 })
814 if !ok {
815 for _, c := range suite.calls {
816 switch c[0] {
817 case 'S', 'B', 'D':
818 t.Log(c)
819 default:
820 t.Log(" ", c)
821 }
822 }
823 }
824 })
825 }