forked from cucumber/cucumber-expressions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_cucumber_expression.py
120 lines (101 loc) · 4.3 KB
/
test_cucumber_expression.py
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
import os
from decimal import Decimal
import pytest
from cucumber_expressions.cucumber_expression import CucumberExpression
from cucumber_expressions.parameter_type import ParameterType
from cucumber_expressions.parameter_type_registry import ParameterTypeRegistry
from test.definitions import TESTDATA_ROOT_DIR
def get_expectation_yamls():
YAML_DIR = os.path.join(TESTDATA_ROOT_DIR, "cucumber-expression", "matching")
return [
os.path.join(YAML_DIR, file)
for file in os.listdir(YAML_DIR)
if file.endswith(".yaml")
]
def match(
expression: str,
match_text: str,
parameter_registry: ParameterTypeRegistry = ParameterTypeRegistry(),
):
cucumber_expression = CucumberExpression(expression, parameter_registry)
matches = cucumber_expression.match(match_text)
def transform_value(value):
if isinstance(value, int):
return str(value) if value.bit_length() > 64 else value
elif isinstance(value, Decimal):
return str(value)
else:
return value
return matches and [transform_value(arg.value) for arg in matches]
class TestCucumberExpression:
@pytest.mark.parametrize("load_test_yamls", get_expectation_yamls(), indirect=True)
def test_cucumber_expression_matches(self, load_test_yamls: dict):
expectation = load_test_yamls
if "exception" in expectation:
with pytest.raises(Exception) as excinfo:
match(expectation["expression"], expectation.get("text", ""))
assert excinfo.value.args[0] == expectation["exception"]
else:
values = match(expectation["expression"], expectation["text"])
assert values == expectation["expected_args"]
def test_documents_match_arguments(self):
values = match("I have {int} cuke(s)", "I have 7 cukes")
assert values[0] == 7
def test_matches_float(self):
assert match("{float}", "") is None
assert match("{float}", ".") is None
assert match("{float}", ",") is None
assert match("{float}", "-") is None
assert match("{float}", "E") is None
assert match("{float}", "1,") is None
assert match("{float}", ",1") is None
assert match("{float}", "1.") is None
assert match("{float}", "1") == [1]
assert match("{float}", "-1") == [-1]
assert match("{float}", "1.1") == [1.1]
assert match("{float}", "1,000") is None
assert match("{float}", "1,000,0") is None
assert match("{float}", "1,000.1") is None
assert match("{float}", "1,000,10") is None
assert match("{float}", "1,0.1") is None
assert match("{float}", "1,000,000.1") is None
assert match("{float}", "-1.1") == [-1.1]
assert match("{float}", ".1") == [0.1]
assert match("{float}", "-.1") == [-0.1]
assert match("{float}", "-.1000001") == [-0.1000001]
assert match("{float}", "1E1") == [10.0]
assert match("{float}", ".1E1") == [1]
assert match("{float}", "E1") is None
assert match("{float}", "-.1E-1") == [-0.01]
assert match("{float}", "-.1E-2") == [-0.001]
assert match("{float}", "-.1E+1") == [-1]
assert match("{float}", "-.1E+2") == [-10]
assert match("{float}", "-.1E1") == [-1]
assert match("{float}", "-.1E2") == [-10]
def test_float_with_zero(self):
assert match("{float}", "0") == [0.0]
def test_matches_anonymous(self):
assert match("{}", "0.22") == ["0.22"]
def test_exposes_source(self):
expr = "I have {int} cuke(s)"
assert CucumberExpression(expr, ParameterTypeRegistry()).source == expr
def test_unmatched_optional_groups_have_undefined_values(self):
parameter_type_registry = ParameterTypeRegistry()
parameter_type_registry.define_parameter_type(
ParameterType(
"textAndOrNumber",
r"([A-Z]+)?(?: )?([0-9]+)?",
object,
lambda s1, s2: [s1, s2],
False,
True,
)
)
assert match("{textAndOrNumber}", "TLA", parameter_type_registry)[0] == [
"TLA",
None,
]
assert match("{textAndOrNumber}", "123", parameter_type_registry)[0] == [
None,
"123",
]