-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathexamples_test.go
1592 lines (1293 loc) · 37.5 KB
/
examples_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 (
"context"
"database/sql"
"errors"
"fmt"
"math/rand"
"strings"
"testing"
"time"
"go.llib.dev/testcase/clock"
"go.llib.dev/testcase/clock/timecop"
"go.llib.dev/testcase"
"go.llib.dev/testcase/assert"
"go.llib.dev/testcase/faultinject"
"go.llib.dev/testcase/internal/doubles"
"go.llib.dev/testcase/internal/example/memory"
"go.llib.dev/testcase/internal/example/mydomain"
"go.llib.dev/testcase/internal/example/spechelper"
"go.llib.dev/testcase/random"
)
func ExampleSpec() {
var tb testing.TB
// spec do not use any global magic
// it is just a simple abstraction around testing.T#Context
// Basically you can easily can run it as you would any other go testCase
// -> `go run ./... -v -run "my/edge/case/nested/block/I/want/to/run/only"`
//
spec := testcase.NewSpec(tb)
// when you have no side effects in your testing suite,
// you can enable parallel execution.
// You can play parallel even from nested specs to apply parallel testing for that spec and below.
spec.Parallel()
// or
spec.NoSideEffect()
// testcase.variables are thread safe way of setting up complex contexts
// where some variable need to have different values for edge cases.
// and I usually work with in-memory implementation for certain shared specs,
// to make my testCase coverage run fast and still close to somewhat reality in terms of integration.
// and to me, it is a necessary thing to have "T#parallel" SpecOption safely available
var myType = func(t *testcase.T) *mydomain.MyUseCase {
return &mydomain.MyUseCase{}
}
// Each describe has a testing subject as an "act" function
spec.Describe(`IsLower`, func(s *testcase.Spec) {
var ( // inputs for the Act
input = testcase.Var[string]{ID: `input`}
)
act := func(t *testcase.T) bool {
return myType(t).IsLower(input.Get(t))
}
s.When(`input string has lower case characters`, func(s *testcase.Spec) {
input.Let(s, func(t *testcase.T) string {
return t.Random.StringNWithCharset(t.Random.Int(), strings.ToLower(random.CharsetAlpha()))
})
s.And(`the first character is capitalized`, func(s *testcase.Spec) {
// you can add more nesting for more concrete specifications,
// in each nested block, you work on a separate variable stack,
// so even if you overwrite something here,
// that has no effect outside of this scope
s.Before(func(t *testcase.T) {
upperCaseLetter := t.Random.StringNC(1, strings.ToUpper(random.CharsetAlpha()))
input.Set(t, upperCaseLetter+input.Get(t))
})
s.Then(`it will report false`, func(t *testcase.T) {
t.Must.True(act(t),
assert.Message(fmt.Sprintf(`it was expected that %q will be reported to be not lowercase`, input.Get(t))))
})
})
s.Then(`it will return true`, func(t *testcase.T) {
t.Must.True(act(t),
assert.Message(fmt.Sprintf(`it was expected that the %q will re reported to be lowercase`, input.Get(t))))
})
})
s.When(`input string has upcase case characters`, func(s *testcase.Spec) {
input.Let(s, func(t *testcase.T) string {
return t.Random.StringNWithCharset(t.Random.Int(), strings.ToUpper(random.CharsetAlpha()))
})
s.Then(`it will return false`, func(t *testcase.T) {
t.Must.False(act(t))
})
})
})
}
func Example_assertWaiterWait() {
w := assert.Waiter{WaitDuration: time.Millisecond}
w.Wait() // will wait 1 millisecond and attempt to schedule other go routines
}
func Example_assertWaiterWhile() {
w := assert.Waiter{
WaitDuration: time.Millisecond,
Timeout: time.Second,
}
// will attempt to wait until condition returns false.
// The maximum time it is willing to wait is equal to the wait timeout duration.
w.While(func() bool {
return rand.Intn(1) == 0
})
}
func ExampleVar() {
var t *testing.T
s := testcase.NewSpec(t)
var (
storage = testcase.Let[mydomain.Storage](s, func(t *testcase.T) mydomain.Storage {
return memory.NewStorage()
})
subject = testcase.Let(s, func(t *testcase.T) *mydomain.MyUseCase {
return &mydomain.MyUseCase{Storage: storage.Get(t)}
})
)
s.Describe(`#MyFunc`, func(s *testcase.Spec) {
var subject = func(t *testcase.T) {
// after GO2 this will be replaced with concrete Types instead of interface{}
subject.Get(t).MyFunc()
}
s.Then(`do some testCase`, func(t *testcase.T) {
subject(t) // act
// assertions here.
})
// ...
// other cases with resource xy state change
})
}
func ExampleVar_Get() {
var t *testing.T
s := testcase.NewSpec(t)
value := testcase.Let(s, func(t *testcase.T) interface{} {
return 42
})
s.Test(`some testCase`, func(t *testcase.T) {
_ = value.Get(t) // -> 42
})
}
func ExampleVar_Set() {
var t *testing.T
s := testcase.NewSpec(t)
value := testcase.Let(s, func(t *testcase.T) interface{} {
return 42
})
s.Before(func(t *testcase.T) {
value.Set(t, 24)
})
s.Test(`some testCase`, func(t *testcase.T) {
_ = value.Get(t) // -> 24
})
}
func ExampleVar_Let() {
var t *testing.T
s := testcase.NewSpec(t)
value := testcase.Var[int]{
ID: `the variable group`,
Init: func(t *testcase.T) int {
return 42
},
}
value.Let(s, nil)
s.Test(`some testCase`, func(t *testcase.T) {
_ = value.Get(t) // -> 42
})
}
func ExampleVar_Let_valueDefinedAtTestingContextScope() {
var t *testing.T
s := testcase.NewSpec(t)
value := testcase.Var[int]{ID: `the variable group`}
value.Let(s, func(t *testcase.T) int {
return 42
})
s.Test(`some testCase`, func(t *testcase.T) {
_ = value.Get(t) // -> 42
})
}
func ExampleVar_LetValue() {
var t *testing.T
s := testcase.NewSpec(t)
value := testcase.Var[int]{ID: `the variable group`}
value.LetValue(s, 42)
s.Test(`some testCase`, func(t *testcase.T) {
_ = value.Get(t) // -> 42
})
}
func ExampleVar_EagerLoading() {
var t *testing.T
s := testcase.NewSpec(t)
value := testcase.Let(s, func(t *testcase.T) interface{} {
return 42
})
// will be loaded early on, before the test case block reached.
// This can be useful when you want to have variables,
// that also must be present in some sort of attached resource,
// and as part of the constructor, you want to save it.
// So when the testCase block is reached, the entity is already present in the resource.
value.EagerLoading(s)
s.Test(`some testCase`, func(t *testcase.T) {
_ = value.Get(t) // -> 42
// value returned from cache instead of triggering first time initialization.
})
}
func ExampleVar_Let_eagerLoading() {
var t *testing.T
s := testcase.NewSpec(t)
value := testcase.Var[int]{ID: `value`}
value.Let(s, func(t *testcase.T) int {
return 42
}).EagerLoading(s)
s.Test(`some testCase`, func(t *testcase.T) {
_ = value.Get(t) // -> 42
// value returned from cache instead of triggering first time initialization.
})
}
func ExampleVar_LetValue_eagerLoading() {
var t *testing.T
s := testcase.NewSpec(t)
value := testcase.Var[int]{ID: `value`}
value.LetValue(s, 42).EagerLoading(s)
s.Test(`some testCase`, func(t *testcase.T) {
_ = value.Get(t) // -> 42
// value returned from cache instead of triggering first time initialization.
})
}
func ExampleVar_init() {
var tb testing.TB
s := testcase.NewSpec(tb)
value := testcase.Var[int]{
ID: `value`,
Init: func(t *testcase.T) int {
return 42
},
}
s.Test(`some testCase`, func(t *testcase.T) {
_ = value.Get(t) // 42
})
}
func ExampleVar_onLet() {
// package spechelper
var db = testcase.Var[*sql.DB]{
ID: `db`,
Init: func(t *testcase.T) *sql.DB {
db, err := sql.Open(`driver`, `dataSourceName`)
if err != nil {
t.Fatal(err.Error())
}
return db
},
OnLet: func(s *testcase.Spec, _ testcase.Var[*sql.DB]) {
s.Tag(`database`)
s.Sequential()
},
}
var tb testing.TB
s := testcase.NewSpec(tb)
db.Let(s, nil)
s.Test(`some testCase`, func(t *testcase.T) {
_ = db.Get(t)
t.HasTag(`database`) // true
})
}
func ExampleVar_Bind() {
var tb testing.TB
s := testcase.NewSpec(tb)
v := testcase.Var[int]{ID: "myvar", Init: func(t *testcase.T) int { return 42 }}
v.Bind(s)
s.Test(``, func(t *testcase.T) {
_ = v.Get(t) // -> 42
})
}
func ExampleVar_before() {
var tb testing.TB
s := testcase.NewSpec(tb)
v := testcase.Var[int]{
ID: "myvar",
Init: func(t *testcase.T) int { return 42 },
Before: func(t *testcase.T, v testcase.Var[int]) {
t.Logf(`I'm from the Var.Before block, and the value: %#v`, v.Get(t))
},
}
s.Test(``, func(t *testcase.T) {
_ = v.Get(t)
// log: I'm from the Var.Before block
// -> 42
})
}
func ExampleSkipUntil() {
var t *testing.T
// make tests skip until the given day is reached,
// then make the tests fail.
// This helps to commit code which still work in progress.
testcase.SkipUntil(t, 2020, 01, 01, 12)
}
func ExampleT_SkipUntil() {
var t *testing.T
s := testcase.NewSpec(t)
s.Test(`will be skipped`, func(t *testcase.T) {
// make tests skip until the given day is reached,
// then make the tests fail.
// This helps to commit code which still work in progress.
t.SkipUntil(2020, 01, 01, 12)
})
s.Test(`will not be skipped`, func(t *testcase.T) {})
}
func ExampleT_random() {
var tb testing.TB
s := testcase.NewSpec(tb)
s.Test(``, func(t *testcase.T) {
_ = t.Random.Int()
_ = t.Random.IntBetween(0, 42)
_ = t.Random.IntN(42)
_ = t.Random.Float32()
_ = t.Random.Float64()
_ = t.Random.String()
_ = t.Random.StringN(42)
_ = t.Random.StringNWithCharset(42, "abc")
_ = t.Random.Bool()
_ = t.Random.Time()
_ = t.Random.TimeN(time.Now(), 0, 4, 2)
_ = t.Random.TimeBetween(time.Now().Add(-1*time.Hour), time.Now().Add(time.Hour))
_ = t.Random.SliceElement([]int{1, 2, 3}).(int)
})
}
func ExampleT_HasTag() {
var t *testing.T
var s = testcase.NewSpec(t)
type DB interface { // header interface in supplier pkg
QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
}
testcase.Let(s, func(t *testcase.T) DB {
db, err := sql.Open(`driverName`, `dataSourceName`)
t.Must.Nil(err)
if t.HasTag(`black box`) {
// tests with black box use http testCase server or similar things and high level tx management not maintainable.
t.Defer(db.Close)
return db
}
tx, err := db.BeginTx(context.Background(), nil)
t.Must.Nil(err)
t.Defer(tx.Rollback)
return tx
})
}
func ExampleT_Eventually() {
var tb testing.TB
s := testcase.NewSpec(tb)
s.Test(``, func(t *testcase.T) {
// Eventually this will pass eventually
t.Eventually(func(it *testcase.T) {
it.Must.True(t.Random.Bool())
})
})
}
func ExampleT_Defer_withArgs() {
var t *testing.T
s := testcase.NewSpec(t)
something := testcase.Let(s, func(t *testcase.T) *ExampleDeferTeardownWithArgs {
ptr := &ExampleDeferTeardownWithArgs{}
// T#Defer arguments copied upon pass by value
// and then passed to the function during the execution of the deferred function call.
//
// This is ideal for situations where you need to guarantee that a value cannot be muta
t.Defer(ptr.SomeTeardownWithArg, `Hello, World!`)
return ptr
})
s.Test(`a simple test case`, func(t *testcase.T) {
entity := something.Get(t)
entity.DoSomething()
})
}
type ExampleDeferTeardownWithArgs struct{}
func (*ExampleDeferTeardownWithArgs) SomeTeardownWithArg(_ string) {}
func (*ExampleDeferTeardownWithArgs) DoSomething() {}
func ExampleT_Defer() {
var t *testing.T
s := testcase.NewSpec(t)
// db for example is something that needs to defer an action after the testCase run
db := testcase.Let(s, func(t *testcase.T) *sql.DB {
db, err := sql.Open(`driverName`, `dataSourceName`)
// asserting error here with the *testcase.T ensure that the testCase will don't have some spooky failure.
t.Must.Nil(err)
// db.Close() will be called after the current test case reach the teardown hooks
t.Defer(db.Close)
// check if connection is OK
t.Must.Nil(db.Ping())
// return the verified db instance for the caller
// this db instance will be memorized during the runtime of the test case
return db
})
s.Test(`a simple test case`, func(t *testcase.T) {
db := db.Get(t)
t.Must.Nil(db.Ping()) // just to do something with it.
})
}
func ExampleT_must() {
var tb testing.TB
s := testcase.NewSpec(tb)
s.Test(``, func(t *testcase.T) {
// failed test will stop with FailNow
t.Must.Equal(1, 1, "must be equal")
})
}
func ExampleT_should() {
var tb testing.TB
s := testcase.NewSpec(tb)
s.Test(``, func(t *testcase.T) {
// failed test will proceed, but mart the test failed
t.Should.Equal(1, 1, "should be equal")
})
}
func ExampleStubTB_testingATestHelper() {
stub := &doubles.TB{}
stub.Log("hello", "world")
fmt.Println(stub.Logs.String())
myTestHelper := func(tb testing.TB) {
tb.FailNow()
}
var tb testing.TB
assert.Must(tb).Panic(func() {
myTestHelper(stub)
})
assert.Must(tb).True(stub.IsFailed)
}
func ExampleSpec_withBenchmark() {
var b *testing.B
s := testcase.NewSpec(b)
myType := func(t *testcase.T) *mydomain.MyUseCase {
return &mydomain.MyUseCase{}
}
s.When(`something`, func(s *testcase.Spec) {
s.Before(func(t *testcase.T) {
t.Log(`setup`)
})
s.Then(`this benchmark block will be executed by *testing.B.N times`, func(t *testcase.T) {
myType(t).IsLower(`Hello, World!`)
})
})
}
func ExampleSpec_When() {
var t *testing.T
s := testcase.NewSpec(t)
var (
myType = func(t *testcase.T) *mydomain.MyUseCase { return &mydomain.MyUseCase{} }
input = testcase.Var[string]{ID: `input`}
subject = func(t *testcase.T) bool { return myType(t).IsLower(input.Get(t)) }
)
s.When(`input has only upcase letter`, func(s *testcase.Spec) {
input.LetValue(s, "UPPER")
s.Then(`it will be false`, func(t *testcase.T) {
t.Must.True(!subject(t))
})
})
s.When(`input has only lowercase letter`, func(s *testcase.Spec) {
input.LetValue(s, "lower")
s.Then(`it will be true`, func(t *testcase.T) {
t.Must.True(subject(t))
})
})
}
func ExampleSpec_Then() {
var t *testing.T
s := testcase.NewSpec(t)
s.Then(`it is expected.... so this is the testCase description here`, func(t *testcase.T) {
// ...
})
}
func ExampleSpec_Test() {
var t *testing.T
s := testcase.NewSpec(t)
s.Test(`my testCase description`, func(t *testcase.T) {
// ...
})
}
func ExampleSpec_Tag() {
// example usage:
// TESTCASE_TAG_INCLUDE='E2E' go testCase ./...
// TESTCASE_TAG_EXCLUDE='E2E' go testCase ./...
// TESTCASE_TAG_INCLUDE='E2E' TESTCASE_TAG_EXCLUDE='list,of,excluded,tags' go testCase ./...
//
var t *testing.T
s := testcase.NewSpec(t)
s.Context(`E2E`, func(s *testcase.Spec) {
// by tagging the spec spec, we can filter tests orderingOutput later in our CI/CD pipeline.
// A comma separated list can be set with TESTCASE_TAG_INCLUDE env variable to filter down to tests with certain tags.
// And/Or a comma separated list can be provided with TESTCASE_TAG_EXCLUDE to exclude tests tagged with certain tags.
s.Tag(`E2E`)
s.Test(`some E2E testCase`, func(t *testcase.T) {
// ...
})
})
}
func ExampleSpec_SkipBenchmark() {
var b *testing.B
s := testcase.NewSpec(b)
s.SkipBenchmark()
s.Test(`this will be skipped during benchmark`, func(t *testcase.T) {})
s.Context(`some spec`, func(s *testcase.Spec) {
s.Test(`this as well`, func(t *testcase.T) {})
})
}
func ExampleSpec_SkipBenchmark_scopedWithContext() {
var b *testing.B
s := testcase.NewSpec(b)
s.When(`rainy path`, func(s *testcase.Spec) {
s.SkipBenchmark()
s.Test(`will be skipped during benchmark`, func(t *testcase.T) {})
})
s.Context(`happy path`, func(s *testcase.Spec) {
s.Test(`this will run as benchmark`, func(t *testcase.T) {})
})
}
func ExampleSpec_Sequential() {
var t *testing.T
s := testcase.NewSpec(t)
s.Sequential() // tells the specs to run list test case in sequence
s.Test(`this will run in sequence`, func(t *testcase.T) {})
s.Context(`some spec`, func(s *testcase.Spec) {
s.Test(`this run in sequence`, func(t *testcase.T) {})
s.Test(`this run in sequence`, func(t *testcase.T) {})
})
}
func ExampleSpec_Sequential_scopedWithContext() {
var t *testing.T
s := testcase.NewSpec(t)
s.Parallel() // on top level, spec marked as parallel
s.Context(`spec marked sequential`, func(s *testcase.Spec) {
s.Sequential() // but in subcontext the testCase marked as sequential
s.Test(`this run in sequence`, func(t *testcase.T) {})
})
s.Context(`spec that inherit parallel flag`, func(s *testcase.Spec) {
s.Test(`this will run in parallel`, func(t *testcase.T) {})
})
}
func ExampleSpec_HasSideEffect() {
var t *testing.T
s := testcase.NewSpec(t)
// this mark the testCase to contain side effects.
// this forbids any parallel testCase execution to avoid retry tests.
//
// Under the hood this is a syntax sugar for Sequential
s.HasSideEffect()
s.Test(`this will run in sequence`, func(t *testcase.T) {})
s.Context(`some spec`, func(s *testcase.Spec) {
s.Test(`this run in sequence`, func(t *testcase.T) {})
s.Test(`this run in sequence`, func(t *testcase.T) {})
})
}
func ExampleSpec_Sequential_globalVar() {
var t *testing.T
s := testcase.NewSpec(t)
// might or might not allow parallel execution
// It depends on the
storage := spechelper.Storage.Bind(s)
// Tells that the subject of this specification should be software side effect free on its own.
s.NoSideEffect()
s.Test("will only run parallel if no dependency has side effect", func(t *testcase.T) {
t.Logf("%#v", storage.Get(t))
})
}
func ExampleSpec_Parallel() {
var t *testing.T
s := testcase.NewSpec(t)
s.Parallel() // tells the specs to run list test case in parallel
s.Test(`this will run in parallel`, func(t *testcase.T) {})
s.Context(`some spec`, func(s *testcase.Spec) {
s.Test(`this run in parallel`, func(t *testcase.T) {})
s.Test(`this run in parallel`, func(t *testcase.T) {})
})
}
func ExampleSpec_Parallel_scopedWithContext() {
var t *testing.T
s := testcase.NewSpec(t)
s.Context(`spec marked parallel`, func(s *testcase.Spec) {
s.Parallel()
s.Test(`this run in parallel`, func(t *testcase.T) {})
})
s.Context(`spec without parallel`, func(s *testcase.Spec) {
s.Test(`this will run in sequence`, func(t *testcase.T) {})
})
}
func ExampleSpec_NoSideEffect() {
var t *testing.T
s := testcase.NewSpec(t)
// this is an idiom to express that the subject in the tests here are not expected to have any side-effect.
// this means they are safe to be executed in parallel.
s.NoSideEffect()
s.Test(`this will run in parallel`, func(t *testcase.T) {})
s.Context(`some spec`, func(s *testcase.Spec) {
s.Test(`this run in parallel`, func(t *testcase.T) {})
s.Test(`this run in parallel`, func(t *testcase.T) {})
})
}
func ExampleSpec_LetValue() {
var t *testing.T
s := testcase.NewSpec(t)
variable := testcase.LetValue(s, "value")
s.Then(`test case`, func(t *testcase.T) {
t.Log(variable.Get(t)) // -> "value"
})
}
func ExampleSpec_LetValue_usageWithinNestedScope() {
var t *testing.T
s := testcase.NewSpec(t)
var myType = func(t *testcase.T) *mydomain.MyUseCase { return &mydomain.MyUseCase{} }
s.Describe(`#IsLower`, func(s *testcase.Spec) {
var (
input = testcase.Var[string]{ID: `input`}
subject = func(t *testcase.T) bool {
return myType(t).IsLower(input.Get(t))
}
)
s.When(`input characters are list lowercase`, func(s *testcase.Spec) {
testcase.LetValue(s, "list lowercase")
// or
input.LetValue(s, "list lowercase")
s.Then(`it will report true`, func(t *testcase.T) {
t.Must.True(subject(t))
})
})
s.When(`input is a capitalized`, func(s *testcase.Spec) {
testcase.LetValue(s, "Capitalized")
// or
input.LetValue(s, "Capitalized")
s.Then(`it will report false`, func(t *testcase.T) {
t.Must.True(!subject(t))
})
})
})
}
func ExampleSpec_Let() {
var t *testing.T
s := testcase.NewSpec(t)
myTestVar := testcase.Let(s, func(t *testcase.T) interface{} {
return "value that needs complex construction or can be mutated"
})
s.Then(`test case`, func(t *testcase.T) {
t.Log(myTestVar.Get(t)) // -> returns the value set in the current spec spec for MyTestVar
})
}
func ExampleSpec_Let_eagerLoading() {
var t *testing.T
s := testcase.NewSpec(t)
myTestVar := testcase.Let(s, func(t *testcase.T) interface{} {
return "value that will be eager loaded before the testCase/then block reached"
}).EagerLoading(s)
// EagerLoading will ensure that the value of this Spec Var will be evaluated during the preparation of the testCase.
s.Then(`test case`, func(t *testcase.T) {
t.Log(myTestVar.Get(t))
})
}
type SupplierWithDBDependency struct {
DB interface {
QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
}
}
func (s SupplierWithDBDependency) DoSomething(ctx context.Context) error {
rows, err := s.DB.QueryContext(ctx, `SELECT 1 = 1`)
if err != nil {
return err
}
return rows.Close()
}
func ExampleSpec_Let_sqlDB() {
var t *testing.T
s := testcase.NewSpec(t)
var (
tx = testcase.Let(s, func(t *testcase.T) *sql.Tx {
// it is advised to use a persistent db connection between multiple specification runs,
// because otherwise `go testCase -count $times` can receive random connection failures.
tx, err := getDBConnection(t).Begin()
if err != nil {
t.Fatal(err.Error())
}
// testcase.T#Defer will execute the received function after the current testCase edge case
// where the `tx` testCase variable were accessed.
t.Defer(tx.Rollback)
return tx
})
supplier = testcase.Let(s, func(t *testcase.T) SupplierWithDBDependency {
return SupplierWithDBDependency{DB: tx.Get(t)}
})
)
s.Describe(`#DoSomething`, func(s *testcase.Spec) {
var (
ctx = testcase.Let(s, func(t *testcase.T) context.Context {
return context.Background()
})
subject = func(t *testcase.T) error {
return supplier.Get(t).DoSomething(ctx.Get(t))
}
)
s.When(`...`, func(s *testcase.Spec) {
s.Before(func(t *testcase.T) {
//...
})
s.Then(`...`, func(t *testcase.T) {
t.Must.Nil(subject(t))
})
})
})
}
func getDBConnection(_ testing.TB) *sql.DB {
// logic to retrieve cached db connection in the testing environment
return nil
}
func ExampleSpec_Let_usageWithinNestedScope() {
var t *testing.T
s := testcase.NewSpec(t)
var myType = func(t *testcase.T) *mydomain.MyUseCase { return &mydomain.MyUseCase{} }
s.Describe(`#IsLower`, func(s *testcase.Spec) {
var (
input = testcase.Var[string]{ID: `input`}
subject = func(t *testcase.T) bool {
return myType(t).IsLower(input.Get(t))
}
)
s.When(`input characters are list lowercase`, func(s *testcase.Spec) {
testcase.Let(s, func(t *testcase.T) interface{} {
return "list lowercase"
})
// or
input.Let(s, func(t *testcase.T) string {
return "list lowercase"
})
s.Then(`it will report true`, func(t *testcase.T) {
t.Must.True(subject(t))
})
})
s.When(`input is a capitalized`, func(s *testcase.Spec) {
testcase.Let(s, func(t *testcase.T) interface{} {
return "Capitalized"
})
// or
input.Let(s, func(t *testcase.T) string {
return "Capitalized"
})
s.Then(`it will report false`, func(t *testcase.T) {
t.Must.True(!subject(t))
})
})
})
}
func ExampleSpec_Let_testingDouble() {
var t *testing.T
s := testcase.NewSpec(t)
stubTB := testcase.Let(s, func(t *testcase.T) *doubles.TB {
stub := &doubles.TB{}
t.Defer(stub.Finish)
return stub
})
s.When(`some scope where double should behave in a certain way`, func(s *testcase.Spec) {
s.Before(func(t *testcase.T) {
stubTB.Get(t).StubName = "my stubbed name"
})
s.Then(`double will be available in every test case and finishNow called afterwards`, func(t *testcase.T) {
// ...
})
})
}
func ExampleSpec_Before() {
var t *testing.T
s := testcase.NewSpec(t)
s.Before(func(t *testcase.T) {
// this will run before the test cases.
})
}
func ExampleSpec_After() {
var t *testing.T
s := testcase.NewSpec(t)
s.After(func(t *testcase.T) {
// this will run after the test cases.
// this hook applied to this scope and anything that is nested from here.
// hooks can be stacked with each call.
})
}
func ExampleSpec_Around() {
var t *testing.T
s := testcase.NewSpec(t)
s.Around(func(t *testcase.T) func() {
// this will run before the test cases
// this hook applied to this scope and anything that is nested from here.
// hooks can be stacked with each call
return func() {
// The content of the returned func will be deferred to run after the test cases.
}
})
}
func ExampleSpec_BeforeAll() {
var t *testing.T
s := testcase.NewSpec(t)
s.BeforeAll(func(tb testing.TB) {
// this will run once before every test cases.
})
}
func ExampleSpec_Describe() {
var t *testing.T
s := testcase.NewSpec(t)
myType := testcase.Let(s, func(t *testcase.T) *mydomain.MyUseCase {
return &mydomain.MyUseCase{}
})
// Describe description points orderingOutput the subject of the tests
s.Describe(`#IsLower`, func(s *testcase.Spec) {
var (
input = testcase.Var[string]{ID: `input`}
subject = func(t *testcase.T) bool {
// subject should represent what will be tested in the describe block
return myType.Get(t).IsLower(input.Get(t))
}
)
s.Test(``, func(t *testcase.T) { subject(t) })
})
}