summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorINADA Naoki <inada-n@klab.com>2013-10-20 15:08:31 +0900
committerINADA Naoki <inada-n@klab.com>2013-10-20 15:08:31 +0900
commit27f0cba8a5f36393517ee85d2c339847b76e0c6b (patch)
tree470bc84905240c82d2e1f4e710d55a288661f635 /test
parent7123341ca89a9a3afee8521cc16a1a419ea8871e (diff)
parent6386481024ec045d9ef991a2c975902276812508 (diff)
downloadmsgpack-python-27f0cba8a5f36393517ee85d2c339847b76e0c6b.tar.gz
Merge branch 'master' of https://github.com/antocuni/msgpack-python into newspec
Conflicts: msgpack/fallback.py msgpack/unpack.h msgpack/unpack_define.h msgpack/unpack_template.h
Diffstat (limited to 'test')
-rw-r--r--test/test_extension.py64
-rw-r--r--test/test_sequnpack.py15
2 files changed, 78 insertions, 1 deletions
diff --git a/test/test_extension.py b/test/test_extension.py
new file mode 100644
index 0000000..94117e1
--- /dev/null
+++ b/test/test_extension.py
@@ -0,0 +1,64 @@
+import py
+import array
+import struct
+import msgpack
+
+def test_pack_extended_type():
+ def p(s):
+ packer = msgpack.Packer()
+ packer.pack_extended_type(0x42, s)
+ return packer.bytes()
+ assert p('A') == '\xd4\x42A' # fixext 1
+ assert p('AB') == '\xd5\x42AB' # fixext 2
+ assert p('ABCD') == '\xd6\x42ABCD' # fixext 4
+ assert p('ABCDEFGH') == '\xd7\x42ABCDEFGH' # fixext 8
+ assert p('A'*16) == '\xd8\x42' + 'A'*16 # fixext 16
+ assert p('ABC') == '\xc7\x03\x42ABC' # ext 8
+ assert p('A'*0x0123) == '\xc8\x01\x23\x42' + 'A'*0x0123 # ext 16
+ assert p('A'*0x00012345) == '\xc9\x00\x01\x23\x45\x42' + 'A'*0x00012345 # ext 32
+
+def test_unpack_extended_type():
+ class MyUnpacker(msgpack.Unpacker):
+ def read_extended_type(self, typecode, data):
+ return (typecode, data)
+
+ def u(s):
+ unpacker = MyUnpacker()
+ unpacker.feed(s)
+ return unpacker.unpack_one()
+
+ assert u('\xd4\x42A') == (0x42, 'A') # fixext 1
+ assert u('\xd5\x42AB') == (0x42, 'AB') # fixext 2
+ assert u('\xd6\x42ABCD') == (0x42, 'ABCD') # fixext 4
+ assert u('\xd7\x42ABCDEFGH') == (0x42, 'ABCDEFGH') # fixext 8
+ assert u('\xd8\x42' + 'A'*16) == (0x42, 'A'*16) # fixext 16
+ assert u('\xc7\x03\x42ABC') == (0x42, 'ABC') # ext 8
+ assert (u('\xc8\x01\x23\x42' + 'A'*0x0123) ==
+ (0x42, 'A'*0x0123)) # ext 16
+ assert (u('\xc9\x00\x01\x23\x45\x42' + 'A'*0x00012345) ==
+ (0x42, 'A'*0x00012345)) # ext 32
+
+
+def test_extension_type():
+ class MyPacker(msgpack.Packer):
+ def handle_unknown_type(self, obj):
+ if isinstance(obj, array.array):
+ typecode = 123 # application specific typecode
+ data = obj.tostring()
+ self.pack_extended_type(typecode, data)
+ return True
+
+ class MyUnpacker(msgpack.Unpacker):
+ def read_extended_type(self, typecode, data):
+ assert typecode == 123
+ obj = array.array('d')
+ obj.fromstring(data)
+ return obj
+
+ obj = [42, 'hello', array.array('d', [1.1, 2.2, 3.3])]
+ packer = MyPacker()
+ unpacker = MyUnpacker(None)
+ s = packer.pack(obj)
+ unpacker.feed(s)
+ obj2 = unpacker.unpack_one()
+ assert obj == obj2
diff --git a/test/test_sequnpack.py b/test/test_sequnpack.py
index 9db14ca..abc447a 100644
--- a/test/test_sequnpack.py
+++ b/test/test_sequnpack.py
@@ -1,9 +1,10 @@
#!/usr/bin/env python
# coding: utf-8
+import py
import six
from msgpack import Unpacker, BufferFull
-from msgpack.exceptions import OutOfData
+from msgpack.exceptions import OutOfData, ExtraData, UnpackValueError
from pytest import raises
@@ -85,3 +86,15 @@ def test_readbytes():
assert unpacker.unpack() == ord(b'a')
assert unpacker.unpack() == ord(b'r')
+def test_unpack_one():
+ unpacker = Unpacker()
+ unpacker.feed('\xda\x00\x03abc')
+ assert unpacker.unpack_one() == 'abc'
+ #
+ unpacker = Unpacker()
+ unpacker.feed('\xda\x00\x03abcd')
+ py.test.raises(ExtraData, "unpacker.unpack_one()")
+ #
+ unpacker = Unpacker()
+ unpacker.feed('\xda\x00\x03ab')
+ py.test.raises(UnpackValueError, "unpacker.unpack_one()")