-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_collection.go
281 lines (229 loc) · 7.59 KB
/
example_collection.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
package ginkgo
import (
"github.com/onsi/ginkgo/config"
"github.com/onsi/ginkgo/types"
"math/rand"
"regexp"
"sort"
"time"
)
type exampleCollection struct {
t GinkgoTestingT
description string
examples []*example
exampleCountBeforeParallelization int
reporters []Reporter
startTime time.Time
suiteID string
runningExample *example
writer ginkgoWriterInterface
config config.GinkgoConfigType
}
func newExampleCollection(t GinkgoTestingT, description string, examples []*example, reporters []Reporter, writer ginkgoWriterInterface, config config.GinkgoConfigType) *exampleCollection {
collection := &exampleCollection{
t: t,
description: description,
examples: examples,
reporters: reporters,
writer: writer,
config: config,
suiteID: types.GenerateRandomID(),
exampleCountBeforeParallelization: len(examples),
}
collection.enumerateAndAssignExampleIndices()
r := rand.New(rand.NewSource(config.RandomSeed))
if config.RandomizeAllSpecs {
collection.shuffle(r)
}
if config.FocusString == "" && config.SkipString == "" {
collection.applyProgrammaticFocus()
} else {
collection.applyRegExpFocus(config.FocusString, config.SkipString)
}
if config.SkipMeasurements {
collection.skipMeasurements()
}
if config.ParallelTotal > 1 {
collection.trimForParallelization(config.ParallelTotal, config.ParallelNode)
}
return collection
}
func (collection *exampleCollection) enumerateAndAssignExampleIndices() {
for index, example := range collection.examples {
example.exampleIndex = index
}
}
func (collection *exampleCollection) applyProgrammaticFocus() {
hasFocusedTests := false
for _, example := range collection.examples {
if example.focused {
hasFocusedTests = true
break
}
}
if hasFocusedTests {
for _, example := range collection.examples {
if !example.focused {
example.skip()
}
}
}
}
func (collection *exampleCollection) applyRegExpFocus(focusString string, skipString string) {
for _, example := range collection.examples {
matchesFocus := true
matchesSkip := false
stringToMatch := collection.description + " " + example.concatenatedString()
if focusString != "" {
focusFilter := regexp.MustCompile(focusString)
matchesFocus = focusFilter.Match([]byte(stringToMatch))
}
if skipString != "" {
skipFilter := regexp.MustCompile(skipString)
matchesSkip = skipFilter.Match([]byte(stringToMatch))
}
if !matchesFocus || matchesSkip {
example.skip()
}
}
}
func (collection *exampleCollection) trimForParallelization(parallelTotal int, parallelNode int) {
startIndex, count := parallelizedIndexRange(len(collection.examples), parallelTotal, parallelNode)
if count == 0 {
collection.examples = make([]*example, 0)
} else {
collection.examples = collection.examples[startIndex : startIndex+count]
}
}
func (collection *exampleCollection) skipMeasurements() {
for _, example := range collection.examples {
if example.subjectComponentType() == types.ExampleComponentTypeMeasure {
example.skip()
}
}
}
func (collection *exampleCollection) shuffle(r *rand.Rand) {
sort.Sort(collection)
permutation := r.Perm(len(collection.examples))
shuffledExamples := make([]*example, len(collection.examples))
for i, j := range permutation {
shuffledExamples[i] = collection.examples[j]
}
collection.examples = shuffledExamples
}
func (collection *exampleCollection) run() bool {
collection.reportSuiteWillBegin()
suiteFailed := false
for _, example := range collection.examples {
collection.writer.Truncate()
collection.reportExampleWillRun(example)
if !example.skippedOrPending() {
collection.runningExample = example
example.run()
if example.failed() {
suiteFailed = true
collection.writer.DumpOut()
}
} else if example.pending() && collection.config.FailOnPending {
suiteFailed = true
}
collection.reportExampleDidComplete(example)
}
collection.reportSuiteDidEnd()
if suiteFailed {
collection.t.Fail()
}
return !suiteFailed
}
func (collection *exampleCollection) fail(failure failureData) {
if collection.runningExample != nil {
collection.runningExample.fail(failure)
}
}
func (collection *exampleCollection) currentGinkgoTestDescription() GinkgoTestDescription {
currentExample := collection.runningExample
if currentExample == nil {
return GinkgoTestDescription{}
}
return currentExample.ginkgoTestDescription()
}
func (collection *exampleCollection) reportSuiteWillBegin() {
collection.startTime = time.Now()
summary := collection.summary()
for _, reporter := range collection.reporters {
reporter.SpecSuiteWillBegin(collection.config, summary)
}
}
func (collection *exampleCollection) reportExampleWillRun(example *example) {
summary := example.summary(collection.suiteID)
for _, reporter := range collection.reporters {
reporter.ExampleWillRun(summary)
}
}
func (collection *exampleCollection) reportExampleDidComplete(example *example) {
summary := example.summary(collection.suiteID)
for _, reporter := range collection.reporters {
reporter.ExampleDidComplete(summary)
}
}
func (collection *exampleCollection) reportSuiteDidEnd() {
summary := collection.summary()
summary.RunTime = time.Since(collection.startTime)
for _, reporter := range collection.reporters {
reporter.SpecSuiteDidEnd(summary)
}
}
func (collection *exampleCollection) countExamplesSatisfying(filter func(ex *example) bool) (count int) {
count = 0
for _, example := range collection.examples {
if filter(example) {
count++
}
}
return count
}
func (collection *exampleCollection) summary() *types.SuiteSummary {
numberOfExamplesThatWillBeRun := collection.countExamplesSatisfying(func(ex *example) bool {
return !ex.skippedOrPending()
})
numberOfPendingExamples := collection.countExamplesSatisfying(func(ex *example) bool {
return ex.state == types.ExampleStatePending
})
numberOfSkippedExamples := collection.countExamplesSatisfying(func(ex *example) bool {
return ex.state == types.ExampleStateSkipped
})
numberOfPassedExamples := collection.countExamplesSatisfying(func(ex *example) bool {
return ex.state == types.ExampleStatePassed
})
numberOfFailedExamples := collection.countExamplesSatisfying(func(ex *example) bool {
return ex.failed()
})
success := true
if numberOfFailedExamples > 0 {
success = false
} else if numberOfPendingExamples > 0 && collection.config.FailOnPending {
success = false
}
return &types.SuiteSummary{
SuiteDescription: collection.description,
SuiteSucceeded: success,
SuiteID: collection.suiteID,
NumberOfExamplesBeforeParallelization: collection.exampleCountBeforeParallelization,
NumberOfTotalExamples: len(collection.examples),
NumberOfExamplesThatWillBeRun: numberOfExamplesThatWillBeRun,
NumberOfPendingExamples: numberOfPendingExamples,
NumberOfSkippedExamples: numberOfSkippedExamples,
NumberOfPassedExamples: numberOfPassedExamples,
NumberOfFailedExamples: numberOfFailedExamples,
}
}
//sort.Interface
func (collection *exampleCollection) Len() int {
return len(collection.examples)
}
func (collection *exampleCollection) Less(i, j int) bool {
return collection.examples[i].concatenatedString() < collection.examples[j].concatenatedString()
}
func (collection *exampleCollection) Swap(i, j int) {
collection.examples[i], collection.examples[j] = collection.examples[j], collection.examples[i]
}