From 0353c9d4fc2acf049cff81a952235fbe165213e6 Mon Sep 17 00:00:00 2001 From: Kieran Ryan Date: Sat, 13 Apr 2024 15:18:39 +0100 Subject: [PATCH] python: provide informative exception for trailing escapes in tables (#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. --- CHANGELOG.md | 5 +++-- python/gherkin/gherkin_line.py | 2 +- python/test/gherkin_test.py | 17 ++++++++++++++++- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d08824b7..ebd6e305c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/python/gherkin/gherkin_line.py b/python/gherkin/gherkin_line.py index 0342d8c20..8ca945282 100644 --- a/python/gherkin/gherkin_line.py +++ b/python/gherkin/gherkin_line.py @@ -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' diff --git a/python/test/gherkin_test.py b/python/test/gherkin_test.py index 826f72c07..34dc99c29 100644 --- a/python/test/gherkin_test.py +++ b/python/test/gherkin_test.py @@ -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 @@ -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))