Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from rayjolt/escape-doc-string
Escape triple quotes in doc strings
- Loading branch information
Showing
3 changed files
with
63 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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,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
28
tests/data/valid/doc_string_escape/expected_default.feature
This file contains 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 |
---|---|---|
@@ -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 |
This file contains 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 |
---|---|---|
@@ -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 |