Removed support for float and double, use autotag to tag in serializers
[ton] / test_string.py
1 import collections
2 import unittest
3
4 from don import string, tags
5
6 class TestStringSerialize(unittest.TestCase):
7     def test_serializes_null(self):
8         self.assertEqual(string.serialize(None), 'null')
9
10     def test_serializes_true(self):
11         self.assertEqual(string.serialize(True), 'true')
12
13     def test_serializes_false(self):
14         self.assertEqual(string.serialize(False), 'false')
15
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')
20
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')
25
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')
30
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')
35
36     def test_serializes_binary(self):
37         self.assertEqual(string.serialize(tags.TaggedObject(tags.BINARY, b'\xde\xad\xbe\xef')), '"deadbeef"b')
38
39     def test_serializes_utf8(self):
40         self.assertEqual(string.serialize(tags.TaggedObject(tags.UTF8, 'Hello, world')), '"Hello, world"utf8')
41
42     def test_serializes_utf16(self):
43         self.assertEqual(string.serialize(tags.TaggedObject(tags.UTF16, 'Hello, world')), '"Hello, world"utf16')
44
45     def test_serializes_utf32(self):
46         self.assertEqual(string.serialize(tags.TaggedObject(tags.UTF32, 'Hello, world')), '"Hello, world"utf32')
47
48     def test_serializes_list(self):
49         self.assertEqual(string.serialize(tags.TaggedObject(tags.LIST, [1,2,3])), '[1i32, 2i32, 3i32]')
50
51     def test_serializes_dictionary(self):
52         self.assertEqual(
53             string.serialize(tags.TaggedObject(tags.DICTIONARY, collections.OrderedDict([
54                 ('foo', 1),
55                 ('bar', 'baz'),
56             ]))),
57             '{ "foo"utf8: 1i32, "bar"utf8: "baz"utf8 }'
58         )
59
60 class TestStringDeserialize(unittest.TestCase):
61     def test_deserializes_null(self):
62         self.assertEqual(
63             None,
64             string.deserialize('null'),
65         )
66
67     def test_deserializes_null_with_leading_whitespace(self):
68         self.assertEqual(
69             None,
70             string.deserialize(' \t\nnull'),
71         )
72
73     def test_deserializes_true(self):
74         self.assertEqual(
75             True,
76             string.deserialize('true'),
77         )
78
79     def test_deserializes_true_with_leading_whitespace(self):
80         self.assertEqual(
81             True,
82             string.deserialize(' \t\ntrue'),
83         )
84
85     def test_deserializes_false(self):
86         self.assertEqual(
87             False,
88             string.deserialize('false'),
89         )
90
91     def test_deserializes_false_with_leading_whitespace(self):
92         self.assertEqual(
93             False,
94             string.deserialize(' \t\nfalse'),
95         )
96
97     def test_deserializes_int8(self):
98         self.assertEqual(10, string.deserialize('10i8'))
99         self.assertEqual(-1, string.deserialize('-1i8'))
100
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'))
104
105     def test_deserializes_int16(self):
106         self.assertEqual(10, string.deserialize('10i16'))
107         self.assertEqual(-1, string.deserialize('-1i16'))
108
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'))
112
113     def test_deserializes_int32(self):
114         self.assertEqual(10, string.deserialize('10i32'))
115         self.assertEqual(-1, string.deserialize('-1i32'))
116
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'))
120
121     def test_deserializes_int64(self):
122         self.assertEqual(10, string.deserialize('10i64'))
123         self.assertEqual(-1, string.deserialize('-1i64'))
124
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'))
128
129     def test_deserializes_binary(self):
130         self.assertEqual(
131             b'\xde\xad\xbe\xef',
132             string.deserialize('"deadbeef"b'),
133         )
134
135     def test_deserializes_binary_with_leading_whitespace(self):
136         self.assertEqual(
137             b'\xde\xad\xbe\xef',
138             string.deserialize(' \t\n"deadbeef"b'),
139         )
140
141     def test_deserializes_utf8(self):
142         self.assertEqual(
143             "Hello, world",
144             string.deserialize('"Hello, world"utf8'),
145         )
146
147     def test_deserializes_utf16(self):
148         self.assertEqual(
149             "Hello, world",
150             string.deserialize('"Hello, world"utf16'),
151         )
152
153     def test_deserializes_utf32(self):
154         self.assertEqual(
155             "Hello, world",
156             string.deserialize('"Hello, world"utf32'),
157         )
158
159     def test_deserializes_utf8_with_leading_whitespace(self):
160         self.assertEqual(
161             "Hello, world",
162             string.deserialize(' \t\n"Hello, world"utf8'),
163         )
164
165     def test_deserializes_utf16_with_leading_whitespace(self):
166         self.assertEqual(
167             "Hello, world",
168             string.deserialize(' \t\n"Hello, world"utf16'),
169         )
170
171     def test_deserializes_utf32_with_leading_whitespace(self):
172         self.assertEqual(
173             "Hello, world",
174             string.deserialize(' \t\n"Hello, world"utf32'),
175         )
176
177     def test_deserializes_list(self):
178         self.assertEqual(
179             [1,2,3,4,5],
180             string.deserialize("[1i8,2i8,3i8,4i8,5i8]"),
181         )
182
183     def test_deserializes_list_with_leading_whitespace(self):
184         self.assertEqual(
185             [1,2,3,4,5],
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]"),
187         )
188
189     def test_deserializes_dictionary(self):
190         self.assertEqual(
191             collections.OrderedDict([
192                 ('foo', 1),
193                 ('bar', 'baz'),
194             ]),
195             string.deserialize('{"foo"utf8:1i32,"bar"utf8:"baz"utf8}'),
196         )
197
198     def test_deserializes_dictionary_with_leading_whitespace(self):
199         self.assertEqual(
200             collections.OrderedDict([
201                 ('foo', 1),
202                 ('bar', 'baz'),
203             ]),
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}'),
205         )
206
207 unittest.main()