Fix incorrect backslash before multi-lines string by LiuYinCarl · Pull Request #1828 · nedbat/coveragepy (original) (raw)

Tests added in dc819ff

I stumbled across this while reviewing #1838. I think the tests (and this fix) are incomplete.

def test_bug_1828(self) -> None:
# https://github.com/nedbat/coveragepy/pull/1828
self.make_file("backslashes.py", """\
a = ["aaa",\\
"bbb \\
ccc"]
""")
cov = coverage.Coverage()
backslashes = self.start_import_stop(cov, "backslashes")
cov.html_report(backslashes, directory="out")
contains(
"out/backslashes_py.html",
# line 2 is `"bbb \`
r'2'
+ r' "bbb \',
# line 3 is `ccc"]`
r'3'
+ r' ccc"]',
)

In lines 1146–1154, you are testing whether the HTML output correctly reproduces the backslash in the "bbb ccc" string. However, the rendered text does not contain the backslash and linebreak within line 1137. This leads to a missing linebreak, and therefore to misaligned coverage information similar to #1836, just in the other direction.

def test_1828(self) -> None:
# https://github.com/nedbat/coveragepy/pull/1828
tokens = list(source_token_lines(textwrap.dedent("""
x = \
1
a = ["aaa",\\
"bbb \\
ccc"]
""")))
assert tokens == [
[],
[('nam', 'x'), ('ws', ' '), ('op', '='), ('ws', ' '), ('num', '1')],
[('nam', 'a'), ('ws', ' '), ('op', '='), ('ws', ' '),
('op', '['), ('str', '"aaa"'), ('op', ','), ('xx', '\\')],
[('ws', ' '), ('str', '"bbb \\')],
[('str', ' ccc"'), ('op', ']')],
]

Testing the same input, just with the tokenizer instead of the HTML report renderer. You can see in line 112 that there is a ws token with many spaces (which doesn't occur in the tokenizer input) instead of two tokens with a backspace continuation (which does occur). Again, this leads to a missing linebreak, and to misaligned coverage info for all following lines.


Concerning this pull request in general: