-
-
Notifications
You must be signed in to change notification settings - Fork 95
test: Handle optional tableHeader when loading NDJson examples #294
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
Merged
acoulton
merged 11 commits into
Behat:master
from
uuf6429:bugfix/verify-optional-table-header-exists
May 16, 2025
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
28481f2
Handle missing table headers; add tests
uuf6429 70cb0f3
Add general test
uuf6429 032113a
Update code according to situation; fix CR comment
uuf6429 9b730f0
Fix bug; add test case
uuf6429 c85579b
Fixes as per spec clarification
uuf6429 50adba9
Throw exception when table header is missing/empty, but body isn't
uuf6429 2a8a917
CS fixes
uuf6429 5b8eb3f
Merge remote-tracking branch 'origin/master' into bugfix/verify-optio…
uuf6429 6dce817
Fix test warnings
uuf6429 8fe33f6
A few CR fixes
uuf6429 9816767
Handle missing case; add test for it
uuf6429 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,239 @@ | ||
| <?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 Tests\Behat\Gherkin\Loader; | ||
|
|
||
| use Behat\Gherkin\Exception\NodeException; | ||
| use Behat\Gherkin\Loader\CucumberNDJsonAstLoader; | ||
| use Behat\Gherkin\Node\BackgroundNode; | ||
| use Behat\Gherkin\Node\ExampleTableNode; | ||
| use Behat\Gherkin\Node\FeatureNode; | ||
| use Behat\Gherkin\Node\OutlineNode; | ||
| use Behat\Gherkin\Node\ScenarioNode; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| final class CucumberNDJsonAstLoaderTest extends TestCase | ||
| { | ||
| private CucumberNDJsonAstLoader $loader; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| $this->loader = new CucumberNDJsonAstLoader(); | ||
| } | ||
|
|
||
| public function testStringResourcesAreSupported(): void | ||
| { | ||
| $this->assertTrue($this->loader->supports('a string')); | ||
| } | ||
|
|
||
| public function testValidLoading(): void | ||
| { | ||
| $file = $this->serializeCucumberMessagesToFile([ | ||
| 'gherkinDocument' => [ | ||
| 'feature' => [ | ||
| 'location' => ['line' => 111], | ||
| 'name' => 'Feature with a valid Scenario', | ||
| 'description' => '', | ||
| 'keyword' => 'fea', | ||
| 'language' => 'en', | ||
| 'children' => [ | ||
| [ | ||
| 'background' => [ | ||
| 'location' => ['line' => 222], | ||
| 'keyword' => 'bac', | ||
| 'name' => 'Empty Background', | ||
| ], | ||
| ], | ||
| [ | ||
| 'scenario' => [ | ||
| 'location' => ['line' => 333], | ||
| 'name' => 'Empty Scenario', | ||
| 'keyword' => 'sce', | ||
| 'examples' => [], | ||
| ], | ||
| ], | ||
| [ | ||
| 'scenario' => [ | ||
| 'location' => ['line' => 444], | ||
| 'name' => 'Examples Scenario', | ||
| 'keyword' => 'out', | ||
| 'examples' => [ | ||
| [ | ||
| 'location' => ['line' => 555], | ||
| 'keyword' => 'exa', | ||
| 'tableHeader' => [ | ||
| 'location' => ['line' => 666], | ||
| 'cells' => [ | ||
| ['value' => 'A'], | ||
| ['value' => 'B'], | ||
| ], | ||
| ], | ||
| 'tableBody' => [ | ||
| [ | ||
| 'location' => ['line' => 777], | ||
| 'cells' => [ | ||
| ['value' => 'A1'], | ||
| ['value' => 'B1'], | ||
| ], | ||
| ], | ||
| [ | ||
| 'location' => ['line' => 888], | ||
| 'cells' => [ | ||
| ['value' => 'A2'], | ||
| ['value' => 'B2'], | ||
| ], | ||
| ], | ||
| ], | ||
| ], | ||
| ], | ||
| ], | ||
| ], | ||
| ], | ||
| ], | ||
| ], | ||
| ]); | ||
|
|
||
| $features = $this->loader->load($file); | ||
|
|
||
| $this->assertEquals( | ||
| [ | ||
| new FeatureNode( | ||
| 'Feature with a valid Scenario', | ||
| null, | ||
| [], | ||
| new BackgroundNode('Empty Background', [], 'bac', 222), | ||
| [ | ||
| new ScenarioNode('Empty Scenario', [], [], 'sce', 333), | ||
| new OutlineNode('Examples Scenario', [], [], [ | ||
| new ExampleTableNode([ | ||
| 666 => ['A', 'B'], | ||
| 777 => ['A1', 'B1'], | ||
| 888 => ['A2', 'B2'], | ||
| ], 'exa'), | ||
| ], 'out', 444), | ||
| ], | ||
| 'fea', | ||
| 'en', | ||
| $file, | ||
| 111, | ||
| ), | ||
| ], | ||
| $features, | ||
| ); | ||
| } | ||
|
|
||
| public function testNonEmptyOutlineTableBodyRequiresTableHeader(): void | ||
| { | ||
| $file = $this->serializeCucumberMessagesToFile([ | ||
| 'gherkinDocument' => [ | ||
| 'feature' => [ | ||
| 'location' => ['line' => 1], | ||
| 'name' => 'Feature with an invalid example table', | ||
| 'description' => '', | ||
| 'keyword' => 'feature', | ||
| 'language' => 'en', | ||
| 'children' => [ | ||
| [ | ||
| 'scenario' => [ | ||
| 'location' => ['line' => 2], | ||
| 'name' => 'Examples Scenario', | ||
| 'keyword' => 'outline', | ||
| 'examples' => [ | ||
| [ | ||
| 'location' => ['line' => 3], | ||
| 'keyword' => 'example', | ||
| 'tableBody' => [ | ||
| [ | ||
| 'location' => ['line' => 777], | ||
| 'cells' => [ | ||
| ['value' => 'A1'], | ||
| ['value' => 'B1'], | ||
| ], | ||
| ], | ||
| [ | ||
| 'location' => ['line' => 888], | ||
| 'cells' => [ | ||
| ['value' => 'A2'], | ||
| ['value' => 'B2'], | ||
| ], | ||
| ], | ||
| ], | ||
| ], | ||
| ], | ||
| ], | ||
| ], | ||
| ], | ||
| ], | ||
| ], | ||
| ]); | ||
|
|
||
| $this->expectExceptionObject(new NodeException('Table header is required when a table body is provided for the example on line 3.')); | ||
|
|
||
| $this->loader->load($file); | ||
| } | ||
|
|
||
| public function testEmptyOutlineTableBodyDoesNotRequireTableHeader(): void | ||
| { | ||
| $file = $this->serializeCucumberMessagesToFile([ | ||
| 'gherkinDocument' => [ | ||
| 'feature' => [ | ||
| 'location' => ['line' => 1], | ||
| 'name' => 'Feature with an empty example table', | ||
| 'description' => '', | ||
| 'keyword' => 'feature', | ||
| 'language' => 'en', | ||
| 'children' => [ | ||
| [ | ||
| 'scenario' => [ | ||
| 'location' => ['line' => 2], | ||
| 'name' => 'Examples Scenario', | ||
| 'keyword' => 'outline', | ||
| 'examples' => [ | ||
| [ | ||
| 'location' => ['line' => 3], | ||
| 'keyword' => 'example', | ||
| 'tableBody' => [], | ||
| ], | ||
| ], | ||
| ], | ||
| ], | ||
| ], | ||
| ], | ||
| ], | ||
| ]); | ||
|
|
||
| $features = $this->loader->load($file); | ||
|
|
||
| $this->assertEquals( | ||
| [ | ||
| new FeatureNode( | ||
| 'Feature with an empty example table', | ||
| '', | ||
| [], | ||
| null, | ||
| [ | ||
| new OutlineNode('Examples Scenario', [], [], [], 'outline', 2), | ||
| ], | ||
| 'feature', | ||
| 'en', | ||
| $file, | ||
| 1, | ||
| ), | ||
| ], | ||
| $features, | ||
| ); | ||
| } | ||
|
|
||
| private function serializeCucumberMessagesToFile(mixed ...$messages): string | ||
| { | ||
| return 'data://application/x-ndjson;base64,' | ||
| . base64_encode(implode("\n", array_map(json_encode(...), $messages)) . "\n"); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.