summaryrefslogtreecommitdiff
path: root/tests/helpers.py
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2017-01-15 15:54:21 -0500
committerNed Batchelder <ned@nedbatchelder.com>2017-01-15 15:54:21 -0500
commit62e03759315260aa26192454bcaa65075ff41cb2 (patch)
tree0cbd6db2f99b70168dccde6f03f0a5d0395afab8 /tests/helpers.py
parent50b37557df12f0ceb117860ce80a46e13b9d2beb (diff)
downloadpython-coveragepy-git-62e03759315260aa26192454bcaa65075ff41cb2.tar.gz
Promote re_line and re_lines to real helpers, with tests.
Diffstat (limited to 'tests/helpers.py')
-rw-r--r--tests/helpers.py24
1 files changed, 24 insertions, 0 deletions
diff --git a/tests/helpers.py b/tests/helpers.py
index a132872e..344ed71e 100644
--- a/tests/helpers.py
+++ b/tests/helpers.py
@@ -4,6 +4,7 @@
"""Helpers for coverage.py tests."""
import os
+import re
import subprocess
import sys
@@ -77,3 +78,26 @@ class CheckUniqueFilenames(object):
self.filenames.add(filename)
ret = self.wrapped(filename, *args, **kwargs)
return ret
+
+
+def re_lines(text, pat, match=True):
+ """Return the text of lines that match `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.
+
+ """
+ return "".join(l for l in text.splitlines(True) if bool(re.search(pat, l)) == match)
+
+
+def re_line(text, pat):
+ """Return the one line in `text` that matches regex `pat`.
+
+ Raises an AssertionError if more than one, or less than one, line matches.
+
+ """
+ lines = re_lines(text, pat).splitlines()
+ assert len(lines) == 1
+ return lines[0]