Simplified tests, added deserialization for unsigned integers
authorDavid Kerkeslager <kerkeslager@gmail.com>
Sat, 24 Sep 2016 21:22:59 +0000 (17:22 -0400)
committerDavid Kerkeslager <kerkeslager@gmail.com>
Sat, 24 Sep 2016 21:22:59 +0000 (17:22 -0400)
serial/binary.py
serial/test_binary.py

index f6bcff7..c4693db 100644 (file)
@@ -56,10 +56,26 @@ def _make_tag_only_parser(tag, value):
 
     return parser
 
+def _make_struct_deserializer(tag, fmt):
+    fmt = '!' + fmt
+    size = struct.calcsize(fmt)
+    unpacker = functools.partial(struct.unpack, fmt)
+
+    def parser(b):
+        b = b.read(size)
+        assert len(b) == size
+        return TaggedObject(tag = tag, instance = unpacker(b)[0])
+
+    return parser
+
 _TAGS_TO_PARSERS = {
     TAG_NULL: _make_tag_only_parser(TAG_NULL, None),
     TAG_TRUE: _make_tag_only_parser(TAG_TRUE, True),
     TAG_FALSE: _make_tag_only_parser(TAG_FALSE, False),
+    TAG_UINT8: _make_struct_deserializer(TAG_UINT8, 'B'),
+    TAG_UINT16: _make_struct_deserializer(TAG_UINT16, 'H'),
+    TAG_UINT32: _make_struct_deserializer(TAG_UINT32, 'I'),
+    TAG_UINT64: _make_struct_deserializer(TAG_UINT64, 'Q'),
 }
 
 def deserialize(b):
index 2223ef8..e72447f 100644 (file)
@@ -2,83 +2,36 @@ import unittest
 
 import binary
 
-class SerializeTests(unittest.TestCase):
-    def test_serializes_tag_only_types(self):
-        self.assertEqual(
-            binary.serialize(binary.TaggedObject(
-                tag = binary.TAG_NULL,
-                instance = None,
-            )),
-            b'\x00',
-        )
-        self.assertEqual(
-            binary.serialize(binary.TaggedObject(
-                tag = binary.TAG_TRUE,
-                instance = True,
-            )),
-            b'\x01',
-        )
-        self.assertEqual(
-            binary.serialize(binary.TaggedObject(
-                tag = binary.TAG_FALSE,
-                instance = False,
-            )),
-            b'\x02',
-        )
-
-    def test_serializes_unsigned_integer_types(self):
-        self.assertEqual(
-            binary.serialize(binary.TaggedObject(
-                tag = binary.TAG_UINT8,
-                instance = 7,
-            )),
-            b'\x03\x07',
-        )
-        self.assertEqual(
-            binary.serialize(binary.TaggedObject(
-                tag = binary.TAG_UINT16,
-                instance = 7,
-            )),
-            b'\x04\x00\x07',
-        )
-        self.assertEqual(
-            binary.serialize(binary.TaggedObject(
-                tag = binary.TAG_UINT32,
-                instance = 7,
-            )),
-            b'\x05\x00\x00\x00\x07',
-        )
-        self.assertEqual(
-            binary.serialize(binary.TaggedObject(
-                tag = binary.TAG_UINT64,
-                instance = 7,
-            )),
-            b'\x06\x00\x00\x00\x00\x00\x00\x00\x07',
-        )
+EXAMPLE_REPRESENTATIONS = [
+    (binary.TAG_NULL, None, b'\x00'),
+    (binary.TAG_TRUE, True, b'\x01'),
+    (binary.TAG_FALSE, False, b'\x02'),
+    (binary.TAG_UINT8, 7, b'\x03\x07'),
+    (binary.TAG_UINT16, 7, b'\x04\x00\x07'),
+    (binary.TAG_UINT32, 7, b'\x05\x00\x00\x00\x07'),
+    (binary.TAG_UINT64, 7, b'\x06\x00\x00\x00\x00\x00\x00\x00\x07'),
+]
 
+class SerializeTests(unittest.TestCase):
+    def test_serialize(self):
+        for tag, instance, representation in EXAMPLE_REPRESENTATIONS:
+            self.assertEqual(
+                binary.serialize(binary.TaggedObject(
+                    tag = tag,
+                    instance = instance,
+                )),
+                representation,
+            )
 
 class DeserializeTests(unittest.TestCase):
-    def test_deserializes_tag_only_types(self):
-        self.assertEqual(
-            binary.deserialize(b'\x00'),
-            binary.TaggedObject(
-                tag = binary.TAG_NULL,
-                instance = None,
-            ),
-        )
-        self.assertEqual(
-            binary.deserialize(b'\x01'),
-            binary.TaggedObject(
-                tag = binary.TAG_TRUE,
-                instance = True,
-            ),
-        )
-        self.assertEqual(
-            binary.deserialize(b'\x02'),
-            binary.TaggedObject(
-                tag = binary.TAG_FALSE,
-                instance = False,
+    def test_deserialize(self):
+        for tag, instance, representation in EXAMPLE_REPRESENTATIONS:
+            self.assertEqual(
+                binary.deserialize(representation),
+                binary.TaggedObject(
+                    tag = tag,
+                    instance = instance,
+                ),
             )
-        )
 
 unittest.main()