-
-
Notifications
You must be signed in to change notification settings - Fork 95
fix: Parse tags correctly in gherkin-32 compatibility mode
#400
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Behat Gherkin Parser. | ||
| * (c) Konstantin Kudryashov <ever.zet@gmail.com> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Behat\Gherkin\Exception; | ||
|
|
||
| class InvalidTagContentException extends ParserException | ||
| { | ||
| public function __construct(string $tag, ?string $file) | ||
| { | ||
| parent::__construct( | ||
| sprintf( | ||
| 'Tags cannot include whitespace, found "%s"%s', | ||
| $tag, | ||
| is_string($file) | ||
| ? "in file {$file}" | ||
| : '' | ||
| ), | ||
| ); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -825,9 +825,23 @@ protected function scanTags() | |
| } | ||
|
|
||
| $token = $this->takeToken('Tag'); | ||
| $tags = explode('@', mb_substr($line, 1, mb_strlen($line, 'utf8') - 1, 'utf8')); | ||
| $tags = array_map(trim(...), $tags); | ||
| $token['tags'] = $tags; | ||
|
|
||
| if ($this->compatibilityMode->shouldRemoveTagPrefixChar()) { | ||
| // Legacy behaviour | ||
| $tags = explode('@', mb_substr($line, 1, mb_strlen($line, 'utf8') - 1, 'utf8')); | ||
| $tags = array_map(trim(...), $tags); | ||
| $token['tags'] = $tags; | ||
|
|
||
| return $token; | ||
| } | ||
|
|
||
| $tags = preg_split('/(?=@)/u', $line); | ||
| assert($tags !== false); | ||
| // Remove the empty content before the first tag prefix | ||
| array_shift($tags); | ||
|
|
||
| // Note: checking for whitespace in tags is done in the Parser to fit with existing logic | ||
| $token['tags'] = array_map(trim(...), $tags); | ||
|
Comment on lines
+838
to
+844
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This broadly follows the logic in cucumber/gherkin except I have used a lookahead regex to save having to add the |
||
|
|
||
| return $token; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ | |
| namespace Behat\Gherkin; | ||
|
|
||
| use Behat\Gherkin\Exception\FilesystemException; | ||
| use Behat\Gherkin\Exception\InvalidTagContentException; | ||
| use Behat\Gherkin\Exception\LexerException; | ||
| use Behat\Gherkin\Exception\NodeException; | ||
| use Behat\Gherkin\Exception\ParserException; | ||
|
|
@@ -584,8 +585,12 @@ protected function guardTags(array $tags) | |
| { | ||
| foreach ($tags as $tag) { | ||
| if (preg_match('/\s/', $tag)) { | ||
| if ($this->compatibilityMode->shouldThrowOnWhitespaceInTag()) { | ||
| throw new InvalidTagContentException($tag, $this->file); | ||
| } | ||
|
|
||
| trigger_error( | ||
| sprintf('Whitespace in tags is deprecated, found "%s"', $tag), | ||
| sprintf('Whitespace in tags is deprecated, found "%s" in %s', $tag, $this->file ?? 'unknown file'), | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I noticed the deprecation message did not include the name of the file (if available) - I've added that to help users trace any of these before enabling the new mode. |
||
| E_USER_DEPRECATED | ||
| ); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We no longer need to remove the
@from the tag filter expression, because the tags we're comparing to will now always have their leading@.