Debian Codegraph @

gherkin/parser_test.go

raw
1 package gherkin
2
3 import (
4 "testing"
5
6 "github.com/stretchr/testify/assert"
7 )
8
9 func TestParseSingleScenario(t *testing.T) {
10 s := `
11 # a line comment
12 @tag1 @tag2
13 Feature: Refund item
14
15 # a comment embedded in the description
16 A multiline description of the Feature
17 that can contain any text like
18 Rules:
19 - a
20 - b
21
22 @tag1 @tag2
23 Scenario: Jeff returns a faulty microwave
24 Given Jeff has bought a microwave for $100
25 And he has a receipt
26 When he returns the microwave
27 Then Jeff should be refunded $100
28 `
29 features, err := Parse(s)
30
31 assert.NoError(t, err)
32 assert.Equal(t, 1, len(features))
33 assert.Equal(t, "Refund item", features[0].Title)
34 assert.Equal(t, "@tag1", features[0].Tags[0])
35 assert.Equal(t, "@tag2", features[0].Tags[1])
36 assert.Equal(t, 1, len(features[0].Scenarios))
37 assert.Equal(t, "Jeff returns a faulty microwave", features[0].Scenarios[0].Title)
38 assert.Equal(t, "A multiline description of the Feature\nthat can contain any text like\nRules:\n- a\n- b", features[0].Description)
39 assert.Equal(t, 4, len(features[0].Scenarios[0].Steps))
40 assert.Equal(t, "@tag1", features[0].Scenarios[0].Tags[0])
41 assert.Equal(t, "@tag2", features[0].Scenarios[0].Tags[1])
42 assert.Equal(t, StepType("Given"), features[0].Scenarios[0].Steps[0].Type)
43 assert.Equal(t, "Jeff has bought a microwave for $100", features[0].Scenarios[0].Steps[0].Text)
44 assert.Equal(t, StepType("And"), features[0].Scenarios[0].Steps[1].Type)
45 assert.Equal(t, "he has a receipt", features[0].Scenarios[0].Steps[1].Text)
46 assert.Equal(t, StepType("When"), features[0].Scenarios[0].Steps[2].Type)
47 assert.Equal(t, "he returns the microwave", features[0].Scenarios[0].Steps[2].Text)
48 assert.Equal(t, StepType("Then"), features[0].Scenarios[0].Steps[3].Type)
49 assert.Equal(t, "Jeff should be refunded $100", features[0].Scenarios[0].Steps[3].Text)
50 }
51
52 func TestMultipleScenarios(t *testing.T) {
53 s := `
54 Feature: Parsing multiple scenarios
55 Scenario: Scenario name here
56 Given some precondition
57 Then something happens
58
59 Scenario: Another scenario
60 Given another precondition
61 Then something happens
62 `
63 features, err := Parse(s)
64 assert.NoError(t, err)
65 assert.Equal(t, 2, len(features[0].Scenarios))
66 assert.Equal(t, 2, len(features[0].Scenarios[0].Steps))
67 assert.Equal(t, 2, len(features[0].Scenarios[1].Steps))
68 }
69
70 func TestBackgroundAndScenarios(t *testing.T) {
71 s := `
72 Feature: Parsing multiple scenarios
73 @tag_on_background
74 Background:
75 Given there is some background
76
77 Scenario: Scenario name here
78 Given some precondition
79 Then something happens
80
81 Scenario: Another scenario
82 Given another precondition
83 Then something happens
84 `
85 features, err := Parse(s)
86 assert.NoError(t, err)
87 assert.Equal(t, "@tag_on_background", features[0].Background.Tags[0])
88 assert.Equal(t, "there is some background", features[0].Background.Steps[0].Text)
89 assert.Equal(t, 2, len(features[0].Scenarios))
90 }
91
92 func TestMultipleFeatures(t *testing.T) {
93 s := `
94 Feature: Feature 1
95 Scenario: Scenario name here
96 Given some precondition
97 Then something happens
98
99 Feature: Feature 2
100 Scenario: Another scenario
101 Given another precondition
102 Then something happens
103 `
104 f, err := Parse(s)
105 assert.NoError(t, err)
106 assert.Equal(t, 2, len(f))
107 assert.Equal(t, "Feature 1", f[0].Title)
108 assert.Equal(t, "Feature 2", f[1].Title)
109 assert.Equal(t, 1, len(f[0].Scenarios))
110 assert.Equal(t, 1, len(f[1].Scenarios))
111
112 }
113
114 func TestTagParsing(t *testing.T) {
115 f, err := Parse("@tag1 @tag2@tag3\nFeature: Tag parsing")
116 assert.NoError(t, err)
117 assert.Equal(t, 2, len(f[0].Tags))
118 assert.Equal(t, "@tag1", f[0].Tags[0])
119 assert.Equal(t, "@tag2@tag3", f[0].Tags[1])
120 }
121
122 func TestBacktrackingCommentsAtEnd(t *testing.T) {
123 s := `
124 Feature: Comments at end
125 Scenario: Scenario name here
126 Given some precondition
127 Then something happens
128 # comments here
129 `
130 _, err := Parse(s)
131 assert.NoError(t, err)
132 }
133
134 func TestBacktrackingCommentsDontAffectIndent(t *testing.T) {
135 s := `
136 Feature: Comments at end
137 Scenario: Scenario name here
138 Given some precondition
139 Then something happens
140 # comments here
141 Scenario: Another scenario
142 Given a step
143 `
144 f, err := Parse(s)
145 assert.NoError(t, err)
146 assert.Equal(t, 2, len(f[0].Scenarios))
147 }
148
149 func TestScenarioOutlines(t *testing.T) {
150 s := `
151 Feature: Scenario outlines
152 Scenario Outline: Scenario 1
153 Given some value <foo>
154 Then some result <bar>
155
156 Examples:
157 | foo | bar |
158 | 1 | 2 |
159 | 3 | 4 |
160
161 Scenario: Scenario 2
162 Given some other scenario
163 `
164 f, err := Parse(s)
165 assert.NoError(t, err)
166 assert.Equal(t, 2, len(f[0].Scenarios))
167 assert.Equal(t, StringData("| foo | bar |\n| 1 | 2 |\n| 3 | 4 |"), f[0].Scenarios[0].Examples)
168 }
169
170 func TestStepArguments(t *testing.T) {
171 s := `
172 Feature: Step arguments
173 Scenario: Scenario 1
174 Given some data
175 | 1 | 2 |
176 | 3 | 4 |
177 And some docstring
178 """
179 hello
180 world
181 """
182 And some table
183 | 1 | 2 |
184 Then other text
185
186 Scenario: Scenario 2
187 Given some other scenario
188 `
189 f, err := Parse(s)
190 assert.NoError(t, err)
191 assert.Equal(t, 2, len(f[0].Scenarios))
192 assert.Equal(t, StringData("| 1 | 2 |\n| 3 | 4 |"), f[0].Scenarios[0].Steps[0].Argument)
193 assert.Equal(t, StringData(" hello\n world"), f[0].Scenarios[0].Steps[1].Argument)
194 assert.Equal(t, StringData("| 1 | 2 |"), f[0].Scenarios[0].Steps[2].Argument)
195 assert.Equal(t, "other text", f[0].Scenarios[0].Steps[3].Text)
196 }
197
198 func TestFailureNoFeature(t *testing.T) {
199 _, err := Parse("")
200 assert.EqualError(t, err, `parse error (<unknown>.feature:1): no features parsed.`)
201 }
202
203 func TestTagWithoutFeature(t *testing.T) {
204 _, err := Parse("@tag")
205 assert.EqualError(t, err, `parse error (<unknown>.feature:1): tags not applied to feature.`)
206 }
207
208 func TestFailureExpectingFeature(t *testing.T) {
209 _, err := Parse("@tag\n@tag")
210 assert.EqualError(t, err, `parse error (<unknown>.feature:2): expected "Feature:", found "@tag".`)
211 }
212
213 func TestFailureInvalidTag(t *testing.T) {
214 _, err := Parse("@tag tag")
215 assert.EqualError(t, err, `parse error (<unknown>.feature:1): invalid tag "tag".`)
216 }
217
218 func TestFailureDescriptionAfterTags(t *testing.T) {
219 s := `
220 Feature: Descriptions after tags
221 @tag1
222 Descriptions should not be allowed after tags
223
224 Scenario: Scenario name here
225 Given some precondition
226 Then something happens
227 `
228 _, err := Parse(s)
229 assert.EqualError(t, err, `parse error (<unknown>.feature:4): illegal description text after tags.`)
230 }
231
232 func TestFailureDescriptionAfterScenario(t *testing.T) {
233 s := `
234 Feature: Descriptions after scenario
235 Scenario: Scenario name here
236 Given some precondition
237 Then something happens
238
239 Descriptions should not be allowed after scenario
240
241 Scenario: Another scenario
242 Given some precondition
243 Then something happens
244 `
245 _, err := Parse(s)
246 assert.EqualError(t, err, `parse error (<unknown>.feature:7): illegal description text after scenario.`)
247 }
248
249 func TestFailureMultipleBackgrounds(t *testing.T) {
250 s := `
251 Feature: Multiple backgrounds
252 Background:
253 Given one
254
255 Background:
256 Given two
257 `
258 _, err := Parse(s)
259 assert.EqualError(t, err, `parse error (<unknown>.feature:6): multiple backgrounds not allowed.`)
260 }
261
262 func TestFailureBackgroundAfterScenario(t *testing.T) {
263 s := `
264 Feature: Background after scenario
265 Scenario: Scenario name here
266 Given some precondition
267 Then something happens
268
269 Background:
270 Given it's after a scenario
271 `
272 _, err := Parse(s)
273 assert.EqualError(t, err, `parse error (<unknown>.feature:7): illegal background after scenario.`)
274 }
275
276 func TestFailureInvalidStep(t *testing.T) {
277 s := `
278 Feature: Invalid steps
279 Scenario: Scenario name here
280 Invalid step
281 `
282 _, err := Parse(s)
283 assert.EqualError(t, err, `parse error (<unknown>.feature:4): illegal step prefix "Invalid".`)
284 }
285
286 func TestFailureNoStepText(t *testing.T) {
287 s := `
288 Feature: No step text
289 Scenario: Scenario name here
290 Given
291 `
292 _, err := Parse(s)
293 assert.EqualError(t, err, `parse error (<unknown>.feature:4): expected step text after "Given".`)
294 }
295
296 func TestFailureInvalidTagOnScenario(t *testing.T) {
297 s := `
298 Feature: Invalid tag on scenario
299 @invalid tags
300 Scenario:
301 Given a scenario
302 `
303 _, err := Parse(s)
304 assert.EqualError(t, err, `parse error (<unknown>.feature:3): invalid tag "tags".`)
305
306 }
307
308 func TestFailureInvalidBackground(t *testing.T) {
309 s := `
310 Feature: Invalid background
311 Background:
312 Invalid step
313 Scenario: A scenario
314 Given a scenario
315 `
316 _, err := Parse(s)
317 assert.EqualError(t, err, `parse error (<unknown>.feature:4): illegal step prefix "Invalid".`)
318 }