Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(230)

Side by Side Diff: build/android/pylib/utils/mock_calls_test.py

Issue 723343002: Update from https://crrev.com/304121 (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « build/android/pylib/utils/mock_calls.py ('k') | build/android/test_runner.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #!/usr/bin/env python
2 # Copyright 2014 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
5
6 """
7 Unit tests for the contents of mock_calls.py.
8 """
9
10 import logging
11 import os
12 import sys
13 import unittest
14
15 from pylib import constants
16 from pylib.utils import mock_calls
17
18 sys.path.append(os.path.join(
19 constants.DIR_SOURCE_ROOT, 'third_party', 'pymock'))
20 import mock # pylint: disable=F0401
21
22
23 class _DummyAdb(object):
24 def __str__(self):
25 return '0123456789abcdef'
26
27 def Push(self, host_path, device_path):
28 logging.debug('(device %s) pushing %r to %r', self, host_path, device_path)
29
30 def IsOnline(self):
31 logging.debug('(device %s) checking device online', self)
32 return True
33
34 def Shell(self, cmd):
35 logging.debug('(device %s) running command %r', self, cmd)
36 return "nice output\n"
37
38 def Reboot(self):
39 logging.debug('(device %s) rebooted!', self)
40
41
42 class TestCaseWithAssertCallsTest(mock_calls.TestCase):
43 def setUp(self):
44 self.adb = _DummyAdb()
45
46 def ShellError(self):
47 def action(cmd):
48 raise ValueError('(device %s) command %r is not nice' % (self.adb, cmd))
49 return action
50
51 def get_answer(self):
52 logging.debug("called 'get_answer' of %r object", self)
53 return 42
54
55 def echo(self, thing):
56 logging.debug("called 'echo' of %r object", self)
57 return thing
58
59 def testCallTarget_succeds(self):
60 self.assertEquals(self.adb.Shell,
61 self.call_target(self.call.adb.Shell))
62
63 def testCallTarget_failsExternal(self):
64 with self.assertRaises(ValueError):
65 self.call_target(mock.call.sys.getcwd)
66
67 def testCallTarget_failsUnknownAttribute(self):
68 with self.assertRaises(AttributeError):
69 self.call_target(self.call.adb.Run)
70
71 def testCallTarget_failsIntermediateCalls(self):
72 with self.assertRaises(AttributeError):
73 self.call_target(self.call.adb.RunShell('cmd').append)
74
75 def testPatchCall_method(self):
76 self.assertEquals(42, self.get_answer())
77 with self.patch_call(self.call.get_answer, return_value=123):
78 self.assertEquals(123, self.get_answer())
79 self.assertEquals(42, self.get_answer())
80
81 def testPatchCall_attribute_method(self):
82 with self.patch_call(self.call.adb.Shell, return_value='hello'):
83 self.assertEquals('hello', self.adb.Shell('echo hello'))
84
85 def testPatchCall_global(self):
86 with self.patch_call(mock.call.os.getcwd, return_value='/some/path'):
87 self.assertEquals('/some/path', os.getcwd())
88
89 def testPatchCall_withSideEffect(self):
90 with self.patch_call(self.call.adb.Shell, side_effect=ValueError):
91 with self.assertRaises(ValueError):
92 self.adb.Shell('echo hello')
93
94 def testAssertCalls_succeeds_simple(self):
95 self.assertEquals(42, self.get_answer())
96 with self.assertCall(self.call.get_answer(), 123):
97 self.assertEquals(123, self.get_answer())
98 self.assertEquals(42, self.get_answer())
99
100 def testAssertCalls_succeeds_multiple(self):
101 with self.assertCalls(
102 (mock.call.os.getcwd(), '/some/path'),
103 (self.call.echo('hello'), 'hello'),
104 (self.call.get_answer(), 11),
105 self.call.adb.Push('this_file', 'that_file'),
106 (self.call.get_answer(), 12)):
107 self.assertEquals(os.getcwd(), '/some/path')
108 self.assertEquals('hello', self.echo('hello'))
109 self.assertEquals(11, self.get_answer())
110 self.adb.Push('this_file', 'that_file')
111 self.assertEquals(12, self.get_answer())
112
113 def testAsserCalls_succeeds_withAction(self):
114 with self.assertCall(
115 self.call.adb.Shell('echo hello'), self.ShellError()):
116 with self.assertRaises(ValueError):
117 self.adb.Shell('echo hello')
118
119 def testAssertCalls_fails_tooManyCalls(self):
120 with self.assertRaises(AssertionError):
121 with self.assertCalls(self.call.adb.IsOnline()):
122 self.adb.IsOnline()
123 self.adb.IsOnline()
124
125 def testAssertCalls_fails_tooFewCalls(self):
126 with self.assertRaises(AssertionError):
127 with self.assertCalls(self.call.adb.IsOnline()):
128 pass
129
130 def testAssertCalls_succeeds_extraCalls(self):
131 # we are not watching Reboot, so the assertion succeeds
132 with self.assertCalls(self.call.adb.IsOnline()):
133 self.adb.IsOnline()
134 self.adb.Reboot()
135
136 def testAssertCalls_fails_extraCalls(self):
137 self.watchCalls([self.call.adb.Reboot])
138 # this time we are also watching Reboot, so the assertion fails
139 with self.assertRaises(AssertionError):
140 with self.assertCalls(self.call.adb.IsOnline()):
141 self.adb.IsOnline()
142 self.adb.Reboot()
143
144 def testAssertCalls_succeeds_NoCalls(self):
145 self.watchMethodCalls(self.call.adb) # we are watching all adb methods
146 with self.assertCalls():
147 pass
148
149 def testAssertCalls_fails_NoCalls(self):
150 self.watchMethodCalls(self.call.adb)
151 with self.assertRaises(AssertionError):
152 with self.assertCalls():
153 self.adb.IsOnline()
154
155
156 if __name__ == '__main__':
157 logging.getLogger().setLevel(logging.DEBUG)
158 unittest.main(verbosity=2)
159
OLDNEW
« no previous file with comments | « build/android/pylib/utils/mock_calls.py ('k') | build/android/test_runner.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698