|
| 1 | +from warnings import catch_warnings |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from pywisetransfer.deprecation import deprecated |
| 6 | + |
| 7 | +record_warnings = lambda: catch_warnings(record=True) |
| 8 | + |
| 9 | + |
| 10 | +def undecorated(): |
| 11 | + return 1 |
| 12 | + |
| 13 | + |
| 14 | +@deprecated |
| 15 | +def bare_decorator(): |
| 16 | + return 1 |
| 17 | + |
| 18 | + |
| 19 | +@deprecated() |
| 20 | +def zero_args_decorator(): |
| 21 | + return 1 |
| 22 | + |
| 23 | + |
| 24 | +@deprecated("positional") |
| 25 | +def posarg_decorator(): |
| 26 | + return 1 |
| 27 | + |
| 28 | + |
| 29 | +@deprecated(message="keyword") |
| 30 | +def kwarg_decorator(): |
| 31 | + return 1 |
| 32 | + |
| 33 | + |
| 34 | +@pytest.mark.parametrize( |
| 35 | + "func, name, deprecated, message", |
| 36 | + [ |
| 37 | + (undecorated, "undecorated", False, None), |
| 38 | + (bare_decorator, "bare_decorator", True, None), |
| 39 | + (zero_args_decorator, "zero_args_decorator", True, None), |
| 40 | + (posarg_decorator, "posarg_decorator", True, "positional"), |
| 41 | + (kwarg_decorator, "kwarg_decorator", True, "keyword"), |
| 42 | + ], |
| 43 | +) |
| 44 | +def test_no_decorator(func, name, deprecated, message): |
| 45 | + actual_repr = repr(func) |
| 46 | + with record_warnings() as ws: |
| 47 | + result = func() |
| 48 | + |
| 49 | + # Check the Python repr of the function |
| 50 | + assert name in actual_repr |
| 51 | + actual_deprecated = "deprecated" in actual_repr |
| 52 | + assert deprecated == actual_deprecated |
| 53 | + |
| 54 | + # Check the behaviour of the function |
| 55 | + assert result == 1 |
| 56 | + |
| 57 | + # Check the warnings emitted by the function |
| 58 | + assert ws if deprecated else not ws |
| 59 | + assert any([message in str(w.message) for w in ws] if message else [True]) |
0 commit comments