MCPcopy Index your code
hub / github.com/nodejs/node / SimpleMock

Class SimpleMock

deps/v8/tools/release/test_scripts.py:130–192  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

128
129
130class SimpleMock(object):
131 def __init__(self):
132 self._recipe = []
133 self._index = -1
134
135 def Expect(self, recipe):
136 self._recipe = recipe
137
138 def Call(self, name, *args, **kwargs): # pragma: no cover
139 self._index += 1
140
141 try:
142 expected_call = self._recipe[self._index]
143 except IndexError:
144 raise NoRetryException("Calling %s %s" % (name, " ".join(args)))
145
146 if not isinstance(expected_call, dict):
147 raise NoRetryException("Found wrong expectation type for %s %s" %
148 (name, " ".join(args)))
149
150 if expected_call["name"] != name:
151 raise NoRetryException("Expected action: %s %s - Actual: %s" %
152 (expected_call["name"], expected_call["args"], name))
153
154 # Check if the given working directory matches the expected one.
155 if expected_call["cwd"] != kwargs.get("cwd"):
156 raise NoRetryException("Expected cwd: %s in %s %s - Actual: %s" %
157 (expected_call["cwd"],
158 expected_call["name"],
159 expected_call["args"],
160 kwargs.get("cwd")))
161
162 # The number of arguments in the expectation must match the actual
163 # arguments.
164 if len(args) > len(expected_call['args']):
165 raise NoRetryException("When calling %s with arguments, the "
166 "expectations must consist of at least as many arguments." %
167 name)
168
169 # Compare expected and actual arguments.
170 for (expected_arg, actual_arg) in zip(expected_call['args'], args):
171 if expected_arg != actual_arg:
172 raise NoRetryException("Expected: %s - Actual: %s" %
173 (expected_arg, actual_arg))
174
175 # The expected call contains an optional callback for checking the context
176 # at the time of the call.
177 if expected_call['cb']:
178 try:
179 expected_call['cb']()
180 except:
181 tb = traceback.format_exc()
182 raise NoRetryException("Caught exception from callback: %s" % tb)
183
184 # If the return value is an exception, raise it instead of returning.
185 if isinstance(expected_call['ret'], Exception):
186 raise expected_call['ret']
187 return expected_call['ret']

Callers 1

setUpMethod · 0.85

Calls

no outgoing calls

Tested by 1

setUpMethod · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…