Skip to content

Commit 7239277

Browse files
authored
Merge pull request #158 from radish-bdd/bugfix/match-longer-sentence
Correctly match "matching" sentences which do not match perfectly
2 parents 3a76620 + eb58075 commit 7239277

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

radish/matcher.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ def match_step(sentence, steps):
8888
:returns: the arguments and the func which were matched
8989
:rtype: tuple
9090
"""
91+
potentional_matches = []
9192
for pattern, func in steps.items():
9293
if isinstance(pattern, re._pattern_type): # pylint: disable=protected-access
9394
match = pattern.search(sentence)
@@ -110,8 +111,18 @@ def match_step(sentence, steps):
110111
longest_group = 0
111112

112113
if match:
114+
step_match = StepMatch(argument_match=argument_match, func=func)
113115
if len(sentence) == longest_group:
114-
return StepMatch(argument_match=argument_match, func=func)
116+
# if perfect match can be made we return it no
117+
# matter of the other potentional matches
118+
return step_match
119+
120+
distance_to_perfect = abs(len(sentence) - longest_group)
121+
potentional_matches.append((step_match, distance_to_perfect))
122+
123+
if potentional_matches:
124+
# get best match
125+
return min(potentional_matches, key=lambda x: x[1])[0]
115126

116127
return None
117128

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
# -*- coding: utf-8 -*-
22

3+
import re
4+
35
from radish.stepregistry import step
46
from radish import given, when, then
57

8+
9+
# @step(re.compile(r"I have the number (\d+)"))
610
@step("I have the number {number:g}")
711
def have_number(step, number):
8-
step.context.numbers.append(number)
12+
step.context.numbers.append(int(number))
13+
914

1015
@when("I sum them")
1116
def sum_numbers(step):
1217
step.context.result = sum(step.context.numbers)
1318

19+
1420
@then("I expect the result to be {result:g}")
1521
def expect_result(step, result):
1622
assert step.context.result == result

tests/unit/test_matcher.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,20 @@ def test_merge_non_existing_step(self):
142142
feature.scenarios.append(scenario)
143143

144144
merge_steps.when.called_with([feature], steps).should.throw(StepDefinitionNotFoundError)
145+
146+
def test_match_longer_sentence(self):
147+
"""
148+
Test matching steps with a longer sentence
149+
"""
150+
steps = {"This is a short sentence": "some_func",
151+
"a short example sentence": "some_other_func"}
152+
153+
match, func = match_step("This is a short sentence to test something", steps)
154+
arguments, keyword_arguments = match.evaluate()
155+
keyword_arguments.should.be.equal({})
156+
func.should.be.equal("some_func")
157+
158+
match, func = match_step("I see a short example sentence to test", steps)
159+
arguments, keyword_arguments = match.evaluate()
160+
keyword_arguments.should.be.equal({})
161+
func.should.be.equal("some_other_func")

0 commit comments

Comments
 (0)