diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2019-06-18 21:50:50 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2019-06-19 06:00:35 -0400 |
commit | 7a7a93d7abc710f11c93a6f0a4edbea7cb0c47e5 (patch) | |
tree | ca8c5f089d8bf59b66ed65d99d96e0e5a4c86a48 | |
parent | f36c3acd2f236cebbac254fc9c71e2080cbfece2 (diff) | |
download | python-coveragepy-git-7a7a93d7abc710f11c93a6f0a4edbea7cb0c47e5.tar.gz |
Move arcz_to_arcs to misc so we can use it elsewhere
-rw-r--r-- | coverage/misc.py | 39 | ||||
-rw-r--r-- | tests/coveragetest.py | 46 | ||||
-rw-r--r-- | tests/test_misc.py | 11 | ||||
-rw-r--r-- | tests/test_parser.py | 8 | ||||
-rw-r--r-- | tests/test_testing.py | 8 |
5 files changed, 57 insertions, 55 deletions
diff --git a/coverage/misc.py b/coverage/misc.py index d87a7b89..ad7b834f 100644 --- a/coverage/misc.py +++ b/coverage/misc.py @@ -281,6 +281,45 @@ def substitute_variables(text, variables=os.environ): 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) + + class BaseCoverageException(Exception): """The base of all Coverage exceptions.""" pass diff --git a/tests/coveragetest.py b/tests/coveragetest.py index 322f6c72..b06db896 100644 --- a/tests/coveragetest.py +++ b/tests/coveragetest.py @@ -26,7 +26,7 @@ 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.data import STORAGE -from coverage.misc import StopEverything +from coverage.misc import arcz_to_arcs, StopEverything from tests.helpers import run_command, SuperModuleCleaner @@ -138,44 +138,6 @@ class CoverageTest( self.last_module_name = 'coverage_test_' + str(random.random())[2:] return self.last_module_name - # 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(self, 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 * self._arcz_map[a], bsgn * self._arcz_map[b])) - return sorted(arcs) - 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. @@ -214,11 +176,11 @@ class CoverageTest( self.make_file(modname + ".py", text) if arcs is None and arcz is not None: - arcs = self.arcz_to_arcs(arcz) + arcs = arcz_to_arcs(arcz) if arcs_missing is None: - arcs_missing = self.arcz_to_arcs(arcz_missing) + arcs_missing = arcz_to_arcs(arcz_missing) if arcs_unpredicted is None: - arcs_unpredicted = self.arcz_to_arcs(arcz_unpredicted) + arcs_unpredicted = arcz_to_arcs(arcz_unpredicted) # Start up coverage.py. cov = coverage.Coverage(branch=True) diff --git a/tests/test_misc.py b/tests/test_misc.py index bde35b66..09fb5c4d 100644 --- a/tests/test_misc.py +++ b/tests/test_misc.py @@ -5,7 +5,7 @@ import pytest -from coverage.misc import contract, dummy_decorator_with_args, file_be_gone +from coverage.misc import arcz_to_arcs, contract, dummy_decorator_with_args, file_be_gone from coverage.misc import Hasher, one_of, substitute_variables from coverage.misc import CoverageException @@ -152,3 +152,12 @@ 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 diff --git a/tests/test_parser.py b/tests/test_parser.py index 6d86822f..6fd31ca5 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -8,7 +8,7 @@ import textwrap from tests.coveragetest import CoverageTest from coverage import env -from coverage.misc import NotPython +from coverage.misc import arcz_to_arcs, NotPython from coverage.parser import PythonParser @@ -201,16 +201,16 @@ class PythonParserTest(CoverageTest): if env.PYBEHAVIOR.trace_decorated_def: expected_statements = {1, 2, 4, 5, 8, 9, 10} - expected_arcs = set(self.arcz_to_arcs(".1 14 45 58 89 9. .2 2. -8A A-8")) + expected_arcs = set(arcz_to_arcs(".1 14 45 58 89 9. .2 2. -8A A-8")) expected_exits = {1: 1, 2: 1, 4: 1, 5: 1, 8: 1, 9: 1, 10: 1} else: expected_statements = {1, 2, 4, 8, 10} - expected_arcs = set(self.arcz_to_arcs(".1 14 48 8. .2 2. -8A A-8")) + expected_arcs = set(arcz_to_arcs(".1 14 48 8. .2 2. -8A A-8")) expected_exits = {1: 1, 2: 1, 4: 1, 8: 1, 10: 1} if env.PYVERSION >= (3, 7, 0, 'beta', 5): # 3.7 changed how functions with only docstrings are numbered. - expected_arcs.update(set(self.arcz_to_arcs("-46 6-4"))) + expected_arcs.update(set(arcz_to_arcs("-46 6-4"))) expected_exits.update({6: 1}) self.assertEqual(expected_statements, parser.statements) diff --git a/tests/test_testing.py b/tests/test_testing.py index 2d9aeb21..4a4a013c 100644 --- a/tests/test_testing.py +++ b/tests/test_testing.py @@ -41,14 +41,6 @@ class TestingTest(TestCase): class CoverageTestTest(CoverageTest): """Test the methods in `CoverageTest`.""" - def test_arcz_to_arcs(self): - self.assertEqual(self.arcz_to_arcs(".1 12 2."), [(-1, 1), (1, 2), (2, -1)]) - self.assertEqual(self.arcz_to_arcs("-11 12 2-5"), [(-1, 1), (1, 2), (2, -5)]) - self.assertEqual( - self.arcz_to_arcs("-QA CB IT Z-A"), - [(-26, 10), (12, 11), (18, 29), (35, -10)] - ) - def test_file_exists(self): self.make_file("whoville.txt", "We are here!") self.assert_exists("whoville.txt") |