|
4 | 4 |
|
5 | 5 | from pywisetransfer.deprecation import deprecated
|
6 | 6 |
|
7 |
| -record_warnings = lambda: catch_warnings(record=True) |
8 | 7 |
|
| 8 | +def record_warnings(): |
| 9 | + return catch_warnings(record=True) |
9 | 10 |
|
10 |
| -def undecorated(): |
11 |
| - return 1 |
| 11 | + |
| 12 | +def undecorated(n): |
| 13 | + return n |
12 | 14 |
|
13 | 15 |
|
14 | 16 | @deprecated
|
15 |
| -def bare_decorator(): |
16 |
| - return 1 |
| 17 | +def bare_decorator(n): |
| 18 | + return n + 1 |
17 | 19 |
|
18 | 20 |
|
19 | 21 | @deprecated()
|
20 |
| -def zero_args_decorator(): |
21 |
| - return 1 |
| 22 | +def zero_args_decorator(n): |
| 23 | + return n + 2 |
22 | 24 |
|
23 | 25 |
|
24 | 26 | @deprecated("positional")
|
25 |
| -def posarg_decorator(): |
26 |
| - return 1 |
| 27 | +def posarg_decorator(n): |
| 28 | + return n + 3 |
27 | 29 |
|
28 | 30 |
|
29 | 31 | @deprecated(message="keyword")
|
30 |
| -def kwarg_decorator(): |
31 |
| - return 1 |
| 32 | +def kwarg_decorator(n): |
| 33 | + return n + 4 |
| 34 | + |
| 35 | + |
| 36 | +class Class: |
| 37 | + base = 2 |
| 38 | + |
| 39 | + @deprecated(message="method") |
| 40 | + def method_decorator(self, n): |
| 41 | + return self.base + n + 3 |
32 | 42 |
|
33 | 43 |
|
34 | 44 | @pytest.mark.parametrize(
|
35 |
| - "func, name, deprecated, message", |
| 45 | + "func, name, deprecated, result, message", |
36 | 46 | [
|
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"), |
| 47 | + (undecorated, "undecorated", False, 0, None), |
| 48 | + (bare_decorator, "bare_decorator", True, 1, None), |
| 49 | + (zero_args_decorator, "zero_args_decorator", True, 2, None), |
| 50 | + (posarg_decorator, "posarg_decorator", True, 3, "positional"), |
| 51 | + (kwarg_decorator, "kwarg_decorator", True, 4, "keyword"), |
| 52 | + (Class().method_decorator, "method_decorator", True, 5, "method"), |
42 | 53 | ],
|
43 | 54 | )
|
44 |
| -def test_no_decorator(func, name, deprecated, message): |
| 55 | +def test_decorator_variants(func, name, deprecated, result, message): |
45 | 56 | actual_repr = repr(func)
|
46 | 57 | with record_warnings() as ws:
|
47 |
| - result = func() |
| 58 | + actual_result = func(0) |
48 | 59 |
|
49 | 60 | # Check the Python repr of the function
|
50 | 61 | assert name in actual_repr
|
51 |
| - actual_deprecated = "deprecated" in actual_repr |
| 62 | + actual_deprecated = "deprecated." in actual_repr |
52 | 63 | assert deprecated == actual_deprecated
|
53 | 64 |
|
54 | 65 | # Check the behaviour of the function
|
55 |
| - assert result == 1 |
| 66 | + assert result == actual_result |
56 | 67 |
|
57 | 68 | # Check the warnings emitted by the function
|
58 |
| - assert ws if deprecated else not ws |
| 69 | + assert len(ws) == 1 if deprecated else not ws |
59 | 70 | assert any([message in str(w.message) for w in ws] if message else [True])
|
| 71 | + |
| 72 | + |
| 73 | +def test_standalone_decorator_repr(): |
| 74 | + decorator = deprecated(message="standalone") |
| 75 | + assert repr(decorator) == "<deprecation decorator ('standalone')>" |
0 commit comments