diff options
Diffstat (limited to 'test.py')
-rwxr-xr-x | test.py | 104 |
1 files changed, 102 insertions, 2 deletions
@@ -25,7 +25,7 @@ from mocker import \ mock_returner_recorder, FunctionRunner, Orderer, SpecChecker, \ spec_checker_recorder, match_params, ANY, IS, CONTAINS, IN, MATCH, ARGS, \ KWARGS, MatchError, PathExecuter, ProxyReplacer, Patcher, Undefined, \ - PatchedMethod, MockerTestCase, ReplayRestoreEvent, OnRestoreCaller + PatchedMethod, MockerTestCase, ReplayRestoreEvent, OnRestoreCaller, Expect class TestCase(unittest.TestCase): @@ -210,6 +210,13 @@ class IntegrationTest(TestCase): self.assertTrue(os.path.isfile("unexistent")) self.assertFalse(os.path.isfile("unexistent")) + def test_replace_class_method(self): + empty = self.mocker.replace("Queue.Queue.empty") + expect(empty()).result(False) + self.mocker.replay() + from Queue import Queue + self.assertEquals(Queue().empty(), False) + def test_patch_with_spec(self): class C(object): def method(self, a, b): @@ -272,6 +279,13 @@ class ExpectTest(TestCase): self.mocker.replay() self.assertEquals(obj.attr, 42) + def test_explicit_expect_instance(self): + obj = self.mocker.mock() + myexpect = Expect(self.mocker) + myexpect(iter(obj)).generate([1, 2, 3]).count(1, 2) + self.mocker.replay() + self.assertEquals(list(obj), [1, 2, 3]) + class MockerTestCaseTest(TestCase): @@ -287,7 +301,12 @@ class MockerTestCaseTest(TestCase): self.assertEquals(type(self.test.mocker), Mocker) def test_has_expect(self): - self.assertTrue(self.test.expect is expect) + self.assertTrue(issubclass(self.test.expect, expect)) + + def test_expect_works_with_non_mocks(self): + # We must be using the Expect helper for this to work at all: + obj = self.test.mocker.mock() + self.test.expect(iter(obj)).generate([1,2,3]) def test_attributes_are_the_same(self): class MyTest(MockerTestCase): @@ -431,6 +450,44 @@ class MockerTestCaseTest(TestCase): self.assertEquals(stash, [[], (1, 2), (3, 4)]) + def test_cleanup_wrapper_in__call__for_2_3(self): + version_info = sys.version_info + __call__ = unittest.TestCase.__call__ + try: + sys.version_info = (2, 3, 5) + stash = [] + def call(self, *args, **kwargs): + self.addCleanup(lambda: stash.append(True)) + unittest.TestCase.__call__ = call + class MyTest(MockerTestCase): + def test_method(self): + pass + MyTest("test_method")() + self.assertEquals(stash, [True]) + finally: + unittest.TestCase.__call__ = __call__ + sys.version_info = version_info + + def test_cleanup_wrapper_in__call__for_2_4(self): + version_info = sys.version_info + __call__ = unittest.TestCase.__call__ + try: + sys.version_info = (2, 4) + stash = [] + def call(self, *args, **kwargs): + self.addCleanup(lambda: stash.append(True)) + unittest.TestCase.__call__ = call + class MyTest(MockerTestCase): + def test_method(self): + pass + MyTest("test_method")() + # Python 2.4+ handles cleanup in run(), registered inside + # MockerTestCase.__init__, so this should *not* work. + self.assertEquals(stash, []) + finally: + unittest.TestCase.__call__ = __call__ + sys.version_info = version_info + def test_twisted_trial_deferred_support(self): calls = [] callbacks = [] @@ -1373,6 +1430,11 @@ class MockerTest(TestCase): mock = self.mocker.replace(original, passthrough=False) self.assertEquals(mock.__mocker_passthrough__, False) + def test_replace_with_bound_method(self): + from Queue import Queue + mock = self.mocker.replace(Queue.empty) + self.assertEquals(mock.__mocker_object__, Queue.empty.im_func) + def test_add_and_get_event(self): self.mocker.add_event(41) self.assertEquals(self.mocker.add_event(42), 42) @@ -2561,6 +2623,25 @@ class MockTest(TestCase): else: self.fail("AttributeError not raised.") + def test_mock_raises_attribute_error_on_length_hint(self): + """ + In Python 2.6+ list() uses __length_hint__() as a hint. When + we mock iter(), it shouldn't explode due to the lack of + __length_hint__. + """ + def raise_error(path): + raise MatchError("Kaboom!") + + self.mocker.act = raise_error + try: + self.mock.__length_hint__ + except AttributeError, e: + self.assertEquals(str(e), "No __length_hint__ here!") + except MatchError: + self.fail("Expected AttributeError, not MatchError.") + else: + self.fail("AttributeError not raised.") + def test_nonzero(self): self.assertEquals(bool(self.mock), True) # True due to 42. (path,) = self.paths @@ -2580,6 +2661,10 @@ class MockTest(TestCase): self.mocker.act = raise_error self.assertEquals(bool(self.mock), True) + def test_nonzero_with_mock_result(self): + self.mocker.act = lambda path: Mock(self.mocker) + self.assertEquals(bool(self.mock), True) + def test_iter(self): result_mock = Mock(self.mocker) self.mocker.act = lambda path: self.paths.append(path) or result_mock @@ -4020,6 +4105,21 @@ class PatcherTest(TestCase): self.assertEquals(obj.method(), "original") self.assertRaises(AssertionError, obj.method) + def test_original_exception_raised(self): + class C(object): + def use_non_existing_attribute(self): + return self.bad_attribute + + mock = self.mocker.patch(C) + mock.any_other_method() + self.mocker.replay() + obj = C() + try: + obj.use_non_existing_attribute() + except AttributeError, error: + message = "'C' object has no attribute 'bad_attribute'" + self.assertEquals(message, str(error)) + def main(): try: |