Started implementing object serialization
[sandbox] / serial / test.py
1 import unittest
2
3 from serial import binary, tags, text
4
5 EXAMPLE_BINARY_REPRESENTATIONS = [
6     (tags.TaggedObject(tags.NULL, None), b'\x00'),
7     (tags.TaggedObject(tags.TRUE, True), b'\x01'),
8     (tags.TaggedObject(tags.FALSE, False), b'\x02'),
9     (tags.TaggedObject(tags.UINT8, 7), b'\x03\x07'),
10     (tags.TaggedObject(tags.UINT16, 7), b'\x04\x00\x07'),
11     (tags.TaggedObject(tags.UINT32, 7), b'\x05\x00\x00\x00\x07'),
12     (tags.TaggedObject(tags.UINT64, 7), b'\x06\x00\x00\x00\x00\x00\x00\x00\x07'),
13     (tags.TaggedObject(tags.INT8, 7), b'\x10\x07'),
14     (tags.TaggedObject(tags.INT16, 7), b'\x11\x00\x07'),
15     (tags.TaggedObject(tags.INT32, 7), b'\x12\x00\x00\x00\x07'),
16     (tags.TaggedObject(tags.INT64, 7), b'\x13\x00\x00\x00\x00\x00\x00\x00\x07'),
17     (tags.TaggedObject(tags.UINT8, 254), b'\x03\xfe'),
18     (tags.TaggedObject(tags.UINT16, 65534), b'\x04\xff\xfe'),
19     (tags.TaggedObject(tags.UINT32, 4294967294), b'\x05\xff\xff\xff\xfe'),
20     (tags.TaggedObject(tags.UINT64, 18446744073709551614), b'\x06\xff\xff\xff\xff\xff\xff\xff\xfe'),
21     (tags.TaggedObject(tags.INT8, -2), b'\x10\xfe'),
22     (tags.TaggedObject(tags.INT16, -2), b'\x11\xff\xfe'),
23     (tags.TaggedObject(tags.INT32, -2), b'\x12\xff\xff\xff\xfe'),
24     (tags.TaggedObject(tags.INT64, -2), b'\x13\xff\xff\xff\xff\xff\xff\xff\xfe'),
25     (tags.TaggedObject(tags.BINARY, b'\xde\xad\xbe\xef'), b'\x20\x00\x00\x00\x04\xde\xad\xbe\xef'),
26     (tags.TaggedObject(tags.UTF8, 'Lol!'), b'\x21\x00\x00\x00\x04Lol!'),
27     (tags.TaggedObject(tags.UTF16, 'かわ'), b'\x22\x00\x00\x00\x06\xff\xfeK0\x8f0'),
28     (tags.TaggedObject(tags.UTF32, '漢'), b'\x23\x00\x00\x00\x08\xff\xfe\x00\x00"o\x00\x00'),
29     (
30         tags.TaggedObject(
31             tags.TUPLE,
32             (
33                 tags.TaggedObject(
34                     tags.TRUE,
35                     True,
36                 ),
37                 tags.TaggedObject(
38                     tags.UINT8,
39                     7,
40                 ),
41             ),
42         ),
43         b'\x30\x00\x00\x00\x03\x01\x03\x07'
44     ),
45     (
46         tags.TaggedObject(
47             tag = tags.LIST,
48             instance = [
49                 tags.TaggedObject(tag = tags.UINT8, instance = 9),
50                 tags.TaggedObject(tag = tags.UINT8, instance = 22),
51                 tags.TaggedObject(tag = tags.UINT8, instance = 36),
52             ],
53         ),
54         b'\x31\x03\x00\x00\x00\x03\x09\x16\x24',
55     ),
56     (
57         tags.TaggedObject(
58             tag = tags.OBJECT,
59             instance = [
60                 (
61                     tags.TaggedObject(tag = tags.UTF8, instance = 'foo'),
62                     tags.TaggedObject(tag = tags.UTF8, instance = 'bar'),
63                 ),
64                 (
65                     tags.TaggedObject(tag = tags.UTF8, instance = 'baz'),
66                     tags.TaggedObject(tag = tags.UINT8, instance = 42),
67                 ),
68             ],
69         ),
70         b'\x32\x21\x00\x00\x00\x03foo\x21\x00\x00\x00\x03bar\x00\x00\x00\x03baz\x03\x2a',
71     ),
72 ]
73
74 class BinarySerializeTests(unittest.TestCase):
75     def test_serialize(self):
76         for tagged_object, expected in EXAMPLE_BINARY_REPRESENTATIONS:
77             actual = binary.serialize(tagged_object)
78             self.assertEqual(expected, actual)
79
80 class BinaryDeserializeTests(unittest.TestCase):
81     def test_deserialize(self):
82         for expected, representation in EXAMPLE_BINARY_REPRESENTATIONS:
83             actual = binary.deserialize(representation)
84             self.assertEqual(expected, actual)
85
86 EXAMPLE_TEXT_REPRESENTATIONS = [
87     (tags.TaggedObject(tags.NULL, None), 'null'),
88     (tags.TaggedObject(tags.TRUE, True), 'true'),
89     (tags.TaggedObject(tags.FALSE, False), 'false'),
90     (tags.TaggedObject(tags.UINT8, 42), '42u8'),
91     (tags.TaggedObject(tags.UINT16, 42), '42u16'),
92     (tags.TaggedObject(tags.UINT32, 42), '42u32'),
93     (tags.TaggedObject(tags.UINT64, 42), '42u64'),
94     (tags.TaggedObject(tags.INT8, 42), '42i8'),
95     (tags.TaggedObject(tags.INT16, 42), '42i16'),
96     (tags.TaggedObject(tags.INT32, 42), '42i32'),
97     (tags.TaggedObject(tags.INT64, 42), '42i64'),
98     (tags.TaggedObject(tags.INT8, -2), '-2i8'),
99     (tags.TaggedObject(tags.INT16, -2), '-2i16'),
100     (tags.TaggedObject(tags.INT32, -2), '-2i32'),
101     (tags.TaggedObject(tags.INT64, -2), '-2i64'),
102     (tags.TaggedObject(tags.BINARY, b'\x42\xde\xad\xbe\xef'), 'bin"42deadbeef"'),
103     (tags.TaggedObject(tags.UTF8, 'Lol!'), 'utf8"Lol!"'),
104     (tags.TaggedObject(tags.UTF16, 'かわ'), 'utf16"かわ"'),
105     (tags.TaggedObject(tags.UTF32, '漢'), 'utf32"漢"'),
106     (
107         tags.TaggedObject(
108             tag = tags.LIST,
109             instance = [
110                 tags.TaggedObject(tag = tags.UINT8, instance = 9),
111                 tags.TaggedObject(tag = tags.UINT8, instance = 22),
112                 tags.TaggedObject(tag = tags.UINT8, instance = 36),
113             ],
114         ),
115         '[\n  9u8,\n  22u8,\n  36u8\n]'
116     ),
117     (
118         tags.TaggedObject(
119             tag = tags.OBJECT,
120             instance = [
121                 (
122                     tags.TaggedObject(tag = tags.UTF8, instance = 'foo'),
123                     tags.TaggedObject(tag = tags.UTF8, instance = 'bar'),
124                 ),
125                 (
126                     tags.TaggedObject(tag = tags.UTF8, instance = 'baz'),
127                     tags.TaggedObject(tag = tags.UINT8, instance = 42),
128                 ),
129             ],
130         ),
131         '{\n  utf8"foo": utf8"bar",\n  utf8"baz": 42u8\n}',
132     ),
133 ]
134
135 class TextSerializeTests(unittest.TestCase):
136     def test_serialize(self):
137         for tagged_object, expected in EXAMPLE_TEXT_REPRESENTATIONS:
138             actual = text.serialize(tagged_object)
139             self.assertEqual(expected, actual)
140
141 class TextDeserializeTests(unittest.TestCase):
142     def test_deserialize(self):
143         for expected, representation in EXAMPLE_TEXT_REPRESENTATIONS:
144             actual = text.deserialize(representation)
145             self.assertEqual(expected, actual)
146
147 unittest.main()