| 1 |
@issue |
| 2 |
Feature: Issue #85: AssertionError with nested regex and pretty formatter |
| 3 |
|
| 4 |
| When --format=pretty is used |
| 5 |
| an AssertationError occurs for missing optional/nested-groups. |
| 6 |
| When --format=plain is used, everything is fine |
| 7 |
|
| 8 |
Scenario: Test Setup |
| 9 |
Given a new working directory |
| 10 |
And a file named "features/steps/regexp_steps.py" with: |
| 11 |
""" |
| 12 |
from __future__ import print_function |
| 13 |
from behave import given, when, then, use_step_matcher |
| 14 |
@given(u'a {re_category} regular expression "{pattern}"') |
| 15 |
def impl(context, re_category, pattern): |
| 16 |
pass |
| 17 |
|
| 18 |
@then(u'the parameter "{name}" is "{expected_value}"') |
| 19 |
def impl(context, name, expected_value): |
| 20 |
actual_value = getattr(context, name, None) |
| 21 |
if actual_value is None: |
| 22 |
actual_value = "" |
| 23 |
assert hasattr(context, name) |
| 24 |
assert actual_value == expected_value, "MISMATCH: actual({0}) == expected({1})".format(actual_value, expected_value) |
| 25 |
|
| 26 |
@then(u'the parameter "{name}" is none') |
| 27 |
def impl(context, name): |
| 28 |
actual_value = getattr(context, name, None) |
| 29 |
assert hasattr(context, name) |
| 30 |
assert actual_value is None, "MISMATCH: actual({0}) == None)".format(actual_value) |
| 31 |
|
| 32 |
def store_in_context(context, data): |
| 33 |
for name, value in data.items(): |
| 34 |
setattr(context, name, value) |
| 35 |
|
| 36 |
use_step_matcher('re') |
| 37 |
|
| 38 |
@when(u'I try to match "(?P<foo>foo and more)"') |
| 39 |
def impl(context, **kwargs): |
| 40 |
kwargs["regexp_case"] = "simple" |
| 41 |
print("CASE UNNESTED: {0}".format(kwargs)) |
| 42 |
store_in_context(context, kwargs) |
| 43 |
|
| 44 |
@when(u'I try to match "(?P<foo>foo(?P<bar>bar)?)"') |
| 45 |
def impl(context, **kwargs): |
| 46 |
kwargs["regexp_case"] = "nested" |
| 47 |
print("CASE NESTED: {0}".format(kwargs)) |
| 48 |
store_in_context(context, kwargs) |
| 49 |
|
| 50 |
@when(u'I try to match "(?P<foo>foo) (?P<bar>bar)?"') |
| 51 |
def impl(context, **kwargs): |
| 52 |
kwargs["regexp_case"] = "optional" |
| 53 |
print("CASE OPTIONAL: {0}".format(kwargs)) |
| 54 |
store_in_context(context, kwargs) |
| 55 |
""" |
| 56 |
And a file named "features/matching.feature" with: |
| 57 |
""" |
| 58 |
Feature: Using regexp matcher with nested and optional parameters |
| 59 |
|
| 60 |
Scenario: regex, no nested groups, matching |
| 61 |
Given a simple regular expression "(?P<foo>foo and more)" |
| 62 |
When I try to match "foo and more" |
| 63 |
Then the parameter "regexp_case" is "simple" |
| 64 |
And the parameter "foo" is "foo and more" |
| 65 |
|
| 66 |
Scenario: Nested groups without nested match |
| 67 |
Given a nested-group regular expression "(?P<foo>foo(?P<bar>bar)?)" |
| 68 |
When I try to match "foo" |
| 69 |
Then the parameter "regexp_case" is "nested" |
| 70 |
And the parameter "foo" is "foo" |
| 71 |
And the parameter "bar" is none |
| 72 |
|
| 73 |
Scenario: Nested groups with nested match |
| 74 |
Given a nested-group regular expression "(?P<foo>foo(?P<bar>bar)?)" |
| 75 |
When I try to match "foobar" |
| 76 |
Then the parameter "regexp_case" is "nested" |
| 77 |
And the parameter "foo" is "foobar" |
| 78 |
And the parameter "bar" is "bar" |
| 79 |
|
| 80 |
Scenario: Optional group without match |
| 81 |
Given a optional-group regular expression "(?P<foo>foo) (?P<bar>bar)?" |
| 82 |
When I try to match "foo " |
| 83 |
Then the parameter "regexp_case" is "optional" |
| 84 |
And the parameter "foo" is "foo" |
| 85 |
And the parameter "bar" is none |
| 86 |
|
| 87 |
Scenario: Optional group with match |
| 88 |
Given a optional-group regular expression "(?P<foo>foo) (?P<bar>bar)?" |
| 89 |
When I try to match "foo bar" |
| 90 |
Then the parameter "regexp_case" is "optional" |
| 91 |
And the parameter "foo" is "foo" |
| 92 |
And the parameter "bar" is "bar" |
| 93 |
""" |
| 94 |
|
| 95 |
Scenario: Run regexp steps with --format=plain |
| 96 |
When I run "behave --format=plain features/matching.feature" |
| 97 |
Then it should pass with: |
| 98 |
""" |
| 99 |
1 feature passed, 0 failed, 0 skipped |
| 100 |
5 scenarios passed, 0 failed, 0 skipped |
| 101 |
24 steps passed, 0 failed, 0 skipped, 0 undefined |
| 102 |
""" |
| 103 |
|
| 104 |
Scenario: Run regexp steps with --format=pretty |
| 105 |
When I run "behave -c --format=pretty features/matching.feature" |
| 106 |
Then it should pass with: |
| 107 |
""" |
| 108 |
1 feature passed, 0 failed, 0 skipped |
| 109 |
5 scenarios passed, 0 failed, 0 skipped |
| 110 |
24 steps passed, 0 failed, 0 skipped, 0 undefined |
| 111 |
""" |
| 112 |
And the command output should not contain |
| 113 |
""" |
| 114 |
assert isinstance(text, unicode) |
| 115 |
""" |
| 116 |
And the command output should not contain |
| 117 |
""" |
| 118 |
AssertationError |
| 119 |
""" |