Skip to content

Commit 4c8f2a3

Browse files
committed
Add tests for run_if decorator
1 parent dc63fc8 commit 4c8f2a3

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

tests/launcher/test_callable_manager.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import pytest
55

6-
from clabe.launcher._callable_manager import Promise, _CallableManager, _UnsetType, ignore_errors
6+
from clabe.launcher._callable_manager import Promise, _CallableManager, _UnsetType, ignore_errors, run_if
77

88

99
class TestCallableManager:
@@ -278,3 +278,49 @@ def test_ignore_errors_lambda_function(self, caplog):
278278
assert result2 == "lambda_failed"
279279
# Lambda functions have a generic name
280280
assert "Exception in <lambda>: division by zero" in caplog.text
281+
282+
283+
class TestRunIfDecorator:
284+
def test_run_if_runs_when_predicate_true(self):
285+
def always_true(*args, **kwargs):
286+
return True
287+
288+
@run_if(always_true)
289+
def my_func(x):
290+
return x * 2
291+
292+
assert my_func(3) == 6
293+
294+
def test_run_if_returns_none_when_predicate_false(self):
295+
def always_false(*args, **kwargs):
296+
return False
297+
298+
@run_if(always_false)
299+
def my_func(x):
300+
return x * 2
301+
302+
assert my_func(3) is None
303+
304+
def test_run_if_predicate_depends_on_args(self):
305+
def is_positive(x):
306+
return x > 0
307+
308+
@run_if(is_positive)
309+
def square(x):
310+
return x * x
311+
312+
assert square(2) == 4
313+
assert square(-1) is None
314+
315+
def test_run_if_preserves_function_metadata(self):
316+
def always_true(*args, **kwargs):
317+
return True
318+
319+
@run_if(always_true)
320+
def documented_func(x):
321+
"""This function squares its input."""
322+
return x * x
323+
324+
assert hasattr(documented_func, "__name__")
325+
assert hasattr(documented_func, "__doc__")
326+
assert documented_func.__doc__ == "This function squares its input."

0 commit comments

Comments
 (0)