|
3 | 3 |
|
4 | 4 | import pytest |
5 | 5 |
|
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 |
7 | 7 |
|
8 | 8 |
|
9 | 9 | class TestCallableManager: |
@@ -278,3 +278,49 @@ def test_ignore_errors_lambda_function(self, caplog): |
278 | 278 | assert result2 == "lambda_failed" |
279 | 279 | # Lambda functions have a generic name |
280 | 280 | 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