Skip to content

Commit

Permalink
Extend tag support for simple tags
Browse files Browse the repository at this point in the history
Support multiple tags per line.
And simple tags like the other bdd tools without
open `(` and close `)` parenthesis.
  • Loading branch information
fliiiix committed Aug 12, 2023
1 parent cd3fe64 commit f60f4e9
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 42 deletions.
103 changes: 63 additions & 40 deletions radish/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,20 +212,12 @@ def _parse_feature(self, line):
line = line.strip()
detected_feature = self._detect_feature(line)
if not detected_feature:
has_tags = self._detect_tag(line)
if has_tags:
# If a tag is detected we split on @ and try to parse
# each part as tag (also for constant)
# this is required for @foo @bar @baz
for line in ["@" + l.strip() for l in line.split("@")]:
tag = self._detect_tag(line)
if tag:
self._current_tags.append(Tag(tag[0], tag[1]))
if tag[0] == "constant":
name, value = self._parse_constant(tag[1])
self._current_constants.append((name, value))
return True
return False
tags = self._try_parse_tags(line)
for tag in tags:
if tag.name == "constant":
name, value = self._parse_constant(tag.arg)
self._current_constants.append((name, value))
return bool(tags)

self.feature = Feature(
self._featureid,
Expand Down Expand Up @@ -287,24 +279,18 @@ def _parse_scenario(self, line):
if not detected_scenario:
detected_scenario = self._detect_scenario_loop(line)
if not detected_scenario:
has_tags = self._detect_tag(line)
if has_tags:
# If a tag is detected we split on @ and try to parse
# each part as tag (also for constant)
# this is required for @foo @bar @baz
for line in ["@" + l.strip() for l in line.split("@")]:
tag = self._detect_tag(line)
if tag:
self._current_tags.append(Tag(tag[0], tag[1]))
if tag[0] == "precondition":
scenario = self._parse_precondition(tag[1])
if scenario is not None:
self._current_preconditions.append(scenario)
elif tag[0] == "constant":
name, value = self._parse_constant(tag[1])
self._current_constants.append((name, value))
return True
tags = self._try_parse_tags(line)
for tag in tags:
if tag.name == "precondition":
scenario = self._parse_precondition(tag.arg)
if scenario is not None:
self._current_preconditions.append(scenario)
elif tag.name == "constant":
name, value = self._parse_constant(tag.arg)
self._current_constants.append((name, value))

if tags:
return True
raise FeatureFileSyntaxError(
"The parser expected a scenario or a tag on this line. Given: '{0}'".format(line)
)
Expand Down Expand Up @@ -729,20 +715,57 @@ def _detect_language(self, line):

def _detect_tag(self, line):
"""
Detects and parses tag on the given line
Supports @name(value) syntax where a tag can have a name
and a value inside open parenthesis `(` and close parenthesis `)`
Detects tag or tags on the given line
(A line starting with @)
:param string line: the line to detect the tag
:returns: the tag (name and value) or None
:rtype: str or None
:returns: if the line contains tags
:rtype: bool
"""
match = re.search(r"^@([^\s(]+)(?:\((.*?)\))?", line)
if match:
return match.group(1), match.group(2)
return line[0] == "@"

return None
def _try_parse_tags(self, line):
"""
Tries to parse all tags from a line.
A line with tags need to start with @.
Each tag needs to start with @.
Splits multiple tags in a single line
@foo @bar @baz.
Supports a value per tag eg.
@foo value @bar the second value @baz.
Because of legacy reasons supports and strips
open parenthesis `(` and close parenthesis `)`.
Eg. @tag(value).
:param string line: the line to detect the tag
:returns: list of Tag or empty list
:rtype: list
"""
has_tags = self._detect_tag(line)
tags = []
if has_tags:
# If a tag is detected we split on @ and try to parse
# each part as tag (also for constant)
# this is required for @foo @bar @baz
for line in [l.strip() for l in line.split("@") if l]:
match = re.search(r"^([^\s]+)\((.*)\)", line)
if match:
tag = Tag(match.group(1), match.group(2))
else:
line = line.split(" ")

value = line[1:]
value = " ".join(value)
if not value:
value = None
tag = Tag(line[0], value)

tags.append(tag)
self._current_tags.append(tag)
return tags

def _create_scenario_background(self, steps_runable):
"""
Expand Down
1 change: 1 addition & 0 deletions tests/exploratory/tags/features/SumNumbers.feature
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Feature: Test summing numbers
to sum numbers.

@FooBar
@author tuxtimo @reviewer l33tname @date Sun, 26 Feb 2023 17:52:52 +0100 @requirements 1,2
Scenario: Sum two numbers
Given I have the number 5
And I have the number 3
Expand Down
2 changes: 1 addition & 1 deletion tests/features/tags-arguments.feature
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Feature: Tag with Arguments
Radish shall support Tags with Arguments.

@sometag(somevalue)
@sometag(somevalue) @othertag Sat, 12 Aug 2023 11:47:24 +0200
Scenario: Some Scenario
Given I have a Step
When I do something
Expand Down
6 changes: 5 additions & 1 deletion tests/functional/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,11 @@ def test_parse_step_text_data(parser):
[Tag("scenario_loop")],
],
),
(["tags-arguments"], [Tag("foo", "bar")], [[Tag("sometag", "somevalue")]]),
(
["tags-arguments"],
[Tag("foo", "bar")],
[[Tag("sometag", "somevalue"), Tag("othertag", "Sat, 12 Aug 2023 11:47:24 +0200")]],
),
(
["tags-on-single-line"],
[Tag("author", "mario"), Tag("date", "Sat, 25 Feb 2023 19:53:53 +0100")],
Expand Down
1 change: 1 addition & 0 deletions tests/output/unix/tags-arguments.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Radish shall support Tags with Arguments.

 @sometag(somevalue)
 @othertag(Sat, 12 Aug 2023 11:47:24 +0200)
 Scenario: Some Scenario
Given I have a Step
 Given I have a Step
Expand Down
1 change: 1 addition & 0 deletions tests/output/windows/tags-arguments.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Radish shall support Tags with Arguments.

 @sometag(somevalue)
 @othertag(Sat, 12 Aug 2023 11:47:24 +0200)
 Scenario: Some Scenario

Given I have a Step
Expand Down

0 comments on commit f60f4e9

Please sign in to comment.