diff options
-rw-r--r-- | coverage/codeunit.py | 22 | ||||
-rw-r--r-- | coverage/execfile.py | 4 | ||||
-rw-r--r-- | test/test_codeunit.py | 11 | ||||
-rw-r--r-- | test/test_coverage.py | 65 | ||||
-rw-r--r-- | test/test_execfile.py | 2 |
5 files changed, 68 insertions, 36 deletions
diff --git a/coverage/codeunit.py b/coverage/codeunit.py index 87782cd0..7f4ed966 100644 --- a/coverage/codeunit.py +++ b/coverage/codeunit.py @@ -83,8 +83,26 @@ class CodeUnit: def __repr__(self): return "<CodeUnit name=%r filename=%r>" % (self.name, self.filename) - def __cmp__(self, other): - return cmp(self.name, other.name) + # Annoying comparison operators. Py3k wants __lt__ etc, and Py2k needs all + # of them defined. + + def __lt__(self, other): + return self.name < other.name + + def __le__(self, other): + return self.name <= other.name + + def __eq__(self, other): + return self.name == other.name + + def __ne__(self, other): + return self.name != other.name + + def __gt__(self, other): + return self.name > other.name + + def __ge__(self, other): + return self.name >= other.name def flat_rootname(self): """A base for a flat filename to correspond to this code unit. diff --git a/coverage/execfile.py b/coverage/execfile.py index 09947bcd..a345c767 100644 --- a/coverage/execfile.py +++ b/coverage/execfile.py @@ -6,8 +6,8 @@ try: # In Py 2.x, the builtins were in __builtin__ BUILTINS = sys.modules['__builtin__'] except KeyError: - # In Py 3.x, they're in builtin - BUILTINS = sys.modules['builtin'] + # In Py 3.x, they're in builtins + BUILTINS = sys.modules['builtins'] def run_python_file(filename, args): diff --git a/test/test_codeunit.py b/test/test_codeunit.py index d03ff8d9..46853b76 100644 --- a/test/test_codeunit.py +++ b/test/test_codeunit.py @@ -58,3 +58,14 @@ class CodeUnitTest(CoverageTest): self.assertEqual(cu[0].source_file().read(), "# afile.py\n") self.assertEqual(cu[1].source_file().read(), "# bfile.py\n") self.assertEqual(cu[2].source_file().read(), "# cfile.py\n") + + def test_comparison(self): + acu = code_unit_factory("aa/afile.py", FileLocator()) + acu2 = code_unit_factory("aa/afile.py", FileLocator()) + zcu = code_unit_factory("aa/zfile.py", FileLocator()) + bcu = code_unit_factory("aa/bb/bfile.py", FileLocator()) + assert acu == acu2 and acu <= acu2 and acu >= acu2 + assert acu < zcu and acu <= zcu and acu != zcu + assert zcu > acu and zcu >= acu and zcu != acu + assert acu < bcu and acu <= bcu and acu != bcu + assert bcu > acu and bcu >= acu and bcu != acu diff --git a/test/test_coverage.py b/test/test_coverage.py index d0b2f9bc..1084b984 100644 --- a/test/test_coverage.py +++ b/test/test_coverage.py @@ -206,18 +206,19 @@ class SimpleStatementTest(CoverageTest): """, [1,2,3,6,9], "") - def testPrint(self): - self.checkCoverage("""\ - print "hello, world!" - print ("hey: %d" % - 17) - print "goodbye" - print "hello, world!", - print ("hey: %d" % - 17), - print "goodbye", - """, - [1,2,4,5,6,8], "") + if sys.hexversion < 0x03000000: # Print statement is gone in Py3k. + def testPrint(self): + self.checkCoverage("""\ + print "hello, world!" + print ("hey: %d" % + 17) + print "goodbye" + print "hello, world!", + print ("hey: %d" % + 17), + print "goodbye", + """, + [1,2,4,5,6,8], "") def testRaise(self): self.checkCoverage("""\ @@ -281,20 +282,22 @@ class SimpleStatementTest(CoverageTest): def testBreak(self): self.checkCoverage("""\ for x in range(10): - print "Hello" + a = 2 + x break - print "Not here" + a = 4 + assert a == 2 """, - [1,2,3,4], "4") + [1,2,3,4,5], "4") def testContinue(self): self.checkCoverage("""\ for x in range(10): - print "Hello" + a = 2 + x continue - print "Not here" + a = 4 + assert a == 11 """, - [1,2,3,4], "4") + [1,2,3,4,5], "4") if 0: # Peephole optimization of jumps to jumps can mean that some statements @@ -1107,8 +1110,8 @@ class ExcludeTest(CoverageTest): def testExcludingAColonNotASuite(self): self.checkCoverage("""\ def foo(): - l = range(10) - print l[:3] # no cover + l = list(range(10)) + a = l[:3] # no cover b = 4 foo() @@ -1448,22 +1451,22 @@ if sys.hexversion >= 0x020500f0: class Managed: def __enter__(self): - print "enter" + desc = "enter" def __exit__(self, type, value, tb): - print "exit", type + desc = "exit" m = Managed() with m: - print "block1a" - print "block1b" + desc = "block1a" + desc = "block1b" try: with m: - print "block2" + desc = "block2" raise Exception("Boo!") except: - print "caught" + desc = "caught" """, [1,3,4,5,7,8,10,11,12,13,15,16,17,18,19,20], "") @@ -1577,7 +1580,7 @@ class ProcessTest(CoverageTest): import covmod1 import covmodzip1 a = 1 - print 'done' + print ('done') """) self.assert_(not os.path.exists(".coverage")) @@ -1590,7 +1593,7 @@ class ProcessTest(CoverageTest): import covmod1 import covmodzip1 a = 1 - print 'done' + print ('done') """) out = self.run_command("coverage -x mycode.py") @@ -1649,7 +1652,7 @@ class ProcessTest(CoverageTest): else: c = 1 d = 1 - print 'done' + print ('done') """) out = self.run_command("coverage -x -p b_or_c.py b") @@ -1725,12 +1728,12 @@ class PyexpatTest(CoverageTest): def foo(): dom = xml.dom.minidom.parseString(XML) assert len(dom.getElementsByTagName('child')) == 2 - print "Parsed" + a = 11 foo() """) - self.makeFile("outer.py", "\n"*100 + "import trydom\nprint 'done'\n") + self.makeFile("outer.py", "\n"*100 + "import trydom\na = 102\n") cov = coverage.coverage() cov.erase() diff --git a/test/test_execfile.py b/test/test_execfile.py index 6cc7cab8..9c838e1a 100644 --- a/test/test_execfile.py +++ b/test/test_execfile.py @@ -40,7 +40,7 @@ class RunTest(CoverageTest): def test_no_extra_file(self): # Make sure that running a file doesn't create an extra compiled file. self.makeFile("xxx", """\ - print "a non-.py file!" + desc = "a non-.py file!" """) self.assertEqual(os.listdir("."), ["xxx"]) |