diff options
author | Gustavo Niemeyer <gustavo@niemeyer.net> | 2007-11-11 22:11:24 -0200 |
---|---|---|
committer | Gustavo Niemeyer <gustavo@niemeyer.net> | 2007-11-11 22:11:24 -0200 |
commit | 02c0298f780d8dcb435fad8967de2a956ac78d14 (patch) | |
tree | 65228f4810393771ef0d69627c81462e35b135a3 | |
parent | 990945320feecf1c5352382c1de2ec86bf6993b2 (diff) | |
download | mocker-02c0298f780d8dcb435fad8967de2a956ac78d14.tar.gz |
Fixed SpecChecker to not break with callables not supported by
getargspec (e.g. builtin functions).
-rw-r--r-- | mocker.py | 20 | ||||
-rwxr-xr-x | test.py | 23 |
2 files changed, 37 insertions, 6 deletions
@@ -1581,13 +1581,19 @@ class SpecChecker(Task): def __init__(self, method): self._method = method + self._unsupported = False + if method: - self._args, self._varargs, self._varkwargs, self._defaults = \ - inspect.getargspec(method) - if self._defaults is None: - self._defaults = () - if type(method) is type(self.run): - self._args = self._args[1:] + try: + self._args, self._varargs, self._varkwargs, self._defaults = \ + inspect.getargspec(method) + except TypeError: + self._unsupported = True + else: + if self._defaults is None: + self._defaults = () + if type(method) is type(self.run): + self._args = self._args[1:] def get_method(self): return self._method @@ -1601,6 +1607,8 @@ class SpecChecker(Task): def run(self, path): if not self._method: raise AssertionError("Method not found in real specification") + if self._unsupported: + return # Can't check it. Happens with builtin functions. :-( action = path.actions[-1] obtained_len = len(action.args) obtained_kwargs = action.kwargs.copy() @@ -211,6 +211,18 @@ class IntegrationTest(unittest.TestCase): self.assertEquals(list(mock), [1, 2, 3]) self.mocker.verify() + def test_replace_builtin_function(self): + """ + Inspection doesn't work on builtin functions, but proxying should + work even then (without spec enforcement in these cases). + """ + from zlib import adler32 + mock = self.mocker.proxy(adler32) + mock() + self.mocker.result(42) + self.mocker.replay() + self.assertEquals(mock(), 42) + class ExpectTest(unittest.TestCase): @@ -2644,6 +2656,17 @@ class SpecCheckerTest(unittest.TestCase): else: self.fail("AssertionError not raised") + def test_unsupported_object_for_getargspec(self): + from zlib import adler32 + # If that fails, this test has to change because either adler32 has + # changed, or the implementation of getargspec has changed. + self.assertRaises(TypeError, inspect.getargspec, adler32) + try: + task = SpecChecker(adler32) + task.run(self.path("asd")) + except TypeError, e: + self.fail("TypeError: %s" % str(e)) + def test_recorder(self): self.mocker.add_recorder(spec_checker_recorder) obj = self.mocker.mock(spec=self.cls) |