You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- Added a test generator for `Serializable` classes:
2
-
3
-
The `gen_serializable_test` function generates tests for `Serializable` classes. It takes the following arguments:
4
-
5
-
-`context`: The dictionary containing the context in which the generated test class will be placed (e.g. `globals()`).
6
-
> Dictionary updates must reflect in the context. This is the case for `globals()` but implementation-specific for `locals()`.
7
-
-`cls`: The `Serializable` class to generate tests for.
8
-
-`fields`: A list of fields where the test values will be placed.
9
-
10
-
> In the example above, the `ToyClass` class has two fields: `a` and `b`.
11
-
12
-
-`test_data`: A list of tuples containing either:
13
-
-`((field1_value, field2_value, ...), expected_bytes)`: The values of the fields and the expected serialized bytes. This needs to work both ways, i.e. `cls(field1_value, field2_value, ...) == cls.deserialize(expected_bytes).`
14
-
-`((field1_value, field2_value, ...), exception)`: The values of the fields and the expected exception when validating the object.
15
-
-`(exception, bytes)`: The expected exception when deserializing the bytes and the bytes to deserialize.
16
-
17
-
The `gen_serializable_test` function generates a test class with the following tests:
18
-
19
-
```python
20
-
gen_serializable_test(
21
-
context=globals(),
22
-
cls=ToyClass,
23
-
fields=[("a", int), ("b", str)],
24
-
test_data=[
25
-
((1, "hello"), b"\x01\x05hello"),
26
-
((2, "world"), b"\x02\x05world"),
27
-
((3, 1234567890), b"\x03\x0a1234567890"),
28
-
((0, "hello"), ZeroDivisionError("a must be non-zero")), # With an error message
29
-
((1, "hello world"), ValueError), # No error message
30
-
((1, 12345678900), ValueError("b must be less than 10 .*")), # With an error message and regex
31
-
(ZeroDivisionError, b"\x00"),
32
-
(ZeroDivisionError, b"\x01\x05hello"),
33
-
(IOError, b"\x01"),
34
-
],
35
-
)
36
-
```
37
-
38
-
The generated test class will have the following tests:
39
-
40
-
```python
41
-
classTestGenToyClass:
42
-
deftest_serialization(self):
43
-
# 2 subtests for the cases 1 and 2
44
-
45
-
deftest_deserialization(self):
46
-
# 2 subtests for the cases 1 and 2
47
-
48
-
deftest_validation(self):
49
-
# 2 subtests for the cases 3 and 4
50
-
51
-
deftest_exceptions(self):
52
-
# 2 subtests for the cases 5 and 6
53
-
```
1
+
-**Class**: `Serializable`
2
+
- Base class for types that should be (de)serializable into/from `mcproto.Buffer` data.
3
+
-**Methods**:
4
+
-`__attrs_post_init__()`: Runs validation after object initialization, override to define custom behavior.
5
+
-`serialize() -> Buffer`: Returns the object as a `Buffer`.
6
+
-`serialize_to(buf: Buffer)`: Abstract method to write the object to a `Buffer`.
7
+
-`validate()`: Validates the object's attributes; can be overridden for custom validation.
8
+
-`deserialize(cls, buf: Buffer) -> Self`: Abstract method to construct the object from a `Buffer`.
9
+
-**Note**: Use the `dataclass` decorator when adding parameters to subclasses.
0 commit comments