Skip to content
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

Cast table node exceptions into ParserExceptions when throwing #216

Merged
merged 4 commits into from
Feb 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions src/Behat/Gherkin/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace Behat\Gherkin;

use Behat\Gherkin\Exception\LexerException;
use Behat\Gherkin\Exception\NodeException;
use Behat\Gherkin\Exception\ParserException;
use Behat\Gherkin\Node\BackgroundNode;
use Behat\Gherkin\Node\ExampleTableNode;
Expand Down Expand Up @@ -549,13 +550,15 @@ protected function parseStep()
*/
protected function parseExamples()
{
$token = $this->expectTokenType('Examples');

$keyword = $token['keyword'];

$keyword = ($this->expectTokenType('Examples'))['keyword'];
$tags = empty($this->tags) ? array() : $this->popTags();
$table = $this->parseTableRows();

return new ExampleTableNode($this->parseTableRows(), $keyword, $tags);
try {
return new ExampleTableNode($table, $keyword, $tags);
} catch(NodeException $e) {
$this->rethrowNodeException($e);
}
}

/**
Expand All @@ -565,7 +568,13 @@ protected function parseExamples()
*/
protected function parseTable()
{
return new TableNode($this->parseTableRows());
$table = $this->parseTableRows();

try {
return new TableNode($table);
} catch(NodeException $e) {
$this->rethrowNodeException($e);
}
}

/**
Expand Down Expand Up @@ -733,4 +742,13 @@ private function normalizeStepNodeKeywordType(StepNode $node, array $steps = arr
}
return $node;
}

private function rethrowNodeException(NodeException $e): void
{
throw new ParserException(
$e->getMessage() . ($this->file ? ' in file ' . $this->file : ''),
0,
$e
);
}
}
1 change: 0 additions & 1 deletion tests/Behat/Gherkin/Cucumber/CompatibilityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ class CompatibilityTest extends TestCase
];

private $parsedButShouldNotBe = [
'inconsistent_cell_count.feature' => 'Inconsistent cells count throws low level exception not ParserException',
'invalid_language.feature' => 'Invalid language is silently ignored',
'whitespace_in_tags.feature' => 'Whitespace in tags is tolerated',
];
Expand Down