From 735255fc8a14faa8ef4144c494ed95e52b04d2e5 Mon Sep 17 00:00:00 2001 From: Jack Taylor Date: Sun, 19 Jan 2020 18:58:21 +0900 Subject: [PATCH] Escape triple quotes in doc strings If a doc string contains escaped triple quotes, then they must also be escaped when reformatting, otherwise the resulting document will be invalid. Fixes #31 --- reformat_gherkin/ast_node/doc_string.py | 9 +++++- .../expected_default.feature | 28 +++++++++++++++++++ .../valid/doc_string_escape/input.feature | 27 ++++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 tests/data/valid/doc_string_escape/expected_default.feature create mode 100644 tests/data/valid/doc_string_escape/input.feature diff --git a/reformat_gherkin/ast_node/doc_string.py b/reformat_gherkin/ast_node/doc_string.py index 2e184de..90edd98 100644 --- a/reformat_gherkin/ast_node/doc_string.py +++ b/reformat_gherkin/ast_node/doc_string.py @@ -1,10 +1,17 @@ +from attr import attrib + from ._base import prepare from .location import LocationMixin +def escape_doc_string_value(text: str) -> str: + """Escape triple-quotes in doc strings.""" + return text.replace('"""', '\\"\\"\\"') + + @prepare class DocString(LocationMixin): - content: str + content: str = attrib(converter=escape_doc_string_value) def __iter__(self): yield self diff --git a/tests/data/valid/doc_string_escape/expected_default.feature b/tests/data/valid/doc_string_escape/expected_default.feature new file mode 100644 index 0000000..feb1756 --- /dev/null +++ b/tests/data/valid/doc_string_escape/expected_default.feature @@ -0,0 +1,28 @@ +Feature: Docstrings + + Scenario: Escaping docstrings + Given I have a docstring + """ + This docstring has \"escaped\" double quotes. + It also has \`escaped\` backticks. + """ + And I have a docstring with an escaped docstring in + """ + This is another docstring. + \"\"\" + It has an escaped docstring inside it. + \"\"\" + """ + And I have a backtick docstring + """ + This docstring has \"escaped\" double quotes. + It also has \`escaped\` backticks. + """ + And I have a nested single-quote docstring + """ + This is a backtick docstring. + \`\`\` + It also has an escaped docstring inside it. + \`\`\` + """ + Then the escaped characters are printed when the document is reformatted diff --git a/tests/data/valid/doc_string_escape/input.feature b/tests/data/valid/doc_string_escape/input.feature new file mode 100644 index 0000000..9173557 --- /dev/null +++ b/tests/data/valid/doc_string_escape/input.feature @@ -0,0 +1,27 @@ +Feature: Docstrings + Scenario: Escaping docstrings + Given I have a docstring + """ + This docstring has \"escaped\" double quotes. + It also has \`escaped\` backticks. + """ + And I have a docstring with an escaped docstring in + """ + This is another docstring. + \"\"\" + It has an escaped docstring inside it. + \"\"\" + """ + And I have a backtick docstring + ``` + This docstring has \"escaped\" double quotes. + It also has \`escaped\` backticks. + ``` + And I have a nested single-quote docstring + ``` + This is a backtick docstring. + \`\`\` + It also has an escaped docstring inside it. + \`\`\` + ``` + Then the escaped characters are printed when the document is reformatted