Skip to content

Commit

Permalink
Tag parsing (#215)
Browse files Browse the repository at this point in the history
* Remove any trailing comments before parsing the tags

* Disallow whitespace in tags

* Don't consume all of line when parsing tags

* Change error to deprecation
  • Loading branch information
ciaranmcnulty authored Jul 12, 2021
1 parent fbd6baa commit 3f94bb5
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 4 deletions.
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertDeprecationsToExceptions="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
Expand Down
7 changes: 7 additions & 0 deletions src/Behat/Gherkin/Filter/TagFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ class TagFilter extends ComplexFilter
public function __construct($filterString)
{
$this->filterString = trim($filterString);

if(preg_match('/\s/u', $this->filterString)) {
trigger_error(
"Tags with whitespace are deprecated and may be removed in a future version",
E_USER_DEPRECATED
);
}
}

/**
Expand Down
18 changes: 17 additions & 1 deletion src/Behat/Gherkin/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,15 @@ protected function consumeLine()
$this->trimmedLine = null;
}

/**
* Consumes first part of line from input without incrementing the line number
*/
protected function consumeLineUntil(int $trimmedOffset)
{
$this->line = mb_substr(ltrim($this->line), $trimmedOffset, null, 'utf-8');
$this->trimmedLine = null;
}

/**
* Returns trimmed version of line.
*
Expand Down Expand Up @@ -515,16 +524,23 @@ protected function scanTableRow()
protected function scanTags()
{
$line = $this->getTrimmedLine();

if (!isset($line[0]) || '@' !== $line[0]) {
return null;
}

if(preg_match('/^(?<line>.*)\s+#.*$/', $line, $matches)) {
['line' => $line] = $matches;
$this->consumeLineUntil(mb_strlen($line, 'utf-8'));
} else {
$this->consumeLine();
}

$token = $this->takeToken('Tag');
$tags = explode('@', mb_substr($line, 1, mb_strlen($line, 'utf8') - 1, 'utf8'));
$tags = array_map('trim', $tags);
$token['tags'] = $tags;

$this->consumeLine();

return $token;
}
Expand Down
17 changes: 17 additions & 0 deletions src/Behat/Gherkin/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,9 @@ protected function parsePyString()
protected function parseTags()
{
$token = $this->expectTokenType('Tag');

$this->guardTags($token['tags']);

$this->tags = array_merge($this->tags, $token['tags']);

$possibleTransitions = array(
Expand Down Expand Up @@ -644,6 +647,20 @@ protected function popTags()
return $tags;
}

/**
* Checks the tags fit the required format
*
* @param string[] $tags
*/
protected function guardTags(array $tags)
{
foreach ($tags as $tag) {
if (preg_match('/\s/', $tag)) {
trigger_error('Whitespace in tags is deprecated, found "$tag"', E_USER_DEPRECATED);
}
}
}

/**
* Parses next text line & returns it.
*
Expand Down
13 changes: 10 additions & 3 deletions tests/Behat/Gherkin/Cucumber/CompatibilityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ class CompatibilityTest extends TestCase
'escaped_pipes.feature' => 'Feature description has wrong whitespace captured',
'incomplete_scenario.feature' => 'Wrong background parsing when there are no steps',
'incomplete_background_2.feature' => 'Wrong background parsing when there are no steps',
'tags.feature' => 'Tags followed by comments not parsed correctly'
];

private $parsedButShouldNotBe = [
'invalid_language.feature' => 'Invalid language is silently ignored',
'whitespace_in_tags.feature' => 'Whitespace in tags is tolerated',
];

private $deprecatedInsteadOfParseError = [
'whitespace_in_tags.feature' => '/Whitespace in tags is deprecated/',
];

/**
Expand Down Expand Up @@ -96,7 +98,12 @@ public function testBadFeaturesDoNotParse(\SplFileInfo $file)
$this->markTestIncomplete($this->parsedButShouldNotBe[$file->getFilename()]);
}

$this->expectException(ParserException::class);
if (isset($this->deprecatedInsteadOfParseError[$file->getFilename()])) {
$this->expectDeprecation();
$this->expectDeprecationMessageMatches($this->deprecatedInsteadOfParseError[$file->getFilename()]);
} else {
$this->expectException(ParserException::class);
}
$gherkinFile = $file->getPathname();
$feature = $this->parser->parse(file_get_contents($gherkinFile), $gherkinFile);
}
Expand Down
12 changes: 12 additions & 0 deletions tests/Behat/Gherkin/Filter/TagFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,4 +268,16 @@ public function testFilterFeatureWithTaggedExamples()
/** @noinspection PhpUndefinedMethodInspection */
$this->assertEquals(array($exampleTableNode3), $scenarioInterfaces[1]->getExampleTables());
}

public function testFilterWithWhitespaceIsDeprecated()
{
$this->expectDeprecation();
$tagFilter = new TagFilter('@tag with space');
$scenario = new ScenarioNode(null, ['tag with space'], array(), null, 2);
$feature = new FeatureNode(null, null, [], null, [$scenario], null, null, null, 1);

$scenarios = $tagFilter->filterFeature($feature)->getScenarios();

$this->assertEquals([$scenario], $scenarios);
}
}

0 comments on commit 3f94bb5

Please sign in to comment.