File: parser_test.go

package info (click to toggle)
golang-github-gucumber-gucumber 0.0~git20160715.0.71608e2-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 172 kB
  • ctags: 160
  • sloc: makefile: 10
file content (318 lines) | stat: -rw-r--r-- 8,626 bytes parent folder | download | duplicates (4)
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
package gherkin

import (
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestParseSingleScenario(t *testing.T) {
	s := `
# a line comment
@tag1 @tag2
Feature: Refund item

  # a comment embedded in the description
  A multiline description of the Feature
  that can contain any text like
  Rules:
  - a
  - b

  @tag1 @tag2
  Scenario: Jeff returns a faulty microwave
    Given Jeff has bought a microwave for $100
    And he has a receipt
    When he returns the microwave
    Then Jeff should be refunded $100
`
	features, err := Parse(s)

	assert.NoError(t, err)
	assert.Equal(t, 1, len(features))
	assert.Equal(t, "Refund item", features[0].Title)
	assert.Equal(t, "@tag1", features[0].Tags[0])
	assert.Equal(t, "@tag2", features[0].Tags[1])
	assert.Equal(t, 1, len(features[0].Scenarios))
	assert.Equal(t, "Jeff returns a faulty microwave", features[0].Scenarios[0].Title)
	assert.Equal(t, "A multiline description of the Feature\nthat can contain any text like\nRules:\n- a\n- b", features[0].Description)
	assert.Equal(t, 4, len(features[0].Scenarios[0].Steps))
	assert.Equal(t, "@tag1", features[0].Scenarios[0].Tags[0])
	assert.Equal(t, "@tag2", features[0].Scenarios[0].Tags[1])
	assert.Equal(t, StepType("Given"), features[0].Scenarios[0].Steps[0].Type)
	assert.Equal(t, "Jeff has bought a microwave for $100", features[0].Scenarios[0].Steps[0].Text)
	assert.Equal(t, StepType("And"), features[0].Scenarios[0].Steps[1].Type)
	assert.Equal(t, "he has a receipt", features[0].Scenarios[0].Steps[1].Text)
	assert.Equal(t, StepType("When"), features[0].Scenarios[0].Steps[2].Type)
	assert.Equal(t, "he returns the microwave", features[0].Scenarios[0].Steps[2].Text)
	assert.Equal(t, StepType("Then"), features[0].Scenarios[0].Steps[3].Type)
	assert.Equal(t, "Jeff should be refunded $100", features[0].Scenarios[0].Steps[3].Text)
}

func TestMultipleScenarios(t *testing.T) {
	s := `
Feature: Parsing multiple scenarios
  Scenario: Scenario name here
    Given some precondition
    Then something happens

  Scenario: Another scenario
    Given another precondition
    Then something happens
`
	features, err := Parse(s)
	assert.NoError(t, err)
	assert.Equal(t, 2, len(features[0].Scenarios))
	assert.Equal(t, 2, len(features[0].Scenarios[0].Steps))
	assert.Equal(t, 2, len(features[0].Scenarios[1].Steps))
}

func TestBackgroundAndScenarios(t *testing.T) {
	s := `
Feature: Parsing multiple scenarios
  @tag_on_background
  Background:
    Given there is some background

  Scenario: Scenario name here
    Given some precondition
    Then something happens

  Scenario: Another scenario
    Given another precondition
    Then something happens
`
	features, err := Parse(s)
	assert.NoError(t, err)
	assert.Equal(t, "@tag_on_background", features[0].Background.Tags[0])
	assert.Equal(t, "there is some background", features[0].Background.Steps[0].Text)
	assert.Equal(t, 2, len(features[0].Scenarios))
}

func TestMultipleFeatures(t *testing.T) {
	s := `
Feature: Feature 1
  Scenario: Scenario name here
    Given some precondition
    Then something happens

Feature: Feature 2
  Scenario: Another scenario
    Given another precondition
    Then something happens
`
	f, err := Parse(s)
	assert.NoError(t, err)
	assert.Equal(t, 2, len(f))
	assert.Equal(t, "Feature 1", f[0].Title)
	assert.Equal(t, "Feature 2", f[1].Title)
	assert.Equal(t, 1, len(f[0].Scenarios))
	assert.Equal(t, 1, len(f[1].Scenarios))

}

func TestTagParsing(t *testing.T) {
	f, err := Parse("@tag1   @tag2@tag3\nFeature: Tag parsing")
	assert.NoError(t, err)
	assert.Equal(t, 2, len(f[0].Tags))
	assert.Equal(t, "@tag1", f[0].Tags[0])
	assert.Equal(t, "@tag2@tag3", f[0].Tags[1])
}

func TestBacktrackingCommentsAtEnd(t *testing.T) {
	s := `
Feature: Comments at end
  Scenario: Scenario name here
    Given some precondition
    Then something happens
  # comments here
`
	_, err := Parse(s)
	assert.NoError(t, err)
}

func TestBacktrackingCommentsDontAffectIndent(t *testing.T) {
	s := `
Feature: Comments at end
  Scenario: Scenario name here
    Given some precondition
    Then something happens
# comments here
  Scenario: Another scenario
    Given a step
`
	f, err := Parse(s)
	assert.NoError(t, err)
	assert.Equal(t, 2, len(f[0].Scenarios))
}

func TestScenarioOutlines(t *testing.T) {
	s := `
Feature: Scenario outlines
  Scenario Outline: Scenario 1
    Given some value <foo>
    Then some result <bar>

    Examples:
    | foo | bar |
    | 1   | 2   |
    | 3   | 4   |

  Scenario: Scenario 2
    Given some other scenario
`
	f, err := Parse(s)
	assert.NoError(t, err)
	assert.Equal(t, 2, len(f[0].Scenarios))
	assert.Equal(t, StringData("| foo | bar |\n| 1   | 2   |\n| 3   | 4   |"), f[0].Scenarios[0].Examples)
}

func TestStepArguments(t *testing.T) {
	s := `
Feature: Step arguments
  Scenario: Scenario 1
    Given some data
                          | 1   | 2   |
                          | 3   | 4   |
    And some docstring
    """
     hello
     world
    """
    And some table
    | 1 | 2 |
    Then other text

  Scenario: Scenario 2
    Given some other scenario
`
	f, err := Parse(s)
	assert.NoError(t, err)
	assert.Equal(t, 2, len(f[0].Scenarios))
	assert.Equal(t, StringData("| 1   | 2   |\n| 3   | 4   |"), f[0].Scenarios[0].Steps[0].Argument)
	assert.Equal(t, StringData(" hello\n world"), f[0].Scenarios[0].Steps[1].Argument)
	assert.Equal(t, StringData("| 1 | 2 |"), f[0].Scenarios[0].Steps[2].Argument)
	assert.Equal(t, "other text", f[0].Scenarios[0].Steps[3].Text)
}

func TestFailureNoFeature(t *testing.T) {
	_, err := Parse("")
	assert.EqualError(t, err, `parse error (<unknown>.feature:1): no features parsed.`)
}

func TestTagWithoutFeature(t *testing.T) {
	_, err := Parse("@tag")
	assert.EqualError(t, err, `parse error (<unknown>.feature:1): tags not applied to feature.`)
}

func TestFailureExpectingFeature(t *testing.T) {
	_, err := Parse("@tag\n@tag")
	assert.EqualError(t, err, `parse error (<unknown>.feature:2): expected "Feature:", found "@tag".`)
}

func TestFailureInvalidTag(t *testing.T) {
	_, err := Parse("@tag tag")
	assert.EqualError(t, err, `parse error (<unknown>.feature:1): invalid tag "tag".`)
}

func TestFailureDescriptionAfterTags(t *testing.T) {
	s := `
Feature: Descriptions after tags
  @tag1
  Descriptions should not be allowed after tags

  Scenario: Scenario name here
    Given some precondition
    Then something happens
`
	_, err := Parse(s)
	assert.EqualError(t, err, `parse error (<unknown>.feature:4): illegal description text after tags.`)
}

func TestFailureDescriptionAfterScenario(t *testing.T) {
	s := `
Feature: Descriptions after scenario
  Scenario: Scenario name here
    Given some precondition
    Then something happens

  Descriptions should not be allowed after scenario

  Scenario: Another scenario
    Given some precondition
    Then something happens
`
	_, err := Parse(s)
	assert.EqualError(t, err, `parse error (<unknown>.feature:7): illegal description text after scenario.`)
}

func TestFailureMultipleBackgrounds(t *testing.T) {
	s := `
Feature: Multiple backgrounds
  Background:
    Given one

  Background:
    Given two
`
	_, err := Parse(s)
	assert.EqualError(t, err, `parse error (<unknown>.feature:6): multiple backgrounds not allowed.`)
}

func TestFailureBackgroundAfterScenario(t *testing.T) {
	s := `
Feature: Background after scenario
  Scenario: Scenario name here
    Given some precondition
    Then something happens

  Background:
    Given it's after a scenario
`
	_, err := Parse(s)
	assert.EqualError(t, err, `parse error (<unknown>.feature:7): illegal background after scenario.`)
}

func TestFailureInvalidStep(t *testing.T) {
	s := `
Feature: Invalid steps
  Scenario: Scenario name here
    Invalid step
`
	_, err := Parse(s)
	assert.EqualError(t, err, `parse error (<unknown>.feature:4): illegal step prefix "Invalid".`)
}

func TestFailureNoStepText(t *testing.T) {
	s := `
Feature: No step text
  Scenario: Scenario name here
    Given
`
	_, err := Parse(s)
	assert.EqualError(t, err, `parse error (<unknown>.feature:4): expected step text after "Given".`)
}

func TestFailureInvalidTagOnScenario(t *testing.T) {
	s := `
Feature: Invalid tag on scenario
  @invalid tags
  Scenario:
    Given a scenario
`
	_, err := Parse(s)
	assert.EqualError(t, err, `parse error (<unknown>.feature:3): invalid tag "tags".`)

}

func TestFailureInvalidBackground(t *testing.T) {
	s := `
Feature: Invalid background
  Background:
    Invalid step
  Scenario: A scenario
    Given a scenario
`
	_, err := Parse(s)
	assert.EqualError(t, err, `parse error (<unknown>.feature:4): illegal step prefix "Invalid".`)
}