33
44import pytest
55
6+ from quickstruct .error import InheritanceError , OverrideError , SizeError , UnoverridbaleFieldError , UnsafeOverrideError
7+
68
79class Person (DataStruct ):
810 name : String
11+ _pad0 : Padding [1 ]
912 age : i8
1013
1114 def __eq__ (self , other : "Person" ) -> bool :
@@ -22,6 +25,17 @@ def __eq__(self, other: "Employee") -> bool:
2225 return super ().__eq__ (other )
2326
2427
28+ class Company (DataStruct ):
29+ name : String
30+ owner : Person
31+ employees : Array [Employee ]
32+
33+ def __eq__ (self , other : object ) -> bool :
34+ if not isinstance (other , Company ):
35+ return super ().__eq__ (other )
36+ return self .name == other .name and self .owner == other .owner and self .employees == other .employees
37+
38+
2539class PackedStruct1 (DataStruct , flags = StructFlags .Align1Byte ):
2640 a : i16
2741 b : i32
@@ -47,7 +61,7 @@ class PackedStruct8(DataStruct, flags=StructFlags.Align8Bytes):
4761
4862
4963def test_version ():
50- assert __version__ == '0.1.4 '
64+ assert __version__ == '0.1.5 '
5165
5266
5367def test_struct_data ():
@@ -129,6 +143,124 @@ def test_aligned_struct4():
129143def test_aligned_struct8 ():
130144 struct = PackedStruct8 (a = 1 , b = 2 )
131145
146+ print (struct )
147+
132148 assert struct .a == 1
133149 assert struct .b == 2
134150 assert struct .size == 16
151+
152+
153+ def test_inheriting_final_struct ():
154+ class FinalStruct (DataStruct , flags = StructFlags .Final ):
155+ a : i16
156+ b : i32
157+
158+ def test1 ():
159+ class _ (FinalStruct ):
160+ c : i32
161+
162+ def test2 ():
163+ class _ (Person , FinalStruct ):
164+ c : i32
165+
166+ with pytest .raises (InheritanceError ):
167+ test1 ()
168+
169+ with pytest .raises (InheritanceError ):
170+ test2 ()
171+
172+
173+ def test_override_struct ():
174+ class _ (Person ):
175+ age : i8
176+
177+ person = _ (name = "John Doe" , age = 42 )
178+
179+ assert person .name == "John Doe"
180+ assert person .age == 42
181+
182+
183+ def test_override_struct_error ():
184+ def test ():
185+ class _ (Person , flags = StructFlags .Default & ~ StructFlags .AllowOverride ):
186+ age : i8
187+
188+ with pytest .raises (OverrideError ):
189+ test ()
190+
191+
192+ def test_override_struct_safe ():
193+ class _ (Person , flags = StructFlags .TypeSafeOverride ):
194+ age : i8
195+
196+ person = _ (name = "John Doe" , age = 42 )
197+
198+ assert person .name == "John Doe"
199+ assert person .age == 42
200+
201+
202+ def test_override_struct_safe_error ():
203+ def test ():
204+ class _ (Person , flags = StructFlags .TypeSafeOverride ):
205+ age : i16 # original type: i8
206+
207+ with pytest .raises (UnsafeOverrideError ):
208+ test ()
209+
210+
211+ def test_locked_structure_override ():
212+ class LockedStruct (DataStruct , flags = StructFlags .LockedStructure ):
213+ a : i16
214+ b : i32
215+
216+ def test ():
217+ class _ (LockedStruct ):
218+ a : i16
219+
220+ with pytest .raises (UnoverridbaleFieldError ):
221+ test ()
222+
223+
224+ def test_force_fixed_size_struct ():
225+ class Fixed (DataStruct , flags = StructFlags .FixedSize ):
226+ a : i16
227+ b : i32
228+ c : String [10 ]
229+
230+ assert Fixed .is_fixed_size
231+
232+
233+ def test_force_fixed_size_struct_error ():
234+ def test ():
235+ class _ (DataStruct , flags = StructFlags .FixedSize ):
236+ a : i16
237+ b : i32
238+ c : String
239+
240+ with pytest .raises (SizeError ):
241+ test ()
242+
243+
244+ def test_composed_struct ():
245+ company = Company (name = "Acme" , owner = Person (name = "John Doe" , age = 42 ), employees = [
246+ Employee (name = "Jane Doe" , age = 32 , salary = 123.45 ),
247+ Employee (name = "John Smith" , age = 42 , salary = 123.45 ),
248+ ])
249+
250+ assert company .name == "Acme"
251+ assert company .owner .name == "John Doe"
252+ assert company .owner .age == 42
253+
254+ assert len (company .employees ) == 2
255+ assert company .employees [0 ].name == "Jane Doe"
256+ assert company .employees [0 ].age == 32
257+ assert company .employees [0 ].salary == 123.45
258+
259+ assert company .employees [1 ].name == "John Smith"
260+ assert company .employees [1 ].age == 42
261+ assert company .employees [1 ].salary == 123.45
262+
263+ data = company .to_bytes ()
264+ result = Company .from_bytes (data )
265+
266+ assert company == result
0 commit comments