From 745fc9a11340f192e2f73c0f1f3101866d26f74f Mon Sep 17 00:00:00 2001 From: Gustavo Niemeyer Date: Sat, 17 Nov 2007 15:30:34 -0200 Subject: Added MockerTestCase.assertMethodsMatch(). It will verify if all public methods found in the class passed as the first argument are also present in the class passed as the second argument, and that they accept the same arguments. This is useful to verify if a fake or stub class have the same API as the real class being simulated. --- mocker.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'mocker.py') diff --git a/mocker.py b/mocker.py index fe27892..7d7160c 100644 --- a/mocker.py +++ b/mocker.py @@ -192,12 +192,46 @@ class MockerTestCase(unittest.TestCase): raise self.failureException(msg or "abs(%r - %r) <= %r" % (first, second, tolerance)) + def failUnlessMethodsMatch(self, first, second): + """Assert that public methods in C{first} are present in C{second}. + + This method asserts that all public methods found in C{first} are also + present in C{second} and accept the same arguments. C{first} may + have its own private methods, though, and may not have all methods + found in C{second}. Note that if a private method in C{first} matches + the name of one in C{second}, their specification is still compared. + + This is useful to verify if a fake or stub class have the same API as + the real class being simulated. + """ + first_methods = dict(inspect.getmembers(first, inspect.ismethod)) + second_methods = dict(inspect.getmembers(second, inspect.ismethod)) + for name, first_method in first_methods.items(): + first_argspec = inspect.getargspec(first_method) + first_formatted = inspect.formatargspec(*first_argspec) + + second_method = second_methods.get(name) + if second_method is None: + if name[:1] == "_": + continue # First may have its own private methods. + raise self.failureException("%s.%s%s not present in %s" % + (first.__name__, name, first_formatted, second.__name__)) + + second_argspec = inspect.getargspec(second_method) + if first_argspec != second_argspec: + second_formatted = inspect.formatargspec(*second_argspec) + raise self.failureException("%s.%s%s != %s.%s%s" % + (first.__name__, name, first_formatted, + second.__name__, name, second_formatted)) + + assertIs = failUnlessIs assertIsNot = failIfIs assertIn = failUnlessIn assertNotIn = failIfIn assertApproximates = failUnlessApproximates assertNotApproximates = failIfApproximates + assertMethodsMatch = failUnlessMethodsMatch # The following is provided for compatibility with Twisted's trial. assertIdentical = assertIs -- cgit v1.2.1