-
Notifications
You must be signed in to change notification settings - Fork 2
/
context_test.go
193 lines (170 loc) · 4.45 KB
/
context_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
package test_test
import (
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/tkrop/go-testing/mock"
"github.com/tkrop/go-testing/test"
)
// TestRun is testing the test context with single test cases running in
// parallel.
func TestRun(t *testing.T) {
t.Parallel()
for name, param := range testParams {
name, param := name, param
t.Run(name, test.Run(param.expect, func(t test.Test) {
ExecTest(t, param)
}))
}
}
// TestRunSeq is testing the test context with single test cases running in
// sequence.
func TestRunSeq(t *testing.T) {
t.Parallel()
for name, param := range testParams {
name, param := name, param
t.Run(name, test.RunSeq(param.expect, func(t test.Test) {
ExecTest(t, param)
}))
}
}
// TestTempDir is testing the test context creating temporary directory.
func TestTempDir(t *testing.T) {
t.Parallel()
t.Run("create", test.Run(test.Success, func(t test.Test) {
assert.NotEmpty(t, t.TempDir())
}))
}
// PanicParam is a test parameter type for testing the test context with panic
// cases.
type PanicParam struct {
parallel bool
before func(test.Test)
during func(test.Test)
expect mock.SetupFunc
}
// testPanicParams is a map of test parameters for testing the test context with
// panic cases.
var testPanicParams = map[string]PanicParam{
"setenv in run without parallel": {
during: func(t test.Test) {
t.Setenv("TESTING", "during")
assert.Equal(t, "during", os.Getenv("TESTING"))
},
},
"setenv in run with parallel": {
parallel: true,
during: func(t test.Test) {
t.Setenv("TESTING", "during")
assert.Equal(t, "during", os.Getenv("TESTING"))
},
expect: test.Panic("testing: t.Setenv called after t.Parallel;" +
" cannot set environment variables in parallel tests"),
},
"setenv before run without parallel": {
before: func(t test.Test) {
t.Setenv("TESTING", "before")
assert.Equal(t, "before", os.Getenv("TESTING"))
},
during: func(t test.Test) {
t.Setenv("TESTING", "during")
assert.Equal(t, "during", os.Getenv("TESTING"))
},
},
"setenv before run with parallel": {
parallel: true,
before: func(t test.Test) {
t.Setenv("TESTING", "before")
assert.Equal(t, "before", os.Getenv("TESTING"))
},
expect: test.Panic("testing: t.Parallel called after t.Setenv;" +
" cannot set environment variables in parallel tests"),
},
"swallow multiple parallel calls": {
during: func(t test.Test) {
t.Parallel()
t.Parallel()
},
},
}
// TestContextPanic is testing the test context with panic cases.
func TestContextPanic(t *testing.T) {
for name, param := range testPanicParams {
name, param := name, param
t.Run(name, test.RunSeq(test.Success, func(t test.Test) {
// Given
if param.before != nil {
mock.NewMocks(t).Expect(param.expect)
param.before(t)
}
// When
test.New(t, test.Success).Run(func(t test.Test) {
mock.NewMocks(t).Expect(param.expect)
param.during(t)
}, param.parallel)
}))
}
}
type TestDeadlineParam struct {
time, early, sleep time.Duration
expect mock.SetupFunc
failure test.Expect
}
var TestDeadlineParams = map[string]TestDeadlineParam{
"failed": {
time: 0,
early: 0,
sleep: time.Millisecond,
expect: test.Fatalf("finished regularly"),
failure: test.Failure,
},
"timeout": {
time: time.Millisecond,
early: 0,
sleep: time.Second,
expect: test.Fatalf("stopped by deadline"),
failure: test.Failure,
},
"early": {
time: 5 * time.Millisecond,
early: 4 * time.Millisecond,
sleep: 4 * time.Millisecond,
expect: test.Fatalf("stopped by deadline"),
failure: test.Failure,
},
"to-late": {
time: 5 * time.Millisecond,
early: 1 * time.Millisecond,
sleep: 1 * time.Millisecond,
expect: test.Fatalf("finished regularly"),
failure: test.Failure,
},
"parent": {
time: 0,
early: 0,
sleep: 12 * time.Millisecond,
expect: mock.Chain(
test.Fatalf("stopped by deadline"),
test.Errorf("Expected test to succeed but it failed: %s",
"TestDeadline/parent"),
),
failure: test.Failure,
},
}
func TestDeadline(t *testing.T) {
t.Parallel()
test.Map(t, TestDeadlineParams).
Timeout(0).StopEarly(0).
Run(func(t test.Test, param TestDeadlineParam) {
mock.NewMocks(t).Expect(param.expect)
test.New(t, !param.failure).
Timeout(param.time).StopEarly(param.early).
Run(func(t test.Test) {
// When
time.Sleep(param.sleep)
// Then
t.Fatalf("finished regularly")
}, !test.Parallel)
})
}