-
Notifications
You must be signed in to change notification settings - Fork 10
/
Var_test.go
1258 lines (1067 loc) · 32.4 KB
/
Var_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 (
"fmt"
"strconv"
"sync"
"testing"
"time"
"go.llib.dev/testcase/assert"
"go.llib.dev/testcase/internal/doubles"
"go.llib.dev/testcase/random"
"go.llib.dev/testcase"
)
func TestVar(t *testing.T) {
s := testcase.NewSpec(t)
// to testCase testVar, I need side effect by resetting the expected in a before hook
// the var of testVar needs to be leaked into the testing subjects,
// and I can't use a testVar to testCase testVar because I need this expected at spec level as well.
// So to testCase testcase.Var, I can't use fully testcase.Var.
// This should not be the case for anything else outside the testing framework.
s.HasSideEffect()
rnd := random.New(random.CryptoSeed{})
var testVar = testcase.Var[int]{ID: testcase.VarID(rnd.StringNWithCharset(5, "abcdefghijklmnopqrstuvwxyz"))}
expected := rnd.Int()
stub := &doubles.TB{}
willFatal := willFatalWithMessageFn(stub)
willFatalWithVariableNotFoundMessage := func(s *testcase.Spec, tb testing.TB, varName testcase.VarID, blk func(*testcase.T)) {
tct := testcase.NewTWithSpec(stub, s)
assert.Must(tb).Contain(willFatal(t, func() { blk(tct) }),
fmt.Sprintf("Variable %q is not found.", varName))
}
s.Describe(`#Get`, func(s *testcase.Spec) {
subject := func(t *testcase.T) int {
return testVar.Get(t)
}
s.When(`no expected defined in the spec and no init logic provided`, func(s *testcase.Spec) {
s.Then(`it will panic, and warn about the unknown expected`, func(t *testcase.T) {
willFatalWithVariableNotFoundMessage(s, t, testVar.ID, func(t *testcase.T) { subject(t) })
})
})
s.When(`spec has value by testCase runtime Var#Set`, func(s *testcase.Spec) {
s.Before(func(t *testcase.T) {
testVar.Set(t, expected)
})
s.Then(`the expected is returned`, func(t *testcase.T) {
assert.Must(t).Equal(expected, testVar.Get(t))
})
})
s.When(`spec has value set by Var#Let`, func(s *testcase.Spec) {
testVar.Let(s, func(t *testcase.T) int {
return expected
})
s.Then(`the expected is returned`, func(t *testcase.T) {
assert.Must(t).Equal(expected, testVar.Get(t))
})
})
s.When(`spec has value set by Var#LetValue`, func(s *testcase.Spec) {
testVar.LetValue(s, expected)
s.Then(`the expected is returned`, func(t *testcase.T) {
assert.Must(t).Equal(expected, testVar.Get(t))
})
})
s.When(`Var#Init is defined`, func(s *testcase.Spec) {
s.HasSideEffect()
s.Before(func(t *testcase.T) {
// WARN: do not use any other hook that manipulates the testVar here
// else the side effect is not guaranteed
testVar.Init = func(t *testcase.T) int { return expected }
// reset side effect
t.Cleanup(func() { testVar.Init = nil })
})
thenValueIsCached := func(s *testcase.Spec) {
s.Then(`value is cached`, func(t *testcase.T) {
values := make(map[int]struct{})
for i := 0; i < 128; i++ {
values[testVar.Get(t)] = struct{}{}
}
const failReason = `it was expected that the value from the var is deterministic/cached within the testCase lifetime`
assert.Must(t).True(len(values) == 1, failReason)
})
}
s.And(`spec don't have value set in any way`, func(s *testcase.Spec) {
s.Then(`it will return the Value from init`, func(t *testcase.T) {
assert.Must(t).Equal(expected, testVar.Get(t))
})
s.And(`.Init creates a non deterministic value`, func(s *testcase.Spec) {
s.HasSideEffect()
testVar.Init = func(t *testcase.T) int { return t.Random.Int() }
defer func() { testVar.Init = nil }()
thenValueIsCached(s)
})
})
s.And(`spec have value set in some form`, func(s *testcase.Spec) {
testVar.LetValue(s, 42)
s.Then(`the expected is returned`, func(t *testcase.T) {
assert.Must(t).Equal(42, testVar.Get(t))
})
})
})
})
s.Describe(`#Set`, func(s *testcase.Spec) {
subject := func(t *testcase.T) {
testVar.Set(t, expected)
}
s.When(`subject used`, func(s *testcase.Spec) {
s.Before(subject)
s.Then(`it will set value in the current testCase`, func(t *testcase.T) {
assert.Must(t).Equal(expected, testVar.Get(t))
})
})
s.When(`subject is not used`, func(s *testcase.Spec) {
s.Then(`value will be absent`, func(t *testcase.T) {
willFatalWithVariableNotFoundMessage(s, t, testVar.ID, func(t *testcase.T) { testVar.Get(t) })
})
})
})
s.Describe(`#Let`, func(s *testcase.Spec) {
subject := func(s *testcase.Spec) {
testVar.Let(s, func(t *testcase.T) int { return expected })
}
s.When(`subject used`, func(s *testcase.Spec) {
subject(s)
s.Then(`it will set value in the spec level`, func(t *testcase.T) {
assert.Must(t).Equal(expected, testVar.Get(t))
})
})
s.When(`subject is not used on a clean Spec`, func(s *testcase.Spec) {
s.Then(`value will be absent`, func(t *testcase.T) {
willFatalWithVariableNotFoundMessage(s, t, testVar.ID, func(t *testcase.T) { testVar.Get(t) })
})
})
})
s.Describe(`#LetValue`, func(s *testcase.Spec) {
subject := func(s *testcase.Spec) {
testVar.LetValue(s, expected)
}
s.When(`subject used`, func(s *testcase.Spec) {
subject(s)
s.Then(`it will set value in the spec level`, func(t *testcase.T) {
assert.Must(t).Equal(expected, testVar.Get(t))
})
})
s.When(`subject is not used on a clean Spec`, func(s *testcase.Spec) {
s.Then(`value will be absent`, func(t *testcase.T) {
willFatalWithVariableNotFoundMessage(s, t, testVar.ID, func(t *testcase.T) { testVar.Get(t) })
})
})
})
s.Describe(`#EagerLoading`, func(s *testcase.Spec) {
subject := func(s *testcase.Spec) {
testVar.EagerLoading(s)
}
testVar.Let(s, func(t *testcase.T) int {
return int(time.Now().UnixNano())
})
s.When(`subject used`, func(s *testcase.Spec) {
subject(s)
s.Then(`value will be eager loaded`, func(t *testcase.T) {
time.Sleep(5 * time.Nanosecond)
now := int(time.Now().UnixNano())
t.Must.True(testVar.Get(t) < now)
})
})
s.When(`subject not used`, func(s *testcase.Spec) {
s.Then(`value will be lazy loaded`, func(t *testcase.T) {
now := int(time.Now().UnixNano())
t.Must.True(now < testVar.Get(t))
})
})
})
willFatalWithOnLetMissing := func(s *testcase.Spec, tb testing.TB, varName testcase.VarID, blk func(*testcase.T)) {
tct := testcase.NewTWithSpec(stub, s)
assert.Must(tb).Contain(willFatal(t, func() { blk(tct) }),
fmt.Sprintf("%s Var has Var.OnLet. You must use Var.Let, Var.LetValue to initialize it properly.", varName))
}
s.Describe(`#OnLet`, func(s *testcase.Spec) {
s.When(`it is provided`, func(s *testcase.Spec) {
v := testcase.Var[int]{
ID: `foo`,
OnLet: func(s *testcase.Spec, _ testcase.Var[int]) {
s.Tag(`on-let`) // test trough side effect
},
}
s.And(`variable is not bound to Spec`, func(s *testcase.Spec) {
s.Test(`it will panic on Var.Get`, func(t *testcase.T) {
willFatalWithOnLetMissing(s, t, v.ID, func(t *testcase.T) { v.Get(t) })
})
s.Test(`it will panic on Var.Set`, func(t *testcase.T) {
willFatalWithOnLetMissing(s, t, v.ID, func(t *testcase.T) { v.Set(t, 42) })
})
})
s.And(`variable is bound to Spec with Var.Let`, func(s *testcase.Spec) {
v.Let(s, func(t *testcase.T) int { return 42 })
s.Test(`Var.Get returns value`, func(t *testcase.T) {
assert.Must(t).Equal(42, v.Get(t))
})
s.Test(`it will apply the setup in the context`, func(t *testcase.T) {
assert.Must(t).True(t.HasTag(`on-let`))
})
})
s.And(`variable is bound to Spec with Var.LetValue`, func(s *testcase.Spec) {
v.LetValue(s, 42)
s.Test(`Var.Get returns value`, func(t *testcase.T) {
assert.Must(t).Equal(42, v.Get(t))
})
s.Test(`it will apply the setup in the context`, func(t *testcase.T) {
assert.Must(t).True(t.HasTag(`on-let`))
})
})
})
s.When(`it is absent`, func(s *testcase.Spec) {
v := testcase.Var[int]{
ID: `foo`,
}
s.And(`variable is not bound to Spec`, func(s *testcase.Spec) {
v := testcase.Var[int]{
ID: `foo`,
Init: func(t *testcase.T) int {
// required to be used without binding Var to Spec
return 42
},
}
s.Test(`it will return initialized value on Var.Get`, func(t *testcase.T) {
assert.Must(t).Equal(42, v.Get(t))
})
})
s.And(`variable is bound to Spec with Var.Let`, func(s *testcase.Spec) {
v.Let(s, func(t *testcase.T) int { return 42 })
s.Test(`Var.Get returns value`, func(t *testcase.T) {
assert.Must(t).Equal(42, v.Get(t))
})
s.Test(`no hook, no setup`, func(t *testcase.T) {
assert.Must(t).True(!t.HasTag(`on-let`))
})
})
s.And(`variable is bound to Spec with Var.LetValue`, func(s *testcase.Spec) {
v.LetValue(s, 42)
s.Test(`Var.Get returns value`, func(t *testcase.T) {
assert.Must(t).Equal(42, v.Get(t))
})
s.Test(`no hook, no setup`, func(t *testcase.T) {
assert.Must(t).True(!t.HasTag(`on-let`))
})
})
})
})
s.Describe("#Super", func(s *testcase.Spec) {
s.Test("when no super is actually set", func(t *testcase.T) {
s := testcase.NewSpec(t)
var v = testcase.Var[int]{ID: "Var[int]"}
v.Let(s, func(t *testcase.T) int {
t.Must.Panic(func() { _ = v.Super(t) })
return 42
})
s.Test("", func(t *testcase.T) {
v.Get(t)
})
s.Finish()
})
s.Test("when super is only defined with Init block", func(t *testcase.T) {
s := testcase.NewSpec(t.TB)
var v = testcase.Var[int]{ID: "Var[int]", Init: func(t *testcase.T) int {
return 32
}}
v.Let(s, func(t *testcase.T) int {
return v.Super(t) + 10
})
s.Test("", func(t *testcase.T) {
t.Must.Equal(42, v.Get(t))
})
})
s.Test("when super is defined with a testcase.Let", func(t *testcase.T) {
s := testcase.NewSpec(t.TB)
v := testcase.Let(s, func(t *testcase.T) int {
return 32
})
s.Context("", func(s *testcase.Spec) {
v.Let(s, func(t *testcase.T) int {
return v.Super(t) + 10
})
s.Test("", func(t *testcase.T) {
t.Must.Equal(42, v.Get(t))
})
})
})
s.Test("when super is defined with a Var[V].Let along with an Init", func(t *testcase.T) {
s := testcase.NewSpec(t.TB)
v := testcase.Var[int]{
ID: "Var[int]",
Init: func(t *testcase.T) int { return 128 },
}
v.Let(s, func(t *testcase.T) int {
return 32
})
s.Context("", func(s *testcase.Spec) {
v.Let(s, func(t *testcase.T) int {
t.Log("n-lvl-1", v.Super(t))
return v.Super(t) + 10
})
s.Test("", func(t *testcase.T) {
t.Must.Equal(42, v.Get(t))
})
})
})
s.Test("when Init is set along with OnLet, but it was never set with a Let", func(t *testcase.T) {
s := testcase.NewSpec(t.TB)
v := testcase.Var[int]{
ID: "Var[int]",
Init: func(t *testcase.T) int { return 32 },
OnLet: func(s *testcase.Spec, v testcase.Var[int]) {},
}
v.Let(s, func(t *testcase.T) int {
return v.Super(t) + 10
})
s.Test("", func(t *testcase.T) {
t.Must.Equal(42, v.Get(t))
})
})
s.Test("when super is defined with a testcase.Let", func(t *testcase.T) {
s := testcase.NewSpec(t.TB)
v := testcase.Let(s, func(t *testcase.T) int {
return 32
})
s.Context("", func(s *testcase.Spec) {
v.Let(s, func(t *testcase.T) int {
return v.Super(t) + 10
})
s.Test("", func(t *testcase.T) {
t.Must.Equal(42, v.Get(t))
})
})
})
s.Test("the results of super is cached", func(t *testcase.T) {
s := testcase.NewSpec(t.TB)
v := testcase.Let(s, func(t *testcase.T) int {
return t.Random.Int()
})
s.Context("", func(s *testcase.Spec) {
v.Let(s, func(t *testcase.T) int {
t.Must.Equal(v.Super(t), v.Super(t))
return 42
})
s.Test("", func(t *testcase.T) {
t.Must.Equal(42, v.Get(t))
})
})
})
s.Test("when super is defined with a testcase.Let", func(t *testcase.T) {
s := testcase.NewSpec(t.TB)
v := testcase.Let(s, func(t *testcase.T) int {
return t.Random.Int() + 1
})
s.Context("", func(s *testcase.Spec) {
v.Let(s, func(t *testcase.T) int {
t.Must.Equal(v.Super(t), v.Super(t))
return v.Super(t)
})
s.Test("", func(t *testcase.T) {
t.Must.NotEmpty(v.Get(t))
})
})
})
s.Test("super is inherited and always represent the previous declaration", func(t *testcase.T) {
s := testcase.NewSpec(t.TB)
v := testcase.Var[int]{
ID: "v",
Init: func(t *testcase.T) int {
t.Log("init", 1)
return 1
},
}
v.Let(s, func(t *testcase.T) int {
super := v.Super(t)
t.Log("nl-0", super)
t.Must.Equal(super, v.Super(t))
return super + 1
})
s.Context("nl-1", func(s *testcase.Spec) {
v.Let(s, func(t *testcase.T) int {
super := v.Super(t)
t.Log("nl-1", super)
t.Must.Equal(super, v.Super(t))
return super + 1
})
s.Then("", func(t *testcase.T) {
t.Must.Equal(3, v.Get(t))
})
s.Context("nl-2", func(s *testcase.Spec) {
v.Let(s, func(t *testcase.T) int {
super := v.Super(t)
t.Log("nl-2", super)
t.Must.Equal(super, v.Super(t))
return super + 1
})
s.Then("", func(t *testcase.T) {
t.Must.Equal(4, v.Get(t))
})
s.Context("nl-3", func(s *testcase.Spec) {
v.Let(s, func(t *testcase.T) int {
super := v.Super(t)
t.Log("nl-3", super)
t.Must.Equal(super, v.Super(t))
return super + 1
})
s.Then("", func(t *testcase.T) {
t.Must.Equal(5, v.Get(t))
})
})
})
})
})
s.Test("super declaration is inherited along the nested testing context branch", func(t *testcase.T) {
t.Log("and always the previous super call will be inherited")
s := testcase.NewSpec(t.TB)
v := testcase.Var[int]{
ID: "v",
Init: func(t *testcase.T) int {
t.Log("n-lvl-nan -> init")
return 32
},
}
v.Let(s, func(t *testcase.T) int {
t.Log("n-lvl-0", v.Super(t))
return v.Super(t) + 10
})
s.Context("", func(s *testcase.Spec) {
s.Context("", func(s *testcase.Spec) {
v.Let(s, func(t *testcase.T) int {
t.Log("n-lvl-2", v.Super(t))
return v.Super(t) + 22
})
s.Context("", func(s *testcase.Spec) {
s.Context("", func(s *testcase.Spec) {
s.Then("", func(t *testcase.T) {
t.Must.Equal(64, v.Get(t))
})
})
})
})
s.Then("", func(t *testcase.T) {
t.Must.Equal(42, v.Get(t))
})
})
})
s.Test("Var.Super can be used from an another testcase.Let as well", func(t *testcase.T) {
t.Log("this is ideal to make a stub that wraps the original value of an another variable")
t.Log("and then the orignal var use the result of this new variable.")
t.Log("For example a Role Interface value is decorated with fault injection.")
s := testcase.NewSpec(t.TB)
v1 := testcase.Let(s, func(t *testcase.T) int {
return 32
})
s.Context("", func(s *testcase.Spec) {
v2 := testcase.Let(s, func(t *testcase.T) int {
return v1.Super(t) + 10
})
v1.Let(s, func(t *testcase.T) int { return v2.Get(t) })
s.Test("", func(t *testcase.T) {
t.Must.Equal(42, v1.Get(t))
})
})
})
})
}
func TestVar_smokeTest(t *testing.T) {
s := testcase.NewSpec(t)
s.NoSideEffect()
type Entity struct {
TS int64
}
entity1 := testcase.Let(s, func(t *testcase.T) Entity {
return Entity{TS: time.Now().UnixNano()}
})
entity2 := testcase.Let(s, func(t *testcase.T) Entity {
return Entity{TS: time.Now().UnixNano()}
})
s.When(`var is allowed to use lazy loading`, func(s *testcase.Spec) {
// nothing to do here, lazy loading is the default behavior
s.Then(`it should be initialized when it is first accessed`, func(t *testcase.T) {
e1ts := entity1.Get(t).TS
time.Sleep(42 * time.Nanosecond)
e2ts := entity2.Get(t).TS
assert.Must(t).True(e1ts < e2ts)
})
})
s.When(`var eager loading is requested`, func(s *testcase.Spec) {
entity2.EagerLoading(s)
s.Then(`the value should be evaluated `, func(t *testcase.T) {
e1ts := entity1.Get(t).TS
time.Sleep(42 * time.Nanosecond)
t.Log(`now we access entity 2,`)
t.Log(`but the value should already be evaluated by the time the test case block is reached`)
e2ts := entity2.Get(t).TS
assert.Must(t).True(e2ts < e1ts)
})
})
s.When(`var override done at spec spec level`, func(s *testcase.Spec) {
entity1.Let(s, func(t *testcase.T) Entity {
return Entity{TS: 0}
})
s.Then(`in the test case the overridden value will be the initial value`, func(t *testcase.T) {
assert.Must(t).True(entity1.Get(t).TS == 0)
})
s.Context(``, func(s *testcase.Spec) {
entity1.Let(s, func(t *testcase.T) Entity {
// defined at spec level -> will be initial value with lazy load
return Entity{TS: time.Now().UnixNano()}
})
s.Before(func(t *testcase.T) {
// defined at testCase run time, will be eager loaded
entity2.Set(t, Entity{TS: time.Now().UnixNano()})
})
s.Test(`spec level definition should be the `, func(t *testcase.T) {
})
})
})
s.When(`var override done at testCase runtime level`, func(s *testcase.Spec) {
s.Before(func(t *testcase.T) {
entity1.Set(t, Entity{TS: 0})
})
s.Then(``, func(t *testcase.T) {
assert.Must(t).True(entity1.Get(t).TS == 0)
})
})
s.Context(`var override at testCase runtime level`, func(s *testcase.Spec) {
s.Before(func(t *testcase.T) {
entity1.Set(t, Entity{TS: 0})
})
s.Then(``, func(t *testcase.T) {
assert.Must(t).True(entity1.Get(t).TS == 0)
})
})
s.When(`init block defined for the variable`, func(s *testcase.Spec) {
entity3 := testcase.Var[Entity]{
ID: "entity 4",
Init: func(t *testcase.T) Entity {
return Entity{TS: 42}
},
}
s.And(`var is bound to a spec without providing a let variable init block as part of the function`, func(s *testcase.Spec) {
entity3.Let(s, nil)
s.Then(`it will use the var init block`, func(t *testcase.T) {
assert.Must(t).True(entity3.Get(t).TS == 42)
})
})
s.And(`var is bound to a spec with a new let variable init block as part of the function parameter`, func(s *testcase.Spec) {
entity3.Let(s, func(t *testcase.T) Entity {
return Entity{TS: 24}
})
s.Then(`it will use passed let init block`, func(t *testcase.T) {
assert.Must(t).True(entity3.Get(t).TS == 24)
})
})
})
}
func TestVar_Get_interface_as_nil(t *testing.T) {
s := testcase.NewSpec(t)
v := testcase.Let(s, func(t *testcase.T) interface{} {
return nil
})
s.Test(``, func(t *testcase.T) {
t.Must.Nil(v.Get(t))
})
}
func TestVar_Get_pointer_as_nil(t *testing.T) {
s := testcase.NewSpec(t)
type T struct{}
v := testcase.Let(s, func(t *testcase.T) *T {
return nil
})
s.Test(``, func(t *testcase.T) {
t.Must.Nil(v.Get(t))
})
}
func TestVar_Get_threadSafe(t *testing.T) {
s := testcase.NewSpec(t)
v := testcase.Var[int]{
ID: `num`,
Init: func(t *testcase.T) int { return 0 },
OnLet: func(s *testcase.Spec, _ testcase.Var[int]) {},
}
v.Let(s, nil)
s.Test(``, func(t *testcase.T) {
blk := func() {
value := v.Get(t)
v.Set(t, value+1)
}
testcase.Race(blk, blk)
})
}
func TestVar_Get_valueSetDuringAnotherVarInitBlock(t *testing.T) {
unsupported(t)
s := testcase.NewSpec(t)
getValues := func(t *testcase.T) (int, int) {
return t.Random.Int(), t.Random.Int()
}
var a, b testcase.Var[int]
a = testcase.Var[int]{
ID: `A`,
Init: func(t *testcase.T) int {
av, bv := getValues(t)
b.Set(t, bv)
return av
},
}
b = testcase.Var[int]{
ID: `B`,
Init: func(t *testcase.T) int {
a.Get(t) // lazy load init
return b.Get(t)
},
}
s.Test(``, func(t *testcase.T) {
b.Get(t)
})
}
func TestVar_Let_initBlock(t *testing.T) {
s := testcase.NewSpec(t)
type Entity struct {
V int
}
s.When(`init block is absent`, func(s *testcase.Spec) {
entity := testcase.Var[Entity]{ID: "entity 1"}
//s.And(`var is bound to a spec without providing a Let variable init block as part of the function`, func(s *testcase.Spec) {
// assert.Must(t).Panic(func() {
// entity.Let(s, nil)
// })
//})
s.And(`var is bound to a spec with a new Let variable init block as part of the function parameter`, func(s *testcase.Spec) {
entity.Let(s, func(t *testcase.T) Entity {
return Entity{V: 42}
})
s.Then(`it will use passed Let init block`, func(t *testcase.T) {
assert.Must(t).True(entity.Get(t).V == 42)
})
})
})
s.When(`init block defined for the variable`, func(s *testcase.Spec) {
entity := testcase.Var[Entity]{
ID: "entity 2",
Init: func(t *testcase.T) Entity {
return Entity{V: 84}
},
}
s.And(`var is bound to a spec without providing a Let variable init block as part of the function`, func(s *testcase.Spec) {
entity.Let(s, nil)
s.Then(`it will use the var init block`, func(t *testcase.T) {
assert.Must(t).True(entity.Get(t).V == 84)
})
})
s.And(`var is bound to a spec with a new Let variable init block as part of the function parameter`, func(s *testcase.Spec) {
entity.Let(s, func(t *testcase.T) Entity {
return Entity{V: 168}
})
s.Then(`it will use passed Let init block`, func(t *testcase.T) {
assert.Must(t).True(entity.Get(t).V == 168)
})
})
})
s.When(`init block defined through Spec#Let`, func(s *testcase.Spec) {
entity := testcase.Let(s, func(t *testcase.T) interface{} {
return Entity{V: 336}
})
s.Test(``, func(t *testcase.T) {
t.Must.NotNil(entity.Init)
t.Must.True(336 == entity.Init(t).(Entity).V)
})
})
}
func TestVar_EagerLoading_daisyChain(t *testing.T) {
s := testcase.NewSpec(t)
value := testcase.Let(s, func(t *testcase.T) interface{} {
return 42
}).EagerLoading(s)
s.Test(`EagerLoading returns the var object for syntax sugar purposes`, func(t *testcase.T) {
assert.Must(t).Equal(42, value.Get(t))
})
}
func TestAppend(t *testing.T) {
s := testcase.NewSpec(t)
var (
v = testcase.Var[[]int]{ID: `testcase.Var`}
e = testcase.Var[int]{ID: `new slice element`}
)
act := func(t *testcase.T) {
testcase.Append(t, v, e.Get(t))
}
s.When(`var content is a slice[T]`, func(s *testcase.Spec) {
v.Let(s, func(t *testcase.T) []int {
return []int{}
})
s.And(`the element is a T type`, func(s *testcase.Spec) {
e.Let(s, func(t *testcase.T) int {
return t.Random.Int()
})
s.Then(`it will append the value to the slice[T] type testcase.Var`, func(t *testcase.T) {
t.Must.Equal(len(v.Get(t)), 0)
act(t)
list := v.Get(t)
elem := e.Get(t)
t.Must.Equal(len(list), 1)
t.Must.Contain(list, elem)
})
s.Then(`on multiple use it will append all`, func(t *testcase.T) {
var expected []int
for i := 0; i < 1024; i++ {
expected = append(expected, i)
e.Set(t, i)
act(t)
}
assert.Must(t).Equal(expected, v.Get(t))
})
})
})
s.Test(`multiple value`, func(t *testcase.T) {
listVar := testcase.Var[[]string]{ID: `slice[T]`, Init: func(t *testcase.T) []string { return []string{} }}
testcase.Append(t, listVar, `foo`, `bar`, `baz`)
assert.Must(t).Equal([]string{`foo`, `bar`, `baz`}, listVar.Get(t))
})
}
func TestVar_Get_concurrentInit_initOnlyOnce(t *testing.T) {
s := testcase.NewSpec(t)
var (
mutex sync.Mutex
counter int
variable = testcase.Let(s, func(t *testcase.T) interface{} {
mutex.Lock()
counter++
mutex.Unlock()
return t.Random.Int()
})
)
s.Test(``, func(t *testcase.T) {
blk := func() { _ = variable.Get(t) }
var blks []func()
for i := 0; i < 42; i++ {
blks = append(blks, blk)
}
testcase.Race(blk, blk, blks...)
assert.Must(t).Equal(1, counter)
})
}
func TestVar_Get_race(t *testing.T) {
var (
s = testcase.NewSpec(t)
a = testcase.Let(s, func(t *testcase.T) int { return t.Random.Int() })
b = testcase.Let(s, func(t *testcase.T) int { return t.Random.Int() })
c = testcase.Let(s, func(t *testcase.T) int { return b.Get(t) })
subject = func(t *testcase.T) int { return a.Get(t) + c.Get(t) }
)
s.Test(``, func(t *testcase.T) {
blk := func() { _ = subject(t) }
testcase.Race(blk, blk, blk)
})
}
func TestVar_Bind(t *testing.T) {
rnd := random.New(random.CryptoSeed{})
t.Run("bind eill ensure that the variable is bound to the specification", func(t *testing.T) {
s := testcase.NewSpec(t)
expected := rnd.Int()
var onLetRan bool
v := testcase.Var[int]{
ID: "variable", Init: func(t *testcase.T) int { return expected },
OnLet: func(s *testcase.Spec, self testcase.Var[int]) { onLetRan = true },
}
v2 := v.Bind(s)
assert.Must(t).Equal(v.ID, v2.ID)
s.Test(``, func(t *testcase.T) {
assert.Must(t).Equal(expected, v.Get(t))
})
s.Finish()
assert.True(t, onLetRan)
})
t.Run("bind will not overwrite a previous value assignment", func(t *testing.T) {
s := testcase.NewSpec(t)
expected := rnd.Int()
var onLetRan bool
v := testcase.Var[int]{
ID: "variable", Init: func(t *testcase.T) int { return rnd.Int() },
OnLet: func(s *testcase.Spec, self testcase.Var[int]) { onLetRan = true },
}
v.Let(s, func(s *testcase.T) int { return expected })
v = v.Bind(s)
s.Test(``, func(t *testcase.T) {
assert.Must(t).Equal(expected, v.Get(t))
})
s.Finish()
assert.True(t, onLetRan)
})
t.Run("bind will not overwrite a previous value assignment in an outer scope", func(t *testing.T) {
s := testcase.NewSpec(t)
expected := rnd.Int()
var onLetRan bool
v := testcase.Var[int]{
ID: "variable", Init: func(t *testcase.T) int { return rnd.Int() },
OnLet: func(s *testcase.Spec, self testcase.Var[int]) { onLetRan = true },
}
v.Let(s, func(s *testcase.T) int { return expected })
s.Context("", func(s *testcase.Spec) {
v = v.Bind(s)
s.Test(``, func(t *testcase.T) {
assert.Must(t).Equal(expected, v.Get(t))
})
})
s.Finish()
assert.True(t, onLetRan)
})
}
func TestVar_Before(t *testing.T) {
t.Run(`When var not bounded to the Spec, then it will execute on Var.Get`, func(t *testing.T) {
s := testcase.NewSpec(t)
executed := testcase.LetValue(s, false)
v := testcase.Var[int]{
ID: "variable",
Init: func(t *testcase.T) int {
return t.Random.Int()
},
Before: func(t *testcase.T, v testcase.Var[int]) { executed.Set(t, true) },
}
s.Test(``, func(t *testcase.T) {
assert.Must(t).True(!executed.Get(t))
_ = v.Get(t)
assert.Must(t).True(executed.Get(t))
})
})
t.Run(`When Var initialized by an other Var, Before can eager load the other variable on Var.Get`, func(t *testing.T) {
expected := random.New(random.CryptoSeed{}).Int()
var sbov, oth testcase.Var[int]
oth = testcase.Var[int]{ID: "other variable", Init: func(t *testcase.T) int {
sbov.Set(t, expected)
return 42
}}