4 from don import binary, string, tags
6 class TestBinarySerialize(unittest.TestCase):
7 def test_serializes_null(self):
8 self.assertEqual(binary.serialize(None), b'\x00')
10 def test_serializes_true(self):
11 self.assertEqual(binary.serialize(True), b'\x01')
13 def test_serializes_false(self):
14 self.assertEqual(binary.serialize(False), b'\x02')
16 def test_serializes_integers_in_32_bit_twos_complement_with_network_byte_order(self):
17 self.assertEqual(binary.serialize(-2147483648), b'\x12\x80\x00\x00\x00')
18 self.assertEqual(binary.serialize(-1), b'\x12\xff\xff\xff\xff')
19 self.assertEqual(binary.serialize(0), b'\x12\x00\x00\x00\x00')
20 self.assertEqual(binary.serialize(1), b'\x12\x00\x00\x00\x01')
21 self.assertEqual(binary.serialize(2147483647), b'\x12\x7f\xff\xff\xff')
23 def test_serializes_floats_into_binary64_with_network_byte_order(self):
24 self.assertEqual(binary.serialize(1.0), b'\x21\x3f\xf0\x00\x00\x00\x00\x00\x00')
25 self.assertEqual(binary.serialize(2.0), b'\x21\x40\x00\x00\x00\x00\x00\x00\x00')
26 self.assertEqual(binary.serialize(-2.0), b'\x21\xc0\x00\x00\x00\x00\x00\x00\x00')
27 self.assertEqual(binary.serialize(0.5), b'\x21\x3f\xe0\x00\x00\x00\x00\x00\x00')
28 self.assertEqual(binary.serialize(2.0 ** -1074), b'\x21\x00\x00\x00\x00\x00\x00\x00\x01')
29 self.assertEqual(binary.serialize(2.0 ** -1022), b'\x21\x00\x10\x00\x00\x00\x00\x00\x00')
30 self.assertEqual(binary.serialize(0.0), b'\x21\x00\x00\x00\x00\x00\x00\x00\x00')
32 def test_serializes_binary(self):
33 self.assertEqual(binary.serialize(b'\xde\xad\xbe\xef'), b'\x30\x00\x00\x00\x04\xde\xad\xbe\xef')
35 def test_serializes_utf8(self):
36 self.assertEqual(binary.serialize('Hello, world'), b'\x31\x00\x00\x00\x0cHello, world')
37 self.assertEqual(binary.serialize('世界'), b'\x31\x00\x00\x00\x06\xe4\xb8\x96\xe7\x95\x8c')
39 def test_serializes_list(self):
40 self.assertEqual(binary.serialize([]), b'\x40\x00\x00\x00\x00\x00\x00\x00\x00\x00')
41 self.assertEqual(binary.serialize([1,2,3]), b'\x40\x12\x00\x00\x00\x0c\x00\x00\x00\x03\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x03')
42 self.assertEqual(binary.serialize(['Hello, world', 'Goodnight, moon']), b'\x40\x31\x00\x00\x00#\x00\x00\x00\x02\x00\x00\x00\x0cHello, world\x00\x00\x00\x0fGoodnight, moon')
43 self.assertEqual(binary.serialize([1.618, 2.718, 3.142]), b'\x40\x21\x00\x00\x00\x18\x00\x00\x00\x03?\xf9\xe3S\xf7\xce\xd9\x17@\x05\xbev\xc8\xb49X@\t"\xd0\xe5`A\x89')
45 def test_serializes_dictionary(self):
46 self.assertEqual(binary.serialize({}), b'\x41\x00\x00\x00\x00\x00\x00\x00\x00')
47 self.assertEqual(binary.serialize(collections.OrderedDict([
51 ])), b'A\x00\x00\x00+\x00\x00\x00\x03\x00\x00\x00\x03foo\x12\x00\x00\x00*\x00\x00\x00\x03bar!@\t\x1e\xb8Q\xeb\x85\x1f\x00\x00\x00\x03baz1\x00\x00\x00\x03qux')
53 class TestBinaryDeserialize(unittest.TestCase):
54 def test_deserializes_null(self):
55 self.assertEqual(binary.deserialize(b'\x00'), None)
57 def test_deserializes_true(self):
58 self.assertEqual(binary.deserialize(b'\x01'), True)
60 def test_deserializes_false(self):
61 self.assertEqual(binary.deserialize(b'\x02'), False)
63 def test_deserializes_8_bit_twos_complement_with_network_byte_order(self):
64 self.assertEqual(binary.deserialize(b'\x10\x80'), -128)
65 self.assertEqual(binary.deserialize(b'\x10\xff'), -1)
66 self.assertEqual(binary.deserialize(b'\x10\x00'), 0)
67 self.assertEqual(binary.deserialize(b'\x10\x01'), 1)
68 self.assertEqual(binary.deserialize(b'\x10\x7f'), 127)
70 def test_deserializes_16_bit_twos_complement_with_network_byte_order(self):
71 self.assertEqual(binary.deserialize(b'\x11\x80\x00'), -32768)
72 self.assertEqual(binary.deserialize(b'\x11\xff\xff'), -1)
73 self.assertEqual(binary.deserialize(b'\x11\x00\x00'), 0)
74 self.assertEqual(binary.deserialize(b'\x11\x00\x01'), 1)
75 self.assertEqual(binary.deserialize(b'\x11\x7f\xff'), 32767)
77 def test_deserializes_32_bit_twos_complement_with_network_byte_order(self):
78 self.assertEqual(binary.deserialize(b'\x12\x80\x00\x00\x00'), -2147483648)
79 self.assertEqual(binary.deserialize(b'\x12\xff\xff\xff\xff'), -1)
80 self.assertEqual(binary.deserialize(b'\x12\x00\x00\x00\x00'), 0)
81 self.assertEqual(binary.deserialize(b'\x12\x00\x00\x00\x01'), 1)
82 self.assertEqual(binary.deserialize(b'\x12\x7f\xff\xff\xff'), 2147483647)
84 def test_deserializes_64_bit_twos_complement_with_network_byte_order(self):
85 self.assertEqual(binary.deserialize(b'\x13\x80\x00\x00\x00\x00\x00\x00\x00'), -9223372036854775808)
86 self.assertEqual(binary.deserialize(b'\x13\xff\xff\xff\xff\xff\xff\xff\xff'), -1)
87 self.assertEqual(binary.deserialize(b'\x13\x00\x00\x00\x00\x00\x00\x00\x00'), 0)
88 self.assertEqual(binary.deserialize(b'\x13\x00\x00\x00\x00\x00\x00\x00\x01'), 1)
89 self.assertEqual(binary.deserialize(b'\x13\x7f\xff\xff\xff\xff\xff\xff\xff'), 9223372036854775807)
91 def test_deserializes_binary64_as_float(self):
92 self.assertEqual(binary.deserialize(b'\x21\x3f\xf0\x00\x00\x00\x00\x00\x00'), 1.0)
93 self.assertEqual(binary.deserialize(b'\x21\x40\x00\x00\x00\x00\x00\x00\x00'), 2.0)
94 self.assertEqual(binary.deserialize(b'\x21\xc0\x00\x00\x00\x00\x00\x00\x00'), -2.0)
95 self.assertEqual(binary.deserialize(b'\x21\x3f\xe0\x00\x00\x00\x00\x00\x00'), 0.5)
96 self.assertEqual(binary.deserialize(b'\x21\x00\x00\x00\x00\x00\x00\x00\x01'), 2.0 ** -1074)
97 self.assertEqual(binary.deserialize(b'\x21\x00\x10\x00\x00\x00\x00\x00\x00'), 2.0 ** -1022)
98 self.assertEqual(binary.deserialize(b'\x21\x00\x00\x00\x00\x00\x00\x00\x00'), 0.0)
100 def test_deserializes_binary(self):
101 self.assertEqual(binary.deserialize(b'\x30\x00\x00\x00\x04\xde\xad\xbe\xef'), b'\xde\xad\xbe\xef')
103 def test_deserializes_utf8(self):
104 self.assertEqual(binary.deserialize(b'\x31\x00\x00\x00\x0cHello, world'), 'Hello, world')
105 self.assertEqual(binary.deserialize(b'\x31\x00\x00\x00\x06\xe4\xb8\x96\xe7\x95\x8c'), '世界')
107 def test_deserializes_lists(self):
108 self.assertEqual(list(binary.deserialize(b'\x40\x12\x00\x00\x00\x00\x00\x00\x00\x00')), [])
109 self.assertEqual(list(binary.deserialize(b'\x40\x12\x00\x00\x00\x0c\x00\x00\x00\x03\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x03')), [1,2,3])
111 def test_deserializes_dictionaries(self):
112 self.assertEqual(binary.deserialize(b'\x41\x00\x00\x00\x00\x00\x00\x00\x00'), collections.OrderedDict([]))
113 self.assertEqual(binary.deserialize(b'\x41\x00\x00\x00\x1b\x00\x00\x00\x02\x00\x00\x00\x03foo\x12\x00\x00\x00\x2a\x00\x00\x00\x03bar\x31\x00\x00\x00\x03baz'), collections.OrderedDict([('foo',42), ('bar','baz')]))
116 class TestStringSerialize(unittest.TestCase):
117 def test_serializes_null(self):
118 self.assertEqual(string.serialize(None), 'null')
120 def test_serializes_true(self):
121 self.assertEqual(string.serialize(True), 'true')
123 def test_serializes_false(self):
124 self.assertEqual(string.serialize(False), 'false')
126 def test_serializes_int8(self):
127 self.assertEqual(string.serialize(tags.TaggedObject(tags.INT8, 1)), '1i8')
128 self.assertEqual(string.serialize(tags.TaggedObject(tags.INT8, -1)), '-1i8')
129 self.assertEqual(string.serialize(tags.TaggedObject(tags.INT8, 42)), '42i8')
131 def test_serializes_int16(self):
132 self.assertEqual(string.serialize(tags.TaggedObject(tags.INT16, 1)), '1i16')
133 self.assertEqual(string.serialize(tags.TaggedObject(tags.INT16, -1)), '-1i16')
134 self.assertEqual(string.serialize(tags.TaggedObject(tags.INT16, 42)), '42i16')
136 def test_serializes_int32(self):
137 self.assertEqual(string.serialize(tags.TaggedObject(tags.INT32, 1)), '1i32')
138 self.assertEqual(string.serialize(tags.TaggedObject(tags.INT32, -1)), '-1i32')
139 self.assertEqual(string.serialize(tags.TaggedObject(tags.INT32, 42)), '42i32')
141 def test_serializes_int64(self):
142 self.assertEqual(string.serialize(tags.TaggedObject(tags.INT64, 1)), '1i64')
143 self.assertEqual(string.serialize(tags.TaggedObject(tags.INT64, -1)), '-1i64')
144 self.assertEqual(string.serialize(tags.TaggedObject(tags.INT64, 42)), '42i64')
146 def test_serializes_float(self):
147 self.assertEqual(string.serialize(tags.TaggedObject(tags.FLOAT, 1.0)), '1.0f')
149 def test_serializes_double(self):
150 self.assertEqual(string.serialize(tags.TaggedObject(tags.DOUBLE, 1.0)), '1.0d')
152 def test_serializes_binary(self):
153 self.assertEqual(string.serialize(tags.TaggedObject(tags.BINARY, b'\xde\xad\xbe\xef')), '"deadbeef"b')
155 def test_serializes_utf8(self):
156 self.assertEqual(string.serialize(tags.TaggedObject(tags.UTF8, 'Hello, world')), '"Hello, world"utf8')
158 def test_serializes_utf16(self):
159 self.assertEqual(string.serialize(tags.TaggedObject(tags.UTF16, 'Hello, world')), '"Hello, world"utf16')
161 def test_serializes_utf32(self):
162 self.assertEqual(string.serialize(tags.TaggedObject(tags.UTF32, 'Hello, world')), '"Hello, world"utf32')
164 def test_serializes_list(self):
165 self.assertEqual(string.serialize(tags.TaggedObject(tags.LIST, [1,2,3])), '[1i32, 2i32, 3i32]')
167 def test_serializes_dictionary(self):
169 string.serialize(tags.TaggedObject(tags.DICTIONARY, collections.OrderedDict([
173 '{ "foo"utf8: 1i32, "bar"utf8: "baz"utf8 }'
176 class TestStringDeserialize(unittest.TestCase):
177 def test_deserializes_null(self):
180 string.deserialize('null'),
183 def test_deserializes_null(self):
186 string.deserialize('true'),
189 def test_deserializes_null(self):
192 string.deserialize('false'),
195 def test_deserializes_int8(self):
196 self.assertEqual(10, string.deserialize('10i8'))
197 self.assertEqual(-1, string.deserialize('-1i8'))
199 def test_deserializes_int16(self):
200 self.assertEqual(10, string.deserialize('10i16'))
201 self.assertEqual(-1, string.deserialize('-1i16'))
203 def test_deserializes_int32(self):
204 self.assertEqual(10, string.deserialize('10i32'))
205 self.assertEqual(-1, string.deserialize('-1i32'))
207 def test_deserializes_int64(self):
208 self.assertEqual(10, string.deserialize('10i64'))
209 self.assertEqual(-1, string.deserialize('-1i64'))
211 def test_deserializes_float(self):
212 self.assertEqual(1.0, string.deserialize('1.0f'))
214 def test_deserializes_double(self):
215 self.assertEqual(1.0, string.deserialize('1.0d'))
217 def test_serializes_binary(self):
220 string.deserialize(b'\x30\x00\x00\x00\x04\xde\xad\xbe\xef'),
223 def test_serializes_binary(self):
226 string.deserialize('"deadbeef"b'),