Debian Codegraph @

test/test_parser.py

raw ◀ ▶
1 Debian patch touch this file
1 #-*- coding: UTF-8 -*-
2
3 from __future__ import absolute_import
•4 from .tools import eq_, assert_raises, raises
5
6 from behave import i18n, model, parser
7
8 class Common(object):
9 def compare_steps(self, steps, expected):
10 have = [(s.step_type, s.keyword, s.name, s.text, s.table) for s in steps]
11 eq_(have, expected)
12
13 class TestParser(Common):
14 def test_parses_feature_name(self):
15 feature = parser.parse_feature(u"Feature: Stuff\n")
16 eq_(feature.name, "Stuff")
17
18 def test_parses_feature_name_without_newline(self):
19 feature = parser.parse_feature(u"Feature: Stuff")
20 eq_(feature.name, "Stuff")
21
22 def test_parses_feature_description(self):
23 doc = u"""
24 Feature: Stuff
25 In order to thing
26 As an entity
27 I want to do stuff
28 """.strip()
29 feature = parser.parse_feature(doc)
30 eq_(feature.name, "Stuff")
31 eq_(feature.description,
32 ["In order to thing", "As an entity", "I want to do stuff"])
33
34 def test_parses_feature_with_a_tag(self):
35 doc = u"""
36 @foo
37 Feature: Stuff
38 In order to thing
39 As an entity
40 I want to do stuff
41 """.strip()
42 feature = parser.parse_feature(doc)
43 eq_(feature.name, "Stuff")
44 eq_(feature.description,
45 ["In order to thing", "As an entity", "I want to do stuff"])
46 eq_(feature.tags, [model.Tag(u'foo', 1)])
47
48 def test_parses_feature_with_more_tags(self):
49 doc = u"""
50 @foo @bar @baz @qux @winkle_pickers @number8
51 Feature: Stuff
52 In order to thing
53 As an entity
54 I want to do stuff
55 """.strip()
56 feature = parser.parse_feature(doc)
57 eq_(feature.name, "Stuff")
58 eq_(feature.description,
59 ["In order to thing", "As an entity", "I want to do stuff"])
60 eq_(feature.tags, [model.Tag(name, 1)
61 for name in (u'foo', u'bar', u'baz', u'qux', u'winkle_pickers', u'number8')])
62
63 def test_parses_feature_with_a_tag_and_comment(self):
64 doc = u"""
65 @foo # Comment: ...
66 Feature: Stuff
67 In order to thing
68 As an entity
69 I want to do stuff
70 """.strip()
71 feature = parser.parse_feature(doc)
72 eq_(feature.name, "Stuff")
73 eq_(feature.description,
74 ["In order to thing", "As an entity", "I want to do stuff"])
75 eq_(feature.tags, [model.Tag(u'foo', 1)])
76
77 def test_parses_feature_with_more_tags_and_comment(self):
78 doc = u"""
79 @foo @bar @baz @qux @winkle_pickers # Comment: @number8
80 Feature: Stuff
81 In order to thing
82 As an entity
83 I want to do stuff
84 """.strip()
85 feature = parser.parse_feature(doc)
86 eq_(feature.name, "Stuff")
87 eq_(feature.description,
88 ["In order to thing", "As an entity", "I want to do stuff"])
89 eq_(feature.tags, [model.Tag(name, 1)
90 for name in (u'foo', u'bar', u'baz', u'qux', u'winkle_pickers')])
91 # -- NOT A TAG: u'number8'
92
93 def test_parses_feature_with_background(self):
94 doc = u"""
95 Feature: Stuff
96 Background:
97 Given there is stuff
98 When I do stuff
99 Then stuff happens
100 """.lstrip()
101 feature = parser.parse_feature(doc)
102 eq_(feature.name, "Stuff")
103 assert(feature.background)
104 self.compare_steps(feature.background.steps, [
105 ('given', 'Given', 'there is stuff', None, None),
106 ('when', 'When', 'I do stuff', None, None),
107 ('then', 'Then', 'stuff happens', None, None),
108 ])
109
110 def test_parses_feature_with_description_and_background(self):
111 doc = u"""
112 Feature: Stuff
113 This... is... STUFF!
114
115 Background:
116 Given there is stuff
117 When I do stuff
118 Then stuff happens
119 """.lstrip()
120 feature = parser.parse_feature(doc)
121 eq_(feature.name, "Stuff")
122 eq_(feature.description, ["This... is... STUFF!"])
123 assert(feature.background)
124 self.compare_steps(feature.background.steps, [
125 ('given', 'Given', 'there is stuff', None, None),
126 ('when', 'When', 'I do stuff', None, None),
127 ('then', 'Then', 'stuff happens', None, None),
128 ])
129
130 def test_parses_feature_with_a_scenario(self):
131 doc = u"""
132 Feature: Stuff
133
134 Scenario: Doing stuff
135 Given there is stuff
136 When I do stuff
137 Then stuff happens
138 """.lstrip()
139 feature = parser.parse_feature(doc)
140 eq_(feature.name, "Stuff")
141 assert(len(feature.scenarios) == 1)
142 eq_(feature.scenarios[0].name, 'Doing stuff')
143 self.compare_steps(feature.scenarios[0].steps, [
144 ('given', 'Given', 'there is stuff', None, None),
145 ('when', 'When', 'I do stuff', None, None),
146 ('then', 'Then', 'stuff happens', None, None),
147 ])
148
149 def test_parses_lowercase_step_keywords(self):
150 doc = u"""
151 Feature: Stuff
152
153 Scenario: Doing stuff
154 giVeN there is stuff
155 when I do stuff
156 tHEn stuff happens
157 """.lstrip()
158 feature = parser.parse_feature(doc)
159 eq_(feature.name, "Stuff")
160 assert(len(feature.scenarios) == 1)
161 eq_(feature.scenarios[0].name, 'Doing stuff')
162 self.compare_steps(feature.scenarios[0].steps, [
163 ('given', 'Given', 'there is stuff', None, None),
164 ('when', 'When', 'I do stuff', None, None),
165 ('then', 'Then', 'stuff happens', None, None),
166 ])
167
168 def test_parses_ja_keywords(self):
169 doc = u"""
170 機能: Stuff
171
172 シナリオ: Doing stuff
173 前提there is stuff
174 もしI do stuff
175 ならばstuff happens
176 """.lstrip()
177 feature = parser.parse_feature(doc, language='ja')
178 eq_(feature.name, "Stuff")
179 assert(len(feature.scenarios) == 1)
180 eq_(feature.scenarios[0].name, 'Doing stuff')
181 self.compare_steps(feature.scenarios[0].steps, [
182 ('given', u'前提', 'there is stuff', None, None),
183 ('when', u'もし', 'I do stuff', None, None),
184 ('then', u'ならば', 'stuff happens', None, None),
185 ])
186
187 def test_parses_feature_with_description_and_background_and_scenario(self):
188 doc = u"""
189 Feature: Stuff
190 Oh my god, it's full of stuff...
191
192 Background:
193 Given I found some stuff
194
195 Scenario: Doing stuff
196 Given there is stuff
197 When I do stuff
198 Then stuff happens
199 """.lstrip()
200 feature = parser.parse_feature(doc)
201 eq_(feature.name, "Stuff")
202 eq_(feature.description, ["Oh my god, it's full of stuff..."])
203 assert(feature.background)
204 self.compare_steps(feature.background.steps, [
205 ('given', 'Given', 'I found some stuff', None, None),
206 ])
207
208 assert(len(feature.scenarios) == 1)
209 eq_(feature.scenarios[0].name, 'Doing stuff')
210 self.compare_steps(feature.scenarios[0].steps, [
211 ('given', 'Given', 'there is stuff', None, None),
212 ('when', 'When', 'I do stuff', None, None),
213 ('then', 'Then', 'stuff happens', None, None),
214 ])
215
216 def test_parses_feature_with_multiple_scenarios(self):
217 doc = u"""
218 Feature: Stuff
219
220 Scenario: Doing stuff
221 Given there is stuff
222 When I do stuff
223 Then stuff happens
224
225 Scenario: Doing other stuff
226 When stuff happens
227 Then I am stuffed
228
229 Scenario: Doing different stuff
230 Given stuff
231 Then who gives a stuff
232 """.lstrip()
233 feature = parser.parse_feature(doc)
234 eq_(feature.name, "Stuff")
235
236 assert(len(feature.scenarios) == 3)
237
238 eq_(feature.scenarios[0].name, 'Doing stuff')
239 self.compare_steps(feature.scenarios[0].steps, [
240 ('given', 'Given', 'there is stuff', None, None),
241 ('when', 'When', 'I do stuff', None, None),
242 ('then', 'Then', 'stuff happens', None, None),
243 ])
244
245 eq_(feature.scenarios[1].name, 'Doing other stuff')
246 self.compare_steps(feature.scenarios[1].steps, [
247 ('when', 'When', 'stuff happens', None, None),
248 ('then', 'Then', 'I am stuffed', None, None),
249 ])
250
251 eq_(feature.scenarios[2].name, 'Doing different stuff')
252 self.compare_steps(feature.scenarios[2].steps, [
253 ('given', 'Given', 'stuff', None, None),
254 ('then', 'Then', 'who gives a stuff', None, None),
255 ])
256
257 def test_parses_feature_with_multiple_scenarios_with_tags(self):
258 doc = u"""
259 Feature: Stuff
260
261 Scenario: Doing stuff
262 Given there is stuff
263 When I do stuff
264 Then stuff happens
265
266 @one_tag
267 Scenario: Doing other stuff
268 When stuff happens
269 Then I am stuffed
270
271 @lots @of @tags
272 Scenario: Doing different stuff
273 Given stuff
274 Then who gives a stuff
275 """.lstrip()
276 feature = parser.parse_feature(doc)
277 eq_(feature.name, "Stuff")
278
279 assert(len(feature.scenarios) == 3)
280
281 eq_(feature.scenarios[0].name, 'Doing stuff')
282 self.compare_steps(feature.scenarios[0].steps, [
283 ('given', 'Given', 'there is stuff', None, None),
284 ('when', 'When', 'I do stuff', None, None),
285 ('then', 'Then', 'stuff happens', None, None),
286 ])
287
288 eq_(feature.scenarios[1].name, 'Doing other stuff')
289 eq_(feature.scenarios[1].tags, [model.Tag(u'one_tag', 1)])
290 self.compare_steps(feature.scenarios[1].steps, [
291 ('when', 'When', 'stuff happens', None, None),
292 ('then', 'Then', 'I am stuffed', None, None),
293 ])
294
295 eq_(feature.scenarios[2].name, 'Doing different stuff')
296 eq_(feature.scenarios[2].tags, [model.Tag(n, 1) for n in (u'lots', u'of', u'tags')])
297 self.compare_steps(feature.scenarios[2].steps, [
298 ('given', 'Given', 'stuff', None, None),
299 ('then', 'Then', 'who gives a stuff', None, None),
300 ])
301
302 def test_parses_feature_with_multiple_scenarios_and_other_bits(self):
303 doc = u"""
304 Feature: Stuff
305 Stuffing
306
307 Background:
308 Given you're all stuffed
309
310 Scenario: Doing stuff
311 Given there is stuff
312 When I do stuff
313 Then stuff happens
314
315 Scenario: Doing other stuff
316 When stuff happens
317 Then I am stuffed
318
319 Scenario: Doing different stuff
320 Given stuff
321 Then who gives a stuff
322 """.lstrip()
323 feature = parser.parse_feature(doc)
324 eq_(feature.name, "Stuff")
325 eq_(feature.description, ["Stuffing"])
326
327 assert(feature.background)
328 self.compare_steps(feature.background.steps, [
329 ('given', 'Given', "you're all stuffed", None, None)
330 ])
331
332 assert(len(feature.scenarios) == 3)
333
334 eq_(feature.scenarios[0].name, 'Doing stuff')
335 self.compare_steps(feature.scenarios[0].steps, [
336 ('given', 'Given', 'there is stuff', None, None),
337 ('when', 'When', 'I do stuff', None, None),
338 ('then', 'Then', 'stuff happens', None, None),
339 ])
340
341 eq_(feature.scenarios[1].name, 'Doing other stuff')
342 self.compare_steps(feature.scenarios[1].steps, [
343 ('when', 'When', 'stuff happens', None, None),
344 ('then', 'Then', 'I am stuffed', None, None),
345 ])
346
347 eq_(feature.scenarios[2].name, 'Doing different stuff')
348 self.compare_steps(feature.scenarios[2].steps, [
349 ('given', 'Given', 'stuff', None, None),
350 ('then', 'Then', 'who gives a stuff', None, None),
351 ])
352
353 def test_parses_feature_with_a_scenario_with_and_and_but(self):
354 doc = u"""
355 Feature: Stuff
356
357 Scenario: Doing stuff
358 Given there is stuff
359 And some other stuff
360 When I do stuff
361 Then stuff happens
362 But not the bad stuff
363 """.lstrip()
364 feature = parser.parse_feature(doc)
365 eq_(feature.name, "Stuff")
366 assert(len(feature.scenarios) == 1)
367 eq_(feature.scenarios[0].name, 'Doing stuff')
368 self.compare_steps(feature.scenarios[0].steps, [
369 ('given', 'Given', 'there is stuff', None, None),
370 ('given', 'And', 'some other stuff', None, None),
371 ('when', 'When', 'I do stuff', None, None),
372 ('then', 'Then', 'stuff happens', None, None),
373 ('then', 'But', 'not the bad stuff', None, None),
374 ])
375
376 def test_parses_feature_with_a_step_with_a_string_argument(self):
377 doc = u'''
378 Feature: Stuff
379
380 Scenario: Doing stuff
381 Given there is stuff:
382 """
383 So
384 Much
385 Stuff
386 """
387 Then stuff happens
388 '''.lstrip()
389 feature = parser.parse_feature(doc)
390 eq_(feature.name, "Stuff")
391 assert(len(feature.scenarios) == 1)
392 eq_(feature.scenarios[0].name, 'Doing stuff')
393 self.compare_steps(feature.scenarios[0].steps, [
394 ('given', 'Given', 'there is stuff', "So\nMuch\nStuff", None),
395 ('then', 'Then', 'stuff happens', None, None),
396 ])
397
398 def test_parses_string_argument_correctly_handle_whitespace(self):
399 doc = u'''
400 Feature: Stuff
401
402 Scenario: Doing stuff
403 Given there is stuff:
404 """
405 So
406 Much
407 Stuff
408 Has
409 Indents
410 """
411 Then stuff happens
412 '''.lstrip()
413 feature = parser.parse_feature(doc)
414 eq_(feature.name, "Stuff")
415 assert(len(feature.scenarios) == 1)
416 eq_(feature.scenarios[0].name, 'Doing stuff')
417 string = "So\n Much\n Stuff\n Has\nIndents"
418 self.compare_steps(feature.scenarios[0].steps, [
419 ('given', 'Given', 'there is stuff', string, None),
420 ('then', 'Then', 'stuff happens', None, None),
421 ])
422
423 def test_parses_feature_with_a_step_with_a_string_with_blank_lines(self):
424 doc = u'''
425 Feature: Stuff
426
427 Scenario: Doing stuff
428 Given there is stuff:
429 """
430 So
431
432 Much
433
434
435 Stuff
436 """
437 Then stuff happens
438 '''.lstrip()
439 feature = parser.parse_feature(doc)
440 eq_(feature.name, "Stuff")
441 assert(len(feature.scenarios) == 1)
442 eq_(feature.scenarios[0].name, 'Doing stuff')
443 self.compare_steps(feature.scenarios[0].steps, [
444 ('given', 'Given', 'there is stuff', "So\n\nMuch\n\n\nStuff", None),
445 ('then', 'Then', 'stuff happens', None, None),
446 ])
447
448 # MORE-JE-ADDED:
449 def test_parses_string_argument_without_stripping_empty_lines(self):
450 # -- ISSUE 44: Parser removes comments in multiline text string.
451 doc = u'''
452 Feature: Multiline
453
454 Scenario: Multiline Text with Comments
455 Given a multiline argument with:
456 """
457
458 """
459 And a multiline argument with:
460 """
461 Alpha.
462
463 Omega.
464 """
465 Then empty middle lines are not stripped
466 '''.lstrip()
467 feature = parser.parse_feature(doc)
468 eq_(feature.name, "Multiline")
469 assert(len(feature.scenarios) == 1)
470 eq_(feature.scenarios[0].name, "Multiline Text with Comments")
471 text1 = ""
472 text2 = "Alpha.\n\nOmega."
473 self.compare_steps(feature.scenarios[0].steps, [
474 ('given', 'Given', 'a multiline argument with', text1, None),
475 ('given', 'And', 'a multiline argument with', text2, None),
476 ('then', 'Then', 'empty middle lines are not stripped', None, None),
477 ])
478
479 def test_parses_feature_with_a_step_with_a_string_with_comments(self):
480 doc = u'''
481 Feature: Stuff
482
483 Scenario: Doing stuff
484 Given there is stuff:
485 """
486 So
487 Much
488 # Derp
489 """
490 Then stuff happens
491 '''.lstrip()
492 feature = parser.parse_feature(doc)
493 eq_(feature.name, "Stuff")
494 assert(len(feature.scenarios) == 1)
495 eq_(feature.scenarios[0].name, 'Doing stuff')
496 self.compare_steps(feature.scenarios[0].steps, [
497 ('given', 'Given', 'there is stuff', "So\nMuch\n# Derp",
498 None),
499 ('then', 'Then', 'stuff happens', None, None),
500 ])
501
502 def test_parses_feature_with_a_step_with_a_table_argument(self):
503 doc = u'''
504 Feature: Stuff
505
506 Scenario: Doing stuff
507 Given we classify stuff:
508 | type of stuff | awesomeness | ridiculousness |
509 | fluffy | large | frequent |
510 | lint | low | high |
511 | green | variable | awkward |
512 Then stuff is in buckets
513 '''.lstrip()
514 feature = parser.parse_feature(doc)
515 eq_(feature.name, "Stuff")
516 assert(len(feature.scenarios) == 1)
517 eq_(feature.scenarios[0].name, 'Doing stuff')
518 table = model.Table(
519 [u'type of stuff', u'awesomeness', u'ridiculousness'],
520 0,
521 [
522 [u'fluffy', u'large', u'frequent'],
523 [u'lint', u'low', u'high'],
524 [u'green', u'variable', u'awkward'],
525 ]
526 )
527 self.compare_steps(feature.scenarios[0].steps, [
528 ('given', 'Given', 'we classify stuff', None, table),
529 ('then', 'Then', 'stuff is in buckets', None, None),
530 ])
531
532 def test_parses_feature_with_table_and_escaped_pipe_in_cell_values(self):
533 doc = u'''
534 Feature:
535 Scenario:
536 Given we have special cell values:
537 | name | value |
538 | alice | one\|two |
539 | bob |\|one |
540 | charly | one\||
541 | doro | one\|two\|three\|four |
542 '''.lstrip()
543 feature = parser.parse_feature(doc)
544 assert(len(feature.scenarios) == 1)
545 table = model.Table(
546 [u"name", u"value"],
547 0,
548 [
549 [u"alice", u"one|two"],
550 [u"bob", u"|one"],
551 [u"charly", u"one|"],
552 [u"doro", u"one|two|three|four"],
553 ]
554 )
555 self.compare_steps(feature.scenarios[0].steps, [
556 ('given', 'Given', 'we have special cell values', None, table),
557 ])
558
559 def test_parses_feature_with_a_scenario_outline(self):
560 doc = u'''
561 Feature: Stuff
562
563 Scenario Outline: Doing all sorts of stuff
564 Given we have <Stuff>
565 When we do stuff
566 Then we have <Things>
567
568 Examples: Some stuff
569 | Stuff | Things |
570 | wool | felt |
571 | cotton | thread |
572 | wood | paper |
573 | explosives | hilarity |
574 '''.lstrip()
575 feature = parser.parse_feature(doc)
576 eq_(feature.name, "Stuff")
577
578 assert(len(feature.scenarios) == 1)
579 eq_(feature.scenarios[0].name, 'Doing all sorts of stuff')
580
581 table = model.Table(
582 [u'Stuff', u'Things'],
583 0,
584 [
585 [u'wool', u'felt'],
586 [u'cotton', u'thread'],
587 [u'wood', u'paper'],
588 [u'explosives', u'hilarity'],
589 ]
590 )
591 eq_(feature.scenarios[0].examples[0].name, 'Some stuff')
592 eq_(feature.scenarios[0].examples[0].table, table)
593 self.compare_steps(feature.scenarios[0].steps, [
594 ('given', 'Given', 'we have <Stuff>', None, None),
595 ('when', 'When', 'we do stuff', None, None),
596 ('then', 'Then', 'we have <Things>', None, None),
597 ])
598
599 def test_parses_feature_with_a_scenario_outline_with_multiple_examples(self):
600 doc = u'''
601 Feature: Stuff
602
603 Scenario Outline: Doing all sorts of stuff
604 Given we have <Stuff>
605 When we do stuff
606 Then we have <Things>
607
608 Examples: Some stuff
609 | Stuff | Things |
610 | wool | felt |
611 | cotton | thread |
612
613 Examples: Some other stuff
614 | Stuff | Things |
615 | wood | paper |
616 | explosives | hilarity |
617 '''.lstrip()
618 feature = parser.parse_feature(doc)
619 eq_(feature.name, "Stuff")
620
621 assert(len(feature.scenarios) == 1)
622 eq_(feature.scenarios[0].name, 'Doing all sorts of stuff')
623 self.compare_steps(feature.scenarios[0].steps, [
624 ('given', 'Given', 'we have <Stuff>', None, None),
625 ('when', 'When', 'we do stuff', None, None),
626 ('then', 'Then', 'we have <Things>', None, None),
627 ])
628
629 table = model.Table(
630 [u'Stuff', u'Things'],
631 0,
632 [
633 [u'wool', u'felt'],
634 [u'cotton', u'thread'],
635 ]
636 )
637 eq_(feature.scenarios[0].examples[0].name, 'Some stuff')
638 eq_(feature.scenarios[0].examples[0].table, table)
639
640 table = model.Table(
641 [u'Stuff', u'Things'],
642 0,
643 [
644 [u'wood', u'paper'],
645 [u'explosives', u'hilarity'],
646 ]
647 )
648 eq_(feature.scenarios[0].examples[1].name, 'Some other stuff')
649 eq_(feature.scenarios[0].examples[1].table, table)
650
651 def test_parses_feature_with_a_scenario_outline_with_tags(self):
652 doc = u'''
653 Feature: Stuff
654
655 @stuff @derp
656 Scenario Outline: Doing all sorts of stuff
657 Given we have <Stuff>
658 When we do stuff
659 Then we have <Things>
660
661 Examples: Some stuff
662 | Stuff | Things |
663 | wool | felt |
664 | cotton | thread |
665 | wood | paper |
666 | explosives | hilarity |
667 '''.lstrip()
668 feature = parser.parse_feature(doc)
669 eq_(feature.name, "Stuff")
670
671 assert(len(feature.scenarios) == 1)
672 eq_(feature.scenarios[0].name, 'Doing all sorts of stuff')
673 eq_(feature.scenarios[0].tags, [model.Tag(u'stuff', 1), model.Tag(u'derp', 1)])
674 self.compare_steps(feature.scenarios[0].steps, [
675 ('given', 'Given', 'we have <Stuff>', None, None),
676 ('when', 'When', 'we do stuff', None, None),
677 ('then', 'Then', 'we have <Things>', None, None),
678 ])
679
680 table = model.Table(
681 [u'Stuff', u'Things'],
682 0,
683 [
684 [u'wool', u'felt'],
685 [u'cotton', u'thread'],
686 [u'wood', u'paper'],
687 [u'explosives', u'hilarity'],
688 ]
689 )
690 eq_(feature.scenarios[0].examples[0].name, 'Some stuff')
691 eq_(feature.scenarios[0].examples[0].table, table)
692
693 def test_parses_scenario_outline_with_tagged_examples1(self):
694 # -- CASE: Examples with 1 tag-line (= 1 tag)
695 doc = u'''
696 Feature: Alice
697
698 @foo
699 Scenario Outline: Bob
700 Given we have <Stuff>
701
702 @bar
703 Examples: Charly
704 | Stuff | Things |
705 | wool | felt |
706 | cotton | thread |
707 '''.lstrip()
708 feature = parser.parse_feature(doc)
709 eq_(feature.name, "Alice")
710
711 assert(len(feature.scenarios) == 1)
712 scenario_outline = feature.scenarios[0]
713 eq_(scenario_outline.name, "Bob")
714 eq_(scenario_outline.tags, [model.Tag(u"foo", 1)])
715 self.compare_steps(scenario_outline.steps, [
716 ("given", "Given", "we have <Stuff>", None, None),
717 ])
718
719 table = model.Table(
720 [u"Stuff", u"Things"], 0,
721 [
722 [u"wool", u"felt"],
723 [u"cotton", u"thread"],
724 ]
725 )
726 eq_(scenario_outline.examples[0].name, "Charly")
727 eq_(scenario_outline.examples[0].table, table)
728 eq_(scenario_outline.examples[0].tags, [model.Tag(u"bar", 1)])
729
730 # -- ScenarioOutline.scenarios:
731 # Inherit tags from ScenarioOutline and Examples element.
732 eq_(len(scenario_outline.scenarios), 2)
733 expected_tags = [model.Tag(u"foo", 1), model.Tag(u"bar", 1)]
734 eq_(set(scenario_outline.scenarios[0].tags), set(expected_tags))
735 eq_(set(scenario_outline.scenarios[1].tags), set(expected_tags))
736
737 def test_parses_scenario_outline_with_tagged_examples2(self):
738 # -- CASE: Examples with multiple tag-lines (= 2 tag-lines)
739 doc = u'''
740 Feature: Alice
741
742 @foo
743 Scenario Outline: Bob
744 Given we have <Stuff>
745
746 @bar
747 @baz
748 Examples: Charly
749 | Stuff | Things |
750 | wool | felt |
751 | cotton | thread |
752 '''.lstrip()
753 feature = parser.parse_feature(doc)
754 eq_(feature.name, "Alice")
755
756 assert(len(feature.scenarios) == 1)
757 scenario_outline = feature.scenarios[0]
758 eq_(scenario_outline.name, "Bob")
759 eq_(scenario_outline.tags, [model.Tag(u"foo", 1)])
760 self.compare_steps(scenario_outline.steps, [
761 ("given", "Given", "we have <Stuff>", None, None),
762 ])
763
764 table = model.Table(
765 [u"Stuff", u"Things"], 0,
766 [
767 [u"wool", u"felt"],
768 [u"cotton", u"thread"],
769 ]
770 )
771 eq_(scenario_outline.examples[0].name, "Charly")
772 eq_(scenario_outline.examples[0].table, table)
773 expected_tags = [model.Tag(u"bar", 1), model.Tag(u"baz", 1)]
774 eq_(scenario_outline.examples[0].tags, expected_tags)
775
776 # -- ScenarioOutline.scenarios:
777 # Inherit tags from ScenarioOutline and Examples element.
778 eq_(len(scenario_outline.scenarios), 2)
779 expected_tags = [
780 model.Tag(u"foo", 1),
781 model.Tag(u"bar", 1),
782 model.Tag(u"baz", 1)
783 ]
784 eq_(set(scenario_outline.scenarios[0].tags), set(expected_tags))
785 eq_(set(scenario_outline.scenarios[1].tags), set(expected_tags))
786
787 def test_parses_feature_with_the_lot(self):
788 doc = u'''
789 # This one's got comments too.
790
791 @derp
792 Feature: Stuff
793 In order to test my parser
794 As a test runner
795 I want to run tests
796
797 # A møøse once bit my sister
798 Background:
799 Given this is a test
800
801 @fred
802 Scenario: Testing stuff
803 Given we are testing
804 And this is only a test
805 But this is an important test
806 When we test with a multiline string:
807 """
808 Yarr, my hovercraft be full of stuff.
809 Also, I be feelin' this pirate schtick be a mite overdone, me hearties.
810 Also: rum.
811 """
812 Then we want it to work
813
814 #These comments are everywhere man
815 Scenario Outline: Gosh this is long
816 Given this is <length>
817 Then we want it to be <width>
818 But not <height>
819
820 Examples: Initial
821 | length | width | height |
822 # I don't know why this one is here
823 | 1 | 2 | 3 |
824 | 4 | 5 | 6 |
825
826 Examples: Subsequent
827 | length | width | height |
828 | 7 | 8 | 9 |
829
830 Scenario: This one doesn't have a tag
831 Given we don't have a tag
832 Then we don't really mind
833
834 @stuff @derp
835 Scenario Outline: Doing all sorts of stuff
836 Given we have <Stuff>
837 When we do stuff with a table:
838 | a | b | c | d | e |
839 | 1 | 2 | 3 | 4 | 5 |
840 # I can see a comment line from here
841 | 6 | 7 | 8 | 9 | 10 |
842 Then we have <Things>
843
844 Examples: Some stuff
845 | Stuff | Things |
846 | wool | felt |
847 | cotton | thread |
848 | wood | paper |
849 | explosives | hilarity |
850 '''.lstrip()
851 feature = parser.parse_feature(doc)
852 eq_(feature.name, "Stuff")
853 eq_(feature.tags, [model.Tag(u'derp', 1)])
854 eq_(feature.description, ['In order to test my parser',
855 'As a test runner',
856 'I want to run tests'])
857
858 assert(feature.background)
859 self.compare_steps(feature.background.steps, [
860 ('given', 'Given', 'this is a test', None, None)
861 ])
862
863 assert(len(feature.scenarios) == 4)
864
865 eq_(feature.scenarios[0].name, 'Testing stuff')
866 eq_(feature.scenarios[0].tags, [model.Tag(u'fred', 1)])
867 string = '\n'.join([
868 'Yarr, my hovercraft be full of stuff.',
869 "Also, I be feelin' this pirate schtick be a mite overdone, " + \
870 "me hearties.",
871 ' Also: rum.'
872 ])
873 self.compare_steps(feature.scenarios[0].steps, [
874 ('given', 'Given', 'we are testing', None, None),
875 ('given', 'And', 'this is only a test', None, None),
876 ('given', 'But', 'this is an important test', None, None),
877 ('when', 'When', 'we test with a multiline string', string, None),
878 ('then', 'Then', 'we want it to work', None, None),
879 ])
880
881 eq_(feature.scenarios[1].name, 'Gosh this is long')
882 eq_(feature.scenarios[1].tags, [])
883 table = model.Table(
884 [u'length', u'width', u'height'],
885 0,
886 [
887 [u'1', u'2', u'3'],
888 [u'4', u'5', u'6'],
889 ]
890 )
891 eq_(feature.scenarios[1].examples[0].name, 'Initial')
892 eq_(feature.scenarios[1].examples[0].table, table)
893 table = model.Table(
894 [u'length', u'width', u'height'],
895 0,
896 [
897 [u'7', u'8', u'9'],
898 ]
899 )
900 eq_(feature.scenarios[1].examples[1].name, 'Subsequent')
901 eq_(feature.scenarios[1].examples[1].table, table)
902 self.compare_steps(feature.scenarios[1].steps, [
903 ('given', 'Given', 'this is <length>', None, None),
904 ('then', 'Then', 'we want it to be <width>', None, None),
905 ('then', 'But', 'not <height>', None, None),
906 ])
907
908 eq_(feature.scenarios[2].name, "This one doesn't have a tag")
909 eq_(feature.scenarios[2].tags, [])
910 self.compare_steps(feature.scenarios[2].steps, [
911 ('given', 'Given', "we don't have a tag", None, None),
912 ('then', 'Then', "we don't really mind", None, None),
913 ])
914
915 table = model.Table(
916 [u'Stuff', u'Things'],
917 0,
918 [
919 [u'wool', u'felt'],
920 [u'cotton', u'thread'],
921 [u'wood', u'paper'],
922 [u'explosives', u'hilarity'],
923 ]
924 )
925 eq_(feature.scenarios[3].name, 'Doing all sorts of stuff')
926 eq_(feature.scenarios[3].tags, [model.Tag(u'stuff', 1), model.Tag(u'derp', 1)])
927 eq_(feature.scenarios[3].examples[0].name, 'Some stuff')
928 eq_(feature.scenarios[3].examples[0].table, table)
929 table = model.Table(
930 [u'a', u'b', u'c', u'd', u'e'],
931 0,
932 [
933 [u'1', u'2', u'3', u'4', u'5'],
934 [u'6', u'7', u'8', u'9', u'10'],
935 ]
936 )
937 self.compare_steps(feature.scenarios[3].steps, [
938 ('given', 'Given', 'we have <Stuff>', None, None),
939 ('when', 'When', 'we do stuff with a table', None, table),
940 ('then', 'Then', 'we have <Things>', None, None),
941 ])
942
943
944 def test_fails_to_parse_when_and_is_out_of_order(self):
945 doc = u"""
946 Feature: Stuff
947
948 Scenario: Failing at stuff
949 And we should fail
950 """.lstrip()
951 assert_raises(parser.ParserError, parser.parse_feature, doc)
952
953 def test_fails_to_parse_when_but_is_out_of_order(self):
954 doc = u"""
955 Feature: Stuff
956
957 Scenario: Failing at stuff
958 But we shall fail
959 """.lstrip()
960 assert_raises(parser.ParserError, parser.parse_feature, doc)
961
962 def test_fails_to_parse_when_examples_is_in_the_wrong_place(self):
963 doc = u"""
964 Feature: Stuff
965
966 Scenario: Failing at stuff
967 But we shall fail
968
969 Examples: Failure
970 | Fail | Wheel|
971 """.lstrip()
972 assert_raises(parser.ParserError, parser.parse_feature, doc)
973
974 class TestForeign(Common):
975 def test_first_line_comment_sets_language(self):
976 doc = u"""
977 # language: fr
978 Fonctionnalit\xe9: testing stuff
979 Oh my god, it's full of stuff...
980 """
981
982 feature = parser.parse_feature(doc)
983 eq_(feature.name, "testing stuff")
984 eq_(feature.description, ["Oh my god, it's full of stuff..."])
985
986 def test_multiple_language_comments(self):
987 # -- LAST LANGUAGE is used.
988 doc = u"""
989 # language: en
990 # language: fr
991 Fonctionnalit\xe9: testing stuff
992 Oh my god, it's full of stuff...
993 """
994
995 feature = parser.parse_feature(doc)
996 eq_(feature.name, "testing stuff")
997 eq_(feature.description, ["Oh my god, it's full of stuff..."])
998
999 def test_language_comment_wins_over_commandline(self):
1000 doc = u"""
1001 # language: fr
1002 Fonctionnalit\xe9: testing stuff
1003 Oh my god, it's full of stuff...
1004 """
1005
1006 feature = parser.parse_feature(doc, language="de")
1007 eq_(feature.name, "testing stuff")
1008 eq_(feature.description, ["Oh my god, it's full of stuff..."])
1009
1010 def test_whitespace_before_first_line_comment_still_sets_language(self):
1011 doc = u"""
1012
1013
1014 # language: cs
1015 Po\u017eadavek: testing stuff
1016 Oh my god, it's full of stuff...
1017 """
1018
1019 feature = parser.parse_feature(doc)
1020 eq_(feature.name, "testing stuff")
1021 eq_(feature.description, ["Oh my god, it's full of stuff..."])
1022
1023 def test_anything_before_language_comment_makes_it_not_count(self):
1024 doc = u"""
1025
1026 @wombles
1027 # language: cy-GB
1028 Arwedd: testing stuff
1029 Oh my god, it's full of stuff...
1030 """
1031
1032 assert_raises(parser.ParserError, parser.parse_feature, doc)
1033
1034 def test_defaults_to_DEFAULT_LANGUAGE(self):
1035 feature_kwd = i18n.languages[parser.DEFAULT_LANGUAGE]['feature'][0]
1036 doc = u"""
1037
1038 @wombles
1039 # language: cs
1040 %s: testing stuff
1041 Oh my god, it's full of stuff...
1042 """ % feature_kwd
1043
1044 feature = parser.parse_feature(doc)
1045 eq_(feature.name, "testing stuff")
1046 eq_(feature.description, ["Oh my god, it's full of stuff..."])
1047
1048 def test_whitespace_in_the_language_comment_is_flexible_1(self):
1049 doc = u"""
1050 #language:da
1051 Egenskab: testing stuff
1052 Oh my god, it's full of stuff...
1053 """
1054
1055 feature = parser.parse_feature(doc)
1056 eq_(feature.name, "testing stuff")
1057 eq_(feature.description, ["Oh my god, it's full of stuff..."])
1058
1059 def test_whitespace_in_the_language_comment_is_flexible_2(self):
1060 doc = u"""
1061 # language:de
1062 Funktionalit\xe4t: testing stuff
1063 Oh my god, it's full of stuff...
1064 """
1065
1066 feature = parser.parse_feature(doc)
1067 eq_(feature.name, "testing stuff")
1068 eq_(feature.description, ["Oh my god, it's full of stuff..."])
1069
1070 def test_whitespace_in_the_language_comment_is_flexible_3(self):
1071 doc = u"""
1072 #language: en-lol
1073 OH HAI: testing stuff
1074 Oh my god, it's full of stuff...
1075 """
1076
1077 feature = parser.parse_feature(doc)
1078 eq_(feature.name, "testing stuff")
1079 eq_(feature.description, ["Oh my god, it's full of stuff..."])
1080
1081 def test_whitespace_in_the_language_comment_is_flexible_4(self):
1082 doc = u"""
1083 # language: lv
1084 F\u012b\u010da: testing stuff
1085 Oh my god, it's full of stuff...
1086 """
1087
1088 feature = parser.parse_feature(doc)
1089 eq_(feature.name, "testing stuff")
1090 eq_(feature.description, ["Oh my god, it's full of stuff..."])
1091
1092 def test_parses_french(self):
1093 doc = u"""
1094 Fonctionnalit\xe9: testing stuff
1095 Oh my god, it's full of stuff...
1096
1097 Contexte:
1098 Soit I found some stuff
1099
1100 Sc\xe9nario: test stuff
1101 Soit I am testing stuff
1102 Alors it should work
1103
1104 Sc\xe9nario: test more stuff
1105 Soit I am testing stuff
1106 Alors it will work
1107 """.lstrip()
1108 feature = parser.parse_feature(doc, 'fr')
1109 eq_(feature.name, "testing stuff")
1110 eq_(feature.description, ["Oh my god, it's full of stuff..."])
1111 assert(feature.background)
1112 self.compare_steps(feature.background.steps, [
1113 ('given', 'Soit', 'I found some stuff', None, None),
1114 ])
1115
1116 assert(len(feature.scenarios) == 2)
1117 eq_(feature.scenarios[0].name, 'test stuff')
1118 self.compare_steps(feature.scenarios[0].steps, [
1119 ('given', 'Soit', 'I am testing stuff', None, None),
1120 ('then', 'Alors', 'it should work', None, None),
1121 ])
1122
1123 def test_parses_french_multi_word(self):
1124 doc = u"""
1125 Fonctionnalit\xe9: testing stuff
1126 Oh my god, it's full of stuff...
1127
1128 Sc\xe9nario: test stuff
1129 Etant donn\xe9 I am testing stuff
1130 Alors it should work
1131 """.lstrip()
1132 feature = parser.parse_feature(doc, 'fr')
1133 eq_(feature.name, "testing stuff")
1134 eq_(feature.description, ["Oh my god, it's full of stuff..."])
1135
1136 assert(len(feature.scenarios) == 1)
1137 eq_(feature.scenarios[0].name, 'test stuff')
1138 self.compare_steps(feature.scenarios[0].steps, [
1139 ('given', u'Etant donn\xe9', 'I am testing stuff', None, None),
1140 ('then', 'Alors', 'it should work', None, None),
1141 ])
1142 test_parses_french_multi_word.go = 1
1143
1144 def test_properly_handles_whitespace_on_keywords_that_do_not_want_it(self):
1145 doc = u"""
1146 # language: zh-TW
1147
1148 \u529f\u80fd: I have no idea what I'm saying
1149
1150 \u5834\u666f: No clue whatsoever
1151 \u5047\u8a2dI've got no idea
1152 \u7576I say things
1153 \u800c\u4e14People don't understand
1154 \u90a3\u9ebcPeople should laugh
1155 \u4f46\u662fI should take it well
1156 """
1157
1158 feature = parser.parse_feature(doc)
1159 eq_(feature.name, "I have no idea what I'm saying")
1160
1161 eq_(len(feature.scenarios), 1)
1162 eq_(feature.scenarios[0].name, 'No clue whatsoever')
1163 self.compare_steps(feature.scenarios[0].steps, [
1164 ('given', u'\u5047\u8a2d', "I've got no idea", None, None),
1165 ('when', u'\u7576', 'I say things', None, None),
1166 ('when', u'\u800c\u4e14', "People don't understand", None, None),
1167 ('then', u'\u90a3\u9ebc', "People should laugh", None, None),
1168 ('then', u'\u4f46\u662f', "I should take it well", None, None),
1169 ])
1170
1171
1172 class TestParser4ScenarioDescription(Common):
1173
1174 def test_parse_scenario_description(self):
1175 doc = u'''
1176 Feature: Scenario Description
1177
1178 Scenario: With scenario description
1179
1180 First line of scenario description.
1181 Second line of scenario description.
1182
1183 Third line of scenario description (after an empty line).
1184
1185 Given we have stuff
1186 When we do stuff
1187 Then we have things
1188 '''.lstrip()
1189 feature = parser.parse_feature(doc)
1190 eq_(feature.name, "Scenario Description")
1191
1192 assert(len(feature.scenarios) == 1)
1193 eq_(feature.scenarios[0].name, "With scenario description")
1194 eq_(feature.scenarios[0].tags, [])
1195 eq_(feature.scenarios[0].description, [
1196 "First line of scenario description.",
1197 "Second line of scenario description.",
1198 "Third line of scenario description (after an empty line).",
1199 ])
1200 self.compare_steps(feature.scenarios[0].steps, [
1201 ('given', 'Given', 'we have stuff', None, None),
1202 ('when', 'When', 'we do stuff', None, None),
1203 ('then', 'Then', 'we have things', None, None),
1204 ])
1205
1206
1207 def test_parse_scenario_with_description_but_without_steps(self):
1208 doc = u'''
1209 Feature: Scenario Description
1210
1211 Scenario: With description but without steps
1212
1213 First line of scenario description.
1214 Second line of scenario description.
1215
1216 Scenario: Another one
1217 Given we have stuff
1218 When we do stuff
1219 Then we have things
1220 '''.lstrip()
1221 feature = parser.parse_feature(doc)
1222 eq_(feature.name, "Scenario Description")
1223
1224 assert(len(feature.scenarios) == 2)
1225 eq_(feature.scenarios[0].name, "With description but without steps")
1226 eq_(feature.scenarios[0].tags, [])
1227 eq_(feature.scenarios[0].description, [
1228 "First line of scenario description.",
1229 "Second line of scenario description.",
1230 ])
1231 eq_(feature.scenarios[0].steps, [])
1232
1233 eq_(feature.scenarios[1].name, "Another one")
1234 eq_(feature.scenarios[1].tags, [])
1235 eq_(feature.scenarios[1].description, [])
1236 self.compare_steps(feature.scenarios[1].steps, [
1237 ('given', 'Given', 'we have stuff', None, None),
1238 ('when', 'When', 'we do stuff', None, None),
1239 ('then', 'Then', 'we have things', None, None),
1240 ])
1241
1242
1243 def test_parse_scenario_with_description_but_without_steps_followed_by_scenario_with_tags(self):
1244 doc = u'''
1245 Feature: Scenario Description
1246
1247 Scenario: With description but without steps
1248
1249 First line of scenario description.
1250 Second line of scenario description.
1251
1252 @foo @bar
1253 Scenario: Another one
1254 Given we have stuff
1255 When we do stuff
1256 Then we have things
1257 '''.lstrip()
1258 feature = parser.parse_feature(doc)
1259 eq_(feature.name, "Scenario Description")
1260
1261 assert(len(feature.scenarios) == 2)
1262 eq_(feature.scenarios[0].name, "With description but without steps")
1263 eq_(feature.scenarios[0].tags, [])
1264 eq_(feature.scenarios[0].description, [
1265 "First line of scenario description.",
1266 "Second line of scenario description.",
1267 ])
1268 eq_(feature.scenarios[0].steps, [])
1269
1270 eq_(feature.scenarios[1].name, "Another one")
1271 eq_(feature.scenarios[1].tags, ["foo", "bar"])
1272 eq_(feature.scenarios[1].description, [])
1273 self.compare_steps(feature.scenarios[1].steps, [
1274 ('given', 'Given', 'we have stuff', None, None),
1275 ('when', 'When', 'we do stuff', None, None),
1276 ('then', 'Then', 'we have things', None, None),
1277 ])
1278
1279 def test_parse_two_scenarios_with_description(self):
1280 doc = u'''
1281 Feature: Scenario Description
1282
1283 Scenario: One with description but without steps
1284
1285 First line of scenario description.
1286 Second line of scenario description.
1287
1288 Scenario: Two with description and with steps
1289
1290 Another line of scenario description.
1291
1292 Given we have stuff
1293 When we do stuff
1294 Then we have things
1295 '''.lstrip()
1296 feature = parser.parse_feature(doc)
1297 eq_(feature.name, "Scenario Description")
1298
1299 assert(len(feature.scenarios) == 2)
1300 eq_(feature.scenarios[0].name, "One with description but without steps")
1301 eq_(feature.scenarios[0].tags, [])
1302 eq_(feature.scenarios[0].description, [
1303 "First line of scenario description.",
1304 "Second line of scenario description.",
1305 ])
1306 eq_(feature.scenarios[0].steps, [])
1307
1308 eq_(feature.scenarios[1].name, "Two with description and with steps")
1309 eq_(feature.scenarios[1].tags, [])
1310 eq_(feature.scenarios[1].description, [
1311 "Another line of scenario description.",
1312 ])
1313 self.compare_steps(feature.scenarios[1].steps, [
1314 ('given', 'Given', 'we have stuff', None, None),
1315 ('when', 'When', 'we do stuff', None, None),
1316 ('then', 'Then', 'we have things', None, None),
1317 ])
1318
1319
1320 def parse_tags(line):
1321 the_parser = parser.Parser()
1322 return the_parser.parse_tags(line.strip())
1323
1324 class TestParser4Tags(Common):
1325
1326 def test_parse_tags_with_one_tag(self):
1327 tags = parse_tags('@one ')
1328 eq_(len(tags), 1)
1329 eq_(tags[0], "one")
1330
1331 def test_parse_tags_with_more_tags(self):
1332 tags = parse_tags('@one @two.three-four @xxx')
1333 eq_(len(tags), 3)
1334 eq_(tags, [model.Tag(name, 1)
1335 for name in (u'one', u'two.three-four', u'xxx' )])
1336
1337 def test_parse_tags_with_tag_and_comment(self):
1338 tags = parse_tags('@one # @fake-tag-in-comment xxx')
1339 eq_(len(tags), 1)
1340 eq_(tags[0], "one")
1341
1342 def test_parse_tags_with_tags_and_comment(self):
1343 tags = parse_tags('@one @two.three-four @xxx # @fake-tag-in-comment xxx')
1344 eq_(len(tags), 3)
1345 eq_(tags, [model.Tag(name, 1)
1346 for name in (u'one', u'two.three-four', u'xxx' )])
1347
1348 @raises(parser.ParserError)
1349 def test_parse_tags_with_invalid_tags(self):
1350 parse_tags('@one invalid.tag boom')
1351
1352
1353 class TestParser4Background(Common):
1354
1355 def test_parse_background(self):
1356 doc = u'''
1357 Feature: Background
1358
1359 A feature description line 1.
1360 A feature description line 2.
1361
1362 Background: One
1363 Given we init stuff
1364 When we init more stuff
1365
1366 Scenario: One
1367 Given we have stuff
1368 When we do stuff
1369 Then we have things
1370 '''.lstrip()
1371 feature = parser.parse_feature(doc)
1372 eq_(feature.name, "Background")
1373 eq_(feature.description, [
1374 "A feature description line 1.",
1375 "A feature description line 2.",
1376 ])
1377 assert feature.background is not None
1378 eq_(feature.background.name, "One")
1379 self.compare_steps(feature.background.steps, [
1380 ('given', 'Given', 'we init stuff', None, None),
1381 ('when', 'When', 'we init more stuff', None, None),
1382 ])
1383
1384 assert(len(feature.scenarios) == 1)
1385 eq_(feature.scenarios[0].name, "One")
1386 eq_(feature.scenarios[0].tags, [])
1387 self.compare_steps(feature.scenarios[0].steps, [
1388 ('given', 'Given', 'we have stuff', None, None),
1389 ('when', 'When', 'we do stuff', None, None),
1390 ('then', 'Then', 'we have things', None, None),
1391 ])
1392
1393
1394 def test_parse_background_with_tags_should_fail(self):
1395 doc = u'''
1396 Feature: Background with tags
1397 Expect that a ParserError occurs
1398 because Background does not support tags/tagging.
1399
1400 @tags_are @not_supported
1401 @here
1402 Background: One
1403 Given we init stuff
1404 '''.lstrip()
1405 assert_raises(parser.ParserError, parser.parse_feature, doc)
1406
1407
1408 def test_parse_two_background_should_fail(self):
1409 doc = u'''
1410 Feature: Two Backgrounds
1411 Expect that a ParserError occurs
1412 because at most one Background is supported.
1413
1414 Background: One
1415 Given we init stuff
1416
1417 Background: Two
1418 When we init more stuff
1419 '''.lstrip()
1420 assert_raises(parser.ParserError, parser.parse_feature, doc)
1421
1422
1423 def test_parse_background_after_scenario_should_fail(self):
1424 doc = u'''
1425 Feature: Background after Scenario
1426 Expect that a ParserError occurs
1427 because Background is only allowed before any Scenario.
1428
1429 Scenario: One
1430 Given we have stuff
1431
1432 Background: Two
1433 When we init more stuff
1434 '''.lstrip()
1435 assert_raises(parser.ParserError, parser.parse_feature, doc)
1436
1437
1438 def test_parse_background_after_scenario_outline_should_fail(self):
1439 doc = u'''
1440 Feature: Background after ScenarioOutline
1441 Expect that a ParserError occurs
1442 because Background is only allowed before any ScenarioOuline.
1443 Scenario Outline: ...
1444 Given there is <name>
1445
1446 Examples:
1447 | name |
1448 | Alice |
1449
1450 Background: Two
1451 When we init more stuff
1452 '''.lstrip()
1453 assert_raises(parser.ParserError, parser.parse_feature, doc)
1454
1455
1456 class TestParser4Steps(Common):
1457 """
1458 Tests parser.parse_steps() and parser.Parser.parse_steps() functionality.
1459 """
1460
1461 def test_parse_steps_with_simple_steps(self):
1462 doc = u'''
1463 Given a simple step
1464 When I have another simple step
1465 And I have another simple step
1466 Then every step will be parsed without errors
1467 '''.lstrip()
1468 steps = parser.parse_steps(doc)
1469 eq_(len(steps), 4)
1470 # -- EXPECTED STEP DATA:
1471 # SCHEMA: step_type, keyword, name, text, table
1472 self.compare_steps(steps, [
1473 ("given", "Given", "a simple step", None, None),
1474 ("when", "When", "I have another simple step", None, None),
1475 ("when", "And", "I have another simple step", None, None),
1476 ("then", "Then", "every step will be parsed without errors",
1477 None, None),
1478 ])
1479
1480 def test_parse_steps_with_multiline_text(self):
1481 doc = u'''
1482 Given a step with multi-line text:
1483 """
1484 Lorem ipsum
1485 Ipsum lorem
1486 """
1487 When I have a step with multi-line text:
1488 """
1489 Ipsum lorem
1490 Lorem ipsum
1491 """
1492 Then every step will be parsed without errors
1493 '''.lstrip()
1494 steps = parser.parse_steps(doc)
1495 eq_(len(steps), 3)
1496 # -- EXPECTED STEP DATA:
1497 # SCHEMA: step_type, keyword, name, text, table
1498 text1 = "Lorem ipsum\nIpsum lorem"
1499 text2 = "Ipsum lorem\nLorem ipsum"
1500 self.compare_steps(steps, [
1501 ("given", "Given", "a step with multi-line text", text1, None),
1502 ("when", "When", "I have a step with multi-line text", text2, None),
1503 ("then", "Then", "every step will be parsed without errors",
1504 None, None),
1505 ])
1506
1507 def test_parse_steps_when_last_step_has_multiline_text(self):
1508 doc = u'''
1509 Given a simple step
1510 Then the last step has multi-line text:
1511 """
1512 Lorem ipsum
1513 Ipsum lorem
1514 """
1515 '''.lstrip()
1516 steps = parser.parse_steps(doc)
1517 eq_(len(steps), 2)
1518 # -- EXPECTED STEP DATA:
1519 # SCHEMA: step_type, keyword, name, text, table
1520 text2 = "Lorem ipsum\nIpsum lorem"
1521 self.compare_steps(steps, [
1522 ("given", "Given", "a simple step", None, None),
1523 ("then", "Then", "the last step has multi-line text", text2, None),
1524 ])
1525
1526 def test_parse_steps_with_table(self):
1527 doc = u'''
1528 Given a step with a table:
1529 | Name | Age |
1530 | Alice | 12 |
1531 | Bob | 23 |
1532 When I have a step with a table:
1533 | Country | Capital |
1534 | France | Paris |
1535 | Germany | Berlin |
1536 | Spain | Madrid |
1537 | USA | Washington |
1538 Then every step will be parsed without errors
1539 '''.lstrip()
1540 steps = parser.parse_steps(doc)
1541 eq_(len(steps), 3)
1542 # -- EXPECTED STEP DATA:
1543 # SCHEMA: step_type, keyword, name, text, table
1544 table1 = model.Table([u"Name", u"Age"], 0, [
1545 [ u"Alice", u"12" ],
1546 [ u"Bob", u"23" ],
1547 ])
1548 table2 = model.Table([u"Country", u"Capital"], 0, [
1549 [ u"France", u"Paris" ],
1550 [ u"Germany", u"Berlin" ],
1551 [ u"Spain", u"Madrid" ],
1552 [ u"USA", u"Washington" ],
1553 ])
1554 self.compare_steps(steps, [
1555 ("given", "Given", "a step with a table", None, table1),
1556 ("when", "When", "I have a step with a table", None, table2),
1557 ("then", "Then", "every step will be parsed without errors",
1558 None, None),
1559 ])
1560
1561 def test_parse_steps_when_last_step_has_a_table(self):
1562 doc = u'''
1563 Given a simple step
1564 Then the last step has a final table:
1565 | Name | City |
1566 | Alonso | Barcelona |
1567 | Bred | London |
1568 '''.lstrip()
1569 steps = parser.parse_steps(doc)
1570 eq_(len(steps), 2)
1571 # -- EXPECTED STEP DATA:
1572 # SCHEMA: step_type, keyword, name, text, table
1573 table2 = model.Table([u"Name", u"City"], 0, [
1574 [ u"Alonso", u"Barcelona" ],
1575 [ u"Bred", u"London" ],
1576 ])
1577 self.compare_steps(steps, [
1578 ("given", "Given", "a simple step", None, None),
1579 ("then", "Then", "the last step has a final table", None, table2),
1580 ])
1581
1582 @raises(parser.ParserError)
1583 def test_parse_steps_with_malformed_table(self):
1584 doc = u'''
1585 Given a step with a malformed table:
1586 | Name | City |
1587 | Alonso | Barcelona | 2004 |
1588 | Bred | London | 2010 |
1589 '''.lstrip()
1590 steps = parser.parse_steps(doc)