3
3
import unittest
4
4
import warnings
5
5
6
- from can .util import _create_bus_config , _rename_kwargs , channel2int
6
+ import pytest
7
+
8
+ from can .util import (
9
+ _create_bus_config ,
10
+ _rename_kwargs ,
11
+ channel2int ,
12
+ deprecated_args_alias ,
13
+ )
7
14
8
15
9
16
class RenameKwargsTest (unittest .TestCase ):
10
17
expected_kwargs = dict (a = 1 , b = 2 , c = 3 , d = 4 )
11
18
12
- def _test (self , kwargs , aliases ):
19
+ def _test (self , start : str , end : str , kwargs , aliases ):
13
20
14
21
# Test that we do get the DeprecationWarning when called with deprecated kwargs
15
- with self .assertWarnsRegex (DeprecationWarning , "is deprecated" ):
16
- _rename_kwargs ("unit_test" , kwargs , aliases )
22
+ with self .assertWarnsRegex (
23
+ DeprecationWarning , "is deprecated.*?" + start + ".*?" + end
24
+ ):
25
+ _rename_kwargs ("unit_test" , start , end , kwargs , aliases )
17
26
18
27
# Test that the aliases contains the deprecated values and
19
28
# the obsolete kwargs have been removed
@@ -25,30 +34,98 @@ def _test(self, kwargs, aliases):
25
34
# Cause all warnings to always be triggered.
26
35
warnings .simplefilter ("error" , DeprecationWarning )
27
36
try :
28
- _rename_kwargs ("unit_test" , kwargs , aliases )
37
+ _rename_kwargs ("unit_test" , start , end , kwargs , aliases )
29
38
finally :
30
39
warnings .resetwarnings ()
31
40
32
41
def test_rename (self ):
33
42
kwargs = dict (old_a = 1 , old_b = 2 , c = 3 , d = 4 )
34
43
aliases = {"old_a" : "a" , "old_b" : "b" }
35
- self ._test (kwargs , aliases )
44
+ self ._test ("1.0" , "2.0" , kwargs , aliases )
36
45
37
46
def test_obsolete (self ):
38
47
kwargs = dict (a = 1 , b = 2 , c = 3 , d = 4 , z = 10 )
39
48
aliases = {"z" : None }
40
- self ._test (kwargs , aliases )
49
+ self ._test ("1.0" , "2.0" , kwargs , aliases )
41
50
42
51
def test_rename_and_obsolete (self ):
43
52
kwargs = dict (old_a = 1 , old_b = 2 , c = 3 , d = 4 , z = 10 )
44
53
aliases = {"old_a" : "a" , "old_b" : "b" , "z" : None }
45
- self ._test (kwargs , aliases )
54
+ self ._test ("1.0" , "2.0" , kwargs , aliases )
46
55
47
56
def test_with_new_and_alias_present (self ):
48
57
kwargs = dict (old_a = 1 , a = 1 , b = 2 , c = 3 , d = 4 , z = 10 )
49
58
aliases = {"old_a" : "a" , "old_b" : "b" , "z" : None }
50
59
with self .assertRaises (TypeError ):
51
- self ._test (kwargs , aliases )
60
+ self ._test ("1.0" , "2.0" , kwargs , aliases )
61
+
62
+
63
+ class DeprecatedArgsAliasTest (unittest .TestCase ):
64
+ def test_decorator (self ):
65
+ @deprecated_args_alias ("1.0.0" , old_a = "a" )
66
+ def _test_func1 (a ):
67
+ pass
68
+
69
+ with pytest .warns (DeprecationWarning ) as record :
70
+ _test_func1 (old_a = 1 )
71
+ assert len (record ) == 1
72
+ assert (
73
+ record [0 ].message .args [0 ]
74
+ == "The 'old_a' argument is deprecated since python-can v1.0.0. Use 'a' instead."
75
+ )
76
+
77
+ @deprecated_args_alias ("1.6.0" , "3.4.0" , old_a = "a" , old_b = None )
78
+ def _test_func2 (a ):
79
+ pass
80
+
81
+ with pytest .warns (DeprecationWarning ) as record :
82
+ _test_func2 (old_a = 1 , old_b = 2 )
83
+ assert len (record ) == 2
84
+ assert record [0 ].message .args [0 ] == (
85
+ "The 'old_a' argument is deprecated since python-can v1.6.0, and scheduled for "
86
+ "removal in python-can v3.4.0. Use 'a' instead."
87
+ )
88
+ assert record [1 ].message .args [0 ] == (
89
+ "The 'old_b' argument is deprecated since python-can v1.6.0, and scheduled for "
90
+ "removal in python-can v3.4.0."
91
+ )
92
+
93
+ @deprecated_args_alias ("1.6.0" , "3.4.0" , old_a = "a" )
94
+ @deprecated_args_alias ("2.0.0" , "4.0.0" , old_b = None )
95
+ def _test_func3 (a ):
96
+ pass
97
+
98
+ with pytest .warns (DeprecationWarning ) as record :
99
+ _test_func3 (old_a = 1 , old_b = 2 )
100
+ assert len (record ) == 2
101
+ assert record [0 ].message .args [0 ] == (
102
+ "The 'old_a' argument is deprecated since python-can v1.6.0, and scheduled "
103
+ "for removal in python-can v3.4.0. Use 'a' instead."
104
+ )
105
+ assert record [1 ].message .args [0 ] == (
106
+ "The 'old_b' argument is deprecated since python-can v2.0.0, and scheduled "
107
+ "for removal in python-can v4.0.0."
108
+ )
109
+
110
+ with pytest .warns (DeprecationWarning ) as record :
111
+ _test_func3 (old_a = 1 )
112
+ assert len (record ) == 1
113
+ assert record [0 ].message .args [0 ] == (
114
+ "The 'old_a' argument is deprecated since python-can v1.6.0, and scheduled "
115
+ "for removal in python-can v3.4.0. Use 'a' instead."
116
+ )
117
+
118
+ with pytest .warns (DeprecationWarning ) as record :
119
+ _test_func3 (a = 1 , old_b = 2 )
120
+ assert len (record ) == 1
121
+ assert record [0 ].message .args [0 ] == (
122
+ "The 'old_b' argument is deprecated since python-can v2.0.0, and scheduled "
123
+ "for removal in python-can v4.0.0."
124
+ )
125
+
126
+ with warnings .catch_warnings ():
127
+ warnings .simplefilter ("error" )
128
+ _test_func3 (a = 1 )
52
129
53
130
54
131
class TestBusConfig (unittest .TestCase ):
0 commit comments