summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/test_extension.py57
-rw-r--r--test/test_newspec.py23
-rw-r--r--test/test_obj.py2
-rw-r--r--test/test_sequnpack.py1
4 files changed, 79 insertions, 4 deletions
diff --git a/test/test_extension.py b/test/test_extension.py
new file mode 100644
index 0000000..2f85ce3
--- /dev/null
+++ b/test/test_extension.py
@@ -0,0 +1,57 @@
+from __future__ import print_function
+import array
+import msgpack
+from msgpack import ExtType
+
+
+def test_pack_ext_type():
+ def p(s):
+ packer = msgpack.Packer()
+ packer.pack_ext_type(0x42, s)
+ return packer.bytes()
+ assert p(b'A') == b'\xd4\x42A' # fixext 1
+ assert p(b'AB') == b'\xd5\x42AB' # fixext 2
+ assert p(b'ABCD') == b'\xd6\x42ABCD' # fixext 4
+ assert p(b'ABCDEFGH') == b'\xd7\x42ABCDEFGH' # fixext 8
+ assert p(b'A'*16) == b'\xd8\x42' + b'A'*16 # fixext 16
+ assert p(b'ABC') == b'\xc7\x03\x42ABC' # ext 8
+ assert p(b'A'*0x0123) == b'\xc8\x01\x23\x42' + b'A'*0x0123 # ext 16
+ assert p(b'A'*0x00012345) == b'\xc9\x00\x01\x23\x45\x42' + b'A'*0x00012345 # ext 32
+
+
+def test_unpack_ext_type():
+ def check(b, expected):
+ assert msgpack.unpackb(b) == expected
+
+ check(b'\xd4\x42A', ExtType(0x42, b'A')) # fixext 1
+ check(b'\xd5\x42AB', ExtType(0x42, b'AB')) # fixext 2
+ check(b'\xd6\x42ABCD', ExtType(0x42, b'ABCD')) # fixext 4
+ check(b'\xd7\x42ABCDEFGH', ExtType(0x42, b'ABCDEFGH')) # fixext 8
+ check(b'\xd8\x42' + b'A'*16, ExtType(0x42, b'A'*16)) # fixext 16
+ check(b'\xc7\x03\x42ABC', ExtType(0x42, b'ABC')) # ext 8
+ check(b'\xc8\x01\x23\x42' + b'A'*0x0123,
+ ExtType(0x42, b'A'*0x0123)) # ext 16
+ check(b'\xc9\x00\x01\x23\x45\x42' + b'A'*0x00012345,
+ ExtType(0x42, b'A'*0x00012345)) # ext 32
+
+
+def test_extension_type():
+ def default(obj):
+ print('default called', obj)
+ if isinstance(obj, array.array):
+ typecode = 123 # application specific typecode
+ data = obj.tostring()
+ return ExtType(typecode, data)
+ raise TypeError("Unknwon type object %r" % (obj,))
+
+ def ext_hook(code, data):
+ print('ext_hook called', code, data)
+ assert code == 123
+ obj = array.array('d')
+ obj.fromstring(data)
+ return obj
+
+ obj = [42, b'hello', array.array('d', [1.1, 2.2, 3.3])]
+ s = msgpack.packb(obj, default=default)
+ obj2 = msgpack.unpackb(s, ext_hook=ext_hook)
+ assert obj == obj2
diff --git a/test/test_newspec.py b/test/test_newspec.py
index 8bc2cfe..ab05029 100644
--- a/test/test_newspec.py
+++ b/test/test_newspec.py
@@ -1,6 +1,6 @@
# coding: utf-8
-from msgpack import packb, unpackb
+from msgpack import packb, unpackb, ExtType
def test_str8():
@@ -66,4 +66,23 @@ def test_bin32():
assert b[5:] == data
assert unpackb(b) == data
-
+def test_ext():
+ def check(ext, packed):
+ assert packb(ext) == packed
+ assert unpackb(packed) == ext
+ check(ExtType(0x42, b'Z'), b'\xd4\x42Z') # fixext 1
+ check(ExtType(0x42, b'ZZ'), b'\xd5\x42ZZ') # fixext 2
+ check(ExtType(0x42, b'Z'*4), b'\xd6\x42' + b'Z'*4) # fixext 4
+ check(ExtType(0x42, b'Z'*8), b'\xd7\x42' + b'Z'*8) # fixext 8
+ check(ExtType(0x42, b'Z'*16), b'\xd8\x42' + b'Z'*16) # fixext 16
+ # ext 8
+ check(ExtType(0x42, b''), b'\xc7\x00\x42')
+ check(ExtType(0x42, b'Z'*255), b'\xc7\xff\x42' + b'Z'*255)
+ # ext 16
+ check(ExtType(0x42, b'Z'*256), b'\xc8\x01\x00\x42' + b'Z'*256)
+ check(ExtType(0x42, b'Z'*0xffff), b'\xc8\xff\xff\x42' + b'Z'*0xffff)
+ # ext 32
+ check(ExtType(0x42, b'Z'*0x10000), b'\xc9\x00\x01\x00\x00\x42' + b'Z'*0x10000)
+ # needs large memory
+ #check(ExtType(0x42, b'Z'*0xffffffff),
+ # b'\xc9\xff\xff\xff\xff\x42' + b'Z'*0xffffffff)
diff --git a/test/test_obj.py b/test/test_obj.py
index fbf610c..9083218 100644
--- a/test/test_obj.py
+++ b/test/test_obj.py
@@ -35,7 +35,7 @@ def test_only_one_obj_hook():
unpackb(b'', object_hook=lambda x: x, object_pairs_hook=lambda x: x)
def test_bad_hook():
- with raises(ValueError):
+ with raises(TypeError):
packed = packb([3, 1+2j], default=lambda o: o)
unpacked = unpackb(packed, use_list=1)
diff --git a/test/test_sequnpack.py b/test/test_sequnpack.py
index 9db14ca..f541207 100644
--- a/test/test_sequnpack.py
+++ b/test/test_sequnpack.py
@@ -84,4 +84,3 @@ def test_readbytes():
assert unpacker.read_bytes(3) == b'oob'
assert unpacker.unpack() == ord(b'a')
assert unpacker.unpack() == ord(b'r')
-