| 1 |
@issue |
| 2 |
Feature: Issue #280: AmbiguousStep error with similar step definitions and use_step_matcher("re") |
| 3 |
|
| 4 |
. While using the RegexMatcher with steps that have the same step prefix |
| 5 |
. an AmbiguousStep exception occurs if the shorter step is registered first. |
| 6 |
. |
| 7 |
. EXAMPLE: |
| 8 |
. Two steps with definitions that have the same step prefix: |
| 9 |
. |
| 10 |
. * I do something |
| 11 |
. * I do something more |
| 12 |
. |
| 13 |
. cause an AmbiguousStep error to be thrown: |
| 14 |
. |
| 15 |
. behave.step_registry.AmbiguousStep: @when('I do something more') has already |
| 16 |
. been defined in existing step @when('I do something') at ... |
| 17 |
. |
| 18 |
. SOLUTION: Add regex begin-/end-markers around the step text( '^'+ step + '$') |
| 19 |
. NOTE: Only RegexMatcher is affected. |
| 20 |
|
| 21 |
@setup |
| 22 |
Scenario: Feature Setup |
| 23 |
Given a new working directory |
| 24 |
And a file named "features/steps/calculator_steps1.py" with: |
| 25 |
""" |
| 26 |
from behave import given, then |
| 27 |
from hamcrest import assert_that, equal_to |
| 28 |
|
| 29 |
class SimpleCalculator(object): |
| 30 |
def __init__(self): |
| 31 |
self.result = 0 |
| 32 |
|
| 33 |
def add(self, value): |
| 34 |
self.result += value |
| 35 |
|
| 36 |
@given(u'a calculator') |
| 37 |
def step_impl(context): |
| 38 |
context.calculator = SimpleCalculator() |
| 39 |
|
| 40 |
@then(u'the calculator result is "{expected_result:d}"') |
| 41 |
def step_impl(context, expected_result): |
| 42 |
assert_that(context.calculator.result, equal_to(expected_result)) |
| 43 |
""" |
| 44 |
|
| 45 |
Scenario: Ensure RegexMatcher is not ordering sensitive |
| 46 |
Given a file named "features/syndrome_280_1.feature" with: |
| 47 |
""" |
| 48 |
Feature: |
| 49 |
Scenario: Use both steps |
| 50 |
Given I do something |
| 51 |
When I do something more |
| 52 |
""" |
| 53 |
And a file named "features/steps/simple_steps.py" with: |
| 54 |
""" |
| 55 |
from behave import step, use_step_matcher |
| 56 |
use_step_matcher("re") |
| 57 |
|
| 58 |
# -- ORDERING SENSITIVE PART: |
| 59 |
@step(u'I do something') |
| 60 |
def step_impl(context): |
| 61 |
pass |
| 62 |
|
| 63 |
@step(u'I do something more') |
| 64 |
def step_impl(context): |
| 65 |
pass |
| 66 |
""" |
| 67 |
When I run "behave -f plain features/syndrome_280_1.feature" |
| 68 |
Then it should pass with: |
| 69 |
""" |
| 70 |
1 scenario passed, 0 failed, 0 skipped |
| 71 |
""" |
| 72 |
But the command output should not contain "AmbiguousStep:" |
| 73 |
And the command output should not contain: |
| 74 |
""" |
| 75 |
AmbiguousStep: @step('I do something more') has already been defined in |
| 76 |
existing step @step('I do something') at features/steps/simple_steps.py:5' |
| 77 |
""" |
| 78 |
|
| 79 |
Scenario: Ensure correct step implementation is selected |
| 80 |
Given a file named "features/syndrome_280_2.feature" with: |
| 81 |
""" |
| 82 |
Feature: Steps with same step prefix -- Use correct step implementation |
| 83 |
Scenario: Use shorter step |
| 84 |
Given a calculator |
| 85 |
When I add "2" to it |
| 86 |
And I add "3" to it |
| 87 |
Then the calculator result is "5" |
| 88 |
|
| 89 |
Scenario: Use longer step |
| 90 |
Given a calculator |
| 91 |
When I add "2" to it twice |
| 92 |
And I add "3" to it |
| 93 |
Then the calculator result is "7" |
| 94 |
""" |
| 95 |
And a file named "features/steps/calculator_steps2.py" with: |
| 96 |
""" |
| 97 |
from behave import when, use_step_matcher |
| 98 |
use_step_matcher("re") |
| 99 |
|
| 100 |
# -- ORDERING SENSITIVE PART: |
| 101 |
@when(u'I add "(?P<value>\d+)" to it') |
| 102 |
def step_impl(context, value): |
| 103 |
number_value = int(value) |
| 104 |
context.calculator.add(number_value) |
| 105 |
|
| 106 |
@when(u'I add "(?P<value>\d+)" to it twice') |
| 107 |
def step_impl(context, value): |
| 108 |
number_value = int(value) |
| 109 |
context.calculator.add(number_value) |
| 110 |
context.calculator.add(number_value) |
| 111 |
""" |
| 112 |
When I run "behave -f pretty --no-color features/syndrome_280_2.feature" |
| 113 |
Then it should pass with: |
| 114 |
""" |
| 115 |
When I add "2" to it twice # features/steps/calculator_steps2.py:10 |
| 116 |
And I add "3" to it # features/steps/calculator_steps2.py:5 |
| 117 |
""" |
| 118 |
But the command output should not contain "AmbiguousStep" |