Basic tests for the new parser!

This commit is contained in:
Alessio Bogon 2020-09-15 11:15:55 +02:00
parent 37a4ce061a
commit 0d447dc738
5 changed files with 154 additions and 32 deletions

View File

@ -1,5 +1,4 @@
import io
import logging
import os.path
from collections import OrderedDict
@ -25,25 +24,10 @@ with io.open(os.path.join(os.path.dirname(__file__), "parser_data/gherkin.gramma
parser = lark.Lark(grammar, start="start", parser="lalr", postlex=TreeIndenter())
test_src = '''\
@atag @asecondtag
Feature: a feature
Scenario: scenario 1
Given I have a bar
"""
docstring
dsad
"""
When I want a bar
Then I get a bar
Scenario: scenario 2
'''
class TreeToGherkin(lark.Transformer):
def gherkin_document(self, value):
return value
[feature] = value
return feature
def string(self, value):
[s] = value
@ -76,16 +60,29 @@ class TreeToGherkin(lark.Transformer):
"steps": steps,
}
def tag(self, value):
[tag] = value
return tag
def feature(self, value):
feature_header, feature_line, raw_scenarios = value
try:
feature_header = next(el for el in value if el.data == "feature_header")
tag_lines = [el for el in feature_header.children if el.data == "tag_line"]
tags = [el for tag_line in tag_lines for el in tag_line.children]
except StopIteration:
tags = []
feature_line = next((el for el in value if el.data == "feature_line"))
raw_scenarios = next((el for el in value if el.data == "scenarios"))
[feature_name] = feature_line.children
feature = Feature(
scenarios=OrderedDict(),
filename=None,
rel_filename=None,
name=six.text_type(feature_line),
tags=None,
name=six.text_type(feature_name),
tags=tags,
examples=None,
background=None,
line_number=feature_name.line,
@ -99,12 +96,8 @@ class TreeToGherkin(lark.Transformer):
return feature
def test():
tree = parser.parse(test_src)
def parse(content):
tree = parser.parse(content)
# print(tree.pretty())
gherkin = TreeToGherkin().transform(tree)
print(gherkin)
if __name__ == "__main__":
test()
return gherkin

View File

@ -207,10 +207,7 @@ class Feature(object):
self.name = name
self.tags = tags
self.examples = examples
self.name = name
self.line_number = line_number
self.tags = tags
self.scenarios = scenarios
self.description = description
self.background = background

View File

@ -9,7 +9,7 @@ feature: _NL* feature_header? feature_line scenarios
scenarios: [_INDENT scenario* _DEDENT]
feature_header: tag_line?
feature_header: tag_line*
feature_line: "Feature: " string _NL
scenario: scenario_line [_INDENT step* _DEDENT]

View File

@ -1 +1,2 @@
packaging
more-itertools

131
tests/test_new_parser.py Normal file
View File

@ -0,0 +1,131 @@
from __future__ import absolute_import, unicode_literals
import pytest
from pytest_bdd.new_parser import parse
from pytest_bdd.parser import Feature
from pytest_bdd.types import GIVEN, WHEN, THEN
from more_itertools import zip_equal
def test_feature():
feature = parse(
"""\
@atag @another_tag
@a_third_tag
Feature: a feature
"""
)
assert isinstance(feature, Feature)
assert feature.name == "a feature"
assert feature.tags == ["atag", "another_tag", "a_third_tag"]
# TODO: assert feature.examples
assert feature.line_number == 3
# TODO: assert feature.description
# TODO: assert feature.background
@pytest.mark.parametrize(
"src, expected_scenarios",
[
(
"""\
Feature: a feature
""",
0,
),
(
"""\
Feature: a feature
Scenario: scenario 1
""",
1,
),
(
"""\
Feature: a feature
Scenario: scenario 1
Scenario: scenario 2
""",
2,
),
],
)
def test_scenario(src, expected_scenarios):
feature = parse(src)
assert len(feature.scenarios) == expected_scenarios
for i, (k, scenario) in enumerate(feature.scenarios.items(), start=1):
assert k == scenario.name == "scenario {}".format(i)
assert scenario.feature == feature
assert scenario.line_number == 1 + i
# TODO: assert scenario.example_converters
# TODO: assert scenario.tags
@pytest.mark.parametrize(
"src, expected_steps",
[
(
"""\
Feature: a feature
Scenario: a scenario
""",
[],
),
(
"""\
Feature: a feature
Scenario: a scenario
Given there is a foo
""",
[(GIVEN, "there is a foo")],
),
(
"""\
Feature: a feature
Scenario: a scenario
When I click the foo
""",
[(WHEN, "I click the foo")],
),
(
"""\
Feature: a feature
Scenario: a scenario
Then I should see a foo
""",
[(THEN, "I should see a foo")],
),
(
"""\
Feature: a feature
Scenario: a scenario
Given there is a foo
When I click the foo
Then I should see a foo
""",
[(GIVEN, "there is a foo"), (WHEN, "I click the foo"), (THEN, "I should see a foo")],
),
],
)
def test_step(src, expected_steps):
feature = parse(src)
[scenario] = feature.scenarios.values()
for i, (step, expected_step) in enumerate(zip_equal(scenario.steps, expected_steps), start=3):
expected_type, expected_name = expected_step
assert step.type == expected_type
assert step.name == expected_name
assert step.line_number == i
# TODO: assert step.name
# TODO: assert step.keyword
# TODO: assert step.lines
# TODO: assert step.indent
# TODO: assert step.type
# TODO: assert step.line_number
# TODO: assert step.failed
# TODO: assert step.start
# TODO: assert step.stop
# TODO: assert step.scenario
# TODO: assert step.background