def check_dependencies(func): action = func.__name__ filename = _get_file_name(func) lineno = _get_line_number(func) for dependency in depends_on: if dependency in context.__sure_providers_of__: providers = context.__sure_providers_of__[dependency] err = dependency_error_hints % ( action, filename, lineno, dependency, ) err += '\n'.join([ ' -> %s at %s:%d' % (p.__name__, _get_file_name(p), _get_line_number(p)) for p in providers ]) else: err = dependency_error_lonely % ( action, filename, lineno, dependency, ) assert dependency in context, err
def check_dependencies(func): action = func.__name__ filename = _get_file_name(func) lineno = _get_line_number(func) for dependency in depends_on: if dependency in context.__sure_providers_of__: providers = context.__sure_providers_of__[dependency] err = dependency_error_hints % ( action, filename, lineno, dependency, ) err += '\n'.join([ ' -> %s at %s:%d' % ( p.__name__, _get_file_name(p), _get_line_number(p)) for p in providers]) else: err = dependency_error_lonely % ( action, filename, lineno, dependency, ) assert dependency in context, err
def raises(self, exc, msg=None): if not callable(self._src): raise TypeError('%r is not callable' % self._src) try: self._src(*self._callable_args, **self._callable_kw) except BaseException as e: if isinstance(exc, string_types): msg = exc exc = type(e) err = text_type(e) if isinstance(exc, type) and issubclass(exc, BaseException): if not isinstance(e, exc): raise AssertionError( '%r should raise %r, but raised %r' % ( self._src, exc, e.__class__)) if isinstance(msg, string_types) and msg not in err: raise AssertionError(''' %r raised %s, but the exception message does not match.\n\nEXPECTED:\n%s\n\nGOT:\n%s'''.strip() % ( self._src, type(e).__name__, msg, err)) elif isinstance(msg, string_types) and msg not in err: raise AssertionError( 'When calling %r the exception message does not match. ' \ 'Expected: %r\n got:\n %r' % (self._src, msg, err)) else: raise e else: if inspect.isbuiltin(self._src): _src_filename = '<built-in function>' else: _src_filename = _get_file_name(self._src) if inspect.isfunction(self._src): _src_lineno = _get_line_number(self._src) raise AssertionError( 'calling function %s(%s at line: "%d") with args %r and kwargs %r did not raise %r' % ( self._src.__name__, _src_filename, _src_lineno, self._callable_args, self._callable_kw, exc)) else: raise AssertionError( 'at %s:\ncalling %s() with args %r and kwargs %r did not raise %r' % ( _src_filename, self._src.__name__, self._callable_args, self._callable_kw, exc)) return True
def raises(self, exc, msg=None): if not callable(self._src): raise TypeError('%r is not callable' % self._src) try: self._src(*self._callable_args, **self._callable_kw) except BaseException as e: if isinstance(exc, string_types): msg = exc exc = type(e) err = text_type(e) if isinstance(exc, type) and issubclass(exc, BaseException): if not isinstance(e, exc): raise AssertionError( '%r should raise %r, but raised %r:\nORIGINAL EXCEPTION:\n\n%s' % ( self._src, exc, e.__class__, traceback.format_exc(e))) if isinstance(msg, string_types) and msg not in err: raise AssertionError(''' %r raised %s, but the exception message does not match.\n\nEXPECTED:\n%s\n\nGOT:\n%s'''.strip() % ( self._src, type(e).__name__, msg, err)) elif isinstance(msg, string_types) and msg not in err: raise AssertionError( 'When calling %r the exception message does not match. ' \ 'Expected: %r\n got:\n %r' % (self._src, msg, err)) else: raise e else: if inspect.isbuiltin(self._src): _src_filename = '<built-in function>' else: _src_filename = _get_file_name(self._src) if inspect.isfunction(self._src): _src_lineno = _get_line_number(self._src) raise AssertionError( 'calling function %s(%s at line: "%d") with args %r and kwargs %r did not raise %r' % ( self._src.__name__, _src_filename, _src_lineno, self._callable_args, self._callable_kw, exc)) else: raise AssertionError( 'at %s:\ncalling %s() with args %r and kwargs %r did not raise %r' % ( _src_filename, self._src.__name__, self._callable_args, self._callable_kw, exc)) return True
self._src, type(e).__name__, msg, err)) elif isinstance(msg, basestring) and msg not in err: raise AssertionError( 'When calling %r the exception message does not match. ' \ 'Expected: %r\n got:\n %r' % (self._src, msg, err)) else: raise e else: if inspect.isbuiltin(self._src): _src_filename = '<built-in function>' else: _src_filename = _get_file_name(self._src) if inspect.isfunction(self._src): _src_lineno = _get_line_number(self._src) raise AssertionError( 'calling function %s(%s at line: "%d") with args %r and kwargs %r did not raise %r' % ( self._src.__name__, _src_filename, _src_lineno, self._callable_args, self._callable_kw, exc)) else: raise AssertionError( 'at %s:\ncalling %s() with args %r and kwargs %r did not raise %r' % ( _src_filename, self._src.__name__, self._callable_args,