diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2017-01-15 09:49:39 -0500 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2017-01-15 09:49:39 -0500 |
commit | 92de04ca5e44532f3fa78dece8df822557805373 (patch) | |
tree | b1be6758ae71de571a8c12ffc7266dd65f703cda | |
parent | 36a1630ab9b54975369a487595380867a470c06e (diff) | |
download | python-coveragepy-git-92de04ca5e44532f3fa78dece8df822557805373.tar.gz |
Add a test of CheckUniqueFilenames
-rw-r--r-- | tests/helpers.py | 16 | ||||
-rw-r--r-- | tests/test_testing.py | 26 |
2 files changed, 38 insertions, 4 deletions
diff --git a/tests/helpers.py b/tests/helpers.py index ebc15cd1..a132872e 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 d91de28d..d0e75797 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. |