4 from don import string, tags
6 class TestStringSerialize(unittest.TestCase):
7 def test_serializes_null(self):
8 self.assertEqual(string.serialize(None), 'null')
10 def test_serializes_true(self):
11 self.assertEqual(string.serialize(True), 'true')
13 def test_serializes_false(self):
14 self.assertEqual(string.serialize(False), 'false')
16 def test_serializes_int8(self):
17 self.assertEqual(string.serialize(tags.TaggedObject(tags.INT8, 1)), '1i8')
18 self.assertEqual(string.serialize(tags.TaggedObject(tags.INT8, -1)), '-1i8')
19 self.assertEqual(string.serialize(tags.TaggedObject(tags.INT8, 42)), '42i8')
21 def test_serializes_int16(self):
22 self.assertEqual(string.serialize(tags.TaggedObject(tags.INT16, 1)), '1i16')
23 self.assertEqual(string.serialize(tags.TaggedObject(tags.INT16, -1)), '-1i16')
24 self.assertEqual(string.serialize(tags.TaggedObject(tags.INT16, 42)), '42i16')
26 def test_serializes_int32(self):
27 self.assertEqual(string.serialize(tags.TaggedObject(tags.INT32, 1)), '1i32')
28 self.assertEqual(string.serialize(tags.TaggedObject(tags.INT32, -1)), '-1i32')
29 self.assertEqual(string.serialize(tags.TaggedObject(tags.INT32, 42)), '42i32')
31 def test_serializes_int64(self):
32 self.assertEqual(string.serialize(tags.TaggedObject(tags.INT64, 1)), '1i64')
33 self.assertEqual(string.serialize(tags.TaggedObject(tags.INT64, -1)), '-1i64')
34 self.assertEqual(string.serialize(tags.TaggedObject(tags.INT64, 42)), '42i64')
36 def test_serializes_binary(self):
37 self.assertEqual(string.serialize(tags.TaggedObject(tags.BINARY, b'\xde\xad\xbe\xef')), '"deadbeef"b')
39 def test_serializes_utf8(self):
40 self.assertEqual(string.serialize(tags.TaggedObject(tags.UTF8, 'Hello, world')), '"Hello, world"utf8')
42 def test_serializes_utf16(self):
43 self.assertEqual(string.serialize(tags.TaggedObject(tags.UTF16, 'Hello, world')), '"Hello, world"utf16')
45 def test_serializes_utf32(self):
46 self.assertEqual(string.serialize(tags.TaggedObject(tags.UTF32, 'Hello, world')), '"Hello, world"utf32')
48 def test_serializes_list(self):
49 self.assertEqual(string.serialize(tags.TaggedObject(tags.LIST, [1,2,3])), '[1i32, 2i32, 3i32]')
51 def test_serializes_dictionary(self):
53 string.serialize(tags.TaggedObject(tags.DICTIONARY, collections.OrderedDict([
57 '{ "foo"utf8: 1i32, "bar"utf8: "baz"utf8 }'
60 class TestStringDeserialize(unittest.TestCase):
61 def test_deserializes_null(self):
64 string.deserialize('null'),
67 def test_deserializes_null_with_leading_whitespace(self):
70 string.deserialize(' \t\nnull'),
73 def test_deserializes_true(self):
76 string.deserialize('true'),
79 def test_deserializes_true_with_leading_whitespace(self):
82 string.deserialize(' \t\ntrue'),
85 def test_deserializes_false(self):
88 string.deserialize('false'),
91 def test_deserializes_false_with_leading_whitespace(self):
94 string.deserialize(' \t\nfalse'),
97 def test_deserializes_int8(self):
98 self.assertEqual(10, string.deserialize('10i8'))
99 self.assertEqual(-1, string.deserialize('-1i8'))
101 def test_deserializes_int8_with_leading_whitespace(self):
102 self.assertEqual(10, string.deserialize(' \t\n10i8'))
103 self.assertEqual(-1, string.deserialize(' \t\n-1i8'))
105 def test_deserializes_int16(self):
106 self.assertEqual(10, string.deserialize('10i16'))
107 self.assertEqual(-1, string.deserialize('-1i16'))
109 def test_deserializes_int16_with_leading_whitespace(self):
110 self.assertEqual(10, string.deserialize(' \t\n10i16'))
111 self.assertEqual(-1, string.deserialize(' \t\n-1i16'))
113 def test_deserializes_int32(self):
114 self.assertEqual(10, string.deserialize('10i32'))
115 self.assertEqual(-1, string.deserialize('-1i32'))
117 def test_deserializes_int32_with_leading_whitespace(self):
118 self.assertEqual(10, string.deserialize(' \t\n10i32'))
119 self.assertEqual(-1, string.deserialize(' \t\n-1i32'))
121 def test_deserializes_int64(self):
122 self.assertEqual(10, string.deserialize('10i64'))
123 self.assertEqual(-1, string.deserialize('-1i64'))
125 def test_deserializes_int64_with_leading_whitespace(self):
126 self.assertEqual(10, string.deserialize(' \t\n10i64'))
127 self.assertEqual(-1, string.deserialize(' \t\n-1i64'))
129 def test_deserializes_binary(self):
132 string.deserialize('"deadbeef"b'),
135 def test_deserializes_binary_with_leading_whitespace(self):
138 string.deserialize(' \t\n"deadbeef"b'),
141 def test_deserializes_utf8(self):
144 string.deserialize('"Hello, world"utf8'),
147 def test_deserializes_utf16(self):
150 string.deserialize('"Hello, world"utf16'),
153 def test_deserializes_utf32(self):
156 string.deserialize('"Hello, world"utf32'),
159 def test_deserializes_utf8_with_leading_whitespace(self):
162 string.deserialize(' \t\n"Hello, world"utf8'),
165 def test_deserializes_utf16_with_leading_whitespace(self):
168 string.deserialize(' \t\n"Hello, world"utf16'),
171 def test_deserializes_utf32_with_leading_whitespace(self):
174 string.deserialize(' \t\n"Hello, world"utf32'),
177 def test_deserializes_list(self):
180 string.deserialize("[1i8,2i8,3i8,4i8,5i8]"),
183 def test_deserializes_list_with_leading_whitespace(self):
186 string.deserialize(" \t\n[ \t\n1i8 \t\n, \t\n2i8 \t\n, \t\n3i8 \t\n, \t\n4i8 \t\n, \t\n5i8 \t\n]"),
189 def test_deserializes_dictionary(self):
191 collections.OrderedDict([
195 string.deserialize('{"foo"utf8:1i32,"bar"utf8:"baz"utf8}'),
198 def test_deserializes_dictionary_with_leading_whitespace(self):
200 collections.OrderedDict([
204 string.deserialize(' \t\n{ \t\n"foo"utf8 \t\n: \t\n1i32 \t\n, \t\n"bar"utf8 \t\n: \t\n"baz"utf8 \t\n}'),