-
Notifications
You must be signed in to change notification settings - Fork 243
Use a match statement to dispatch on gherkin Child fields #822
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
Open
Pierre-Sassoulas
wants to merge
5
commits into
master
Choose a base branch
from
refactor/match-gherkin-child
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+55
−6
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9efc781
Use a match statement to dispatch on gherkin Child fields
Pierre-Sassoulas 697ca4b
Cover the no-match arm of the gherkin Child dispatch
Pierre-Sassoulas 064621b
Enforce Child dispatch exhaustiveness with assert_never, drop the moc…
Pierre-Sassoulas b837fc9
Warn and skip unknown gherkin child kinds instead of asserting
Pierre-Sassoulas f85e551
Use patch.object context manager instead of monkeypatch in warn test
Pierre-Sassoulas 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
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 |
|---|---|---|
| @@ -1,7 +1,12 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import textwrap | ||
| from pathlib import Path | ||
| from unittest.mock import patch | ||
|
|
||
| import pytest | ||
|
|
||
| from src.pytest_bdd import parser as parser_module | ||
| from src.pytest_bdd.gherkin_parser import ( | ||
| Background, | ||
| Cell, | ||
|
|
@@ -851,3 +856,36 @@ def test_parser(): | |
| ) | ||
|
|
||
| assert gherkin_doc == expected_document | ||
|
|
||
|
|
||
| def test_feature_parser_warns_on_unknown_child_kinds(tmp_path): | ||
| """A Child with none of background/rule/scenario set is skipped with a warning. | ||
|
|
||
| The gherkin AST reserves the right to grow new child kinds; ``Child.from_dict`` | ||
| would map such a child to an all-``None`` dataclass, and ``FeatureParser.parse`` | ||
| must warn and skip it instead of crashing, so pytest-bdd keeps working against | ||
| future gherkin releases. | ||
| """ | ||
| (tmp_path / "minimal.feature").write_text( | ||
| textwrap.dedent( | ||
| """\ | ||
| Feature: Minimal | ||
| Scenario: A scenario | ||
| Given a step | ||
| """ | ||
| ) | ||
| ) | ||
|
|
||
| real_get_gherkin_document = parser_module.get_gherkin_document | ||
|
|
||
| def get_gherkin_document_with_unknown_child(abs_filename: str, encoding: str = "utf-8") -> GherkinDocument: | ||
| document = real_get_gherkin_document(abs_filename, encoding) | ||
| document.feature.children.append(Child()) | ||
| return document | ||
|
|
||
| with patch.object(parser_module, "get_gherkin_document", get_gherkin_document_with_unknown_child): | ||
| with pytest.warns(UserWarning, match="Unknown gherkin child"): | ||
|
Contributor
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. No need for a nested with, we can have 2 context managers in the same with-statement |
||
| feature = parser_module.FeatureParser(str(tmp_path), "minimal.feature").parse() | ||
|
|
||
| assert list(feature.scenarios) == ["A scenario"] | ||
| assert feature.background is None | ||
Oops, something went wrong.
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.
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.
Why?