summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorINADA Naoki <inada-n@klab.com>2012-09-24 03:05:39 +0900
committerINADA Naoki <inada-n@klab.com>2012-09-24 03:05:39 +0900
commite38103264176d3f8e715d206e2d9d4155eb5b121 (patch)
tree81b2ed40642a5657e9aeef29adcf665e3daae4e1 /test
parent927d29131dc8d2a9f606cf7c881606d47ace557b (diff)
parent77942514db0c5a80e9f3f9bcb1e1939ecc8705e6 (diff)
downloadmsgpack-python-e38103264176d3f8e715d206e2d9d4155eb5b121.tar.gz
Support object_pairs_hook
Merge remote-tracking branch 'jnothman/object_pairs_hook' into 0.2-maint Conflicts: msgpack/_msgpack.pyx test/test_pack.py test/test_sequnpack.py
Diffstat (limited to 'test')
-rw-r--r--test/test_obj.py10
-rw-r--r--test/test_pack.py7
-rw-r--r--test/test_sequnpack.py15
3 files changed, 28 insertions, 4 deletions
diff --git a/test/test_obj.py b/test/test_obj.py
index d809093..12a149f 100644
--- a/test/test_obj.py
+++ b/test/test_obj.py
@@ -26,6 +26,16 @@ def test_decode_hook():
unpacked = unpackb(packed, object_hook=_decode_complex, use_list=1)
eq_(unpacked[1], 1+2j)
+def test_decode_pairs_hook():
+ packed = packb([3, {1: 2, 3: 4}])
+ prod_sum = 1 * 2 + 3 * 4
+ unpacked = unpackb(packed, object_pairs_hook=lambda l: sum(k * v for k, v in l))
+ eq_(unpacked[1], prod_sum)
+
+@raises(ValueError)
+def test_only_one_obj_hook():
+ unpackb(b'', object_hook=lambda x: x, object_pairs_hook=lambda x: x)
+
@raises(ValueError)
def test_bad_hook():
packed = packb([3, 1+2j], default=lambda o: o)
diff --git a/test/test_pack.py b/test/test_pack.py
index 9bd2b32..9009d35 100644
--- a/test/test_pack.py
+++ b/test/test_pack.py
@@ -110,10 +110,9 @@ def test_odict():
seq = [(b'one', 1), (b'two', 2), (b'three', 3), (b'four', 4)]
od = odict(seq)
assert_equal(unpackb(packb(od), use_list=1), dict(seq))
- # After object_pairs_hook is implemented.
- #def pair_hook(seq):
- # return seq
- #assert_equal(unpackb(packb(od), object_pairs_hook=pair_hook), seq)
+ def pair_hook(seq):
+ return seq
+ assert_equal(unpackb(packb(od), object_pairs_hook=pair_hook), seq)
if __name__ == '__main__':
diff --git a/test/test_sequnpack.py b/test/test_sequnpack.py
index 21fc3be..dac36a8 100644
--- a/test/test_sequnpack.py
+++ b/test/test_sequnpack.py
@@ -28,6 +28,21 @@ def test_foobar():
k += 1
assert k == len(b'foobar')
+def test_foobar_skip():
+ unpacker = Unpacker(read_size=3, use_list=1)
+ unpacker.feed(b'foobar')
+ assert unpacker.unpack() == ord(b'f')
+ unpacker.skip()
+ assert unpacker.unpack() == ord(b'o')
+ unpacker.skip()
+ assert unpacker.unpack() == ord(b'a')
+ unpacker.skip()
+ try:
+ o = unpacker.unpack()
+ assert 0, "should raise exception"
+ except StopIteration:
+ assert 1, "ok"
+
def test_maxbuffersize():
nose.tools.assert_raises(ValueError, Unpacker, read_size=5, max_buffer_size=3)
unpacker = Unpacker(read_size=3, max_buffer_size=3, use_list=1)