summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2017-01-15 09:49:39 -0500
committerNed Batchelder <ned@nedbatchelder.com>2017-01-15 09:49:39 -0500
commita5f5e51f31860c3673a45d74f011fadfeeab1dc4 (patch)
tree09776afa7dad718db03ab5afa12ede6bd476751b
parent1c47034843c1008642f5364094f872197cb87e65 (diff)
downloadpython-coveragepy-a5f5e51f31860c3673a45d74f011fadfeeab1dc4.tar.gz
Add a test of CheckUniqueFilenames
-rw-r--r--tests/helpers.py16
-rw-r--r--tests/test_testing.py26
2 files changed, 38 insertions, 4 deletions
diff --git a/tests/helpers.py b/tests/helpers.py
index ebc15cd..a132872 100644
--- a/tests/helpers.py
+++ b/tests/helpers.py
@@ -54,11 +54,19 @@ class CheckUniqueFilenames(object):
self.wrapped = wrapped
@classmethod
- def hook(cls, cov, method_name):
- """Replace a method with our checking wrapper."""
- method = getattr(cov, method_name)
+ def hook(cls, obj, method_name):
+ """Replace a method with our checking wrapper.
+
+ The method must take a string as a first argument. That argument
+ will be checked for uniqueness across all the calls to this method.
+
+ The values don't have to be file names actually, just strings, but
+ we only use it for filename arguments.
+
+ """
+ method = getattr(obj, method_name)
hook = cls(method)
- setattr(cov, method_name, hook.wrapper)
+ setattr(obj, method_name, hook.wrapper)
return hook
def wrapper(self, filename, *args, **kwargs):
diff --git a/tests/test_testing.py b/tests/test_testing.py
index d91de28..d0e7579 100644
--- a/tests/test_testing.py
+++ b/tests/test_testing.py
@@ -13,6 +13,7 @@ from coverage.backunittest import TestCase
from coverage.files import actual_path
from tests.coveragetest import CoverageTest
+from tests.helpers import CheckUniqueFilenames
class TestingTest(TestCase):
@@ -140,6 +141,31 @@ class CoverageTestTest(CoverageTest):
self.assertEqual(environ.strip(), "COV_FOOBAR = XYZZY")
+class CheckUniqueFilenamesTest(CoverageTest):
+ """Tests of CheckUniqueFilenames."""
+
+ class Stub(object):
+ """A stand-in for the class we're checking."""
+ def __init__(self, x):
+ self.x = x
+
+ def method(self, filename, a=17, b="hello"):
+ """The method we'll wrap, with args to be sure args work."""
+ return (self.x, filename, a, b)
+
+ def test_detect_duplicate(self):
+ stub = self.Stub(23)
+ CheckUniqueFilenames.hook(stub, "method")
+
+ # Two method calls with different names are fine.
+ assert stub.method("file1") == (23, "file1", 17, "hello")
+ assert stub.method("file2", 1723, b="what") == (23, "file2", 1723, "what")
+
+ # A duplicate file name trips an assertion.
+ with self.assertRaises(AssertionError):
+ stub.method("file1")
+
+
def same_python_executable(e1, e2):
"""Determine if `e1` and `e2` refer to the same Python executable.