@@ -20,14 +20,26 @@ def setitem(obj, name, value):
20
20
obj [name ] = value
21
21
22
22
23
+ def setitem_hyphen (obj , name , value ):
24
+ obj [name .replace ("_" , "-" )] = value
25
+
26
+
23
27
def getitem (obj , name ):
24
28
return obj [name ]
25
29
26
30
31
+ def getitem_hyphen (obj , name ):
32
+ return obj [name .replace ("_" , "-" )]
33
+
34
+
27
35
def delitem (obj , name ):
28
36
del obj [name ]
29
37
30
38
39
+ def delitem_hyphen (obj , name ):
40
+ del obj [name .replace ("_" , "-" )]
41
+
42
+
31
43
@pytest .mark .parametrize (
32
44
"old_name, new_name, value, default" ,
33
45
[
@@ -38,14 +50,14 @@ def delitem(obj, name):
38
50
("padding_left" , "margin_left" , 5 , 0 ),
39
51
],
40
52
)
41
- @pytest .mark .parametrize ("set_fn" , (setattr , setitem ))
42
- @pytest .mark .parametrize ("get_fn" , (getattr , getitem ))
43
- @pytest .mark .parametrize ("del_fn" , (delattr , delitem ))
53
+ @pytest .mark .parametrize ("set_fn" , (setattr , setitem , setitem_hyphen ))
54
+ @pytest .mark .parametrize ("get_fn" , (getattr , getitem , getitem_hyphen ))
55
+ @pytest .mark .parametrize ("del_fn" , (delattr , delitem , delitem_hyphen ))
44
56
def test_deprecated_properties (
45
57
old_name , new_name , value , default , set_fn , get_fn , del_fn
46
58
):
47
59
"""Deprecated names alias to new names, and issue deprecation warnings."""
48
- # Set the old name, then check the new name.
60
+ # Set the old name, then check the new name
49
61
style = Pack ()
50
62
with pytest .warns (DeprecationWarning ):
51
63
set_fn (style , old_name , value )
@@ -56,7 +68,7 @@ def test_deprecated_properties(
56
68
del_fn (style , old_name )
57
69
assert get_fn (style , new_name ) == default
58
70
59
- # Set the new name, then check the old name.
71
+ # Set the new name, then check the old name
60
72
style = Pack ()
61
73
set_fn (style , new_name , value )
62
74
with pytest .warns (DeprecationWarning ):
@@ -85,31 +97,40 @@ def test_deprecated_properties(
85
97
(COLUMN , LTR , CENTER , CENTER ),
86
98
],
87
99
)
88
- def test_alignment_align_items (direction , text_direction , alignment , align_items ):
100
+ @pytest .mark .parametrize ("set_fn" , (setattr , setitem , setitem_hyphen ))
101
+ @pytest .mark .parametrize ("get_fn" , (getattr , getitem , getitem_hyphen ))
102
+ @pytest .mark .parametrize ("del_fn" , (delattr , delitem , delitem_hyphen ))
103
+ def test_alignment_align_items (
104
+ direction , text_direction , alignment , align_items , set_fn , get_fn , del_fn
105
+ ):
89
106
"""Alignment (with deprecation warning) and align_items map to each other."""
107
+ # Set alignment, check align_items
90
108
with pytest .warns (DeprecationWarning ):
91
109
style = Pack (
92
110
direction = direction ,
93
111
text_direction = text_direction ,
94
- alignment = alignment ,
95
112
)
96
- assert style .align_items == align_items
113
+ set_fn (style , "alignment" , alignment )
114
+ assert get_fn (style , "align_items" ) == align_items
97
115
116
+ # Delete alignment, check align_items
98
117
with pytest .warns (DeprecationWarning ):
99
- del style . alignment
100
- assert style . align_items is None
118
+ del_fn ( style , " alignment" )
119
+ assert get_fn ( style , " align_items" ) is None
101
120
121
+ # Set align_items, check alignment
102
122
style = Pack (
103
123
direction = direction ,
104
124
text_direction = text_direction ,
105
- align_items = align_items ,
106
125
)
126
+ set_fn (style , "align_items" , align_items )
107
127
with pytest .warns (DeprecationWarning ):
108
- assert style . alignment == alignment
128
+ assert get_fn ( style , " alignment" ) == alignment
109
129
110
- del style .align_items
130
+ # Delete align_items, check alignment
131
+ del_fn (style , "align_items" )
111
132
with pytest .warns (DeprecationWarning ):
112
- assert style . alignment is None
133
+ assert get_fn ( style , " alignment" ) is None
113
134
114
135
115
136
@pytest .mark .parametrize (
@@ -121,8 +142,26 @@ def test_alignment_align_items(direction, text_direction, alignment, align_items
121
142
(COLUMN , BOTTOM ),
122
143
],
123
144
)
124
- def test_alignment_align_items_invalid (direction , alignment ):
145
+ @pytest .mark .parametrize ("get_fn" , (getattr , getitem , getitem_hyphen ))
146
+ def test_alignment_align_items_invalid (direction , alignment , get_fn ):
125
147
"""Invalid settings for alignment should return None for align_items."""
126
148
with pytest .warns (DeprecationWarning ):
127
149
style = Pack (direction = direction , alignment = alignment )
128
- assert style .align_items is None
150
+ assert get_fn (style , "align_items" ) is None
151
+
152
+
153
+ def test_bogus_property_name ():
154
+ """Invalid property name in brackets should be an error.
155
+
156
+ This is tested here along with deprecated property names, because normally it should
157
+ be verified by Travertino's tests. It can only be an issue as long as we're
158
+ overriding the relevant methods in Pack.
159
+ """
160
+ style = Pack ()
161
+
162
+ with pytest .raises (KeyError ):
163
+ style ["bogus" ] = 1
164
+ with pytest .raises (KeyError ):
165
+ style ["bogus" ]
166
+ with pytest .raises (KeyError ):
167
+ del style ["bogus" ]
0 commit comments