def test_flex_should_not_explode_on_unicode_formatting(self): if sys.version_info >= (3, 0): formatted = _format_args( 'method', {'kargs' : (chr(0x86C7),), 'kwargs' : {}}) assertEqual('method("蛇")', formatted) else: formatted = _format_args( 'method', {'kargs' : (unichr(0x86C7),), 'kwargs' : {}}) assertEqual('method("%s")' % unichr(0x86C7), formatted)
def _verify_call_order(self, flex_objects): for exp in flex_objects[self.mock]: if (exp.method == self.method and not _match_args(self.args, exp.args) and not exp._times_called): raise CallOrderError( '%s called before %s' % (_format_args(self.method, self.args), _format_args(exp.method, exp.args))) if (exp.method == self.method and self.args and exp.args and # ignore default stub case _match_args(self.args, exp.args)): break
def _verify(self, final=True): """Verify that this expectation has been met. Args: final: boolean, True if no further calls to this method expected (skip checking at_least expectations when False) Raises: CallOrderError """ if self._verified: return # ensure we only raise one error per failed expectation failed = False message = '' expected_calls = self._expected_calls if expected_calls[EXACTLY] is not None: message = 'exactly %s' % expected_calls[EXACTLY] if final: if self._times_called != expected_calls[EXACTLY]: failed = True else: if self._times_called > expected_calls[EXACTLY]: failed = True else: if final and expected_calls[AT_LEAST] is not None: message = 'at least %s' % expected_calls[AT_LEAST] if self._times_called < expected_calls[AT_LEAST]: failed = True if expected_calls[AT_MOST] is not None: if message: message += ' and ' message += 'at most %s' % expected_calls[AT_MOST] if self._times_called > expected_calls[AT_MOST]: failed = True if not failed: return else: self._verified = True self.__raise( MethodCallError, '%s expected to be called %s times, called %s times' % (_format_args(self.method, self.args), message, self._times_called))
def mock_method(runtime_self, *kargs, **kwargs): arguments = {'kargs': kargs, 'kwargs': kwargs} expectation = self.__get_expectation(method, arguments) if expectation: if not expectation._runnable(): raise StateError( '%s expected to be called when %s is True' % (method, _get_runnable_name(expectation._runnable))) expectation._times_called += 1 expectation._verify(final=False) _pass_thru = expectation._pass_thru _replace_with = expectation._replace_with if _pass_thru: return pass_thru(expectation, *kargs, **kwargs) elif _replace_with: return _replace_with(*kargs, **kwargs) yield_values = expectation._action['yield_values'] return_values = expectation._action['return_values'] if yield_values: return generator_method(yield_values) elif return_values: return_value = return_values[0] del return_values[0] return_values.append(return_value) else: return_value = ReturnValue() if return_value.raises: if _isclass(return_value.raises): raise return_value.raises( *return_value.value['kargs'], **return_value.value['kwargs']) else: raise return_value.raises else: return return_value.value else: # make sure to clean up expectations to ensure none of them # interfere with the runner's error reporing mechanism # e.g. open() for mock_object, expectations in _flex_objects.items(): for expectation in expectations: expectation._reset() raise MethodSignatureError(_format_args(method, arguments))
def __str__(self): return ('%s -> (%s)' % (_format_args(self.method, self.args), ', '.join(['%s' % x for x in self._action['return_values']])))