-
Notifications
You must be signed in to change notification settings - Fork 10
/
Spec_test.go
1789 lines (1486 loc) · 43.3 KB
/
Spec_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
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package testcase_test
import (
"math/rand"
"reflect"
"runtime"
"sort"
"strconv"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
"go.llib.dev/testcase/assert"
"go.llib.dev/testcase/internal/doubles"
"go.llib.dev/testcase/internal/environ"
"go.llib.dev/testcase/internal/spechelper"
"go.llib.dev/testcase/random"
"go.llib.dev/testcase/sandbox"
"go.llib.dev/testcase"
)
func TestSpec_DSL(t *testing.T) {
var sideEffect []string
var actualSE []string
nest1Value := rand.Int()
nest2Value := rand.Int()
nest3Value := rand.Int()
s := testcase.NewSpec(t)
s.Before(func(t *testcase.T) {
actualSE = make([]string, 0)
})
s.Describe(`nest-lvl-1`, func(s *testcase.Spec) {
v := testcase.Let(s, func(t *testcase.T) int { return nest1Value })
subject := func(t *testcase.T) int { return v.Get(t) }
s.When(`nest-lvl-2`, func(s *testcase.Spec) {
v.Let(s, func(t *testcase.T) int { return nest2Value })
s.After(func(t *testcase.T) {
sideEffect = append(sideEffect, "after1")
})
s.Before(func(t *testcase.T) {
actualSE = append(actualSE, `before1`)
sideEffect = append(sideEffect, `before1`)
})
s.Around(func(t *testcase.T) func() {
actualSE = append(actualSE, `around1-begin`)
sideEffect = append(sideEffect, `around1-begin`)
return func() {
sideEffect = append(sideEffect, `around1-end`)
}
})
s.And(`nest-lvl-3`, func(s *testcase.Spec) {
v.Let(s, func(t *testcase.T) int { return nest3Value })
s.After(func(t *testcase.T) {
sideEffect = append(sideEffect, "after2")
})
s.Before(func(t *testcase.T) {
actualSE = append(actualSE, `before2`)
sideEffect = append(sideEffect, `before2`)
})
s.Around(func(t *testcase.T) func() {
actualSE = append(actualSE, `around2-begin`)
sideEffect = append(sideEffect, `around2-begin`)
return func() {
sideEffect = append(sideEffect, `around2-end`)
}
})
s.Then(`lvl-3`, func(t *testcase.T) {
expectedSE := []string{`before1`, `around1-begin`, `before2`, `around2-begin`}
assert.Must(t).Equal(expectedSE, actualSE)
assert.Must(t).Equal(nest3Value, v.Get(t))
assert.Must(t).Equal(nest3Value, subject(t))
})
})
s.Then(`lvl-2`, func(t *testcase.T) {
expectedSE := []string{`before1`, `around1-begin`}
assert.Must(t).Equal(expectedSE, actualSE)
assert.Must(t).Equal(nest2Value, v.Get(t))
assert.Must(t).Equal(nest2Value, subject(t))
})
})
s.Then(`lvl-1`, func(t *testcase.T) {
expectedSE := []string{}
assert.Must(t).Equal(expectedSE, actualSE)
t.Must.Equal(nest1Value, v.Get(t))
t.Must.Equal(nest1Value, subject(t))
})
})
}
func TestSpec_subSpecIsExecuted(t *testing.T) {
var ran bool
t.Run(``, func(t *testing.T) {
s := testcase.NewSpec(t)
s.Describe(`nest-lvl-1`, func(s *testcase.Spec) {
s.When(`nest-lvl-2`, func(s *testcase.Spec) {
s.And(`nest-lvl-3`, func(s *testcase.Spec) {
s.Then(`lvl-3`, func(t *testcase.T) {
ran = true
})
})
})
})
})
assert.Must(t).True(ran)
}
func TestSpec_Context(t *testing.T) {
var allSideEffect [][]string
var sideEffect []string
v := testcase.Var[int]{ID: testcase.VarID(strconv.Itoa(rand.Int()))}
nest1Value := rand.Int()
nest2Value := rand.Int()
nest3Value := rand.Int()
t.Run(``, func(t *testing.T) {
s := testcase.NewSpec(t)
s.Around(func(t *testcase.T) func() {
sideEffect = make([]string, 0)
return func() { allSideEffect = append(allSideEffect, sideEffect) }
})
s.Context(`nest-lvl-1`, func(s *testcase.Spec) {
subject := func(t *testcase.T) int { return v.Get(t) }
v.Let(s, func(t *testcase.T) int { return nest1Value })
s.Context(`nest-lvl-2`, func(s *testcase.Spec) {
v.Let(s, func(t *testcase.T) int { return nest2Value })
s.After(func(t *testcase.T) {
sideEffect = append(sideEffect, "after1")
})
s.Before(func(t *testcase.T) {
sideEffect = append(sideEffect, `before1`)
})
s.Around(func(t *testcase.T) func() {
sideEffect = append(sideEffect, `around1-begin`)
return func() { sideEffect = append(sideEffect, `around1-end`) }
})
s.Context(`nest-lvl-3`, func(s *testcase.Spec) {
v.Let(s, func(t *testcase.T) int { return nest3Value })
s.After(func(t *testcase.T) {
sideEffect = append(sideEffect, "after2")
})
s.Before(func(t *testcase.T) {
sideEffect = append(sideEffect, `before2`)
})
s.Around(func(t *testcase.T) func() {
sideEffect = append(sideEffect, `around2-begin`)
return func() { sideEffect = append(sideEffect, `around2-end`) }
})
s.Test(`lvl-3`, func(t *testcase.T) {
t.Must.Equal([]string{`before1`, `around1-begin`, `before2`, `around2-begin`}, sideEffect)
t.Must.Equal(nest3Value, v.Get(t))
t.Must.Equal(nest3Value, subject(t))
})
})
s.Test(`lvl-2`, func(t *testcase.T) {
t.Must.Equal([]string{`before1`, `around1-begin`}, sideEffect)
t.Must.Equal(nest2Value, v.Get(t))
t.Must.Equal(nest2Value, subject(t))
})
})
s.Test(`lvl-1`, func(t *testcase.T) {
t.Must.Equal([]string{}, sideEffect)
t.Must.Equal(nest1Value, v.Get(t))
t.Must.Equal(nest1Value, subject(t))
})
})
})
//t.Logf(`%#v`, allSideEffect)
assert.Must(t).ContainExactly([][]string{
{},
{"before1", "around1-begin", "around1-end", "after1"},
{"before1", "around1-begin", "before2", "around2-begin", "around2-end", "after2", "around1-end", "after1"},
}, allSideEffect)
}
func TestSpec_Describe_executedAsAGroupInTheEndOfThe(t *testing.T) {
var (
ran bool
once int
)
s := testcase.NewSpec(t)
s.Describe(`executed in the end of the Describe block`, func(s *testcase.Spec) {
s.Test(``, func(t *testcase.T) {
ran = true
once++
})
})
assert.Must(t).True(ran)
assert.Must(t).Equal(1, once)
}
func TestSpec_ParallelSafeVariableSupport(t *testing.T) {
s := testcase.NewSpec(t)
s.Parallel()
v := testcase.Var[int]{ID: testcase.VarID(strconv.Itoa(rand.Int()))}
nest1Value := rand.Int()
nest2Value := rand.Int()
nest3Value := rand.Int()
s.Describe(`nest-lvl-1`, func(s *testcase.Spec) {
subject := func(t *testcase.T) int { return v.Get(t) }
v.Let(s, func(t *testcase.T) int { return nest1Value })
s.When(`nest-lvl-2`, func(s *testcase.Spec) {
v.Let(s, func(t *testcase.T) int { return nest2Value })
s.And(`nest-lvl-3`, func(s *testcase.Spec) {
v.Let(s, func(t *testcase.T) int { return nest3Value })
s.Test(`lvl-3`, func(t *testcase.T) {
t.Must.Equal(nest3Value, v.Get(t))
t.Must.Equal(nest3Value, subject(t))
})
})
s.Test(`lvl-2`, func(t *testcase.T) {
t.Must.Equal(nest2Value, v.Get(t))
t.Must.Equal(nest2Value, subject(t))
})
})
s.Test(`lvl-1`, func(t *testcase.T) {
t.Must.Equal(nest1Value, v.Get(t))
t.Must.Equal(nest1Value, subject(t))
})
})
}
func TestSpec_InvalidUsages(t *testing.T) {
stub := &doubles.TB{}
s := testcase.NewSpec(stub)
nest1Value := rand.Int()
nest2Value := rand.Int()
nest3Value := rand.Int()
_, _, _ = nest1Value, nest2Value, nest3Value
willFatal := isFatalFn(stub)
failNowSpecs := func(t *testing.T, s *testcase.Spec, expectedToFailNow bool) {
t.Helper()
assert.Must(t).Equal(expectedToFailNow, willFatal(func() {
s.Before(func(t *testcase.T) {})
}))
assert.Must(t).Equal(expectedToFailNow, willFatal(func() {
s.After(func(t *testcase.T) {})
}))
assert.Must(t).Equal(expectedToFailNow, willFatal(func() {
s.Around(func(t *testcase.T) func() { return func() {} })
}))
assert.Must(t).Equal(expectedToFailNow, willFatal(func() {
testcase.Let(s, func(t *testcase.T) interface{} { return nil })
}))
assert.Must(t).Equal(expectedToFailNow, willFatal(func() {
s.Parallel()
}))
assert.Must(t).Equal(expectedToFailNow, willFatal(func() {
testcase.LetValue(s, rand.Int())
}))
}
shouldFailNowForHooking := func(t *testing.T, s *testcase.Spec) { t.Helper(); failNowSpecs(t, s, true) }
shouldNotFailForHooking := func(t *testing.T, s *testcase.Spec) { t.Helper(); failNowSpecs(t, s, false) }
topSpec := s
s.Describe(`nest-lvl-1`, func(s *testcase.Spec) {
shouldFailNowForHooking(t, topSpec)
shouldNotFailForHooking(t, s)
testcase.Let(s, func(t *testcase.T) interface{} { return nest1Value })
shouldNotFailForHooking(t, s)
s.Test(`lvl-1-first`, func(t *testcase.T) {})
shouldFailNowForHooking(t, s)
s.When(`nest-lvl-2`, func(s *testcase.Spec) {
shouldNotFailForHooking(t, s)
testcase.Let(s, func(t *testcase.T) interface{} { return nest2Value })
shouldNotFailForHooking(t, s)
s.And(`nest-lvl-3`, func(s *testcase.Spec) {
testcase.Let(s, func(t *testcase.T) interface{} { return nest3Value })
s.Test(`lvl-3`, func(t *testcase.T) {})
shouldFailNowForHooking(t, s)
})
shouldFailNowForHooking(t, s)
s.Test(`lvl-2`, func(t *testcase.T) {})
shouldFailNowForHooking(t, s)
s.And(`nest-lvl-2-2`, func(s *testcase.Spec) {
shouldNotFailForHooking(t, s)
s.Test(`nest-lvl-2-2-then`, func(t *testcase.T) {})
shouldFailNowForHooking(t, s)
})
shouldFailNowForHooking(t, s)
})
shouldFailNowForHooking(t, s)
s.Test(`lvl-1-last`, func(t *testcase.T) {})
shouldFailNowForHooking(t, s)
})
}
func TestSpec_Let_valuesAreDeterministicallyCached(t *testing.T) {
s := testcase.NewSpec(t)
var testCase1Value int
var testCase2Value int
type TestStruct struct {
Value string
}
s.Describe(`Let`, func(s *testcase.Spec) {
v := testcase.Let(s, func(t *testcase.T) int { return rand.Int() })
s.Then(`regardless of multiple call, let value remain the same for each`, func(t *testcase.T) {
value := v.Get(t)
testCase1Value = value
t.Must.Equal(value, v.Get(t))
})
s.Then(`for every then block then block value is reevaluated`, func(t *testcase.T) {
value := v.Get(t)
testCase2Value = value
t.Must.Equal(value, v.Get(t))
})
s.And(`the value is accessible from the hooks as well`, func(s *testcase.Spec) {
var value int
s.Before(func(t *testcase.T) {
value = v.Get(t)
})
s.Then(`it will remain the same value in the test case as well compared to the before block`, func(t *testcase.T) {
t.Must.NotEqual(0, value)
t.Must.Equal(value, v.Get(t))
})
})
s.And(`struct value can be modified by hooks for preparation purposes like setting up mocks expectations`, func(s *testcase.Spec) {
ts := testcase.Let(s, func(t *testcase.T) *TestStruct {
return &TestStruct{}
})
s.Before(func(t *testcase.T) {
value := ts.Get(t)
value.Value = "testing"
})
s.Then(`the value can be seen from the test case scope`, func(t *testcase.T) {
t.Must.Equal(`testing`, ts.Get(t).Value)
})
})
})
assert.Must(t).NotEqual(testCase1Value, testCase2Value)
}
func TestSpec_Let_valueScopesAppliedOnHooks(t *testing.T) {
s := testcase.NewSpec(t)
var leaker int
s.Context(`1`, func(s *testcase.Spec) {
value := testcase.Let(s, func(t *testcase.T) int {
return 24
})
s.Before(func(t *testcase.T) {
leaker = value.Get(t)
})
s.Context(`2`, func(s *testcase.Spec) {
value.Let(s, func(t *testcase.T) int {
return 42
})
s.Test(`testCase`, func(t *testcase.T) {
t.Must.Equal(42, leaker)
})
})
})
}
func hackCallParallel(tb testing.TB) {
switch tb := tb.(type) {
case *testing.T:
tb.Parallel()
case *doubles.RecorderTB:
hackCallParallel(tb.TB)
case *testcase.T:
hackCallParallel(tb.TB)
default:
tb.Fatalf(`%T don't implement #Parallel`, tb)
}
}
func TestSpec_Parallel(t *testing.T) {
s := testcase.NewSpec(t)
isPanic := func(block func()) (panicked bool) {
defer func() {
if r := recover(); r != nil {
panicked = true
}
}()
block()
return false
}
s.Describe(`Parallel`, func(s *testcase.Spec) {
s.When(`no parallel set on top level nesting`, func(s *testcase.Spec) {
s.And(`on each sub level`, func(s *testcase.Spec) {
s.Then(`it will accept T#Parallel call`, func(t *testcase.T) {
assert.Must(t).True(!isPanic(func() { hackCallParallel(t.TB) }))
})
})
s.Then(`it will accept T#Parallel call`, func(t *testcase.T) {
assert.Must(t).True(!isPanic(func() { hackCallParallel(t.TB) }))
})
})
s.When(`on the first level there is no parallel configured`, func(s *testcase.Spec) {
s.And(`on the second one, yes`, func(s *testcase.Spec) {
s.Parallel()
s.And(`parallel will be "inherited" for each nested spec`, func(s *testcase.Spec) {
s.Then(`it will panic on T#Parallel call`, func(t *testcase.T) {
assert.Must(t).True(isPanic(func() { hackCallParallel(t.TB) }))
})
})
s.Then(`it panic on T#Parallel call`, func(t *testcase.T) {
assert.Must(t).True(isPanic(func() { hackCallParallel(t.TB) }))
})
})
s.Then(`it will accept T#Parallel call`, func(t *testcase.T) {
assert.Must(t).True(!isPanic(func() { hackCallParallel(t.TB) }))
})
})
})
}
func TestSpec_testsWithName_shouldRun(t *testing.T) {
var a, b bool
t.Run(``, func(t *testing.T) {
s := testcase.NewSpec(t)
s.Test(``, func(t *testcase.T) {
a = true
})
s.Test(``, func(t *testcase.T) {
b = true
})
})
assert.Must(t).True(a)
assert.Must(t).True(b)
}
func TestSpec_NoSideEffect(t *testing.T) {
s := testcase.NewSpec(t)
isPanic := func(block func()) (panicked bool) {
defer func() {
if r := recover(); r != nil {
panicked = true
}
}()
block()
return false
}
s.Describe(`NoSideEffect`, func(s *testcase.Spec) {
s.When(`on the first level there is no parallel configured`, func(s *testcase.Spec) {
s.And(`on the second one, yes`, func(s *testcase.Spec) {
s.NoSideEffect()
s.And(`parallel will be "inherited" for each nested spec`, func(s *testcase.Spec) {
s.Then(`it will panic on T#parallel call`, func(t *testcase.T) {
assert.Must(t).True(isPanic(func() { hackCallParallel(t.TB) }))
})
})
s.Then(`it panic on T#parallel call`, func(t *testcase.T) {
assert.Must(t).True(isPanic(func() { hackCallParallel(t.TB) }))
})
})
s.Then(`it will accept T#parallel call`, func(t *testcase.T) {
assert.Must(t).True(!isPanic(func() { hackCallParallel(t.TB) }))
})
})
})
}
func TestSpec_Let_FallibleValue(t *testing.T) {
s := testcase.NewSpec(t)
fallible := testcase.Let(s, func(t *testcase.T) testing.TB {
return t.TB
})
s.Then(`fallible receive the same testing object as this spec`, func(t *testcase.T) {
t.Must.Equal(t.TB, fallible.Get(t))
})
}
func TestSpec_LetValue_ValueDefinedAtDeclarationWithoutTheNeedOfFunctionCallback(t *testing.T) {
s := testcase.NewSpec(t)
value := testcase.LetValue(s, 42)
s.Then(`the testCase variable will be accessible`, func(t *testcase.T) {
t.Must.Equal(42, value.Get(t))
})
for kind, example := range map[reflect.Kind]interface{}{
reflect.String: "hello world",
reflect.Bool: true,
reflect.Int: int(42),
reflect.Int8: int8(42),
reflect.Int16: int16(42),
reflect.Int32: int32(42),
reflect.Int64: int64(42),
reflect.Uint: uint(42),
reflect.Uint8: uint8(42),
reflect.Uint16: uint16(42),
reflect.Uint32: uint32(42),
reflect.Uint64: uint64(42),
reflect.Float32: float32(42),
reflect.Float64: float64(42),
reflect.Complex64: complex64(42),
reflect.Complex128: complex128(42),
} {
kind := kind
example := example
s.Context(kind.String(), func(s *testcase.Spec) {
vk := testcase.LetValue(s, example)
s.Then(`it will return the value`, func(t *testcase.T) {
t.Must.Equal(example, vk.Get(t))
})
})
}
}
func TestSpec_LetValue_mutableValuesAreNotAllowed(t *testing.T) {
stub := &doubles.TB{}
s := testcase.NewSpec(stub)
var finished bool
sandbox.Run(func() {
type SomeStruct struct {
Text string
}
testcase.LetValue(s, &SomeStruct{Text: `hello world`})
finished = true
})
assert.Must(t).True(!finished)
assert.Must(t).True(stub.IsFailed)
}
func TestSpec_Cleanup_inACleanupWithinACleanup(t *testing.T) {
t.Run(`spike`, func(t *testing.T) {
var ran bool
t.Run(``, func(t *testing.T) {
t.Cleanup(func() {
t.Cleanup(func() {
t.Cleanup(func() {
ran = true
})
})
})
})
assert.Must(t).True(ran)
})
var ran bool
s := testcase.NewSpec(t)
s.After(func(t *testcase.T) {
t.Cleanup(func() {
t.Cleanup(func() {
ran = true
})
})
})
s.Test(``, func(t *testcase.T) {})
assert.Must(t).True(ran)
}
func BenchmarkTest_Spec(b *testing.B) {
b.Log(`this is actually a testCase`)
b.Log(`it will run a bench *testing.B.N times`)
var total int
b.Run(``, func(b *testing.B) {
s := testcase.NewSpec(b)
s.Test(``, func(t *testcase.T) {
time.Sleep(time.Millisecond)
total++
})
})
assert.Must(b).True(1 < total)
}
func BenchmarkTest_Spec_eachBenchmarkingRunsWithFreshState(b *testing.B) {
b.Log(`this is actually a testCase`)
b.Log(`it will run a bench *testing.B.N times but in parallel with b.RunParallel`)
s := testcase.NewSpec(b)
type mutable struct{ used bool }
m := testcase.Let(s, func(t *testcase.T) *mutable {
return &mutable{used: false}
})
s.Before(func(t *testcase.T) {
assert.Must(t).True(!m.Get(t).used)
})
b.Log(`each benchmarking runs with fresh state to avoid side effects between bench mark iterations`)
s.Test(``, func(t *testcase.T) {
m := m.Get(t)
assert.Must(t).True(!m.used)
m.used = true
t.SkipNow()
})
}
type UnknownTestingTB struct {
testing.TB
logs [][]interface{}
}
func (tb *UnknownTestingTB) Log(args ...interface{}) {
tb.logs = append(tb.logs, args)
}
func TestSpec_Test_withSomethingThatImplementsTestcaseTB(t *testing.T) {
rtb := &doubles.RecorderTB{TB: &doubles.TB{}}
var tb testcase.TBRunner = rtb // implements check
s := testcase.NewSpec(tb)
s.Test(`passthrough`, func(t *testcase.T) {
t.FailNow()
})
rtb.CleanupNow()
assert.Must(t).True(rtb.IsFailed)
}
func TestSpec_Sequential(t *testing.T) {
if testing.Short() {
t.Skip()
}
spechelper.OrderAsDefined(t)
s := testcase.NewSpec(t)
// --- somewhere in a spec helper setup function --- //
// Sequential is used to ensure testing executed sequential,
// because here we define a variable that meant to be used sequentially only.
s.Sequential()
// --- in a spec file --- //
// somewhere else in a spec where the code itself has no side effect
// we use parallel to allow the possibility of run testCase concurrently.
s.Parallel()
var bTestRan bool
s.Test(`A`, func(t *testcase.T) {
runtime.Gosched()
time.Sleep(time.Millisecond)
assert.Must(t).True(!bTestRan,
`testCase A ran in parallel with testCase B, but this was not expected after using testcase.Spec#Sequence`)
})
s.Test(`B`, func(t *testcase.T) {
bTestRan = true
})
}
func TestSpec_HasSideEffect(t *testing.T) {
if testing.Short() {
t.Skip()
}
spechelper.OrderAsDefined(t)
s := testcase.NewSpec(t)
s.HasSideEffect()
s.NoSideEffect()
var bTestRan bool
s.Test(`A`, func(t *testcase.T) {
runtime.Gosched()
time.Sleep(time.Millisecond)
assert.Must(t).True(!bTestRan,
`testCase A ran in parallel with testCase B, but this was not expected after using testcase.Spec#Sequence`)
})
s.Test(`B`, func(t *testcase.T) {
bTestRan = true
})
}
func TestSpec_Sequential_scoped(t *testing.T) {
if testing.Short() {
t.Skip()
}
spechelper.OrderAsDefined(t)
s := testcase.NewSpec(t)
s.Parallel()
s.Context(`scope A`, func(s *testcase.Spec) {
s.Sequential()
s.Context(`sub-scope of A`, func(s *testcase.Spec) {
s.Parallel()
var bTestRan bool
s.Test(`A`, func(t *testcase.T) {
runtime.Gosched()
time.Sleep(time.Millisecond)
assert.Must(t).True(!bTestRan,
`testCase A ran in parallel with testCase B, but this was not expected after using testcase.Spec#Sequence`)
})
s.Test(`B`, func(t *testcase.T) {
bTestRan = true
})
})
})
s.Context(`scope B`, func(s *testcase.Spec) {
var wg = &sync.WaitGroup{}
wg.Add(1)
s.Test(`A`, func(t *testcase.T) {
runtime.Gosched()
c := make(chan struct{})
go func() {
defer close(c)
wg.Wait() // if wait is done, we are in the happy group
}()
select {
case <-c: // happy case
case <-time.After(time.Second):
t.Fatal(`B testCase probably not running concurrently`) // timed orderingOutput
}
})
s.Test(`B`, func(t *testcase.T) {
defer wg.Done()
})
})
}
func TestSpec_Sequential_callingItAfterContextDeclarationYieldPanic(t *testing.T) {
s := testcase.NewSpec(t)
s.Context(``, func(s *testcase.Spec) {})
assert.Must(t).Panic(func() { s.HasSideEffect() })
}
func TestSpec_panicDoNotLeakOutFromTestingScope(t *testing.T) {
t.Skip("dropped feature")
// due to the nature of panics,
// recovering them makes the debugging much harder in the testing scenario.
//
var noPanic bool
func() {
defer recover()
rtb := &doubles.RecorderTB{TB: &doubles.TB{}}
defer rtb.CleanupNow()
s := testcase.NewSpec(rtb)
s.Test(``, func(t *testcase.T) { panic(`die`) })
s.Test(``, func(t *testcase.T) { noPanic = true })
}()
assert.Must(t).True(noPanic)
}
func BenchmarkTest_Spec_hooksInBenchmarkCalledInEachRun(b *testing.B) {
var (
beforeTimes int
deferTimes int
afterTimes int
testTimes int
)
b.Run(``, func(b *testing.B) {
s := testcase.NewSpec(b)
s.Sequential()
s.Before(func(t *testcase.T) { t.Defer(func() { deferTimes++ }) })
s.Before(func(t *testcase.T) { beforeTimes++ })
s.After(func(t *testcase.T) { afterTimes++ })
var flag bool
s.Around(func(t *testcase.T) func() {
return func() { flag = false }
})
s.Test(``, func(t *testcase.T) {
assert.Must(t).True(!flag)
flag = true // mutate so we expect After to restore the flag state to "false"
testTimes++
time.Sleep(time.Millisecond) // A bit sleep here helps the benchmark to make a faster conclusion.
})
})
assert.Must(b).NotEqual(0, testTimes)
assert.Must(b).Equal(testTimes, beforeTimes)
assert.Must(b).Equal(testTimes, afterTimes)
assert.Must(b).Equal(testTimes, deferTimes)
}
func TestSpec_hooksAlignWithCleanup(t *testing.T) {
var afters []string
t.Run(``, func(t *testing.T) {
s := testcase.NewSpec(t)
s.After(func(t *testcase.T) {
afters = append(afters, `First After`)
})
s.Before(func(t *testcase.T) {
t.Defer(func() { afters = append(afters, `Defer`) })
})
s.Before(func(t *testcase.T) {
t.Cleanup(func() { afters = append(afters, `Cleanup`) })
})
s.After(func(t *testcase.T) {
afters = append(afters, `Last After`)
})
s.Test(``, func(t *testcase.T) {})
})
assert.Must(t).Equal([]string{`Last After`, `Cleanup`, `Defer`, `First After`}, afters)
}
func BenchmarkTest_Spec_SkipBenchmark1(b *testing.B) {
var (
allowedTestRan bool
forbiddenTestRan bool
)
b.Run(``, func(b *testing.B) {
s := testcase.NewSpec(b)
s.Test(``, func(t *testcase.T) {
allowedTestRan = true
t.SkipNow()
})
s.Test(``, func(t *testcase.T) {
forbiddenTestRan = true
t.SkipNow()
}, testcase.SkipBenchmark())
s.Then(``, func(t *testcase.T) {
forbiddenTestRan = true
t.SkipNow()
}, testcase.SkipBenchmark())
})
assert.Must(b).True(allowedTestRan)
assert.Must(b).True(!forbiddenTestRan)
}
func BenchmarkTest_Spec_SkipBenchmark2(b *testing.B) {
var (
allowedTestRan bool
forbiddenTestRan bool
)
b.Run(``, func(b *testing.B) {
s := testcase.NewSpec(b)
s.Context(``, func(s *testcase.Spec) {
s.Test(``, func(t *testcase.T) {
allowedTestRan = true
t.SkipNow()
})
})
s.Context(``, func(s *testcase.Spec) {
s.SkipBenchmark()
s.Test(``, func(t *testcase.T) {
forbiddenTestRan = true
t.SkipNow()
})
})
})
assert.Must(b).True(allowedTestRan)
assert.Must(b).True(!forbiddenTestRan)
}
type stubB struct {
*doubles.TB
TestingB *testing.B
}
func (b *stubB) Run(name string, fn func(b *testing.B)) bool {
return b.TestingB.Run(name, fn)
}
func BenchmarkTest_Spec_SkipBenchmark_invalidUse(b *testing.B) {
stub := &doubles.TB{}
stb := &stubB{
TB: stub,
TestingB: b,
}
s := testcase.NewSpec(stb)
s.Test(``, func(t *testcase.T) { t.SkipNow() })
var finished bool
sandbox.Run(func() {
s.SkipBenchmark()
finished = false
})
assert.Must(b).True(!finished)
assert.Must(b).True(stub.IsFailed)
assert.Must(b).Contain(stub.Logs.String(), "you can't use .SkipBenchmark after you already used when/and/then")
}
func BenchmarkTest_Spec_Test_flaky(b *testing.B) {