Skip to content

Commit

Permalink
Adds tag parsing to features
Browse files Browse the repository at this point in the history
Add tag parsing logic from scenarios up to the feature as well.  Also
extends a unit test to cover a scenario that we ran into where tags
would break when the previous scenario ended with a table.

[272]
  • Loading branch information
mitgr81 committed Jul 9, 2012
1 parent e788f04 commit d0d5692
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
34 changes: 34 additions & 0 deletions lettuce/core.py
Expand Up @@ -848,6 +848,11 @@ def __init__(self, name, remaining_lines, with_file, original_string,
language)
self._set_definition(feature_definition)

if original_string and '@' in self.original_string:
self.tags = self._find_tags_in(original_string)
else:
self.tags = None

self._add_myself_to_scenarios()

@property
Expand All @@ -867,6 +872,35 @@ def max_length(self):
def _add_myself_to_scenarios(self):
for scenario in self.scenarios:
scenario.feature = self
if scenario.tags and self.tags:
scenario.tags.extend(self.tags)

def _find_tags_in(self, original_string):
broad_regex = re.compile(ur"([@].*)%s: (%s)" % (
self.language.feature,
re.escape(self.name)), re.DOTALL)

regexes = [broad_regex]

def try_finding_with(regex):
found = regex.search(original_string)

if found:
tag_lines = found.group().splitlines()
tags = set(chain(*map(self._extract_tag, tag_lines)))
return tags

for regex in regexes:
found = try_finding_with(regex)
if found:
return found

return []

def _extract_tag(self, item):
regex = re.compile(r'(?:(?:^|\s+)[@]([^@\s]+))')
found = regex.findall(item)
return found

def __repr__(self):
return u'<%s: "%s">' % (self.language.first_of_feature, self.name)
Expand Down
13 changes: 9 additions & 4 deletions tests/unit/test_feature_parser.py
Expand Up @@ -217,7 +217,9 @@
@runme2
Scenario: Holy tag2, Batman (2)
Given this scenario has other tags
Then it can be inspected from within the object
Then it can be inspected from within the object even with the table
| What | Is | This |
| It | is | TABLE |
@runme3
Scenario: Holy tag3, Batman
Expand Down Expand Up @@ -419,14 +421,17 @@ def test_single_feature_single_tag():
"All scenarios within a feature inherit the feature's tags"
feature = Feature.from_string(FEATURE15)

# FIXME (mitgr81): It seems worth the efficiency to not loop through the feature tags and
# check to see if every tag exists in the child. The "right" fix might just be to not
# add the tag from the feature in the first scenario directly.
assert that(feature.scenarios[0].tags).deep_equals([
'feature_runme', 'runme1'])
'feature_runme', 'runme1', 'feature_runme'])

assert that(feature.scenarios[1].tags).deep_equals([
'feature_runme', 'runme2'])
'runme2', 'feature_runme'])

assert that(feature.scenarios[2].tags).deep_equals([
'feature_runme', 'runme3'])
'runme3', 'feature_runme'])


def test_single_scenario_many_scenarios():
Expand Down

0 comments on commit d0d5692

Please sign in to comment.