diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2019-12-31 08:02:17 -0500 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2019-12-31 08:02:17 -0500 |
commit | a6912d67648c1acdae73db7c2b772b09fb236c54 (patch) | |
tree | 8e2b30bb58f6bfc02243e167be2ebe271633a987 | |
parent | a9bc9bfd1179a64839d4908fb8f54e7823ee044e (diff) | |
download | python-coveragepy-git-a6912d67648c1acdae73db7c2b772b09fb236c54.tar.gz |
Move test helpers to tests.helpers
-rw-r--r-- | coverage/misc.py | 70 | ||||
-rw-r--r-- | tests/coveragetest.py | 3 | ||||
-rw-r--r-- | tests/helpers.py | 70 | ||||
-rw-r--r-- | tests/test_misc.py | 28 | ||||
-rw-r--r-- | tests/test_parser.py | 7 | ||||
-rw-r--r-- | tests/test_testing.py | 28 |
6 files changed, 104 insertions, 102 deletions
diff --git a/coverage/misc.py b/coverage/misc.py index f828a7d4..5c4381ab 100644 --- a/coverage/misc.py +++ b/coverage/misc.py @@ -317,76 +317,6 @@ def substitute_variables(text, variables): return text -# Map chars to numbers for arcz_to_arcs -_arcz_map = {'.': -1} -_arcz_map.update(dict((c, ord(c) - ord('0')) for c in '123456789')) -_arcz_map.update(dict( - (c, 10 + ord(c) - ord('A')) for c in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' -)) - -def arcz_to_arcs(arcz): - """Convert a compact textual representation of arcs to a list of pairs. - - The text has space-separated pairs of letters. Period is -1, 1-9 are - 1-9, A-Z are 10 through 36. The resulting list is sorted regardless of - the order of the input pairs. - - ".1 12 2." --> [(-1,1), (1,2), (2,-1)] - - Minus signs can be included in the pairs: - - "-11, 12, 2-5" --> [(-1,1), (1,2), (2,-5)] - - """ - arcs = [] - for pair in arcz.split(): - asgn = bsgn = 1 - if len(pair) == 2: - a, b = pair - else: - assert len(pair) == 3 - if pair[0] == '-': - _, a, b = pair - asgn = -1 - else: - assert pair[1] == '-' - a, _, b = pair - bsgn = -1 - arcs.append((asgn * _arcz_map[a], bsgn * _arcz_map[b])) - 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 905d6098..f9091a8d 100644 --- a/tests/coveragetest.py +++ b/tests/coveragetest.py @@ -26,8 +26,9 @@ 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 arcs_to_arcz_repr, arcz_to_arcs, StopEverything +from coverage.misc import StopEverything +from tests.helpers import arcs_to_arcz_repr, arcz_to_arcs from tests.helpers import run_command, SuperModuleCleaner diff --git a/tests/helpers.py b/tests/helpers.py index 09ba485d..9c6a0ad8 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -133,3 +133,73 @@ class SuperModuleCleaner(ModuleCleaner): shutil.rmtree("__pycache__") invalidate_import_caches() + + +# Map chars to numbers for arcz_to_arcs +_arcz_map = {'.': -1} +_arcz_map.update(dict((c, ord(c) - ord('0')) for c in '123456789')) +_arcz_map.update(dict( + (c, 10 + ord(c) - ord('A')) for c in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' +)) + +def arcz_to_arcs(arcz): + """Convert a compact textual representation of arcs to a list of pairs. + + The text has space-separated pairs of letters. Period is -1, 1-9 are + 1-9, A-Z are 10 through 36. The resulting list is sorted regardless of + the order of the input pairs. + + ".1 12 2." --> [(-1,1), (1,2), (2,-1)] + + Minus signs can be included in the pairs: + + "-11, 12, 2-5" --> [(-1,1), (1,2), (2,-5)] + + """ + arcs = [] + for pair in arcz.split(): + asgn = bsgn = 1 + if len(pair) == 2: + a, b = pair + else: + assert len(pair) == 3 + if pair[0] == '-': + _, a, b = pair + asgn = -1 + else: + assert pair[1] == '-' + a, _, b = pair + bsgn = -1 + arcs.append((asgn * _arcz_map[a], bsgn * _arcz_map[b])) + 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" diff --git a/tests/test_misc.py b/tests/test_misc.py index e6b83236..2f6fbe7c 100644 --- a/tests/test_misc.py +++ b/tests/test_misc.py @@ -5,7 +5,6 @@ import pytest -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 @@ -159,30 +158,3 @@ def test_substitute_variables_errors(text): substitute_variables(text, VARS) assert text in str(exc_info.value) assert "Variable NOTHING is undefined" in str(exc_info.value) - - -@pytest.mark.parametrize("arcz, arcs", [ - (".1 12 2.", [(-1, 1), (1, 2), (2, -1)]), - ("-11 12 2-5", [(-1, 1), (1, 2), (2, -5)]), - ("-QA CB IT Z-A", [(-26, 10), (12, 11), (18, 29), (35, -10)]), -]) -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 diff --git a/tests/test_parser.py b/tests/test_parser.py index 6fd31ca5..053e4fd8 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -5,12 +5,13 @@ import textwrap -from tests.coveragetest import CoverageTest - from coverage import env -from coverage.misc import arcz_to_arcs, NotPython +from coverage.misc import NotPython from coverage.parser import PythonParser +from tests.coveragetest import CoverageTest +from tests.helpers import arcz_to_arcs + class PythonParserTest(CoverageTest): """Tests for coverage.py's Python code parsing.""" diff --git a/tests/test_testing.py b/tests/test_testing.py index cf3e2014..2fda956b 100644 --- a/tests/test_testing.py +++ b/tests/test_testing.py @@ -18,6 +18,7 @@ from coverage.misc import StopEverything import coverage.optional from tests.coveragetest import CoverageTest, convert_skip_exceptions +from tests.helpers import arcs_to_arcz_repr, arcz_to_arcs from tests.helpers import CheckUniqueFilenames, re_lines, re_line @@ -331,3 +332,30 @@ def test_optional_without(): assert toml1 is toml3 is not None assert toml2 is None + + +@pytest.mark.parametrize("arcz, arcs", [ + (".1 12 2.", [(-1, 1), (1, 2), (2, -1)]), + ("-11 12 2-5", [(-1, 1), (1, 2), (2, -5)]), + ("-QA CB IT Z-A", [(-26, 10), (12, 11), (18, 29), (35, -10)]), +]) +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 |