From 6fa609be3fc6cf771bb880bf8c844f874c7acffb Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Tue, 26 Oct 2010 01:31:27 +0900 Subject: [PATCH] Add test for Python3. --- python/test3/test_obj.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 python/test3/test_obj.py diff --git a/python/test3/test_obj.py b/python/test3/test_obj.py new file mode 100644 index 00000000..972d1ca4 --- /dev/null +++ b/python/test3/test_obj.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python +# coding: utf-8 + +from nose import main +from nose.tools import * + +from msgpack import packs, unpacks + +def _decode_complex(obj): + if b'__complex__' in obj: + return complex(obj[b'real'], obj[b'imag']) + return obj + +def _encode_complex(obj): + if isinstance(obj, complex): + return {b'__complex__': True, b'real': 1, b'imag': 2} + return obj + +def test_encode_hook(): + packed = packs([3, 1+2j], default=_encode_complex) + unpacked = unpacks(packed) + eq_(unpacked[1], {b'__complex__': True, b'real': 1, b'imag': 2}) + +def test_decode_hook(): + packed = packs([3, {b'__complex__': True, b'real': 1, b'imag': 2}]) + unpacked = unpacks(packed, object_hook=_decode_complex) + eq_(unpacked[1], 1+2j) + +if __name__ == '__main__': + #main() + test_decode_hook()