summaryrefslogtreecommitdiff
path: root/tests/helpers.py
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2021-10-27 08:02:44 -0400
committerNed Batchelder <ned@nedbatchelder.com>2021-10-27 08:50:31 -0400
commit15aff0ad7d6c92851bc21fc39863a3949edbb9c6 (patch)
tree4c1bbef4e9e6d9d3f789fb3b52649c42100ccbf5 /tests/helpers.py
parent93c9ca9f1b2e5d0b45dbf4b82c77aaf05b458bac (diff)
downloadpython-coveragepy-git-15aff0ad7d6c92851bc21fc39863a3949edbb9c6.tar.gz
refactor(test): re_lines is more useful if it returns a list
Diffstat (limited to 'tests/helpers.py')
-rw-r--r--tests/helpers.py14
1 files changed, 10 insertions, 4 deletions
diff --git a/tests/helpers.py b/tests/helpers.py
index 185f20dd..7e6594ac 100644
--- a/tests/helpers.py
+++ b/tests/helpers.py
@@ -134,15 +134,21 @@ class CheckUniqueFilenames:
def re_lines(text, pat, match=True):
- """Return the text of lines that match `pat` in the string `text`.
+ """Return a list of lines selected by `pat` in the string `text`.
If `match` is false, the selection is inverted: only the non-matching
lines are included.
- Returns a string, the text of only the selected lines.
+ Returns a list, the selected lines, without line endings.
"""
- return "".join(l for l in text.splitlines(True) if bool(re.search(pat, l)) == match)
+ assert len(pat) < 200, "It's super-easy to swap the arguments to re_lines"
+ return [l for l in text.splitlines() if bool(re.search(pat, l)) == match]
+
+
+def re_lines_text(text, pat, match=True):
+ """Return the multi-line text of lines selected by `pat`."""
+ return "".join(l + "\n" for l in re_lines(text, pat, match=match))
def re_line(text, pat):
@@ -151,7 +157,7 @@ def re_line(text, pat):
Raises an AssertionError if more than one, or less than one, line matches.
"""
- lines = re_lines(text, pat).splitlines()
+ lines = re_lines(text, pat)
assert len(lines) == 1
return lines[0]