Skip to content

Commit 7151234

Browse files
committed
Implement localisation support
1 parent 00916ff commit 7151234

File tree

3 files changed

+94
-6
lines changed

3 files changed

+94
-6
lines changed

src/pytest_bdd/parser.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from .gherkin_parser import Step as GherkinStep
1818
from .gherkin_parser import Tag as GherkinTag
1919
from .gherkin_parser import get_gherkin_document
20-
from .types import STEP_TYPES
20+
from .types import STEP_TYPE_BY_PARSER_KEYWORD
2121

2222
STEP_PARAM_RE = re.compile(r"<(.+?)>")
2323

@@ -346,7 +346,7 @@ def parse_steps(self, steps_data: list[GherkinStep]) -> list[Step]:
346346
return []
347347

348348
first_step = steps_data[0]
349-
if first_step.keyword.lower() not in STEP_TYPES:
349+
if first_step.keyword_type not in STEP_TYPE_BY_PARSER_KEYWORD:
350350
raise StepError(
351351
message=f"First step in a scenario or background must start with 'Given', 'When' or 'Then', but got {first_step.keyword}.",
352352
line=first_step.location.line,
@@ -355,11 +355,9 @@ def parse_steps(self, steps_data: list[GherkinStep]) -> list[Step]:
355355
)
356356

357357
steps = []
358-
current_type = first_step.keyword.lower()
358+
current_type = STEP_TYPE_BY_PARSER_KEYWORD[first_step.keyword_type]
359359
for step in steps_data:
360-
keyword = step.keyword.lower()
361-
if keyword in STEP_TYPES:
362-
current_type = keyword
360+
current_type = STEP_TYPE_BY_PARSER_KEYWORD.get(step.keyword_type, current_type)
363361
steps.append(
364362
Step(
365363
name=step.text,

src/pytest_bdd/types.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,9 @@
1212
THEN: Literal["then"] = "then"
1313

1414
STEP_TYPES = (GIVEN, WHEN, THEN)
15+
16+
STEP_TYPE_BY_PARSER_KEYWORD = {
17+
"Context": GIVEN,
18+
"Action": WHEN,
19+
"Outcome": THEN,
20+
}

tests/feature/test_scenario.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import textwrap
44

5+
from pytest_bdd.utils import collect_dumped_objects
6+
57

68
def test_scenario_not_found(pytester, pytest_params):
79
"""Test the situation when scenario is not found."""
@@ -195,3 +197,85 @@ def _():
195197
)
196198
result = pytester.runpytest()
197199
result.assert_outcomes(passed=2)
200+
201+
202+
def test_multilanguage_support(pytester):
203+
"""Test multilanguage support."""
204+
pytester.makefile(
205+
".feature",
206+
simple="""
207+
# language: it
208+
209+
Funzionalità: Funzionalità semplice
210+
211+
Contesto:
212+
Dato che uso uno step nel contesto
213+
Allora va tutto bene
214+
215+
Scenario: Scenario semplice
216+
Dato che uso uno step con "Dato"
217+
E che uso uno step con "E"
218+
Ma che uso uno step con "Ma"
219+
* che uso uno step con "*"
220+
Allora va tutto bene
221+
222+
Schema dello scenario: Scenario con schema
223+
Dato che uso uno step con "<nome esempio>"
224+
Allora va tutto bene
225+
226+
Esempi:
227+
| nome esempio |
228+
| esempio 1 |
229+
| esempio 2 |
230+
""",
231+
)
232+
pytester.makepyfile(
233+
"""
234+
from pytest_bdd import scenario, given, then, parsers
235+
from pytest_bdd.utils import dump_obj
236+
237+
@scenario("simple.feature", "Scenario semplice")
238+
def test_scenario_semplice():
239+
pass
240+
241+
@scenario("simple.feature", "Scenario con schema")
242+
def test_scenario_con_schema():
243+
pass
244+
245+
@given("che uso uno step nel contesto")
246+
def _():
247+
return dump_obj(("given", "che uso uno step nel contesto"))
248+
249+
@given(parsers.parse('che uso uno step con "{step_name}"'))
250+
def _(step_name):
251+
return dump_obj(("given", "che uso uno step con ", step_name))
252+
253+
@then("va tutto bene")
254+
def _():
255+
dump_obj(("then", "va tutto bene"))
256+
"""
257+
)
258+
result = pytester.runpytest("-s")
259+
result.assert_outcomes(passed=3)
260+
261+
assert collect_dumped_objects(result) == [
262+
# 1st scenario
263+
("given", "che uso uno step nel contesto"),
264+
("then", "va tutto bene"),
265+
("given", "che uso uno step con ", "Dato"),
266+
("given", "che uso uno step con ", "E"),
267+
("given", "che uso uno step con ", "Ma"),
268+
("given", "che uso uno step con ", "*"),
269+
("then", "va tutto bene"),
270+
# 2nd scenario
271+
# 1st example
272+
("given", "che uso uno step nel contesto"),
273+
("then", "va tutto bene"),
274+
("given", "che uso uno step con ", "esempio 1"),
275+
("then", "va tutto bene"),
276+
# 2nd example
277+
("given", "che uso uno step nel contesto"),
278+
("then", "va tutto bene"),
279+
("given", "che uso uno step con ", "esempio 2"),
280+
("then", "va tutto bene"),
281+
]

0 commit comments

Comments
 (0)