Simplified tests, added deserialization for unsigned integers
[sandbox] / serial / test_binary.py
1 import unittest
2
3 import binary
4
5 EXAMPLE_REPRESENTATIONS = [
6     (binary.TAG_NULL, None, b'\x00'),
7     (binary.TAG_TRUE, True, b'\x01'),
8     (binary.TAG_FALSE, False, b'\x02'),
9     (binary.TAG_UINT8, 7, b'\x03\x07'),
10     (binary.TAG_UINT16, 7, b'\x04\x00\x07'),
11     (binary.TAG_UINT32, 7, b'\x05\x00\x00\x00\x07'),
12     (binary.TAG_UINT64, 7, b'\x06\x00\x00\x00\x00\x00\x00\x00\x07'),
13 ]
14
15 class SerializeTests(unittest.TestCase):
16     def test_serialize(self):
17         for tag, instance, representation in EXAMPLE_REPRESENTATIONS:
18             self.assertEqual(
19                 binary.serialize(binary.TaggedObject(
20                     tag = tag,
21                     instance = instance,
22                 )),
23                 representation,
24             )
25
26 class DeserializeTests(unittest.TestCase):
27     def test_deserialize(self):
28         for tag, instance, representation in EXAMPLE_REPRESENTATIONS:
29             self.assertEqual(
30                 binary.deserialize(representation),
31                 binary.TaggedObject(
32                     tag = tag,
33                     instance = instance,
34                 ),
35             )
36
37 unittest.main()