-
Notifications
You must be signed in to change notification settings - Fork 10
/
T_test.go
850 lines (732 loc) · 21.3 KB
/
T_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
package testcase_test
import (
"context"
"fmt"
"math/rand"
"os"
"sync/atomic"
"testing"
"time"
"go.llib.dev/testcase/assert"
"go.llib.dev/testcase/contracts"
"go.llib.dev/testcase/internal/doubles"
"go.llib.dev/testcase/internal/environ"
"go.llib.dev/testcase/sandbox"
"go.llib.dev/testcase/random"
"go.llib.dev/testcase"
)
var _ testing.TB = &testcase.T{}
func TestT_implementsTestingTB(t *testing.T) {
testcase.RunSuite(t, contracts.TestingTB{
Subject: func(t *testcase.T) testing.TB {
stub := &doubles.TB{}
t.Cleanup(stub.Finish)
return testcase.NewTWithSpec(stub, nil)
},
})
}
func TestVar_Set_canBeUsedDuringTest(t *testing.T) {
s := testcase.NewSpec(t)
s.Context(`runtime define`, func(s *testcase.Spec) {
nog := testcase.Let(s, func(t *testcase.T) int { return rand.Intn(42) })
mog := testcase.Let(s, func(t *testcase.T) int { return rand.Intn(42) + 100 })
var exampleMultiReturnFunc = func(t *testcase.T) (int, int) {
return nog.Get(t), mog.Get(t)
}
s.Context(`Let being set during testCase runtime`, func(s *testcase.Spec) {
n := testcase.Var[int]{ID: "n"}
m := testcase.Var[int]{ID: "m"}
s.Before(func(t *testcase.T) {
nv, mv := exampleMultiReturnFunc(t)
n.Set(t, nv)
m.Set(t, mv)
})
s.Test(`let values which are defined during runtime present in the testCase`, func(t *testcase.T) {
t.Must.Equal(n.Get(t), nog.Get(t))
t.Must.Equal(m.Get(t), mog.Get(t))
})
})
})
s.Context(`runtime update`, func(s *testcase.Spec) {
var initValue = rand.Intn(42)
x := testcase.Let(s, func(t *testcase.T) int { return initValue })
s.Before(func(t *testcase.T) {
x.Set(t, x.Get(t)+1)
})
s.Before(func(t *testcase.T) {
x.Set(t, x.Get(t)+1)
})
s.Test(`let will returns the value then override the runtime vars`, func(t *testcase.T) {
t.Must.Equal(initValue+2, x.Get(t))
})
})
}
func TestT_Defer(t *testing.T) {
var res []int
t.Run(``, func(t *testing.T) {
s := testcase.NewSpec(t)
s.Context(``, func(s *testcase.Spec) {
s.Before(func(t *testcase.T) {
res = append(res, 0)
})
s.After(func(t *testcase.T) {
res = append(res, -1)
})
s.Context(``, func(s *testcase.Spec) {
s.Around(func(t *testcase.T) func() {
res = append(res, 1)
return func() { res = append(res, -2) }
})
s.Context(``, func(s *testcase.Spec) {
wDefer := testcase.Let(s, func(t *testcase.T) int {
t.Defer(func() { res = append(res, -3) })
return 42
})
s.Before(func(t *testcase.T) {
// calling a variable that has defer will ensure
// that the deferred function call will be executed
// as part of the *T#defer stack, and not afterwards
t.Must.Equal(42, wDefer.Get(t))
})
s.Test(``, func(t *testcase.T) {
t.Defer(func() { res = append(res, -4) })
})
})
})
})
})
assert.Must(t).Equal([]int{0, 1, -4, -3, -2, -1}, res)
}
// TB#Cleanup https://github.com/golang/go/issues/41355
//
//goland:noinspection GoDeferGo
func TestT_Defer_failNowWillNotHang(t *testing.T) {
assert.Within(t, time.Second, func(ctx context.Context) {
sandbox.Run(func() {
s := testcase.NewSpec(&doubles.TB{})
s.Test(``, func(t *testcase.T) {
t.Defer(func() { t.FailNow() })
panic(`die`)
})
})
})
}
func TestT_Defer_whenItIsCalledDuringTestBlock(t *testing.T) {
var itRan bool
t.Run(``, func(t *testing.T) {
s := testcase.NewSpec(t)
s.Test(``, func(t *testcase.T) { t.Defer(func() { itRan = true }) })
})
assert.Must(t).True(itRan, `then it is expected to ran`)
}
func TestT_Defer_withArguments(t *testing.T) {
var (
expected = rand.Int() + 1
actually int
)
t.Run(``, func(t *testing.T) {
s := testcase.NewSpec(t)
type S struct{ ID int }
v := testcase.Let(s, func(t *testcase.T) *S {
s := &S{ID: expected}
t.Defer(func(id int) { actually = id }, s.ID)
return s
})
s.Test(`testCase that alter the content of value`, func(t *testcase.T) {
v.Get(t).ID = 0
})
s.Test(`interface type with concrete input must be allowed`, func(t *testcase.T) {
var fn = func(ctx context.Context) {}
t.Defer(fn, context.Background())
})
})
assert.Must(t).Equal(expected, actually)
}
func TestT_Defer_runsOnlyAfterTestIsdone(t *testing.T) {
s := testcase.NewSpec(t)
CTX := testcase.Let(s, func(t *testcase.T) func() context.Context {
return func() context.Context {
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
t.Defer(cancel)
return ctx
}
})
s.Before(func(t *testcase.T) {
t.Cleanup(func() { t.Must.NoError(CTX.Get(t)().Err()) })
t.Defer(func() { t.Must.NoError(CTX.Get(t)().Err()) })
})
s.Test("", func(t *testcase.T) {})
}
func TestT_Defer_withArgumentsButArgumentCountMismatch(t *testing.T) {
s := testcase.NewSpec(t)
var getPanicMessage = func(fn func()) (r string) {
defer func() { r, _ = recover().(string) }()
fn()
return
}
v := testcase.Let(s, func(t *testcase.T) int {
t.Defer(func(text string) {}, `this would be ok`, `but this extra argument is not ok`)
return 42
})
s.Test(`testCase that it will panics early on to help ease the pain of seeing mistakes`, func(t *testcase.T) {
t.Must.Panic(func() { _ = v.Get(t) })
})
s.Test(`panic message`, func(t *testcase.T) {
message := getPanicMessage(func() { _ = v.Get(t) })
t.Must.Contain(message, `/testcase/T_test.go`)
t.Must.Contain(message, `expected 1`)
t.Must.Contain(message, `got 2`)
})
s.Test(`interface type with wrong implementation`, func(t *testcase.T) {
type notContextForSure struct{}
var fn = func(ctx context.Context) {}
t.Must.Panic(func() { t.Defer(fn, notContextForSure{}) })
message := getPanicMessage(func() { t.Defer(fn, notContextForSure{}) })
t.Must.Contain(message, `/testcase/T_test.go`)
t.Must.Contain(message, `doesn't implements context.Context`)
t.Must.Contain(message, `argument[0]`)
})
}
func TestT_Defer_withArgumentsButArgumentTypeMismatch(t *testing.T) {
s := testcase.NewSpec(t)
v := testcase.Let(s, func(t *testcase.T) int {
t.Defer(func(n int) {}, `this is not ok`)
return 42
})
s.Test(`testCase that it will panics early on to help ease the pain of seeing mistakes`, func(t *testcase.T) {
t.Must.Panic(func() { _ = v.Get(t) })
})
s.Test(`panic message`, func(t *testcase.T) {
message := func() (r string) {
defer func() { r = recover().(string) }()
_ = v.Get(t)
return ``
}()
t.Must.Contain(message, `/testcase/T_test.go`)
t.Must.Contain(message, `expected int`)
t.Must.Contain(message, `got string`)
})
}
func TestT_TB(t *testing.T) {
s := testcase.NewSpec(t)
for i := 0; i < 10; i++ {
var ts []testing.TB
s.Test(`*testcase.TB is set to the given testcase's *testing.T`, func(t *testcase.T) {
t.Must.NotNil(t.TB)
t.Must.NotContain(ts, t.TB, `TB should be unique for each testCase run`)
ts = append(ts, t.TB)
})
}
}
func TestT_Defer_calledWithoutFunctionAndWillPanic(t *testing.T) {
s := testcase.NewSpec(t)
s.Test(`defer expected to panic for non function input as first value`, func(t *testcase.T) {
var withReturnValue = func() int { return 42 }
t.Must.Panic(func() { t.Defer(withReturnValue()) })
})
s.Test(`defer expected to panic for invalid inputs`, func(t *testcase.T) {
var dummyClose = func() error { return nil }
pv := t.Must.Panic(func() { t.Defer(dummyClose()) })
t.Must.Contain(pv, `T#Defer can only take functions`)
})
}
func TestT_Defer_willRunEvenIfSomethingForceTheTestToStopEarly(t *testing.T) {
var ran bool
t.Run(``, func(t *testing.T) {
s := testcase.NewSpec(t)
s.Before(func(t *testcase.T) { t.Defer(func() { ran = true }) })
s.Test(``, func(t *testcase.T) { t.Skip(`please stop early`) })
})
assert.Must(t).True(ran)
}
func TestT_HasTag(t *testing.T) {
s := testcase.NewSpec(t)
s.Context(`a`, func(s *testcase.Spec) {
s.Tag(`a`)
s.Context(`b`, func(s *testcase.Spec) {
s.Tag(`b`)
s.Context(`c`, func(s *testcase.Spec) {
s.Tag(`c`)
s.Test(`c`, func(t *testcase.T) {
t.Must.True(t.HasTag(`a`))
t.Must.True(t.HasTag(`b`))
t.Must.True(t.HasTag(`c`))
t.Must.True(!t.HasTag(`d`))
})
})
s.Test(`b`, func(t *testcase.T) {
t.Must.True(t.HasTag(`a`))
t.Must.True(t.HasTag(`b`))
t.Must.True(!t.HasTag(`c`))
t.Must.True(!t.HasTag(`d`))
})
})
s.Test(`a`, func(t *testcase.T) {
t.Must.True(t.HasTag(`a`))
t.Must.True(!t.HasTag(`b`))
t.Must.True(!t.HasTag(`c`))
t.Must.True(!t.HasTag(`d`))
})
})
s.Test(``, func(t *testcase.T) {
t.Must.True(!t.HasTag(`a`))
t.Must.True(!t.HasTag(`b`))
t.Must.True(!t.HasTag(`c`))
t.Must.True(!t.HasTag(`d`))
})
}
func TestT_Random(t *testing.T) {
randomGenerationWorks := func(t *testcase.T) {
assert.Retry{Strategy: assert.Waiter{WaitDuration: time.Second}}.Assert(t, func(it assert.It) {
it.Must.True(0 < t.Random.Int())
})
}
t.Run(`when environment value is set`, func(t *testing.T) {
testcase.SetEnv(t, environ.KeySeed, `42`)
s := testcase.NewSpec(t)
s.Test(``, func(t *testcase.T) {
t.Must.NotEmpty(t.Random)
randomGenerationWorks(t)
})
})
s := testcase.NewSpec(t)
s.Test(``, func(t *testcase.T) {
randomGenerationWorks(t)
})
}
func TestT_Eventually(t *testing.T) {
rnd := random.New(random.CryptoSeed{})
t.Run(`with default eventually retry strategy`, func(t *testing.T) {
stub := &doubles.TB{}
s := testcase.NewSpec(stub)
s.HasSideEffect()
var eventuallyRan bool
s.Test(``, func(t *testcase.T) {
t.Eventually(func(it *testcase.T) {
eventuallyRan = true
it.Must.True(t.Random.Bool())
}) // eventually pass
})
stub.Finish()
s.Finish()
assert.Must(t).True(!stub.IsFailed, `expected to pass`)
assert.Must(t).True(eventuallyRan)
})
t.Run(`with config passed`, func(t *testing.T) {
stub := &doubles.TB{}
var strategyUsed bool
strategy := assert.RetryStrategyFunc(func(condition func() bool) {
strategyUsed = true
for condition() {
}
})
s := testcase.NewSpec(stub, testcase.WithRetryStrategy(strategy))
s.HasSideEffect()
s.Test(``, func(t *testcase.T) {
t.Eventually(func(it *testcase.T) {
it.Must.True(t.Random.Bool())
}) // eventually pass
})
stub.Finish()
s.Finish()
assert.Must(t).True(!stub.IsFailed, `expected to pass`)
assert.Must(t).True(strategyUsed, `retry strategy of the eventually call was used`)
})
t.Run("Eventually uses a testcase.T that allows its functionalities all from the the eventually block", func(t *testing.T) {
// After extensive testing, we discovered that constantly switching between using `assert.It` and `testcase.T` is risky
// and prone to errors without adding any benefit.
// Therefore, our next step is to streamline the API to simplify testing.
stub := &doubles.TB{}
s := testcase.NewSpec(stub)
s.HasSideEffect()
expTag := rnd.StringNC(5, random.CharsetAlpha())
s.Tag(expTag)
expVal := rnd.Int()
v := testcase.LetValue(s, expVal)
var ran bool
s.Test(``, func(tcT *testcase.T) {
tcT.Eventually(func(it *testcase.T) {
ran = true
assert.Equal(t, expVal, v.Get(it))
assert.Equal(t, tcT.Random, it.Random)
assert.True(t, tcT.HasTag(expTag))
assert.True(t, tcT.TB != it.TB)
})
})
stub.Finish()
s.Finish()
assert.Must(t).True(!stub.IsFailed, `expected to pass`)
assert.True(t, ran)
})
t.Run("smoke", func(t *testing.T) {
stub := &doubles.TB{}
s := testcase.NewSpec(stub)
s.HasSideEffect()
var ran bool
s.Test(``, func(tcT *testcase.T) {
var failed bool
tcT.Eventually(func(t *testcase.T) {
if !failed {
failed = true
t.FailNow()
}
// OK
ran = true
})
assert.False(t, tcT.Failed())
})
stub.Finish()
s.Finish()
assert.Must(t).True(!stub.IsFailed, `expected to pass`)
assert.True(t, ran)
})
t.Run("when failure occurs during the variable initialisation", func(t *testing.T) {
t.Run("permanently", func(t *testing.T) {
stub := &doubles.TB{}
s := testcase.NewSpec(stub, testcase.WithRetryStrategy(assert.RetryCount(3)))
s.HasSideEffect()
v := testcase.Let[int](s, func(t *testcase.T) int {
t.FailNow() // boom
return 42
})
s.Test(``, func(tcT *testcase.T) {
tcT.Eventually(func(it *testcase.T) { v.Get(it) })
})
stub.Finish()
s.Finish()
assert.Must(t).True(stub.IsFailed, `expected to fail`)
})
t.Run("temporarily", func(t *testing.T) {
stub := &doubles.TB{}
s := testcase.NewSpec(stub)
s.HasSideEffect()
failed := testcase.LetValue[bool](s, false)
counter := testcase.LetValue[int](s, 0)
expVal := rnd.Int()
v := testcase.Let[int](s, func(t *testcase.T) int {
counter.Set(t, counter.Get(t)+1)
if !failed.Get(t) {
failed.Set(t, true)
t.FailNow() // boom
}
return expVal
})
s.Test(``, func(tcT *testcase.T) {
tcT.Eventually(func(it *testcase.T) {
assert.Equal(t, v.Get(it), expVal)
})
assert.Equal(t, v.Get(tcT), expVal)
assert.Equal(t, counter.Get(tcT), 2, "it was expected that the variable init block only run twice, one for failure and one for success")
})
stub.Finish()
s.Finish()
assert.Must(t).False(stub.IsFailed, `expected to pass`)
})
})
}
func ExampleNewTWithSpec() {
s := testcase.NewSpec(nil)
// some spec specific configuration
s.Before(func(t *testcase.T) {})
var tb testing.TB // placeholder
tc := testcase.NewTWithSpec(tb, s)
_ = tc
}
func TestNewTWithSpec(t *testing.T) {
rnd := random.New(random.CryptoSeed{})
y := testcase.Var[int]{ID: "Y"}
v := testcase.Var[int]{
ID: "the answer",
Init: func(t *testcase.T) int { return t.Random.Int() },
}
t.Run(`with *Spec`, func(t *testing.T) {
tb := &doubles.TB{}
t.Cleanup(tb.Finish)
s := testcase.NewSpec(tb)
expectedY := rnd.Int()
y.LetValue(s, expectedY)
subject := testcase.NewTWithSpec(tb, s)
assert.Must(t).Equal(expectedY, y.Get(subject), "use the passed spec's runtime context after set-up")
assert.Must(t).Equal(v.Get(subject), v.Get(subject), `has test variable cache`)
})
t.Run(`without *Spec`, func(t *testing.T) {
tb := &doubles.TB{}
t.Cleanup(tb.Finish)
expectedY := rnd.Int()
subject := testcase.NewTWithSpec(tb, nil)
y.Set(subject, expectedY)
assert.Must(t).Equal(expectedY, y.Get(subject))
assert.Must(t).Equal(v.Get(subject), v.Get(subject), `has test variable cache`)
})
t.Run(`with *testcase.T, same returned`, func(t *testing.T) {
tb := &doubles.TB{}
t.Cleanup(tb.Finish)
tcT1 := testcase.NewTWithSpec(tb, nil)
tcT2 := testcase.NewTWithSpec(tcT1, nil)
assert.Must(t).Equal(tcT1, tcT2)
})
t.Run(`when nil received, nil is returned`, func(t *testing.T) {
assert.Must(t).Nil(testcase.NewTWithSpec(nil, nil))
})
t.Run(`when NewT is retrieved multiple times, hooks executed only once`, func(t *testing.T) {
stb := &doubles.TB{}
s := testcase.NewSpec(stb)
var out []struct{}
s.Before(func(t *testcase.T) {
out = append(out, struct{}{})
})
tct := testcase.NewTWithSpec(stb, s)
tct = testcase.NewTWithSpec(tct, s)
tct = testcase.NewTWithSpec(tct, s)
stb.Finish()
assert.Equal(t, 1, len(out))
})
}
func ExampleNewT() {
var tb testing.TB // placeholder
_ = testcase.NewT(tb)
}
func TestNewT(t *testing.T) {
rnd := random.New(random.CryptoSeed{})
y := testcase.Var[int]{ID: "Y"}
v := testcase.Var[int]{
ID: "the answer",
Init: func(t *testcase.T) int { return t.Random.Int() },
}
t.Run(`smoke`, func(t *testing.T) {
tb := &doubles.TB{}
t.Cleanup(tb.Finish)
expectedY := rnd.Int()
subject := testcase.NewT(tb)
y.Set(subject, expectedY)
assert.Must(t).Equal(expectedY, y.Get(subject))
assert.Must(t).Equal(v.Get(subject), v.Get(subject), `has test variable cache`)
})
t.Run(`with *testcase.T, same returned`, func(t *testing.T) {
tb := &doubles.TB{}
t.Cleanup(tb.Finish)
tcT1 := testcase.NewT(tb)
tcT2 := testcase.NewT(tcT1)
assert.Must(t).Equal(tcT1, tcT2)
})
t.Run(`when nil received, nil is returned`, func(t *testing.T) {
assert.Must(t).Nil(testcase.NewT(nil))
})
}
func BenchmarkT_varDoesNotCountTowardsRun(b *testing.B) {
s := testcase.NewSpec(b)
ab := testcase.Let(s, func(t *testcase.T) int {
time.Sleep(time.Second / 2)
return t.Random.Int()
})
bv := testcase.Let(s, func(t *testcase.T) int {
_ = ab.Get(t)
time.Sleep(time.Second / 2)
return t.Random.Int()
})
s.Test(`run`, func(t *testcase.T) {
// if the benchmark subject is too fast
// the benchmark goes into a really long measuring loop.
//
// expected to perform max around ~ 1001000000 ns/op
time.Sleep(time.Second)
_ = bv.Get(t)
})
}
func TestT_SkipUntil(t *testing.T) {
const timeLayout = "2006-01-02"
const skipUntilFormat = "Skip time %s"
const skipExpiredFormat = "[SkipUntil] expired on %s"
rnd := random.New(rand.NewSource(time.Now().UnixNano()))
future := time.Now().AddDate(0, 0, 1)
t.Run("before SkipUntil deadline, test is skipped", func(t *testing.T) {
stubTB := &doubles.TB{}
s := testcase.NewSpec(stubTB)
var ran bool
s.Test("", func(t *testcase.T) {
t.SkipUntil(future.Year(), future.Month(), future.Day(), future.Hour())
ran = true
})
sandbox.Run(func() { s.Finish() })
assert.Must(t).False(ran)
assert.Must(t).False(stubTB.LastTB().IsFailed)
assert.Must(t).True(stubTB.LastTB().IsSkipped)
assert.Must(t).Contain(stubTB.LastTB().Logs.String(), fmt.Sprintf(skipUntilFormat, future.Format(timeLayout)))
})
t.Run("at or after SkipUntil deadline, test is failed", func(t *testing.T) {
stubTB := &doubles.TB{}
s := testcase.NewSpec(stubTB)
today := time.Now().AddDate(0, 0, -1*rnd.IntN(3))
var ran bool
s.Test("", func(t *testcase.T) {
t.SkipUntil(today.Year(), today.Month(), today.Day(), today.Hour())
ran = true
})
sandbox.Run(func() { s.Finish() })
assert.Must(t).True(ran)
assert.Must(t).False(stubTB.LastTB().IsFailed)
assert.Must(t).Contain(stubTB.LastTB().Logs.String(), fmt.Sprintf(skipExpiredFormat, today.Format(timeLayout)))
})
}
func TestT_UnsetEnv(t *testing.T) {
const key = "TEST_KEY"
t.Setenv(key, "this")
s := testcase.NewSpec(t)
s.HasSideEffect()
s.Test("on unset", func(t *testcase.T) {
t.UnsetEnv(key)
_, ok := os.LookupEnv(key)
t.Must.False(ok)
})
s.Test("when not used", func(t *testcase.T) {
_, ok := os.LookupEnv(key)
t.Must.True(ok)
})
s.Finish()
t.Run("on Parallel test", func(t *testing.T) {
dtb := &doubles.TB{}
sandbox.Run(func() {
defer dtb.Finish()
defer s.Finish()
s := testcase.NewSpec(dtb)
s.Parallel()
s.Test("on unset it will fail", func(t *testcase.T) {
t.UnsetEnv(key)
})
})
assert.True(t, dtb.IsFailed)
})
}
func TestT_SetEnv(t *testing.T) {
const key = "TEST_KEY"
defaultValue := "this"
t.Setenv(key, defaultValue)
s := testcase.NewSpec(t)
s.HasSideEffect()
s.Test("on set", func(t *testcase.T) {
r := t.Random.StringNC(5, random.CharsetAlpha())
t.SetEnv(key, r)
val, ok := os.LookupEnv(key)
t.Must.True(ok)
t.Must.Equal(r, val)
})
s.Test("on not used", func(t *testcase.T) {
val, ok := os.LookupEnv(key)
t.Must.True(ok)
t.Must.Equal(defaultValue, val)
})
s.Finish()
}
func TestT_Setenv(t *testing.T) {
const key = "TEST_KEY"
defaultValue := "this"
t.Setenv(key, defaultValue)
s := testcase.NewSpec(t)
s.HasSideEffect()
s.Test("on set", func(t *testcase.T) {
r := t.Random.StringNC(5, random.CharsetAlpha())
t.Setenv(key, r)
val, ok := os.LookupEnv(key)
t.Must.True(ok)
t.Must.Equal(r, val)
})
s.Test("on not used", func(t *testcase.T) {
val, ok := os.LookupEnv(key)
t.Must.True(ok)
t.Must.Equal(defaultValue, val)
})
s.Finish()
}
func TestT_LogPretty(t *testing.T) {
dtb := &doubles.TB{}
tct := testcase.ToT(dtb)
tct.LogPretty([]int{1, 2, 4})
type X struct{ Foo string }
tct.LogPretty(X{Foo: "hello"})
dtb.Finish()
assert.Contain(t, dtb.Logs.String(), "[]int{\n\t1,\n\t2,\n\t4,\n}")
assert.Contain(t, dtb.Logs.String(), "testcase_test.X{\n\tFoo: \"hello\",\n}")
}
func ExampleT_Done() {
s := testcase.NewSpec(nil)
s.Test("", func(t *testcase.T) {
go func() {
select {
// case do something for the test
case <-t.Done():
return // test is over, time to garbage collect
}
}()
})
}
func TestT_Done(t *testing.T) {
s := testcase.NewSpec(t)
var isdone = func(t *testcase.T) bool {
select {
case <-t.Done():
return true
default:
return false
}
}
var done int32
s.Test("", func(t *testcase.T) {
assert.False(t, isdone(t))
go func() {
<-t.Done() // after the test is done
atomic.AddInt32(&done, 1)
}()
t.Cleanup(func() {
assert.False(t, isdone(t),
"during cleanup the done should be not ready")
t.Cleanup(func() {
assert.False(t, isdone(t),
"during a cleanup of cleanup, done should not be ready")
})
})
})
s.Finish()
assert.Eventually(t, time.Second, func(t assert.It) {
assert.Equal(t, atomic.LoadInt32(&done), 1)
})
}
func TestT_OnFail(t *testing.T) {
t.Run("on success", func(t *testing.T) {
dtb := &doubles.TB{}
var done bool
s := testcase.NewSpec(dtb)
s.Test("", func(t *testcase.T) {
t.OnFail(func() { done = true })
})
s.Finish()
dtb.Finish()
assert.Equal(t, false, done)
})
t.Run("on failure", func(t *testing.T) {
dtb := &doubles.TB{}
var done bool
s := testcase.NewSpec(dtb)
s.Test("", func(t *testcase.T) {
t.OnFail(func() { done = true })
t.FailNow()
})
s.Finish()
dtb.Finish()
assert.Equal(t, true, done)
})
t.Run("race", func(t *testing.T) {
dtb := &doubles.TB{}
s := testcase.NewSpec(dtb)
s.Test("", func(t *testcase.T) {
testcase.Race(func() {
t.OnFail(func() {})
}, func() {
t.OnFail(func() {})
})
t.FailNow()
})
s.Finish()
dtb.Finish()
})
}