Skip to content

Commit

Permalink
python: provide informative exception for trailing escapes in tables (#…
Browse files Browse the repository at this point in the history
…241)

With the Python implementation:

1. Whitespace either side of a table row is stripped.
2. The table row is passed to a function to strip and yields its cell
   values
3. Characters in each cell are iterated over to handle them
   appropriately, such as new columns (|), escape characters, etc.
4. As escape characters are typically followed by a special character
   (e.g. n, t), when an escape character is detected \\ an additional
   next call is made to grab the next character
5. As in step 1, whitespace after a trailing escape character (\\)
   would be stripped, there would not be a following character and a
   StopIteration error is raised.
  • Loading branch information
kieran-ryan committed Apr 13, 2024
1 parent cf3d3a0 commit 0353c9d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ This document is formatted according to the principles of [Keep A CHANGELOG](htt

## [Unreleased]
### Fixed
- Provide trailing space in Irish keywords ([#243](https://github.com/cucumber/gherkin/pull/243))
- Tamil "And" and "But" translations should have single trailing space ([#243](https://github.com/cucumber/gherkin/pull/243))
- [Python] Provide informative exception for trailing escapes in tables ([#241](https://github.com/cucumber/gherkin/pull/241))
- (i18n) Provide trailing space in Irish keywords ([#243](https://github.com/cucumber/gherkin/pull/243))
- (i18n) Tamil "And" and "But" translations should have single trailing space ([#243](https://github.com/cucumber/gherkin/pull/243))

## [28.0.0] - 2024-02-15
### Added
Expand Down
2 changes: 1 addition & 1 deletion python/gherkin/gherkin_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def split_table_cells(self, row):
cell = ''
start_col = col + 1
elif char == '\\':
char = next(row)
char = next(row, "")
col += 1
if char == 'n':
cell += '\n'
Expand Down
17 changes: 16 additions & 1 deletion python/test/gherkin_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from gherkin.token_scanner import TokenScanner
from gherkin.token_matcher import TokenMatcher
from gherkin.parser import Parser
from gherkin.errors import ParserError
from gherkin.errors import CompositeParserException, ParserError
import pytest


Expand Down Expand Up @@ -89,3 +89,18 @@ def test_change_the_default_language():
}

assert expected == feature_file

@pytest.mark.parametrize("trailing_text", ["\\", "\\ "])
def test_inconsistent_cell_count_with_trailing_escape(trailing_text):
feature_text = """Feature:
Scenario:
Given I have a table
| Name | Value |
| A | """ + trailing_text
parser = Parser()

with pytest.raises(
CompositeParserException,
match="inconsistent cell count within the table",
):
parser.parse(TokenScanner(feature_text))

0 comments on commit 0353c9d

Please sign in to comment.