def test_returns_sentinel_when_exception_is_raised(self): exception_class = IOError def failing(): raise exception_class() assert_equal(sentinel, exception_guard(failing, exception_class, sentinel)())
def test_does_not_catch_wider_exceptions(self): parent_exception = Exception child_exception = IOError assert_true(issubclass(child_exception, parent_exception)) def failing(): raise parent_exception() guarded_function = exception_guard(failing, child_exception) assert_raises(parent_exception, guarded_function)
def test_passes_arguments(self): assert_equal(sentinel, exception_guard(lambda x: x)(sentinel))
def test_calls_function_when_called(self): f = Mock() f.return_value = sentinel assert_equal(sentinel, exception_guard(f)()) f.assert_called_once_with()
def test_returns_a_wrapper(self): wrapper = exception_guard(lambda: None) assert_is_instance(wrapper, FunctionType)