@@ -98,6 +98,66 @@ def test_enum_eof(TestEnum: type[Enum]) -> None:
98
98
TestEnum [None ](b"\x01 " )
99
99
100
100
101
+ def test_enum_same_value_different_type (cs : cstruct , compiled : bool ) -> None :
102
+ cdef = """
103
+ enum Test16 : uint16 {
104
+ A = 0x1,
105
+ B = 0x2
106
+ };
107
+
108
+ enum Test24 : uint24 {
109
+ A = 0x1,
110
+ B = 0x2
111
+ };
112
+
113
+ enum Test32 : uint32 {
114
+ A = 0x1,
115
+ B = 0x2
116
+ };
117
+ """
118
+ cs .load (cdef , compiled = compiled )
119
+
120
+ obj = {
121
+ cs .Test16 .A : "Test16.A" ,
122
+ cs .Test16 .B : "Test16.B" ,
123
+ cs .Test24 .A : "Test24.A" ,
124
+ cs .Test24 .B : "Test24.B" ,
125
+ }
126
+
127
+ assert obj [cs .Test16 .A ] == "Test16.A"
128
+ assert obj [cs .Test16 (2 )] == "Test16.B"
129
+ assert obj [cs .Test24 (1 )] == "Test24.A"
130
+ assert obj [cs .Test24 .B ] == "Test24.B"
131
+
132
+ with pytest .raises (KeyError ):
133
+ obj [cs .Test32 .A ]
134
+
135
+
136
+ def test_enum_str_repr (TestEnum : type [Enum ]) -> None :
137
+ assert repr (TestEnum .A ) == "<Test.A: 1>"
138
+ assert str (TestEnum .A ) == "Test.A"
139
+ assert repr (TestEnum (69 )) == "<Test: 69>"
140
+ assert str (TestEnum (69 )) == "Test.69"
141
+
142
+
143
+ def test_enum_str_repr_in_struct (cs : cstruct , compiled : bool ) -> None :
144
+ cdef = """
145
+ enum Test16 : uint16 {
146
+ A = 0x1,
147
+ B = 0x2
148
+ };
149
+
150
+ struct test {
151
+ Test16 a;
152
+ };
153
+ """
154
+ cs .load (cdef , compiled = compiled )
155
+
156
+ obj = cs .test (b"\x02 \x00 " )
157
+ assert repr (obj ) == "<test a=<Test16.B: 2>>"
158
+ assert str (obj ) == "<test a=<Test16.B: 2>>"
159
+
160
+
101
161
def test_enum_struct (cs : cstruct , compiled : bool ) -> None :
102
162
cdef = """
103
163
enum Test16 : uint16 {
@@ -174,26 +234,6 @@ def test_enum_struct(cs: cstruct, compiled: bool) -> None:
174
234
assert cs .test_expr (buf ).expr == [cs .Test16 .A , cs .Test16 .B ]
175
235
assert cs .test_expr (size = 1 , expr = [cs .Test16 .A , cs .Test16 .B ]).dumps () == buf
176
236
177
- obj = {
178
- cs .Test16 .A : "Test16.A" ,
179
- cs .Test16 .B : "Test16.B" ,
180
- cs .Test24 .A : "Test24.A" ,
181
- cs .Test24 .B : "Test24.B" ,
182
- }
183
-
184
- assert obj [cs .Test16 .A ] == "Test16.A"
185
- assert obj [cs .Test16 (2 )] == "Test16.B"
186
- assert obj [cs .Test24 (1 )] == "Test24.A"
187
- assert obj [cs .Test24 .B ] == "Test24.B"
188
-
189
- with pytest .raises (KeyError ):
190
- obj [cs .Test32 .A ]
191
-
192
- assert repr (cs .Test16 .A ) == "<Test16.A: 1>"
193
- assert str (cs .Test16 .A ) == "Test16.A"
194
- assert repr (cs .Test16 (69 )) == "<Test16: 69>"
195
- assert str (cs .Test16 (69 )) == "Test16.69"
196
-
197
237
198
238
def test_enum_comments (cs : cstruct ) -> None :
199
239
cdef = """
@@ -259,15 +299,15 @@ def test_enum_name(cs: cstruct, compiled: bool) -> None:
259
299
Color = cs .Color
260
300
Pixel = cs .Pixel
261
301
262
- pixel = Pixel (b"\xFF \x0A \x01 \x00 \xAA \xBB \xCC \xDD " )
302
+ pixel = Pixel (b"\xff \x0a \x01 \x00 \xaa \xbb \xcc \xdd " )
263
303
assert pixel .x == 255
264
304
assert pixel .y == 10
265
305
assert pixel .color .name == "RED"
266
306
assert pixel .color .value == Color .RED
267
307
assert pixel .color .value == 1
268
308
assert pixel .hue == 0xDDCCBBAA
269
309
270
- pixel = Pixel (b"\x00 \x00 \xFF \x00 \xAA \xBB \xCC \xDD " )
310
+ pixel = Pixel (b"\x00 \x00 \xff \x00 \xaa \xbb \xcc \xdd " )
271
311
assert pixel .color .name is None
272
312
assert pixel .color .value == 0xFF
273
313
assert repr (pixel .color ) == "<Color: 255>"
@@ -350,7 +390,7 @@ def test_enum_anonymous_struct(cs: cstruct, compiled: bool) -> None:
350
390
351
391
test = cs .test
352
392
353
- t = test (b"\xff \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x0A \x00 \x00 \x00 " )
393
+ t = test (b"\xff \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x0a \x00 \x00 \x00 " )
354
394
assert t .arr == [255 , 0 , 0 , 10 ]
355
395
356
396
0 commit comments