aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/onsi/ginkgo/internal/suite/suite_test.go
blob: fd2d11dc309b6dd71f51e2b665efd6e22e941398 (plain)
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
package suite_test

import (
	"bytes"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/ginkgo/internal/suite"
	. "github.com/onsi/gomega"

	"math/rand"
	"time"

	"github.com/onsi/ginkgo/config"
	"github.com/onsi/ginkgo/internal/codelocation"
	Failer "github.com/onsi/ginkgo/internal/failer"
	Writer "github.com/onsi/ginkgo/internal/writer"
	"github.com/onsi/ginkgo/reporters"
	"github.com/onsi/ginkgo/types"
)

var _ = Describe("Suite", func() {
	var (
		specSuite *Suite
		fakeT     *fakeTestingT
		fakeR     *reporters.FakeReporter
		writer    *Writer.FakeGinkgoWriter
		failer    *Failer.Failer
	)

	BeforeEach(func() {
		writer = Writer.NewFake()
		fakeT = &fakeTestingT{}
		fakeR = reporters.NewFakeReporter()
		failer = Failer.New()
		specSuite = New(failer)
	})

	Describe("running a suite", func() {
		var (
			runOrder             []string
			randomizeAllSpecs    bool
			randomSeed           int64
			focusString          string
			parallelNode         int
			parallelTotal        int
			runResult            bool
			hasProgrammaticFocus bool
		)

		var f = func(runText string) func() {
			return func() {
				runOrder = append(runOrder, runText)
			}
		}

		BeforeEach(func() {
			randomizeAllSpecs = false
			randomSeed = 11
			parallelNode = 1
			parallelTotal = 1
			focusString = ""

			runOrder = make([]string, 0)
			specSuite.SetBeforeSuiteNode(f("BeforeSuite"), codelocation.New(0), 0)
			specSuite.PushBeforeEachNode(f("top BE"), codelocation.New(0), 0)
			specSuite.PushJustBeforeEachNode(f("top JBE"), codelocation.New(0), 0)
			specSuite.PushAfterEachNode(f("top AE"), codelocation.New(0), 0)

			specSuite.PushContainerNode("container", func() {
				specSuite.PushBeforeEachNode(f("BE"), codelocation.New(0), 0)
				specSuite.PushJustBeforeEachNode(f("JBE"), codelocation.New(0), 0)
				specSuite.PushAfterEachNode(f("AE"), codelocation.New(0), 0)
				specSuite.PushItNode("it", f("IT"), types.FlagTypeNone, codelocation.New(0), 0)

				specSuite.PushContainerNode("inner container", func() {
					specSuite.PushItNode("inner it", f("inner IT"), types.FlagTypeNone, codelocation.New(0), 0)
				}, types.FlagTypeNone, codelocation.New(0))
			}, types.FlagTypeNone, codelocation.New(0))

			specSuite.PushContainerNode("container 2", func() {
				specSuite.PushBeforeEachNode(f("BE 2"), codelocation.New(0), 0)
				specSuite.PushItNode("it 2", f("IT 2"), types.FlagTypeNone, codelocation.New(0), 0)
			}, types.FlagTypeNone, codelocation.New(0))

			specSuite.PushItNode("top level it", f("top IT"), types.FlagTypeNone, codelocation.New(0), 0)

			specSuite.SetAfterSuiteNode(f("AfterSuite"), codelocation.New(0), 0)
		})

		JustBeforeEach(func() {
			runResult, hasProgrammaticFocus = specSuite.Run(fakeT, "suite description", []reporters.Reporter{fakeR}, writer, config.GinkgoConfigType{
				RandomSeed:        randomSeed,
				RandomizeAllSpecs: randomizeAllSpecs,
				FocusString:       focusString,
				ParallelNode:      parallelNode,
				ParallelTotal:     parallelTotal,
			})
		})

		It("provides the config and suite description to the reporter", func() {
			Ω(fakeR.Config.RandomSeed).Should(Equal(int64(randomSeed)))
			Ω(fakeR.Config.RandomizeAllSpecs).Should(Equal(randomizeAllSpecs))
			Ω(fakeR.BeginSummary.SuiteDescription).Should(Equal("suite description"))
		})

		It("reports that the BeforeSuite node ran", func() {
			Ω(fakeR.BeforeSuiteSummary).ShouldNot(BeNil())
		})

		It("reports that the AfterSuite node ran", func() {
			Ω(fakeR.AfterSuiteSummary).ShouldNot(BeNil())
		})

		It("provides information about the current test", func() {
			description := CurrentGinkgoTestDescription()
			Ω(description.ComponentTexts).Should(Equal([]string{"Suite", "running a suite", "provides information about the current test"}))
			Ω(description.FullTestText).Should(Equal("Suite running a suite provides information about the current test"))
			Ω(description.TestText).Should(Equal("provides information about the current test"))
			Ω(description.IsMeasurement).Should(BeFalse())
			Ω(description.FileName).Should(ContainSubstring("suite_test.go"))
			Ω(description.LineNumber).Should(BeNumerically(">", 50))
			Ω(description.LineNumber).Should(BeNumerically("<", 150))
			Ω(description.Failed).Should(BeFalse())
			Ω(description.Duration).Should(BeNumerically(">", 0))
		})

		Measure("should run measurements", func(b Benchmarker) {
			r := rand.New(rand.NewSource(time.Now().UnixNano()))

			runtime := b.Time("sleeping", func() {
				sleepTime := time.Duration(r.Float64() * 0.01 * float64(time.Second))
				time.Sleep(sleepTime)
			})
			Ω(runtime.Seconds()).Should(BeNumerically("<=", 1))
			Ω(runtime.Seconds()).Should(BeNumerically(">=", 0))

			randomValue := r.Float64() * 10.0
			b.RecordValue("random value", randomValue)
			Ω(randomValue).Should(BeNumerically("<=", 10.0))
			Ω(randomValue).Should(BeNumerically(">=", 0.0))

			b.RecordValueWithPrecision("specific value", 123.4567, "ms", 2)
			b.RecordValueWithPrecision("specific value", 234.5678, "ms", 2)
		}, 10)

		It("creates a node hierarchy, converts it to a spec collection, and runs it", func() {
			Ω(runOrder).Should(Equal([]string{
				"BeforeSuite",
				"top BE", "BE", "top JBE", "JBE", "IT", "AE", "top AE",
				"top BE", "BE", "top JBE", "JBE", "inner IT", "AE", "top AE",
				"top BE", "BE 2", "top JBE", "IT 2", "top AE",
				"top BE", "top JBE", "top IT", "top AE",
				"AfterSuite",
			}))
		})
		Context("when in an AfterEach block", func() {
			AfterEach(func() {
				description := CurrentGinkgoTestDescription()
				Ω(description.IsMeasurement).Should(BeFalse())
				Ω(description.FileName).Should(ContainSubstring("suite_test.go"))
				Ω(description.Failed).Should(BeFalse())
				Ω(description.Duration).Should(BeNumerically(">", 0))
			})

			It("still provides information about the current test", func() {
				Ω(true).To(BeTrue())
			})
		})

		Context("when told to randomize all specs", func() {
			BeforeEach(func() {
				randomizeAllSpecs = true
			})

			It("does", func() {
				Ω(runOrder).Should(Equal([]string{
					"BeforeSuite",
					"top BE", "top JBE", "top IT", "top AE",
					"top BE", "BE", "top JBE", "JBE", "inner IT", "AE", "top AE",
					"top BE", "BE", "top JBE", "JBE", "IT", "AE", "top AE",
					"top BE", "BE 2", "top JBE", "IT 2", "top AE",
					"AfterSuite",
				}))
			})
		})

		Context("when provided with a filter", func() {
			BeforeEach(func() {
				focusString = `inner|\d`
			})

			It("converts the filter to a regular expression and uses it to filter the running specs", func() {
				Ω(runOrder).Should(Equal([]string{
					"BeforeSuite",
					"top BE", "BE", "top JBE", "JBE", "inner IT", "AE", "top AE",
					"top BE", "BE 2", "top JBE", "IT 2", "top AE",
					"AfterSuite",
				}))
			})

			It("should not report a programmatic focus", func() {
				Ω(hasProgrammaticFocus).Should(BeFalse())
			})
		})

		Context("with a programatically focused spec", func() {
			BeforeEach(func() {
				specSuite.PushItNode("focused it", f("focused it"), types.FlagTypeFocused, codelocation.New(0), 0)

				specSuite.PushContainerNode("focused container", func() {
					specSuite.PushItNode("inner focused it", f("inner focused it"), types.FlagTypeFocused, codelocation.New(0), 0)
					specSuite.PushItNode("inner unfocused it", f("inner unfocused it"), types.FlagTypeNone, codelocation.New(0), 0)
				}, types.FlagTypeFocused, codelocation.New(0))

			})

			It("should only run the focused test, applying backpropagation to favor most deeply focused leaf nodes", func() {
				Ω(runOrder).Should(Equal([]string{
					"BeforeSuite",
					"top BE", "top JBE", "focused it", "top AE",
					"top BE", "top JBE", "inner focused it", "top AE",
					"AfterSuite",
				}))
			})

			It("should report a programmatic focus", func() {
				Ω(hasProgrammaticFocus).Should(BeTrue())
			})
		})

		Context("when the specs pass", func() {
			It("doesn't report a failure", func() {
				Ω(fakeT.didFail).Should(BeFalse())
			})

			It("should return true", func() {
				Ω(runResult).Should(BeTrue())
			})
		})

		Context("when a spec fails", func() {
			var location types.CodeLocation
			BeforeEach(func() {
				specSuite.PushItNode("top level it", func() {
					location = codelocation.New(0)
					failer.Fail("oops!", location)
				}, types.FlagTypeNone, codelocation.New(0), 0)
			})

			It("should return false", func() {
				Ω(runResult).Should(BeFalse())
			})

			It("reports a failure", func() {
				Ω(fakeT.didFail).Should(BeTrue())
			})

			It("generates the correct failure data", func() {
				Ω(fakeR.SpecSummaries[0].Failure.Message).Should(Equal("oops!"))
				Ω(fakeR.SpecSummaries[0].Failure.Location).Should(Equal(location))
			})
		})

		Context("when runnable nodes are nested within other runnable nodes", func() {
			Context("when an It is nested", func() {
				BeforeEach(func() {
					specSuite.PushItNode("top level it", func() {
						specSuite.PushItNode("nested it", f("oops"), types.FlagTypeNone, codelocation.New(0), 0)
					}, types.FlagTypeNone, codelocation.New(0), 0)
				})

				It("should fail", func() {
					Ω(fakeT.didFail).Should(BeTrue())
				})
			})

			Context("when a Measure is nested", func() {
				BeforeEach(func() {
					specSuite.PushItNode("top level it", func() {
						specSuite.PushMeasureNode("nested measure", func(Benchmarker) {}, types.FlagTypeNone, codelocation.New(0), 10)
					}, types.FlagTypeNone, codelocation.New(0), 0)
				})

				It("should fail", func() {
					Ω(fakeT.didFail).Should(BeTrue())
				})
			})

			Context("when a BeforeEach is nested", func() {
				BeforeEach(func() {
					specSuite.PushItNode("top level it", func() {
						specSuite.PushBeforeEachNode(f("nested bef"), codelocation.New(0), 0)
					}, types.FlagTypeNone, codelocation.New(0), 0)
				})

				It("should fail", func() {
					Ω(fakeT.didFail).Should(BeTrue())
				})
			})

			Context("when a JustBeforeEach is nested", func() {
				BeforeEach(func() {
					specSuite.PushItNode("top level it", func() {
						specSuite.PushJustBeforeEachNode(f("nested jbef"), codelocation.New(0), 0)
					}, types.FlagTypeNone, codelocation.New(0), 0)
				})

				It("should fail", func() {
					Ω(fakeT.didFail).Should(BeTrue())
				})
			})

			Context("when a AfterEach is nested", func() {
				BeforeEach(func() {
					specSuite.PushItNode("top level it", func() {
						specSuite.PushAfterEachNode(f("nested aft"), codelocation.New(0), 0)
					}, types.FlagTypeNone, codelocation.New(0), 0)
				})

				It("should fail", func() {
					Ω(fakeT.didFail).Should(BeTrue())
				})
			})
		})
	})

	Describe("BeforeSuite", func() {
		Context("when setting BeforeSuite more than once", func() {
			It("should panic", func() {
				specSuite.SetBeforeSuiteNode(func() {}, codelocation.New(0), 0)

				Ω(func() {
					specSuite.SetBeforeSuiteNode(func() {}, codelocation.New(0), 0)
				}).Should(Panic())

			})
		})
	})

	Describe("AfterSuite", func() {
		Context("when setting AfterSuite more than once", func() {
			It("should panic", func() {
				specSuite.SetAfterSuiteNode(func() {}, codelocation.New(0), 0)

				Ω(func() {
					specSuite.SetAfterSuiteNode(func() {}, codelocation.New(0), 0)
				}).Should(Panic())
			})
		})
	})

	Describe("By", func() {
		It("writes to the GinkgoWriter", func() {
			originalGinkgoWriter := GinkgoWriter
			buffer := &bytes.Buffer{}

			GinkgoWriter = buffer
			By("Saying Hello GinkgoWriter")
			GinkgoWriter = originalGinkgoWriter

			Ω(buffer.String()).Should(ContainSubstring("STEP"))
			Ω(buffer.String()).Should(ContainSubstring(": Saying Hello GinkgoWriter\n"))
		})

		It("calls the passed-in callback if present", func() {
			a := 0
			By("calling the callback", func() {
				a = 1
			})
			Ω(a).Should(Equal(1))
		})

		It("panics if there is more than one callback", func() {
			Ω(func() {
				By("registering more than one callback", func() {}, func() {})
			}).Should(Panic())
		})
	})

	Describe("GinkgoRandomSeed", func() {
		It("returns the current config's random seed", func() {
			Ω(GinkgoRandomSeed()).Should(Equal(config.GinkgoConfig.RandomSeed))
		})
	})
})