diff options
-rw-r--r-- | coverage/misc.py | 31 | ||||
-rw-r--r-- | tests/coveragetest.py | 6 | ||||
-rw-r--r-- | tests/test_misc.py | 21 |
3 files changed, 54 insertions, 4 deletions
diff --git a/coverage/misc.py b/coverage/misc.py index 6f8c368a..f828a7d4 100644 --- a/coverage/misc.py +++ b/coverage/misc.py @@ -356,6 +356,37 @@ def arcz_to_arcs(arcz): return sorted(arcs) +_arcz_unmap = {val: ch for ch, val in _arcz_map.items()} + +def _arcs_to_arcz_repr_one(num): + """Return an arcz form of the number `num`, or "?" if there is none.""" + if num == -1: + return "." + z = "" + if num < 0: + z += "-" + num *= -1 + z += _arcz_unmap.get(num, "?") + return z + + +def arcs_to_arcz_repr(arcs): + """Convert a list of arcs to a readable multi-line form for asserting. + + Each pair is on its own line, with a comment showing the arcz form, + to make it easier to decode when debugging test failures. + + """ + repr_list = [] + for a, b in arcs: + line = repr((a, b)) + line += " # " + line += _arcs_to_arcz_repr_one(a) + line += _arcs_to_arcz_repr_one(b) + repr_list.append(line) + return "\n".join(repr_list) + "\n" + + class BaseCoverageException(Exception): """The base of all Coverage exceptions.""" pass diff --git a/tests/coveragetest.py b/tests/coveragetest.py index 1917c5b3..905d6098 100644 --- a/tests/coveragetest.py +++ b/tests/coveragetest.py @@ -26,7 +26,7 @@ from coverage import env from coverage.backunittest import TestCase, unittest from coverage.backward import StringIO, import_local_file, string_class, shlex_quote from coverage.cmdline import CoverageScript -from coverage.misc import arcz_to_arcs, StopEverything +from coverage.misc import arcs_to_arcz_repr, arcz_to_arcs, StopEverything from tests.helpers import run_command, SuperModuleCleaner @@ -140,8 +140,8 @@ class CoverageTest( def assert_equal_arcs(self, a1, a2, msg=None): """Assert that the arc lists `a1` and `a2` are equal.""" # Make them into multi-line strings so we can see what's going wrong. - s1 = "\n".join(repr(a) for a in a1) + "\n" - s2 = "\n".join(repr(a) for a in a2) + "\n" + s1 = arcs_to_arcz_repr(a1) + s2 = arcs_to_arcz_repr(a2) self.assertMultiLineEqual(s1, s2, msg) def check_coverage( diff --git a/tests/test_misc.py b/tests/test_misc.py index 896dbf47..e6b83236 100644 --- a/tests/test_misc.py +++ b/tests/test_misc.py @@ -5,7 +5,8 @@ import pytest -from coverage.misc import arcz_to_arcs, contract, dummy_decorator_with_args, file_be_gone +from coverage.misc import arcs_to_arcz_repr, arcz_to_arcs +from coverage.misc import contract, dummy_decorator_with_args, file_be_gone from coverage.misc import Hasher, one_of, substitute_variables from coverage.misc import CoverageException, USE_CONTRACTS @@ -167,3 +168,21 @@ def test_substitute_variables_errors(text): ]) def test_arcz_to_arcs(arcz, arcs): assert arcz_to_arcs(arcz) == arcs + + +@pytest.mark.parametrize("arcs, arcz_repr", [ + ([(-1, 1), (1, 2), (2, -1)], "(-1, 1) # .1\n(1, 2) # 12\n(2, -1) # 2.\n"), + ([(-1, 1), (1, 2), (2, -5)], "(-1, 1) # .1\n(1, 2) # 12\n(2, -5) # 2-5\n"), + ([(-26, 10), (12, 11), (18, 29), (35, -10), (1, 33), (100, 7)], + ( + "(-26, 10) # -QA\n" + "(12, 11) # CB\n" + "(18, 29) # IT\n" + "(35, -10) # Z-A\n" + "(1, 33) # 1X\n" + "(100, 7) # ?7\n" + ) + ), +]) +def test_arcs_to_arcz_repr(arcs, arcz_repr): + assert arcs_to_arcz_repr(arcs) == arcz_repr |