forked from getgauge/gauge
/
specDetails_test.go
466 lines (373 loc) · 13.2 KB
/
specDetails_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
package main
import (
"github.com/getgauge/gauge/config"
"github.com/getgauge/gauge/util"
. "gopkg.in/check.v1"
"io/ioutil"
"os"
"path/filepath"
)
type MySuite struct {
specsDir string
projectDir string
}
func (s *MySuite) SetUpTest(c *C) {
s.projectDir, _ = ioutil.TempDir("", "gaugeTest")
s.specsDir, _ = util.CreateDirIn(s.projectDir, "specs")
config.ProjectRoot = s.projectDir
}
func (s *MySuite) TearDownTest(c *C) {
os.RemoveAll(s.projectDir)
}
func (s *MySuite) TestGetAllStepsFromSpecs(c *C) {
data := []byte(`Specification Heading
=====================
Scenario 1
----------
* say hello
* say "hello" to me
`)
specFile, err := util.CreateFileIn(s.specsDir, "Spec1.spec", data)
c.Assert(err, Equals, nil)
specInfoGatherer := new(specInfoGatherer)
specInfoGatherer.findAllStepsFromSpecs()
c.Assert(len(specInfoGatherer.availableSpecs), Equals, 1)
c.Assert(len(specInfoGatherer.fileToStepsMap), Equals, 1)
steps, ok := specInfoGatherer.fileToStepsMap[specFile]
c.Assert(ok, Equals, true)
c.Assert(len(steps), Equals, 2)
c.Assert(steps[0].lineText, Equals, "say hello")
c.Assert(steps[1].lineText, Equals, "say \"hello\" to me")
}
func (s *MySuite) TestRemoveSpec(c *C) {
data := []byte(`Specification Heading
=====================
Scenario 1
----------
* say hello
* say "hello" to me
`)
data1 := []byte(`Specification Heading2
=====================
Scenario 1
----------
* say hello 1
* say "hello" to me 1
`)
specFile1, err := util.CreateFileIn(s.specsDir, "Spec1.spec", data)
specFile2, err := util.CreateFileIn(s.specsDir, "Spec2.spec", data1)
c.Assert(err, Equals, nil)
specInfoGatherer := new(specInfoGatherer)
specInfoGatherer.findAllStepsFromSpecs()
c.Assert(len(specInfoGatherer.fileToStepsMap), Equals, 2)
c.Assert(len(specInfoGatherer.availableSpecs), Equals, 2)
steps, ok := specInfoGatherer.fileToStepsMap[specFile1]
c.Assert(ok, Equals, true)
c.Assert(len(steps), Equals, 2)
c.Assert(steps[0].lineText, Equals, "say hello")
c.Assert(steps[1].lineText, Equals, "say \"hello\" to me")
steps, ok = specInfoGatherer.fileToStepsMap[specFile2]
c.Assert(ok, Equals, true)
c.Assert(len(steps), Equals, 2)
c.Assert(steps[0].lineText, Equals, "say hello 1")
c.Assert(steps[1].lineText, Equals, "say \"hello\" to me 1")
specInfoGatherer.removeSpec(filepath.Join(s.specsDir, "Spec1.spec"))
c.Assert(len(specInfoGatherer.fileToStepsMap), Equals, 1)
c.Assert(len(specInfoGatherer.availableSpecs), Equals, 2)
steps, ok = specInfoGatherer.fileToStepsMap[specFile2]
c.Assert(ok, Equals, true)
allSteps := specInfoGatherer.getAvailableSteps()
c.Assert(len(allSteps), Equals, 2)
}
func (s *MySuite) TestRemoveSpecWithCommonSteps(c *C) {
data := []byte(`Specification Heading
=====================
Scenario 1
----------
* say hello
* say "hello" to me
* a common step
`)
data1 := []byte(`Specification Heading2
=====================
Scenario 1
----------
* say hello 1
* say "hello" to me 1
* a common step
`)
util.CreateFileIn(s.specsDir, "Spec1.spec", data)
util.CreateFileIn(s.specsDir, "Spec2.spec", data1)
specInfoGatherer := new(specInfoGatherer)
specInfoGatherer.findAllStepsFromSpecs()
specInfoGatherer.updateAllStepsList()
c.Assert(len(specInfoGatherer.fileToStepsMap), Equals, 2)
c.Assert(len(specInfoGatherer.availableSpecs), Equals, 2)
allSteps := specInfoGatherer.getAvailableSteps()
c.Assert(len(allSteps), Equals, 5)
specInfoGatherer.removeSpec(filepath.Join(s.specsDir, "Spec1.spec"))
allSteps = specInfoGatherer.getAvailableSteps()
c.Assert(len(allSteps), Equals, 3)
stepValues := make([]string, 0)
for _, stepValue := range allSteps {
stepValues = append(stepValues, stepValue.stepValue)
}
c.Assert(stringInSlice("say hello 1", stepValues), Equals, true)
c.Assert(stringInSlice("say {} to me 1", stepValues), Equals, true)
c.Assert(stringInSlice("a common step", stepValues), Equals, true)
c.Assert(stringInSlice("say hello", stepValues), Equals, false)
c.Assert(stringInSlice("say {} to me", stepValues), Equals, false)
}
func (s *MySuite) TestAddSpec(c *C) {
data := []byte(`Specification Heading
=====================
Scenario 1
----------
* first step with "foo"
* say "hello" to me
* a "final" step
`)
data1 := []byte(`Specification Heading2
=====================
Scenario 1
----------
* say hello to gauge
* testing steps with "params"
* last step
`)
util.CreateFileIn(s.specsDir, "Spec1.spec", data)
specInfoGatherer := new(specInfoGatherer)
specInfoGatherer.findAllStepsFromSpecs()
specInfoGatherer.updateAllStepsList()
c.Assert(len(specInfoGatherer.fileToStepsMap), Equals, 1)
c.Assert(len(specInfoGatherer.availableSpecs), Equals, 1)
allSteps := specInfoGatherer.getAvailableSteps()
c.Assert(len(allSteps), Equals, 3)
util.CreateFileIn(s.specsDir, "Spec2.spec", data1)
specInfoGatherer.addSpec(filepath.Join(s.specsDir, "Spec2.spec"))
c.Assert(len(specInfoGatherer.fileToStepsMap), Equals, 2)
c.Assert(len(specInfoGatherer.availableSpecs), Equals, 2)
allSteps = specInfoGatherer.getAvailableSteps()
c.Assert(len(allSteps), Equals, 6)
stepValues := make([]string, 0)
for _, stepValue := range allSteps {
stepValues = append(stepValues, stepValue.stepValue)
}
c.Assert(stringInSlice("first step with {}", stepValues), Equals, true)
c.Assert(stringInSlice("say {} to me", stepValues), Equals, true)
c.Assert(stringInSlice("a {} step", stepValues), Equals, true)
c.Assert(stringInSlice("say hello to gauge", stepValues), Equals, true)
c.Assert(stringInSlice("testing steps with {}", stepValues), Equals, true)
c.Assert(stringInSlice("last step", stepValues), Equals, true)
}
func (s *MySuite) TestSameSpecAddedTwice(c *C) {
data := []byte(`Specification Heading
=====================
Scenario 1
----------
* first step with "foo"
* say "hello" to me
* a "final" step
`)
util.CreateFileIn(s.specsDir, "Spec1.spec", data)
specInfoGatherer := new(specInfoGatherer)
specInfoGatherer.findAllStepsFromSpecs()
specInfoGatherer.updateAllStepsList()
c.Assert(len(specInfoGatherer.fileToStepsMap), Equals, 1)
c.Assert(len(specInfoGatherer.availableSpecs), Equals, 1)
allSteps := specInfoGatherer.getAvailableSteps()
c.Assert(len(allSteps), Equals, 3)
specInfoGatherer.addSpec(filepath.Join(s.specsDir, "Spec1.spec"))
c.Assert(len(specInfoGatherer.fileToStepsMap), Equals, 1)
c.Assert(len(specInfoGatherer.availableSpecs), Equals, 1)
allSteps = specInfoGatherer.getAvailableSteps()
c.Assert(len(allSteps), Equals, 3)
stepValues := make([]string, 0)
for _, stepValue := range allSteps {
stepValues = append(stepValues, stepValue.stepValue)
}
c.Assert(stringInSlice("first step with {}", stepValues), Equals, true)
c.Assert(stringInSlice("say {} to me", stepValues), Equals, true)
c.Assert(stringInSlice("a {} step", stepValues), Equals, true)
}
func (s *MySuite) TestAddingSpecWithParseFailures(c *C) {
data := []byte(`NO heading parse failure
* first step with "foo"
* say "hello" to me
* a "final" step
`)
util.CreateFileIn(s.specsDir, "Spec1.spec", data)
specInfoGatherer := new(specInfoGatherer)
specInfoGatherer.findAllStepsFromSpecs()
specInfoGatherer.updateAllStepsList()
c.Assert(len(specInfoGatherer.fileToStepsMap), Equals, 0)
c.Assert(len(specInfoGatherer.availableSpecs), Equals, 0)
allSteps := specInfoGatherer.getAvailableSteps()
c.Assert(len(allSteps), Equals, 0)
}
func (s *MySuite) TestFindingStepsAndConceptInfosFromConcepts(c *C) {
data := []byte(`# foo bar
* first step with "foo"
* say "hello" to me
* a "final" step
`)
util.CreateFileIn(s.specsDir, "concept.cpt", data)
specInfoGatherer := new(specInfoGatherer)
specInfoGatherer.findAllStepsFromConcepts()
specInfoGatherer.updateAllStepsList()
c.Assert(len(specInfoGatherer.fileToStepsMap), Equals, 1)
c.Assert(len(specInfoGatherer.availableSpecs), Equals, 0)
allSteps := specInfoGatherer.getAvailableSteps()
c.Assert(len(allSteps), Equals, 3)
conceptInfos := specInfoGatherer.getConceptInfos()
c.Assert(len(conceptInfos), Equals, 1)
c.Assert(conceptInfos[0].GetFilepath(), Equals, filepath.Join(s.specsDir, "concept.cpt"))
}
func (s *MySuite) TestAddingConcepts(c *C) {
data := []byte(`# A concept
* first step with "foo"
* second say "hello" to me
* third "foo" step
# second concept
* fourth
* A concept
`)
data1 := []byte(`# another cpt file
* second say "hello" to me
* fifth step
# concept with <a>
* sixth step with <a>
* seventh param step
`)
util.CreateFileIn(s.specsDir, "concept.cpt", data)
specInfoGatherer := new(specInfoGatherer)
specInfoGatherer.findAllStepsFromConcepts()
specInfoGatherer.updateAllStepsList()
c.Assert(len(specInfoGatherer.fileToStepsMap), Equals, 1)
allSteps := specInfoGatherer.getAvailableSteps()
c.Assert(len(allSteps), Equals, 4)
c.Assert(len(specInfoGatherer.getConceptInfos()), Equals, 2)
util.CreateFileIn(s.specsDir, "concept1.cpt", data1)
specInfoGatherer.addConcept(filepath.Join(s.specsDir, "concept1.cpt"))
allSteps = specInfoGatherer.getAvailableSteps()
c.Assert(len(allSteps), Equals, 7)
c.Assert(len(specInfoGatherer.getConceptInfos()), Equals, 4)
}
func (s *MySuite) TestRemovingConcepts(c *C) {
data := []byte(`# A concept
* first step with "foo"
* second say "hello" to me
* third "foo" step
# second concept
* fourth
* fifth step
* third concept
`)
data1 := []byte(`# third concept
* second say "hello" to me
* A concept
# fourth concept with <a>
* sixth step with <a>
* seventh "param" step
`)
util.CreateFileIn(s.specsDir, "concept.cpt", data)
util.CreateFileIn(s.specsDir, "concept1.cpt", data1)
specInfoGatherer := new(specInfoGatherer)
specInfoGatherer.findAllStepsFromConcepts()
specInfoGatherer.updateAllStepsList()
c.Assert(len(specInfoGatherer.fileToStepsMap), Equals, 2)
allSteps := specInfoGatherer.getAvailableSteps()
c.Assert(len(allSteps), Equals, 7)
c.Assert(len(specInfoGatherer.getConceptInfos()), Equals, 4)
firstCptFile := filepath.Join(s.specsDir, "concept.cpt")
os.Remove(firstCptFile)
specInfoGatherer.removeConcept(firstCptFile)
c.Assert(len(specInfoGatherer.fileToStepsMap), Equals, 1)
allSteps = specInfoGatherer.getAvailableSteps()
c.Assert(len(allSteps), Equals, 4)
stepValues := make([]string, 0)
for _, stepValue := range allSteps {
stepValues = append(stepValues, stepValue.stepValue)
}
c.Assert(stringInSlice("second say {} to me", stepValues), Equals, true)
c.Assert(stringInSlice("A concept", stepValues), Equals, true)
c.Assert(stringInSlice("sixth step with {}", stepValues), Equals, true)
c.Assert(stringInSlice("seventh {} step", stepValues), Equals, true)
cptInfos := specInfoGatherer.getConceptInfos()
c.Assert(len(cptInfos), Equals, 2)
}
func (s *MySuite) TestAddingSpecsAndConcepts(c *C) {
data := []byte(`# A concept
* first step with "foo"
* second say "hello" to me
* third "foo" step
# second concept
* fourth
* fifth step
* third concept
`)
data1 := []byte(`# third concept
* second say "hello" to me
* A concept
# fourth concept with <a>
* sixth step with <a>
* seventh "param" step
`)
data3 := []byte(`Specification Heading
=====================
Scenario 1
----------
* eighth step with "foo"
## scenario 2
* ninth step
`)
util.CreateFileIn(s.specsDir, "concept.cpt", data)
util.CreateFileIn(s.specsDir, "concept1.cpt", data1)
specInfoGatherer := new(specInfoGatherer)
specInfoGatherer.findAllStepsFromConcepts()
specInfoGatherer.updateAllStepsList()
c.Assert(len(specInfoGatherer.fileToStepsMap), Equals, 2)
allSteps := specInfoGatherer.getAvailableSteps()
c.Assert(len(allSteps), Equals, 7)
stepValues := createStepValueTexts(allSteps)
c.Assert(stringInSlice("first step with {}", stepValues), Equals, true)
c.Assert(stringInSlice("second say {} to me", stepValues), Equals, true)
c.Assert(stringInSlice("third {} step", stepValues), Equals, true)
c.Assert(stringInSlice("fourth", stepValues), Equals, true)
c.Assert(stringInSlice("fifth step", stepValues), Equals, true)
c.Assert(stringInSlice("seventh {} step", stepValues), Equals, true)
c.Assert(stringInSlice("sixth step with {}", stepValues), Equals, true)
c.Assert(len(specInfoGatherer.getConceptInfos()), Equals, 4)
_, err := util.CreateFileIn(filepath.Join(s.specsDir, "nested"), "SPEC.spec", data3)
c.Assert(err, Equals, nil)
specInfoGatherer.addSpec(filepath.Join(s.specsDir, "nested", "SPEC.spec"))
c.Assert(len(specInfoGatherer.fileToStepsMap), Equals, 3)
allSteps = specInfoGatherer.getAvailableSteps()
c.Assert(len(allSteps), Equals, 9)
stepValues = createStepValueTexts(allSteps)
c.Assert(stringInSlice("first step with {}", stepValues), Equals, true)
c.Assert(stringInSlice("second say {} to me", stepValues), Equals, true)
c.Assert(stringInSlice("third {} step", stepValues), Equals, true)
c.Assert(stringInSlice("fourth", stepValues), Equals, true)
c.Assert(stringInSlice("fifth step", stepValues), Equals, true)
c.Assert(stringInSlice("sixth step with {}", stepValues), Equals, true)
c.Assert(stringInSlice("seventh {} step", stepValues), Equals, true)
c.Assert(stringInSlice("eighth step with {}", stepValues), Equals, true)
c.Assert(stringInSlice("ninth step", stepValues), Equals, true)
c.Assert(len(specInfoGatherer.getConceptInfos()), Equals, 4)
}
func createStepValueTexts(steps []*stepValue) []string {
stepValues := make([]string, 0)
for _, stepValue := range steps {
stepValues = append(stepValues, stepValue.stepValue)
}
return stepValues
}
func stringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}