-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathseed_test.go
68 lines (54 loc) · 1.56 KB
/
seed_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
package testcase_test
import (
"strconv"
"testing"
"go.llib.dev/testcase"
"go.llib.dev/testcase/assert"
"go.llib.dev/testcase/internal/doubles"
)
func TestSpec_seed_T_Random(t *testing.T) {
t.Run("random values are unique across the testing scenarios", func(t *testing.T) {
testcase.UnsetEnv(t, "TESTCASE_SEED")
assert.Equal(t, 4, len(runSeedScenarios(t)))
})
t.Run("random values are unique for each spec instance", func(t *testing.T) {
testcase.UnsetEnv(t, "TESTCASE_SEED")
assert.NotEqual(t, runSeedScenarios(t), runSeedScenarios(t))
})
t.Run("using the TESTCASE_SEED env variable allows us to get back the same random values", func(t *testing.T) {
testcase.SetEnv(t, "TESTCASE_SEED", "8426361600145010042")
tbWithDeterministicTestNames := func() *doubles.TB {
var offset int
return &doubles.TB{
StubNameFunc: func() string {
offset++
return strconv.Itoa(offset)
},
}
}
assert.Equal(t,
runSeedScenarios(tbWithDeterministicTestNames()),
runSeedScenarios(tbWithDeterministicTestNames()))
})
}
func runSeedScenarios(tb testing.TB) map[string]struct{} {
s := testcase.NewSpec(tb)
s.Sequential()
var values = make(map[string]struct{})
s.Test("", func(t *testcase.T) {
values[t.Random.UUID()] = struct{}{}
})
s.Test("", func(t *testcase.T) {
values[t.Random.UUID()] = struct{}{}
})
s.Context("", func(s *testcase.Spec) {
s.Test("", func(t *testcase.T) {
values[t.Random.UUID()] = struct{}{}
})
s.Test("", func(t *testcase.T) {
values[t.Random.UUID()] = struct{}{}
})
})
s.Finish()
return values
}