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): | 22 def test_auto_mock_sorted(self): |
23 obj = MockedObject(self) | 23 obj = MockedObject(self) |
| 24 # Regardless of order here, ... |
24 obj.method1(1, c=4, a=2, b=3) | 25 obj.method1(1, c=4, a=2, b=3) |
25 # tandrii@ is very surprised this is actually deterministic. | 26 obj.method1(1, a=2, c=4, b=3) |
26 obj.check_calls(['method1(1, a=2, c=4, b=3)']) | 27 # ... order here is sorted and hence totally deterministic. |
27 # The proper and sane way is to always sort them. | 28 obj.check_calls([ |
28 obj = MockedObject(self, sorted_kwargs=True) | 29 'method1(1, a=2, b=3, c=4)', |
29 obj.method1(1, c=4, a=2, b=3) | 30 auto_stub.format_call('method1', 1, b=3, c=4, a=2), |
30 obj.check_calls(['method1(1, a=2, b=3, c=4)']) | 31 ]) |
31 | 32 |
32 | 33 |
33 def return_one(): | 34 def return_one(): |
34 return 1 | 35 return 1 |
35 | 36 |
36 def return_two(): | 37 def return_two(): |
37 return 2 | 38 return 2 |
38 | 39 |
39 class TestAutoStubTestCase(auto_stub.TestCase): | 40 class TestAutoStubTestCase(auto_stub.TestCase): |
40 def test_mock_method(self): | 41 def test_mock_method(self): |
41 # mock one function | 42 # mock one function |
42 self.assertEqual(return_one(), 1) | 43 self.assertEqual(return_one(), 1) |
43 self.mock(sys.modules[__name__], 'return_one', lambda: 'mocked') | 44 self.mock(sys.modules[__name__], 'return_one', lambda: 'mocked') |
44 self.assertEqual(return_one(), 'mocked') | 45 self.assertEqual(return_one(), 'mocked') |
45 auto_stub.TestCase.tearDown(self) | 46 auto_stub.TestCase.tearDown(self) |
46 self.assertEqual(return_one(), 1) | 47 self.assertEqual(return_one(), 1) |
47 | 48 |
48 # mock two functions | 49 # mock two functions |
49 self.assertEqual(return_one(), 1) | 50 self.assertEqual(return_one(), 1) |
50 self.assertEqual(return_two(), 2) | 51 self.assertEqual(return_two(), 2) |
51 self.mock(sys.modules[__name__], 'return_one', lambda: 'mocked1') | 52 self.mock(sys.modules[__name__], 'return_one', lambda: 'mocked1') |
52 self.mock(sys.modules[__name__], 'return_two', lambda: 'mocked2') | 53 self.mock(sys.modules[__name__], 'return_two', lambda: 'mocked2') |
53 self.assertEqual(return_one(), 'mocked1') | 54 self.assertEqual(return_one(), 'mocked1') |
54 self.assertEqual(return_two(), 'mocked2') | 55 self.assertEqual(return_two(), 'mocked2') |
55 auto_stub.TestCase.tearDown(self) | 56 auto_stub.TestCase.tearDown(self) |
56 self.assertEqual(return_one(), 1) | 57 self.assertEqual(return_one(), 1) |
57 self.assertEqual(return_two(), 2) | 58 self.assertEqual(return_two(), 2) |
OLD | NEW |