Skip to content

Commit

Permalink
Merge pull request #32 from rayjolt/escape-doc-string
Browse files Browse the repository at this point in the history
Escape triple quotes in doc strings
  • Loading branch information
ducminh-phan committed Jan 25, 2020
2 parents 2026614 + 735255f commit 21cff12
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
9 changes: 8 additions & 1 deletion 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
28 changes: 28 additions & 0 deletions 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
27 changes: 27 additions & 0 deletions 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

0 comments on commit 21cff12

Please sign in to comment.