diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2009-10-16 07:25:56 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2009-10-16 07:25:56 -0400 |
commit | 9ca8f380e28b3bd315708da4d0c879549f9e3e42 (patch) | |
tree | 8fbc0925eb2bd741a62a829c4cb41dc2414cc105 /test | |
parent | 86f2a554175d899a227c726e9a894f8cfcba985e (diff) | |
download | python-coveragepy-9ca8f380e28b3bd315708da4d0c879549f9e3e42.tar.gz |
Start unit testing the arc measurement. In arcs, -1 means enter or exit.
Diffstat (limited to 'test')
-rw-r--r-- | test/coveragetest.py | 60 | ||||
-rw-r--r-- | test/test_arcs.py | 96 |
2 files changed, 143 insertions, 13 deletions
diff --git a/test/coveragetest.py b/test/coveragetest.py index 36fe078..e51723e 100644 --- a/test/coveragetest.py +++ b/test/coveragetest.py @@ -3,7 +3,7 @@ import imp, os, random, shutil, sys, tempfile, textwrap, unittest import coverage -from coverage.backward import set, StringIO # pylint: disable-msg=W0622 +from coverage.backward import set, sorted, StringIO # pylint: disable-msg=W0622 from backtest import run_command @@ -102,7 +102,29 @@ class CoverageTest(unittest.TestCase): self.n += 1 return modname - def check_coverage(self, text, lines=None, missing="", excludes=None, report=""): + # 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)] + + """ + arcs = [] + for a,b in arcz.split(): + arcs.append((self._arcz_map[a], self._arcz_map[b])) + return sorted(arcs) + + def check_coverage(self, text, lines=None, missing="", excludes=None, + report="", arcs=None, arcs_missing=None, + arcz=None, arcz_missing=None): """Check the coverage measurement of `text`. The source `text` is run and measured. `lines` are the line numbers @@ -117,8 +139,13 @@ class CoverageTest(unittest.TestCase): self.make_file(modname+".py", text) + if arcz is not None: + arcs = self.arcz_to_arcs(arcz) + if arcz_missing is not None: + arcs_missing = self.arcz_to_arcs(arcz_missing) + # Start up Coverage. - cov = coverage.coverage() + cov = coverage.coverage(branch=(arcs_missing is not None)) cov.erase() for exc in excludes or []: cov.exclude(exc) @@ -144,17 +171,24 @@ class CoverageTest(unittest.TestCase): break else: self.fail("None of the lines choices matched %r" % clines) - if missing is not None: - if type(missing) == type(""): - self.assertEqual(analysis.missing_formatted(), missing) - else: - for missing_list in missing: - if analysis.missing == missing_list: - break + + if missing is not None: + if type(missing) == type(""): + self.assertEqual(analysis.missing_formatted(), missing) else: - self.fail( - "None of the missing choices matched %r" % analysis.missing_formatted() - ) + for missing_list in missing: + if analysis.missing == missing_list: + break + else: + self.fail( + "None of the missing choices matched %r" % analysis.missing_formatted() + ) + + if arcs is not None: + self.assertEqual(analysis.arc_possibilities(), arcs) + + if arcs_missing is not None: + self.assertEqual(analysis.arcs_missing(), arcs_missing) if report: frep = StringIO() diff --git a/test/test_arcs.py b/test/test_arcs.py new file mode 100644 index 0000000..6c68e39 --- /dev/null +++ b/test/test_arcs.py @@ -0,0 +1,96 @@ +"""Tests for Coverage.py's arc measurement.""" + +import os, re, sys, textwrap + +import coverage +from coverage.backward import StringIO + +sys.path.insert(0, os.path.split(__file__)[0]) # Force relative import for Py3k +from coveragetest import CoverageTest + + +class ArcTest(CoverageTest): + """Tests for Coverage.py's arc measurement.""" + + def test_simple_sequence(self): + self.check_coverage("""\ + a = 1 + b = 2 + """, + arcs=[(-1,1), (1,2), (2,-1)], arcs_missing=[]) + self.check_coverage("""\ + a = 1 + + b = 3 + """, + arcs=[(-1,1), (1,3), (3,-1)], arcs_missing=[]) + self.check_coverage("""\ + + a = 2 + b = 3 + + c = 5 + """, + arcs=[(-1,2), (2,3), (3,5),(5,-1)], arcs_missing=[]) + + def test_function_def(self): + self.check_coverage("""\ + def foo(): + a = 2 + + foo() + """, + arcs=[(-1,1), (-1,2),(1,4),(2,-1), (4,-1)], arcs_missing=[]) + + def test_if(self): + self.check_coverage("""\ + a = 1 + if len([]) == 0: + a = 3 + assert a == 3 + """, + arcz=".1 12 23 24 34 4.", arcz_missing="24") + self.check_coverage("""\ + a = 1 + if len([]) == 1: + a = 3 + assert a == 1 + """, + arcz=".1 12 23 24 34 4.", arcz_missing="23 34") + + def test_if_else(self): + self.check_coverage("""\ + if len([]) == 0: + a = 2 + else: + b = 4 + c = 5 + """, + arcz=".1 12 25 14 45 5.", arcz_missing="14 45") + self.check_coverage("""\ + if len([]) == 1: + a = 2 + else: + b = 4 + c = 5 + """, + arcz=".1 12 25 14 45 5.", arcz_missing="12 25") + + def test_loop(self): + self.check_coverage("""\ + for i in range(10): + a = 2 + b = 3 + """, + arcz=".1 12 21 13 3.", arcz_missing="") + self.check_coverage("""\ + for i in range(0): + a = 2 + b = 3 + """, + arcz=".1 12 21 13 3.", arcz_missing="12 21") + + def xest_xx(self): + self.check_coverage("""\ + """, + arcz="", arcz_missing="") |