-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgherkin_test.go
538 lines (464 loc) · 16.3 KB
/
gherkin_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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
package gherkin_test
import (
"fmt"
"strings"
"testing"
"github.com/muhqu/go-gherkin"
"github.com/muhqu/go-gherkin/nodes"
"github.com/stretchr/testify/assert"
)
func prettyPrintingLogFn(logPrefix string) func(msg string, args ...interface{}) {
depth := 0
return func(msg string, args ...interface{}) {
isBegin := msg[0:5] == "Begin"
isEnd := msg[0:3] == "End"
if isEnd {
depth = depth - 1
if depth < 0 {
depth = 0
}
}
line := fmt.Sprintf(msg, args...)
depthPrefix := strings.Repeat(" ", depth)
fmt.Printf("%s%s%s\n", logPrefix, depthPrefix, line)
if isBegin {
depth = depth + 1
}
}
}
func parse(t *testing.T, logPrefix, text string) (gherkin.GherkinDOMParser, error) {
gp := gherkin.NewGherkinDOMParser(text)
if logPrefix != "" {
gp.WithLogFn(prettyPrintingLogFn(logPrefix))
}
gp.Init()
err := gp.Parse()
if err == nil {
gp.Execute()
}
return gp, err
}
func mustDomParse(t *testing.T, logPrefix, text string) gherkin.GherkinDOMParser {
gp, err := parse(t, logPrefix, text)
assert.NoError(t, err)
if err != nil {
t.FailNow()
}
return gp
}
func verifyDeadSimpleCalculator(t *testing.T, logPrefix, text string) {
gp := mustDomParse(t, logPrefix, text)
feature := gp.Feature()
assert.NotNil(t, feature)
assert.Equal(t, "Dead Simple Calculator", feature.Title())
assert.Equal(t, "Bla Bla\nBla", feature.Description())
assert.Equal(t, 3, len(feature.Scenarios()), "Number of scenarios")
assert.Equal(t, 2, len(feature.Tags()), "Number of tags")
assert.Equal(t, []string{"dead", "simple"}, feature.Tags(), "Feature Tags")
assert.NotNil(t, feature.Background())
assert.Equal(t, 1, len(feature.Background().Steps()), "Number of background steps")
assert.Equal(t, "Given", feature.Background().Steps()[0].StepType())
assert.Equal(t, "a Simple Calculator", feature.Background().Steps()[0].Text())
scenario1 := feature.Scenarios()[0]
assert.NotNil(t, scenario1)
assert.Equal(t, nodes.ScenarioNodeType, scenario1.NodeType())
assert.Equal(t, 1, len(scenario1.Tags()), "Number of tags on Scenario 1")
assert.Equal(t, []string{"wip"}, scenario1.Tags(), "Tags on Senario 1")
assert.Equal(t, 5, len(scenario1.Steps()), "Number of steps in Scenario 1")
assert.Equal(t, "When", scenario1.Steps()[0].StepType())
assert.Equal(t, "I press the key \"2\"", scenario1.Steps()[0].Text())
scenario2 := feature.Scenarios()[1]
assert.NotNil(t, scenario2)
assert.Equal(t, nodes.OutlineNodeType, scenario2.NodeType())
scenario2o, ok := scenario2.(nodes.OutlineNode)
assert.True(t, ok)
assert.Equal(t, 2, len(scenario2.Tags()), "Number of tags on Scenario 2")
assert.Equal(t, []string{"wip", "expensive"}, scenario2.Tags(), "Tags on Senario 2")
assert.Equal(t, 5, len(scenario2.Steps()), "Number of steps in Scenario 2")
assert.Equal(t, "When", scenario2.Steps()[0].StepType())
assert.Equal(t, "I press the key \"<left>\"", scenario2.Steps()[0].Text())
assert.Equal(t, [][]string{
{"left", "operator", "right", "result"},
{"2", "+", "2", "4"},
{"3", "+", "4", "7"},
}, scenario2o.Examples().Table().Rows())
scenario3 := feature.Scenarios()[2]
assert.NotNil(t, scenario3)
assert.Equal(t, nodes.ScenarioNodeType, scenario3.NodeType())
assert.Equal(t, 0, len(scenario3.Tags()), "Number of tags on Scenario 3")
assert.Equal(t, 2, len(scenario3.Steps()), "Number of steps in Scenario 3")
assert.Equal(t, "When", scenario3.Steps()[0].StepType())
assert.Equal(t, "I press the following keys:", scenario3.Steps()[0].Text())
assert.NotNil(t, scenario3.Steps()[0].PyString())
assert.Equal(t, " 2\n+ 2\n+ 5\n =\n", scenario3.Steps()[0].PyString().String())
}
const benchmarkGherkinText = `
@dead @simple
Feature: Dead Simple Calculator
Bla Bla
Bla
Background:
Given a Simple Calculator
@wip
Scenario: Adding 2 numbers
When I press the key "2"
And I press the key "+"
And I press the key "2"
And I press the key "="
Then the result should be 4
@wip @expensive
Scenario Outline: Simple Math
When I press the key "<left>"
And I press the key "<operator>"
And I press the key "<right>"
And I press the key "="
Then the result should be "<result>"
Examples:
| left | operator | right | result |
| 2 | + | 2 | 4 |
| 3 | + | 4 | 7 |
Scenario: Adding 3 numbers
When I press the following keys:
"""
2
+ 2
+ 5
=
"""
Then the result should be 9
`
func Benchmark_NewGherkinDOMParser(b *testing.B) { //benchmark function starts with "Benchmark" and takes a pointer to type testing.B
for i := 0; i < b.N; i++ { // use b.N for looping
gherkin.NewGherkinDOMParser(benchmarkGherkinText)
}
}
func Benchmark_NewGherkinDOMParserAndParse(b *testing.B) { //benchmark function starts with "Benchmark" and takes a pointer to type testing.B
for i := 0; i < b.N; i++ { // use b.N for looping
gp := gherkin.NewGherkinDOMParser(benchmarkGherkinText)
gp.Init()
gp.Parse()
}
}
func Benchmark_JustReParseWithCachedGherkinDOMParser(b *testing.B) { //benchmark function starts with "Benchmark" and takes a pointer to type testing.B
b.StopTimer()
gp := gherkin.NewGherkinDOMParser(benchmarkGherkinText)
b.StartTimer()
for i := 0; i < b.N; i++ { // use b.N for looping
gp.Init()
gp.Parse()
}
}
func TestParsingRegular(t *testing.T) {
verifyDeadSimpleCalculator(t, "", `
@dead @simple
Feature: Dead Simple Calculator
Bla Bla
Bla
Background:
Given a Simple Calculator
@wip
Scenario: Adding 2 numbers
When I press the key "2"
And I press the key "+"
And I press the key "2"
And I press the key "="
Then the result should be 4
@wip @expensive
Scenario Outline: Simple Math
When I press the key "<left>"
And I press the key "<operator>"
And I press the key "<right>"
And I press the key "="
Then the result should be "<result>"
Examples:
| left | operator | right | result |
| 2 | + | 2 | 4 |
| 3 | + | 4 | 7 |
Scenario: Adding 3 numbers
When I press the following keys:
"""
2
+ 2
+ 5
=
"""
Then the result should be 9
`)
}
func TestParsingTabAligned(t *testing.T) {
verifyDeadSimpleCalculator(t, "", `
@dead @simple
Feature: Dead Simple Calculator
Bla Bla
Bla
Background:
Given a Simple Calculator
@wip
Scenario: Adding 2 numbers
When I press the key "2"
And I press the key "+"
And I press the key "2"
And I press the key "="
Then the result should be 4
@wip @expensive
Scenario Outline: Simple Math
When I press the key "<left>"
And I press the key "<operator>"
And I press the key "<right>"
And I press the key "="
Then the result should be "<result>"
Examples:
| left | operator | right | result |
| 2 | + | 2 | 4 |
| 3 | + | 4 | 7 |
Scenario: Adding 3 numbers
When I press the following keys:
"""
2
+ 2
+ 5
=
"""
Then the result should be 9
`)
}
func TestParsingCondensedAndTrailingWhitespace(t *testing.T) {
verifyDeadSimpleCalculator(t, "", `@dead @simple Feature: Dead Simple Calculator
Bla Bla
Bla
Background:
Given a Simple Calculator
@wip Scenario: Adding 2 numbers
When I press the key "2"
And I press the key "+"
And I press the key "2"
And I press the key "="
Then the result should be 4
@wip @expensive Scenario Outline: Simple Math
When I press the key "<left>"
And I press the key "<operator>"
And I press the key "<right>"
And I press the key "="
Then the result should be "<result>"
Examples:
| left | operator | right | result |
| 2 | + | 2 | 4 |
| 3 | + | 4 | 7 |
Scenario: Adding 3 numbers
When I press the following keys:
"""
2
+ 2
+ 5
=
"""
Then the result should be 9`)
}
func TestParsingMinimalNoScenarios(t *testing.T) {
gp := mustDomParse(t, "", `Feature: Hello World`)
feature := gp.Feature()
assert.NotNil(t, feature)
assert.Equal(t, "Hello World", feature.Title())
}
func TestParsingMinimalNoSteps(t *testing.T) {
gp := mustDomParse(t, "", `Feature: Hello World
Scenario: Nice people`)
feature := gp.Feature()
assert.NotNil(t, feature)
assert.Equal(t, "Hello World", feature.Title())
assert.Equal(t, 1, len(feature.Scenarios()), "Number of Scenarios")
scenario1 := feature.Scenarios()[0]
assert.NotNil(t, scenario1)
assert.Equal(t, nodes.ScenarioNodeType, scenario1.NodeType())
assert.Equal(t, 0, len(scenario1.Steps()), "Number of steps in Scenario 1")
}
func TestParsingMinimalWithSteps(t *testing.T) {
gp := mustDomParse(t, "", `
Feature: Hello World
Scenario: Nice people
Given a nice person called "Bob"
And a nice person called "Lisa"
When "Bob" says to "Lisa": "Hello!"
Then "Lisa" should reply to "Bob": "Hello!"
`)
feature := gp.Feature()
assert.NotNil(t, feature)
assert.Equal(t, "Hello World", feature.Title())
assert.Equal(t, 1, len(feature.Scenarios()), "Number of Scenarios")
scenario1 := feature.Scenarios()[0]
assert.NotNil(t, scenario1)
assert.Equal(t, nodes.ScenarioNodeType, scenario1.NodeType())
assert.Equal(t, 4, len(scenario1.Steps()), "Number of steps in Scenario 1")
i := 0
assert.Equal(t, "Given", scenario1.Steps()[i].StepType())
assert.Equal(t, `a nice person called "Bob"`, scenario1.Steps()[i].Text())
i += 1
assert.Equal(t, "And", scenario1.Steps()[i].StepType())
assert.Equal(t, `a nice person called "Lisa"`, scenario1.Steps()[i].Text())
i += 1
assert.Equal(t, "When", scenario1.Steps()[i].StepType())
assert.Equal(t, `"Bob" says to "Lisa": "Hello!"`, scenario1.Steps()[i].Text())
i += 1
assert.Equal(t, "Then", scenario1.Steps()[i].StepType())
assert.Equal(t, `"Lisa" should reply to "Bob": "Hello!"`, scenario1.Steps()[i].Text())
}
func TestParsingSimpleComments(t *testing.T) {
gp := mustDomParse(t, "", `
Feature: Hello World # feature comment
Scenario: Nice people # scenario 1 comment
Given a nice person called "Bob" # step 1.1 comment
And a nice person called "Lisa" # step 1.2 comment
When "Bob" says to "Lisa": "Hello!" # step 1.3 comment
Then "Lisa" should reply to "Bob": "Hello!" # step 1.4 comment`)
feature := gp.Feature()
if ok := assert.NotNil(t, feature); !ok {
return
}
assert.Equal(t, "Hello World", feature.Title())
if ok := assert.Equal(t, 1, len(feature.Scenarios()), "Number of Scenarios"); !ok {
return
}
scenario1 := feature.Scenarios()[0]
if ok := assert.NotNil(t, scenario1); !ok {
return
}
assert.Equal(t, nodes.ScenarioNodeType, scenario1.NodeType())
assert.Equal(t, 4, len(scenario1.Steps()), "Number of steps in Scenario 1")
i := 0
assert.Equal(t, "Given", scenario1.Steps()[i].StepType())
assert.Equal(t, `a nice person called "Bob"`, scenario1.Steps()[i].Text())
i += 1
assert.Equal(t, "And", scenario1.Steps()[i].StepType())
assert.Equal(t, `a nice person called "Lisa"`, scenario1.Steps()[i].Text())
i += 1
assert.Equal(t, "When", scenario1.Steps()[i].StepType())
assert.Equal(t, `"Bob" says to "Lisa": "Hello!"`, scenario1.Steps()[i].Text())
i += 1
assert.Equal(t, "Then", scenario1.Steps()[i].StepType())
assert.Equal(t, `"Lisa" should reply to "Bob": "Hello!"`, scenario1.Steps()[i].Text())
}
func TestParsingDifficultComments(t *testing.T) {
gp := mustDomParse(t, "", `
@awesome @dude # feature tag comment
Feature: Hello "#World" # feature comment
Bla bla # feature description comment
Bla # feature description comment
# blank line comment
@wip @wop # scenario tag comment
Scenario: Nice people # scenario 1 comment
Given a nice person called "#Bob" # step 1.1 comment
And a nice person called "Lisa#\"Bang" # step 1.2 comment
When "#Bob" says to "Lisa#\"Bang": "Hello!" # step 1.3 comment
Then "Lisa#\"Bang" should reply to "#Bob": "Hello!" # step 1.4 comment
# blank line comment
`)
feature := gp.Feature()
if ok := assert.NotNil(t, feature); !ok {
return
}
assert.Equal(t, `Hello "#World"`, feature.Title())
assert.Equal(t, "Bla bla\nBla", feature.Description())
assert.Equal(t, []string{"awesome", "dude"}, feature.Tags())
if ok := assert.Equal(t, 1, len(feature.Scenarios()), "Number of Scenarios"); !ok {
return
}
scenario1 := feature.Scenarios()[0]
if ok := assert.NotNil(t, scenario1); !ok {
return
}
assert.Equal(t, nodes.ScenarioNodeType, scenario1.NodeType())
assert.Equal(t, []string{"wip", "wop"}, scenario1.Tags())
assert.Equal(t, 4, len(scenario1.Steps()), "Number of steps in Scenario 1")
i := 0
assert.Equal(t, "Given", scenario1.Steps()[i].StepType())
assert.Equal(t, `a nice person called "#Bob"`, scenario1.Steps()[i].Text())
i += 1
assert.Equal(t, "And", scenario1.Steps()[i].StepType())
assert.Equal(t, `a nice person called "Lisa#\"Bang"`, scenario1.Steps()[i].Text())
i += 1
assert.Equal(t, "When", scenario1.Steps()[i].StepType())
assert.Equal(t, `"#Bob" says to "Lisa#\"Bang": "Hello!"`, scenario1.Steps()[i].Text())
i += 1
assert.Equal(t, "Then", scenario1.Steps()[i].StepType())
assert.Equal(t, `"Lisa#\"Bang" should reply to "#Bob": "Hello!"`, scenario1.Steps()[i].Text())
}
func TestParsingBlankQuotedStrings(t *testing.T) {
gp := mustDomParse(t, "", `
Feature: Empty "" Quotes
Scenario: Allow ""
When "" is present
`)
feature := gp.Feature()
if ok := assert.NotNil(t, feature); !ok {
return
}
assert.Equal(t, `Empty "" Quotes`, feature.Title())
if ok := assert.Equal(t, 1, len(feature.Scenarios()), "Number of Scenarios"); !ok {
return
}
scenario1 := feature.Scenarios()[0]
if ok := assert.NotNil(t, scenario1); !ok {
return
}
assert.Equal(t, `Allow ""`, scenario1.Title())
assert.Equal(t, 1, len(scenario1.Steps()), "Number of steps in Scenario 1")
i := 0
assert.Equal(t, "When", scenario1.Steps()[i].StepType())
assert.Equal(t, `"" is present`, scenario1.Steps()[i].Text())
}
func TestParsingDescriptionWithBlankLines(t *testing.T) {
gp := mustDomParse(t, "", `
Feature: Description With Blank Lines
Whitespace should be allowed.
(Multiple times)
`)
feature := gp.Feature()
if ok := assert.NotNil(t, feature); !ok {
return
}
assert.Equal(t, "Whitespace should be allowed.\n\n(Multiple times)", feature.Description())
}
func TestParsingMultipleExamples(t *testing.T) {
gp := mustDomParse(t, "", `
Feature: Account withdrawal
Scenario Outline: Withdraw fixed amount
Given I have <Balance> in my account
When I choose to withdraw the fixed amount of <Withdrawal>
Then I should <Outcome>
And the balance of my account should be <Remaining>
Examples:
| Balance | Withdrawal | Outcome | Remaining |
| $500 | $50 | receive $50 cash | $450 |
| $500 | $100 | receive $100 cash | $400 |
Examples:
| Balance | Withdrawal | Outcome | Remaining |
| $100 | $200 | see an error message | $100 |
| $0 | $50 | see an error message | $0 |
`)
feature := gp.Feature()
if ok := assert.NotNil(t, feature); !ok {
return
}
assert.Equal(t, "Account withdrawal", feature.Title())
}
func TestParsingExampleWithTitle(t *testing.T) {
gp := mustDomParse(t, "", `
Feature: Account withdrawal
Scenario Outline: Withdraw fixed amount
Given I have <Balance> in my account
When I choose to withdraw the fixed amount of <Withdrawal>
Then I should <Outcome>
And the balance of my account should be <Remaining>
Examples: Attempt to withdraw too much
| Balance | Withdrawal | Outcome | Remaining |
| $100 | $200 | see an error message | $100 |
| $0 | $50 | see an error message | $0 |
`)
feature := gp.Feature()
if ok := assert.NotNil(t, feature); !ok {
return
}
assert.Equal(t, "Account withdrawal", feature.Title())
outline := feature.Scenarios()[0].(nodes.OutlineNode)
assert.Equal(t, "Attempt to withdraw too much", outline.Examples().Title())
}