diff --git a/exception_escaping/proxy_module.py b/exception_escaping/proxy_module.py index aadedb6..0dca7a6 100644 --- a/exception_escaping/proxy_module.py +++ b/exception_escaping/proxy_module.py @@ -30,4 +30,4 @@ async def async_wrapper(*args: Any, **kwargs: Any) -> Any: elif len(args) == 0: return wrapper_of_wrappers else: - raise ValueError() + raise ValueError('You are using the decorator for the wrong purpose.') diff --git a/tests/test_proxy_module.py b/tests/test_proxy_module.py index 7cdd736..3b7700d 100644 --- a/tests/test_proxy_module.py +++ b/tests/test_proxy_module.py @@ -1,5 +1,7 @@ import asyncio +import pytest + import exception_escaping @@ -59,3 +61,128 @@ async def function(a, b, c=5): raise ValueError asyncio.run(function(1, 2)) + + +def test_run_simple_function_with_empty_brackets(): + some_value = 'kek' + + @exception_escaping() + def function(): + return some_value + + assert function() == some_value + + +def test_run_simple_function_with_some_arguments_with_empty_brackets(): + @exception_escaping() + def function(a, b, c=5): + return a + b + c + + assert function(1, 2) == 8 + assert function(1, 2, 5) == 8 + assert function(1, 2, c=5) == 8 + assert function(1, 2, c=8) == 11 + + +def test_run_function_with_exception_with_empty_brackets(): + @exception_escaping() + def function(a, b, c=5): + raise ValueError + + function(1, 2) + + +def test_run_coroutine_function_with_empty_brackets(): + some_value = 'kek' + + @exception_escaping() + async def function(): + return some_value + + assert asyncio.run(function()) == some_value + + +def test_run_coroutine_function_with_some_arguments_with_empty_brackets(): + @exception_escaping() + async def function(a, b, c=5): + return a + b + c + + assert asyncio.run(function(1, 2)) == 8 + assert asyncio.run(function(1, 2, 5)) == 8 + assert asyncio.run(function(1, 2, c=5)) == 8 + assert asyncio.run(function(1, 2, c=8)) == 11 + + +def test_run_coroutine_function_with_exception_with_empty_brackets(): + @exception_escaping() + async def function(a, b, c=5): + raise ValueError + + asyncio.run(function(1, 2)) + + +def test_run_simple_function_with_default_return(): + some_value = 'kek' + + @exception_escaping(default_return='lol') + def function(): + return some_value + + assert function() == some_value + + +def test_run_simple_function_with_some_arguments_with_default_return(): + @exception_escaping(default_return='lol') + def function(a, b, c=5): + return a + b + c + + assert function(1, 2) == 8 + assert function(1, 2, 5) == 8 + assert function(1, 2, c=5) == 8 + assert function(1, 2, c=8) == 11 + + +def test_run_function_with_exception_with_default_return(): + default_value = 13 + + @exception_escaping(default_return=default_value) + def function(a, b, c=5): + raise ValueError + + assert function(1, 2) == default_value + + +def test_run_coroutine_function_with_default_return(): + some_value = 'kek' + + @exception_escaping(default_return='lol') + async def function(): + return some_value + + assert asyncio.run(function()) == some_value + + +def test_run_coroutine_function_with_some_arguments_with_default_return(): + @exception_escaping(default_return='lol') + async def function(a, b, c=5): + return a + b + c + + assert asyncio.run(function(1, 2)) == 8 + assert asyncio.run(function(1, 2, 5)) == 8 + assert asyncio.run(function(1, 2, c=5)) == 8 + assert asyncio.run(function(1, 2, c=8)) == 11 + + +def test_run_coroutine_function_with_exception_with_default_return(): + default_value = 13 + + @exception_escaping(default_return=default_value) + async def function(a, b, c=5): + raise ValueError + + assert asyncio.run(function(1, 2)) == default_value + + +def test_wrong_argument_to_decorator(): + with pytest.raises(ValueError, match='You are using the decorator for the wrong purpose.'): + exception_escaping('kek')