1- - Changed the way ` Serializable ` classes are handled:
2-
3- Here is how a basic ` Serializable ` class looks like:
4- ``` python
5- @final
6- @dataclass
7- class ToyClass (Serializable ):
8- """ Toy class for testing demonstrating the use of gen_serializable_test on `Serializable`."""
9-
10- a: int
11- b: str | int
12-
13- @override
14- def serialize_to (self , buf : Buffer):
15- """ Write the object to a buffer."""
16- self .b = cast(str , self .b) # Handled by the transform method
17- buf.write_varint(self .a)
18- buf.write_utf(self .b)
19-
20- @ classmethod
21- @override
22- def deserialize (cls , buf : Buffer) -> ToyClass:
23- """ Deserialize the object from a buffer."""
24- a = buf.read_varint()
25- if a == 0 :
26- raise ZeroDivisionError (" a must be non-zero" )
27- b = buf.read_utf()
28- return cls (a, b)
29-
30- @override
31- def validate (self ) -> None :
32- """ Validate the object's attributes."""
33- if self .a == 0 :
34- raise ZeroDivisionError (" a must be non-zero" )
35- if (isinstance (self .b, int ) and math.log10(self .b) > 10 ) or (isinstance (self .b, str ) and len (self .b) > 10 ):
36- raise ValueError (" b must be less than 10 characters" )
37-
38- @override
39- def transform (self ) -> None :
40- """ Apply a transformation to the payload of the object."""
41- if isinstance (self .b, int ):
42- self .b = str (self .b)
43- ```
44-
45-
46- The ` Serializable ` class implement the following methods:
47- - ` serialize_to(buf: Buffer) -> None ` : Serializes the object to a buffer.
48- - ` deserialize(buf: Buffer) -> Serializable ` : Deserializes the object from a buffer.
49-
50- And the following optional methods:
51- - ` validate() -> None ` : Validates the object's attributes, raising an exception if they are invalid.
52- - ` transform() -> None ` : Transforms the the object's attributes, this method is meant to convert types like you would in a classic ` __init__ ` .
53- You can rely on this ` validate ` having been executed.
54-
551- Added a test generator for ` Serializable ` classes:
562
573 The ` gen_serializable_test ` function generates tests for ` Serializable ` classes. It takes the following arguments:
@@ -69,6 +15,7 @@ And the following optional methods:
6915 - ` (exception, bytes) ` : The expected exception when deserializing the bytes and the bytes to deserialize.
7016
7117 The ` gen_serializable_test ` function generates a test class with the following tests:
18+
7219``` python
7320gen_serializable_test(
7421 context = globals (),
@@ -80,7 +27,7 @@ gen_serializable_test(
8027 ((3 , 1234567890 ), b " \x03\x0a 1234567890" ),
8128 ((0 , " hello" ), ZeroDivisionError (" a must be non-zero" )), # With an error message
8229 ((1 , " hello world" ), ValueError ), # No error message
83- ((1 , 12345678900 ), ValueError ),
30+ ((1 , 12345678900 ), ValueError ( " b must be less than 10 .* " )), # With an error message and regex
8431 (ZeroDivisionError , b " \x00 " ),
8532 (ZeroDivisionError , b " \x01\x05 hello" ),
8633 (IOError , b " \x01 " ),
@@ -90,7 +37,7 @@ gen_serializable_test(
9037
9138 The generated test class will have the following tests:
9239
93- ``` python
40+ ``` python
9441class TestGenToyClass :
9542 def test_serialization (self ):
9643 # 2 subtests for the cases 1 and 2
@@ -103,4 +50,4 @@ class TestGenToyClass:
10350
10451 def test_exceptions (self ):
10552 # 2 subtests for the cases 5 and 6
106- ```
53+ ```
0 commit comments