OLD | NEW |
1 # Copyright 2015 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import sys | 5 import sys |
6 import unittest | 6 import unittest |
7 | 7 |
8 from testing_support import auto_stub | 8 from testing_support import auto_stub |
9 | 9 |
10 | 10 |
11 class MockedObject(auto_stub.SimpleMock): | 11 class MockedObject(auto_stub.SimpleMock): |
12 def method1(self, *args, **kwargs): | 12 def method1(self, *args, **kwargs): |
13 self._register_call(*args, **kwargs) | 13 self._register_call(*args, **kwargs) |
14 | 14 |
15 | 15 |
16 class TestSimpleMock(unittest.TestCase): | 16 class TestSimpleMock(unittest.TestCase): |
17 def test_auto_mock(self): | 17 def test_auto_mock(self): |
18 obj = MockedObject(self) | 18 obj = MockedObject(self) |
19 obj.method1(1, param=2) | 19 obj.method1(1, param=2) |
20 obj.check_calls(['method1(1, param=2)']) | 20 obj.check_calls(['method1(1, param=2)']) |
21 | 21 |
| 22 def test_auto_mock_sorted(self): |
| 23 obj = MockedObject(self) |
| 24 obj.method1(1, c=4, a=2, b=3) |
| 25 # tandrii@ is very surprised this is actually deterministic. |
| 26 obj.check_calls(['method1(1, a=2, c=4, b=3)']) |
| 27 # The proper and sane way is to always sort them. |
| 28 obj = MockedObject(self, sorted_kwargs=True) |
| 29 obj.method1(1, c=4, a=2, b=3) |
| 30 obj.check_calls(['method1(1, a=2, b=3, c=4)']) |
| 31 |
| 32 |
22 def return_one(): | 33 def return_one(): |
23 return 1 | 34 return 1 |
24 | 35 |
25 def return_two(): | 36 def return_two(): |
26 return 2 | 37 return 2 |
27 | 38 |
28 class TestAutoStubTestCase(auto_stub.TestCase): | 39 class TestAutoStubTestCase(auto_stub.TestCase): |
29 def test_mock_method(self): | 40 def test_mock_method(self): |
30 # mock one function | 41 # mock one function |
31 self.assertEqual(return_one(), 1) | 42 self.assertEqual(return_one(), 1) |
32 self.mock(sys.modules[__name__], 'return_one', lambda: 'mocked') | 43 self.mock(sys.modules[__name__], 'return_one', lambda: 'mocked') |
33 self.assertEqual(return_one(), 'mocked') | 44 self.assertEqual(return_one(), 'mocked') |
34 auto_stub.TestCase.tearDown(self) | 45 auto_stub.TestCase.tearDown(self) |
35 self.assertEqual(return_one(), 1) | 46 self.assertEqual(return_one(), 1) |
36 | 47 |
37 # mock two functions | 48 # mock two functions |
38 self.assertEqual(return_one(), 1) | 49 self.assertEqual(return_one(), 1) |
39 self.assertEqual(return_two(), 2) | 50 self.assertEqual(return_two(), 2) |
40 self.mock(sys.modules[__name__], 'return_one', lambda: 'mocked1') | 51 self.mock(sys.modules[__name__], 'return_one', lambda: 'mocked1') |
41 self.mock(sys.modules[__name__], 'return_two', lambda: 'mocked2') | 52 self.mock(sys.modules[__name__], 'return_two', lambda: 'mocked2') |
42 self.assertEqual(return_one(), 'mocked1') | 53 self.assertEqual(return_one(), 'mocked1') |
43 self.assertEqual(return_two(), 'mocked2') | 54 self.assertEqual(return_two(), 'mocked2') |
44 auto_stub.TestCase.tearDown(self) | 55 auto_stub.TestCase.tearDown(self) |
45 self.assertEqual(return_one(), 1) | 56 self.assertEqual(return_one(), 1) |
46 self.assertEqual(return_two(), 2) | 57 self.assertEqual(return_two(), 2) |
OLD | NEW |