mirror of
https://github.com/msgpack/msgpack-python.git
synced 2025-10-20 04:13:16 +00:00
unify tests for py2 and py3
This commit is contained in:
parent
76f34667a0
commit
0b38e86534
14 changed files with 85 additions and 462 deletions
|
@ -7,10 +7,10 @@ from msgpack import packb, unpackb
|
||||||
|
|
||||||
def test_unpack_buffer():
|
def test_unpack_buffer():
|
||||||
from array import array
|
from array import array
|
||||||
buf = array('c')
|
buf = array('b')
|
||||||
buf.fromstring(packb(('foo', 'bar')))
|
buf.fromstring(packb(('foo', 'bar')))
|
||||||
obj = unpackb(buf)
|
obj = unpackb(buf)
|
||||||
assert_equal(('foo', 'bar'), obj)
|
assert_equal((b'foo', b'bar'), obj)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
|
@ -40,7 +40,7 @@ def test_9():
|
||||||
|
|
||||||
|
|
||||||
def check_raw(overhead, num):
|
def check_raw(overhead, num):
|
||||||
check(num + overhead, " " * num)
|
check(num + overhead, b" " * num)
|
||||||
|
|
||||||
def test_fixraw():
|
def test_fixraw():
|
||||||
check_raw(1, 0)
|
check_raw(1, 0)
|
||||||
|
@ -75,31 +75,31 @@ def match(obj, buf):
|
||||||
|
|
||||||
def test_match():
|
def test_match():
|
||||||
cases = [
|
cases = [
|
||||||
(None, '\xc0'),
|
(None, b'\xc0'),
|
||||||
(False, '\xc2'),
|
(False, b'\xc2'),
|
||||||
(True, '\xc3'),
|
(True, b'\xc3'),
|
||||||
(0, '\x00'),
|
(0, b'\x00'),
|
||||||
(127, '\x7f'),
|
(127, b'\x7f'),
|
||||||
(128, '\xcc\x80'),
|
(128, b'\xcc\x80'),
|
||||||
(256, '\xcd\x01\x00'),
|
(256, b'\xcd\x01\x00'),
|
||||||
(-1, '\xff'),
|
(-1, b'\xff'),
|
||||||
(-33, '\xd0\xdf'),
|
(-33, b'\xd0\xdf'),
|
||||||
(-129, '\xd1\xff\x7f'),
|
(-129, b'\xd1\xff\x7f'),
|
||||||
({1:1}, '\x81\x01\x01'),
|
({1:1}, b'\x81\x01\x01'),
|
||||||
(1.0, "\xcb\x3f\xf0\x00\x00\x00\x00\x00\x00"),
|
(1.0, b"\xcb\x3f\xf0\x00\x00\x00\x00\x00\x00"),
|
||||||
((), '\x90'),
|
((), b'\x90'),
|
||||||
(tuple(range(15)),"\x9f\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e"),
|
(tuple(range(15)),b"\x9f\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e"),
|
||||||
(tuple(range(16)),"\xdc\x00\x10\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"),
|
(tuple(range(16)),b"\xdc\x00\x10\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"),
|
||||||
({}, '\x80'),
|
({}, b'\x80'),
|
||||||
(dict([(x,x) for x in range(15)]), '\x8f\x00\x00\x01\x01\x02\x02\x03\x03\x04\x04\x05\x05\x06\x06\x07\x07\x08\x08\t\t\n\n\x0b\x0b\x0c\x0c\r\r\x0e\x0e'),
|
(dict([(x,x) for x in range(15)]), b'\x8f\x00\x00\x01\x01\x02\x02\x03\x03\x04\x04\x05\x05\x06\x06\x07\x07\x08\x08\t\t\n\n\x0b\x0b\x0c\x0c\r\r\x0e\x0e'),
|
||||||
(dict([(x,x) for x in range(16)]), '\xde\x00\x10\x00\x00\x01\x01\x02\x02\x03\x03\x04\x04\x05\x05\x06\x06\x07\x07\x08\x08\t\t\n\n\x0b\x0b\x0c\x0c\r\r\x0e\x0e\x0f\x0f'),
|
(dict([(x,x) for x in range(16)]), b'\xde\x00\x10\x00\x00\x01\x01\x02\x02\x03\x03\x04\x04\x05\x05\x06\x06\x07\x07\x08\x08\t\t\n\n\x0b\x0b\x0c\x0c\r\r\x0e\x0e\x0f\x0f'),
|
||||||
]
|
]
|
||||||
|
|
||||||
for v, p in cases:
|
for v, p in cases:
|
||||||
match(v, p)
|
match(v, p)
|
||||||
|
|
||||||
def test_unicode():
|
def test_unicode():
|
||||||
assert_equal('foobar', unpacks(packs(u'foobar')))
|
assert_equal(b'foobar', unpacks(packs('foobar')))
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
|
@ -9,65 +9,65 @@ def check(src, should):
|
||||||
assert_equal(unpacks(src), should)
|
assert_equal(unpacks(src), should)
|
||||||
|
|
||||||
def testSimpleValue():
|
def testSimpleValue():
|
||||||
check("\x93\xc0\xc2\xc3",
|
check(b"\x93\xc0\xc2\xc3",
|
||||||
(None, False, True,))
|
(None, False, True,))
|
||||||
|
|
||||||
def testFixnum():
|
def testFixnum():
|
||||||
check("\x92\x93\x00\x40\x7f\x93\xe0\xf0\xff",
|
check(b"\x92\x93\x00\x40\x7f\x93\xe0\xf0\xff",
|
||||||
((0,64,127,), (-32,-16,-1,),)
|
((0,64,127,), (-32,-16,-1,),)
|
||||||
)
|
)
|
||||||
|
|
||||||
def testFixArray():
|
def testFixArray():
|
||||||
check("\x92\x90\x91\x91\xc0",
|
check(b"\x92\x90\x91\x91\xc0",
|
||||||
((),((None,),),),
|
((),((None,),),),
|
||||||
)
|
)
|
||||||
|
|
||||||
def testFixRaw():
|
def testFixRaw():
|
||||||
check("\x94\xa0\xa1a\xa2bc\xa3def",
|
check(b"\x94\xa0\xa1a\xa2bc\xa3def",
|
||||||
("", "a", "bc", "def",),
|
(b"", b"a", b"bc", b"def",),
|
||||||
)
|
)
|
||||||
|
|
||||||
def testFixMap():
|
def testFixMap():
|
||||||
check(
|
check(
|
||||||
"\x82\xc2\x81\xc0\xc0\xc3\x81\xc0\x80",
|
b"\x82\xc2\x81\xc0\xc0\xc3\x81\xc0\x80",
|
||||||
{False: {None: None}, True:{None:{}}},
|
{False: {None: None}, True:{None:{}}},
|
||||||
)
|
)
|
||||||
|
|
||||||
def testUnsignedInt():
|
def testUnsignedInt():
|
||||||
check(
|
check(
|
||||||
"\x99\xcc\x00\xcc\x80\xcc\xff\xcd\x00\x00\xcd\x80\x00"
|
b"\x99\xcc\x00\xcc\x80\xcc\xff\xcd\x00\x00\xcd\x80\x00"
|
||||||
"\xcd\xff\xff\xce\x00\x00\x00\x00\xce\x80\x00\x00\x00"
|
b"\xcd\xff\xff\xce\x00\x00\x00\x00\xce\x80\x00\x00\x00"
|
||||||
"\xce\xff\xff\xff\xff",
|
b"\xce\xff\xff\xff\xff",
|
||||||
(0, 128, 255, 0, 32768, 65535, 0, 2147483648, 4294967295,),
|
(0, 128, 255, 0, 32768, 65535, 0, 2147483648, 4294967295,),
|
||||||
)
|
)
|
||||||
|
|
||||||
def testSignedInt():
|
def testSignedInt():
|
||||||
check("\x99\xd0\x00\xd0\x80\xd0\xff\xd1\x00\x00\xd1\x80\x00"
|
check(b"\x99\xd0\x00\xd0\x80\xd0\xff\xd1\x00\x00\xd1\x80\x00"
|
||||||
"\xd1\xff\xff\xd2\x00\x00\x00\x00\xd2\x80\x00\x00\x00"
|
b"\xd1\xff\xff\xd2\x00\x00\x00\x00\xd2\x80\x00\x00\x00"
|
||||||
"\xd2\xff\xff\xff\xff",
|
b"\xd2\xff\xff\xff\xff",
|
||||||
(0, -128, -1, 0, -32768, -1, 0, -2147483648, -1,))
|
(0, -128, -1, 0, -32768, -1, 0, -2147483648, -1,))
|
||||||
|
|
||||||
def testRaw():
|
def testRaw():
|
||||||
check("\x96\xda\x00\x00\xda\x00\x01a\xda\x00\x02ab\xdb\x00\x00"
|
check(b"\x96\xda\x00\x00\xda\x00\x01a\xda\x00\x02ab\xdb\x00\x00"
|
||||||
"\x00\x00\xdb\x00\x00\x00\x01a\xdb\x00\x00\x00\x02ab",
|
b"\x00\x00\xdb\x00\x00\x00\x01a\xdb\x00\x00\x00\x02ab",
|
||||||
("", "a", "ab", "", "a", "ab"))
|
(b"", b"a", b"ab", b"", b"a", b"ab"))
|
||||||
|
|
||||||
def testArray():
|
def testArray():
|
||||||
check("\x96\xdc\x00\x00\xdc\x00\x01\xc0\xdc\x00\x02\xc2\xc3\xdd\x00"
|
check(b"\x96\xdc\x00\x00\xdc\x00\x01\xc0\xdc\x00\x02\xc2\xc3\xdd\x00"
|
||||||
"\x00\x00\x00\xdd\x00\x00\x00\x01\xc0\xdd\x00\x00\x00\x02"
|
b"\x00\x00\x00\xdd\x00\x00\x00\x01\xc0\xdd\x00\x00\x00\x02"
|
||||||
"\xc2\xc3",
|
b"\xc2\xc3",
|
||||||
((), (None,), (False,True), (), (None,), (False,True))
|
((), (None,), (False,True), (), (None,), (False,True))
|
||||||
)
|
)
|
||||||
|
|
||||||
def testMap():
|
def testMap():
|
||||||
check(
|
check(
|
||||||
"\x96"
|
b"\x96"
|
||||||
"\xde\x00\x00"
|
b"\xde\x00\x00"
|
||||||
"\xde\x00\x01\xc0\xc2"
|
b"\xde\x00\x01\xc0\xc2"
|
||||||
"\xde\x00\x02\xc0\xc2\xc3\xc2"
|
b"\xde\x00\x02\xc0\xc2\xc3\xc2"
|
||||||
"\xdf\x00\x00\x00\x00"
|
b"\xdf\x00\x00\x00\x00"
|
||||||
"\xdf\x00\x00\x00\x01\xc0\xc2"
|
b"\xdf\x00\x00\x00\x01\xc0\xc2"
|
||||||
"\xdf\x00\x00\x00\x02\xc0\xc2\xc3\xc2",
|
b"\xdf\x00\x00\x00\x02\xc0\xc2\xc3\xc2",
|
||||||
({}, {None: False}, {True: False, None: False}, {},
|
({}, {None: False}, {True: False, None: False}, {},
|
||||||
{None: False}, {True: False, None: False}))
|
{None: False}, {True: False, None: False}))
|
||||||
|
|
||||||
|
|
|
@ -7,22 +7,22 @@ from nose.tools import *
|
||||||
from msgpack import packs, unpacks
|
from msgpack import packs, unpacks
|
||||||
|
|
||||||
def _decode_complex(obj):
|
def _decode_complex(obj):
|
||||||
if '__complex__' in obj:
|
if b'__complex__' in obj:
|
||||||
return complex(obj['real'], obj['imag'])
|
return complex(obj[b'real'], obj[b'imag'])
|
||||||
return obj
|
return obj
|
||||||
|
|
||||||
def _encode_complex(obj):
|
def _encode_complex(obj):
|
||||||
if isinstance(obj, complex):
|
if isinstance(obj, complex):
|
||||||
return {'__complex__': True, 'real': 1, 'imag': 2}
|
return {b'__complex__': True, b'real': 1, b'imag': 2}
|
||||||
return obj
|
return obj
|
||||||
|
|
||||||
def test_encode_hook():
|
def test_encode_hook():
|
||||||
packed = packs([3, 1+2j], default=_encode_complex)
|
packed = packs([3, 1+2j], default=_encode_complex)
|
||||||
unpacked = unpacks(packed)
|
unpacked = unpacks(packed)
|
||||||
eq_(unpacked[1], {'__complex__': True, 'real': 1, 'imag': 2})
|
eq_(unpacked[1], {b'__complex__': True, b'real': 1, b'imag': 2})
|
||||||
|
|
||||||
def test_decode_hook():
|
def test_decode_hook():
|
||||||
packed = packs([3, {'__complex__': True, 'real': 1, 'imag': 2}])
|
packed = packs([3, {b'__complex__': True, b'real': 1, b'imag': 2}])
|
||||||
unpacked = unpacks(packed, object_hook=_decode_complex)
|
unpacked = unpacks(packed, object_hook=_decode_complex)
|
||||||
eq_(unpacked[1], 1+2j)
|
eq_(unpacked[1], 1+2j)
|
||||||
|
|
||||||
|
@ -40,7 +40,4 @@ def test_array_hook():
|
||||||
eq_(unpacked, '123')
|
eq_(unpacked, '123')
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
test_decode_hook()
|
main()
|
||||||
test_encode_hook()
|
|
||||||
test_bad_hook()
|
|
||||||
test_array_hook()
|
|
||||||
|
|
|
@ -5,9 +5,9 @@ from nose import main
|
||||||
from nose.tools import *
|
from nose.tools import *
|
||||||
from nose.plugins.skip import SkipTest
|
from nose.plugins.skip import SkipTest
|
||||||
|
|
||||||
from msgpack import packs, unpacks, Packer, Unpacker
|
from msgpack import packs, unpacks, Unpacker, Packer
|
||||||
|
|
||||||
from StringIO import StringIO
|
from io import BytesIO
|
||||||
|
|
||||||
def check(data):
|
def check(data):
|
||||||
re = unpacks(packs(data))
|
re = unpacks(packs(data))
|
||||||
|
@ -18,7 +18,7 @@ def testPack():
|
||||||
0, 1, 127, 128, 255, 256, 65535, 65536,
|
0, 1, 127, 128, 255, 256, 65535, 65536,
|
||||||
-1, -32, -33, -128, -129, -32768, -32769,
|
-1, -32, -33, -128, -129, -32768, -32769,
|
||||||
1.0,
|
1.0,
|
||||||
"", "a", "a"*31, "a"*32,
|
b"", b"a", b"a"*31, b"a"*32,
|
||||||
None, True, False,
|
None, True, False,
|
||||||
(), ((),), ((), None,),
|
(), ((),), ((), None,),
|
||||||
{None: 0},
|
{None: 0},
|
||||||
|
@ -29,20 +29,20 @@ def testPack():
|
||||||
|
|
||||||
def testPackUnicode():
|
def testPackUnicode():
|
||||||
test_data = [
|
test_data = [
|
||||||
u"", u"abcd", (u"defgh",), u"Русский текст",
|
"", "abcd", ("defgh",), "Русский текст",
|
||||||
]
|
]
|
||||||
for td in test_data:
|
for td in test_data:
|
||||||
re = unpacks(packs(td, encoding='utf-8'), encoding='utf-8')
|
re = unpacks(packs(td, encoding='utf-8'), encoding='utf-8')
|
||||||
assert_equal(re, td)
|
assert_equal(re, td)
|
||||||
packer = Packer(encoding='utf-8')
|
packer = Packer(encoding='utf-8')
|
||||||
data = packer.pack(td)
|
data = packer.pack(td)
|
||||||
re = Unpacker(StringIO(data), encoding='utf-8').unpack()
|
re = Unpacker(BytesIO(data), encoding='utf-8').unpack()
|
||||||
assert_equal(re, td)
|
assert_equal(re, td)
|
||||||
|
|
||||||
def testPackUTF32():
|
def testPackUTF32():
|
||||||
try:
|
try:
|
||||||
test_data = [
|
test_data = [
|
||||||
u"", u"abcd", (u"defgh",), u"Русский текст",
|
"", "abcd", ("defgh",), "Русский текст",
|
||||||
]
|
]
|
||||||
for td in test_data:
|
for td in test_data:
|
||||||
re = unpacks(packs(td, encoding='utf-32'), encoding='utf-32')
|
re = unpacks(packs(td, encoding='utf-32'), encoding='utf-32')
|
||||||
|
@ -52,37 +52,35 @@ def testPackUTF32():
|
||||||
|
|
||||||
def testPackBytes():
|
def testPackBytes():
|
||||||
test_data = [
|
test_data = [
|
||||||
"", "abcd", ("defgh",),
|
b"", b"abcd", (b"defgh",),
|
||||||
]
|
]
|
||||||
for td in test_data:
|
for td in test_data:
|
||||||
check(td)
|
check(td)
|
||||||
|
|
||||||
def testIgnoreUnicodeErrors():
|
def testIgnoreUnicodeErrors():
|
||||||
re = unpacks(packs('abc\xeddef'),
|
re = unpacks(packs(b'abc\xeddef'),
|
||||||
encoding='ascii', unicode_errors='ignore')
|
encoding='utf-8', unicode_errors='ignore')
|
||||||
assert_equal(re, "abcdef")
|
assert_equal(re, "abcdef")
|
||||||
|
|
||||||
@raises(UnicodeDecodeError)
|
@raises(UnicodeDecodeError)
|
||||||
def testStrictUnicodeUnpack():
|
def testStrictUnicodeUnpack():
|
||||||
unpacks(packs('abc\xeddef'), encoding='utf-8')
|
unpacks(packs(b'abc\xeddef'), encoding='utf-8')
|
||||||
|
|
||||||
@raises(UnicodeEncodeError)
|
@raises(UnicodeEncodeError)
|
||||||
def testStrictUnicodePack():
|
def testStrictUnicodePack():
|
||||||
packs(u"abc\xeddef", encoding='ascii', unicode_errors='strict')
|
packs("abc\xeddef", encoding='ascii', unicode_errors='strict')
|
||||||
|
|
||||||
def testIgnoreErrorsPack():
|
def testIgnoreErrorsPack():
|
||||||
re = unpacks(
|
re = unpacks(packs("abcФФФdef", encoding='ascii', unicode_errors='ignore'), encoding='utf-8')
|
||||||
packs(u"abcФФФdef", encoding='ascii', unicode_errors='ignore'),
|
assert_equal(re, "abcdef")
|
||||||
encoding='utf-8')
|
|
||||||
assert_equal(re, u"abcdef")
|
|
||||||
|
|
||||||
@raises(TypeError)
|
@raises(TypeError)
|
||||||
def testNoEncoding():
|
def testNoEncoding():
|
||||||
packs(u"abc", encoding=None)
|
packs("abc", encoding=None)
|
||||||
|
|
||||||
def testDecodeBinary():
|
def testDecodeBinary():
|
||||||
re = unpacks(packs(u"abc"), encoding=None)
|
re = unpacks(packs("abc"), encoding=None)
|
||||||
assert_equal(re, "abc")
|
assert_equal(re, b"abc")
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
|
@ -1,33 +1,35 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
# coding: utf-8
|
# coding: utf-8
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
from msgpack import Unpacker
|
from msgpack import Unpacker
|
||||||
|
|
||||||
def test_foobar():
|
def test_foobar():
|
||||||
unpacker = Unpacker(read_size=3)
|
unpacker = Unpacker(read_size=3)
|
||||||
unpacker.feed('foobar')
|
unpacker.feed(b'foobar')
|
||||||
assert unpacker.unpack() == ord('f')
|
assert unpacker.unpack() == ord(b'f')
|
||||||
assert unpacker.unpack() == ord('o')
|
assert unpacker.unpack() == ord(b'o')
|
||||||
assert unpacker.unpack() == ord('o')
|
assert unpacker.unpack() == ord(b'o')
|
||||||
assert unpacker.unpack() == ord('b')
|
assert unpacker.unpack() == ord(b'b')
|
||||||
assert unpacker.unpack() == ord('a')
|
assert unpacker.unpack() == ord(b'a')
|
||||||
assert unpacker.unpack() == ord('r')
|
assert unpacker.unpack() == ord(b'r')
|
||||||
try:
|
try:
|
||||||
o = unpacker.unpack()
|
o = unpacker.unpack()
|
||||||
print "Oops!", o
|
print(("Oops!", o))
|
||||||
assert 0
|
assert 0
|
||||||
except StopIteration:
|
except StopIteration:
|
||||||
assert 1
|
assert 1
|
||||||
else:
|
else:
|
||||||
assert 0
|
assert 0
|
||||||
unpacker.feed('foo')
|
unpacker.feed(b'foo')
|
||||||
unpacker.feed('bar')
|
unpacker.feed(b'bar')
|
||||||
|
|
||||||
k = 0
|
k = 0
|
||||||
for o, e in zip(unpacker, 'foobarbaz'):
|
for o, e in zip(unpacker, b'foobarbaz'):
|
||||||
assert o == ord(e)
|
assert o == e
|
||||||
k += 1
|
k += 1
|
||||||
assert k == len('foobar')
|
assert k == len(b'foobar')
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
test_foobar()
|
test_foobar()
|
||||||
|
|
|
@ -1,16 +0,0 @@
|
||||||
#!/usr/bin/env python
|
|
||||||
# coding: utf-8
|
|
||||||
|
|
||||||
from nose import main
|
|
||||||
from nose.tools import *
|
|
||||||
from msgpack import packb, unpackb
|
|
||||||
|
|
||||||
def test_unpack_buffer():
|
|
||||||
from array import array
|
|
||||||
buf = array('b')
|
|
||||||
buf.fromstring(packb(('foo', 'bar')))
|
|
||||||
obj = unpackb(buf)
|
|
||||||
assert_equal((b'foo', b'bar'), obj)
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
main()
|
|
|
@ -1,105 +0,0 @@
|
||||||
#!/usr/bin/env python
|
|
||||||
# coding: utf-8
|
|
||||||
|
|
||||||
from nose import main
|
|
||||||
from nose.tools import *
|
|
||||||
from msgpack import packs, unpacks
|
|
||||||
|
|
||||||
|
|
||||||
def check(length, obj):
|
|
||||||
v = packs(obj)
|
|
||||||
assert_equal(len(v), length, "%r length should be %r but get %r" % (obj, length, len(v)))
|
|
||||||
assert_equal(unpacks(v), obj)
|
|
||||||
|
|
||||||
def test_1():
|
|
||||||
for o in [None, True, False, 0, 1, (1 << 6), (1 << 7) - 1, -1,
|
|
||||||
-((1<<5)-1), -(1<<5)]:
|
|
||||||
check(1, o)
|
|
||||||
|
|
||||||
def test_2():
|
|
||||||
for o in [1 << 7, (1 << 8) - 1,
|
|
||||||
-((1<<5)+1), -(1<<7)
|
|
||||||
]:
|
|
||||||
check(2, o)
|
|
||||||
|
|
||||||
def test_3():
|
|
||||||
for o in [1 << 8, (1 << 16) - 1,
|
|
||||||
-((1<<7)+1), -(1<<15)]:
|
|
||||||
check(3, o)
|
|
||||||
|
|
||||||
def test_5():
|
|
||||||
for o in [1 << 16, (1 << 32) - 1,
|
|
||||||
-((1<<15)+1), -(1<<31)]:
|
|
||||||
check(5, o)
|
|
||||||
|
|
||||||
def test_9():
|
|
||||||
for o in [1 << 32, (1 << 64) - 1,
|
|
||||||
-((1<<31)+1), -(1<<63),
|
|
||||||
1.0, 0.1, -0.1, -1.0]:
|
|
||||||
check(9, o)
|
|
||||||
|
|
||||||
|
|
||||||
def check_raw(overhead, num):
|
|
||||||
check(num + overhead, b" " * num)
|
|
||||||
|
|
||||||
def test_fixraw():
|
|
||||||
check_raw(1, 0)
|
|
||||||
check_raw(1, (1<<5) - 1)
|
|
||||||
|
|
||||||
def test_raw16():
|
|
||||||
check_raw(3, 1<<5)
|
|
||||||
check_raw(3, (1<<16) - 1)
|
|
||||||
|
|
||||||
def test_raw32():
|
|
||||||
check_raw(5, 1<<16)
|
|
||||||
|
|
||||||
|
|
||||||
def check_array(overhead, num):
|
|
||||||
check(num + overhead, (None,) * num)
|
|
||||||
|
|
||||||
def test_fixarray():
|
|
||||||
check_array(1, 0)
|
|
||||||
check_array(1, (1 << 4) - 1)
|
|
||||||
|
|
||||||
def test_array16():
|
|
||||||
check_array(3, 1 << 4)
|
|
||||||
check_array(3, (1<<16)-1)
|
|
||||||
|
|
||||||
def test_array32():
|
|
||||||
check_array(5, (1<<16))
|
|
||||||
|
|
||||||
|
|
||||||
def match(obj, buf):
|
|
||||||
assert_equal(packs(obj), buf)
|
|
||||||
assert_equal(unpacks(buf), obj)
|
|
||||||
|
|
||||||
def test_match():
|
|
||||||
cases = [
|
|
||||||
(None, b'\xc0'),
|
|
||||||
(False, b'\xc2'),
|
|
||||||
(True, b'\xc3'),
|
|
||||||
(0, b'\x00'),
|
|
||||||
(127, b'\x7f'),
|
|
||||||
(128, b'\xcc\x80'),
|
|
||||||
(256, b'\xcd\x01\x00'),
|
|
||||||
(-1, b'\xff'),
|
|
||||||
(-33, b'\xd0\xdf'),
|
|
||||||
(-129, b'\xd1\xff\x7f'),
|
|
||||||
({1:1}, b'\x81\x01\x01'),
|
|
||||||
(1.0, b"\xcb\x3f\xf0\x00\x00\x00\x00\x00\x00"),
|
|
||||||
((), b'\x90'),
|
|
||||||
(tuple(range(15)),b"\x9f\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e"),
|
|
||||||
(tuple(range(16)),b"\xdc\x00\x10\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"),
|
|
||||||
({}, b'\x80'),
|
|
||||||
(dict([(x,x) for x in range(15)]), b'\x8f\x00\x00\x01\x01\x02\x02\x03\x03\x04\x04\x05\x05\x06\x06\x07\x07\x08\x08\t\t\n\n\x0b\x0b\x0c\x0c\r\r\x0e\x0e'),
|
|
||||||
(dict([(x,x) for x in range(16)]), b'\xde\x00\x10\x00\x00\x01\x01\x02\x02\x03\x03\x04\x04\x05\x05\x06\x06\x07\x07\x08\x08\t\t\n\n\x0b\x0b\x0c\x0c\r\r\x0e\x0e\x0f\x0f'),
|
|
||||||
]
|
|
||||||
|
|
||||||
for v, p in cases:
|
|
||||||
match(v, p)
|
|
||||||
|
|
||||||
def test_unicode():
|
|
||||||
assert_equal(b'foobar', unpacks(packs('foobar')))
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
main()
|
|
|
@ -1,14 +0,0 @@
|
||||||
#!/usr/bin/env python
|
|
||||||
# coding: utf-8
|
|
||||||
|
|
||||||
from nose.tools import *
|
|
||||||
from msgpack import packs, unpacks
|
|
||||||
|
|
||||||
import datetime
|
|
||||||
|
|
||||||
def test_raise_on_find_unsupported_value():
|
|
||||||
assert_raises(TypeError, packs, datetime.datetime.now())
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
from nose import main
|
|
||||||
main()
|
|
|
@ -1,75 +0,0 @@
|
||||||
#!/usr/bin/env python
|
|
||||||
# coding: utf-8
|
|
||||||
|
|
||||||
from nose import main
|
|
||||||
from nose.tools import *
|
|
||||||
from msgpack import unpacks
|
|
||||||
|
|
||||||
def check(src, should):
|
|
||||||
assert_equal(unpacks(src), should)
|
|
||||||
|
|
||||||
def testSimpleValue():
|
|
||||||
check(b"\x93\xc0\xc2\xc3",
|
|
||||||
(None, False, True,))
|
|
||||||
|
|
||||||
def testFixnum():
|
|
||||||
check(b"\x92\x93\x00\x40\x7f\x93\xe0\xf0\xff",
|
|
||||||
((0,64,127,), (-32,-16,-1,),)
|
|
||||||
)
|
|
||||||
|
|
||||||
def testFixArray():
|
|
||||||
check(b"\x92\x90\x91\x91\xc0",
|
|
||||||
((),((None,),),),
|
|
||||||
)
|
|
||||||
|
|
||||||
def testFixRaw():
|
|
||||||
check(b"\x94\xa0\xa1a\xa2bc\xa3def",
|
|
||||||
(b"", b"a", b"bc", b"def",),
|
|
||||||
)
|
|
||||||
|
|
||||||
def testFixMap():
|
|
||||||
check(
|
|
||||||
b"\x82\xc2\x81\xc0\xc0\xc3\x81\xc0\x80",
|
|
||||||
{False: {None: None}, True:{None:{}}},
|
|
||||||
)
|
|
||||||
|
|
||||||
def testUnsignedInt():
|
|
||||||
check(
|
|
||||||
b"\x99\xcc\x00\xcc\x80\xcc\xff\xcd\x00\x00\xcd\x80\x00"
|
|
||||||
b"\xcd\xff\xff\xce\x00\x00\x00\x00\xce\x80\x00\x00\x00"
|
|
||||||
b"\xce\xff\xff\xff\xff",
|
|
||||||
(0, 128, 255, 0, 32768, 65535, 0, 2147483648, 4294967295,),
|
|
||||||
)
|
|
||||||
|
|
||||||
def testSignedInt():
|
|
||||||
check(b"\x99\xd0\x00\xd0\x80\xd0\xff\xd1\x00\x00\xd1\x80\x00"
|
|
||||||
b"\xd1\xff\xff\xd2\x00\x00\x00\x00\xd2\x80\x00\x00\x00"
|
|
||||||
b"\xd2\xff\xff\xff\xff",
|
|
||||||
(0, -128, -1, 0, -32768, -1, 0, -2147483648, -1,))
|
|
||||||
|
|
||||||
def testRaw():
|
|
||||||
check(b"\x96\xda\x00\x00\xda\x00\x01a\xda\x00\x02ab\xdb\x00\x00"
|
|
||||||
b"\x00\x00\xdb\x00\x00\x00\x01a\xdb\x00\x00\x00\x02ab",
|
|
||||||
(b"", b"a", b"ab", b"", b"a", b"ab"))
|
|
||||||
|
|
||||||
def testArray():
|
|
||||||
check(b"\x96\xdc\x00\x00\xdc\x00\x01\xc0\xdc\x00\x02\xc2\xc3\xdd\x00"
|
|
||||||
b"\x00\x00\x00\xdd\x00\x00\x00\x01\xc0\xdd\x00\x00\x00\x02"
|
|
||||||
b"\xc2\xc3",
|
|
||||||
((), (None,), (False,True), (), (None,), (False,True))
|
|
||||||
)
|
|
||||||
|
|
||||||
def testMap():
|
|
||||||
check(
|
|
||||||
b"\x96"
|
|
||||||
b"\xde\x00\x00"
|
|
||||||
b"\xde\x00\x01\xc0\xc2"
|
|
||||||
b"\xde\x00\x02\xc0\xc2\xc3\xc2"
|
|
||||||
b"\xdf\x00\x00\x00\x00"
|
|
||||||
b"\xdf\x00\x00\x00\x01\xc0\xc2"
|
|
||||||
b"\xdf\x00\x00\x00\x02\xc0\xc2\xc3\xc2",
|
|
||||||
({}, {None: False}, {True: False, None: False}, {},
|
|
||||||
{None: False}, {True: False, None: False}))
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
main()
|
|
|
@ -1,44 +0,0 @@
|
||||||
#!/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)
|
|
||||||
|
|
||||||
@raises(ValueError)
|
|
||||||
def test_bad_hook():
|
|
||||||
packed = packs([3, 1+2j], default=lambda o: o)
|
|
||||||
unpacked = unpacks(packed)
|
|
||||||
|
|
||||||
def _arr_to_str(arr):
|
|
||||||
return ''.join(str(c) for c in arr)
|
|
||||||
|
|
||||||
def test_array_hook():
|
|
||||||
packed = packs([1,2,3])
|
|
||||||
unpacked = unpacks(packed, list_hook=_arr_to_str)
|
|
||||||
eq_(unpacked, '123')
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
#main()
|
|
||||||
test_decode_hook()
|
|
|
@ -1,82 +0,0 @@
|
||||||
#!/usr/bin/env python
|
|
||||||
# coding: utf-8
|
|
||||||
|
|
||||||
from nose import main
|
|
||||||
from nose.tools import *
|
|
||||||
|
|
||||||
from msgpack import packs, unpacks, Unpacker, Packer
|
|
||||||
|
|
||||||
from io import BytesIO
|
|
||||||
|
|
||||||
def check(data):
|
|
||||||
re = unpacks(packs(data))
|
|
||||||
assert_equal(re, data)
|
|
||||||
|
|
||||||
def testPack():
|
|
||||||
test_data = [
|
|
||||||
0, 1, 127, 128, 255, 256, 65535, 65536,
|
|
||||||
-1, -32, -33, -128, -129, -32768, -32769,
|
|
||||||
1.0,
|
|
||||||
b"", b"a", b"a"*31, b"a"*32,
|
|
||||||
None, True, False,
|
|
||||||
(), ((),), ((), None,),
|
|
||||||
{None: 0},
|
|
||||||
(1<<23),
|
|
||||||
]
|
|
||||||
for td in test_data:
|
|
||||||
check(td)
|
|
||||||
|
|
||||||
def testPackUnicode():
|
|
||||||
test_data = [
|
|
||||||
"", "abcd", ("defgh",), "Русский текст",
|
|
||||||
]
|
|
||||||
for td in test_data:
|
|
||||||
re = unpacks(packs(td, encoding='utf-8'), encoding='utf-8')
|
|
||||||
assert_equal(re, td)
|
|
||||||
packer = Packer(encoding='utf-8')
|
|
||||||
data = packer.pack(td)
|
|
||||||
re = Unpacker(BytesIO(data), encoding='utf-8').unpack()
|
|
||||||
assert_equal(re, td)
|
|
||||||
|
|
||||||
def testPackUTF32():
|
|
||||||
test_data = [
|
|
||||||
"", "abcd", ("defgh",), "Русский текст",
|
|
||||||
]
|
|
||||||
for td in test_data:
|
|
||||||
re = unpacks(packs(td, encoding='utf-32'), encoding='utf-32')
|
|
||||||
assert_equal(re, td)
|
|
||||||
|
|
||||||
def testPackBytes():
|
|
||||||
test_data = [
|
|
||||||
b"", b"abcd", (b"defgh",),
|
|
||||||
]
|
|
||||||
for td in test_data:
|
|
||||||
check(td)
|
|
||||||
|
|
||||||
def testIgnoreUnicodeErrors():
|
|
||||||
re = unpacks(packs(b'abc\xeddef'),
|
|
||||||
encoding='utf-8', unicode_errors='ignore')
|
|
||||||
assert_equal(re, "abcdef")
|
|
||||||
|
|
||||||
@raises(UnicodeDecodeError)
|
|
||||||
def testStrictUnicodeUnpack():
|
|
||||||
unpacks(packs(b'abc\xeddef'), encoding='utf-8')
|
|
||||||
|
|
||||||
@raises(UnicodeEncodeError)
|
|
||||||
def testStrictUnicodePack():
|
|
||||||
packs("abc\xeddef", encoding='ascii', unicode_errors='strict')
|
|
||||||
|
|
||||||
def testIgnoreErrorsPack():
|
|
||||||
re = unpacks(packs("abcФФФdef", encoding='ascii', unicode_errors='ignore'), encoding='utf-8')
|
|
||||||
assert_equal(re, "abcdef")
|
|
||||||
|
|
||||||
@raises(TypeError)
|
|
||||||
def testNoEncoding():
|
|
||||||
packs("abc", encoding=None)
|
|
||||||
|
|
||||||
def testDecodeBinary():
|
|
||||||
re = unpacks(packs("abc"), encoding=None)
|
|
||||||
assert_equal(re, b"abc")
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
main()
|
|
|
@ -1,36 +0,0 @@
|
||||||
#!/usr/bin/env python
|
|
||||||
# coding: utf-8
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
from msgpack import Unpacker
|
|
||||||
|
|
||||||
def test_foobar():
|
|
||||||
unpacker = Unpacker(read_size=3)
|
|
||||||
unpacker.feed(b'foobar')
|
|
||||||
assert unpacker.unpack() == ord(b'f')
|
|
||||||
assert unpacker.unpack() == ord(b'o')
|
|
||||||
assert unpacker.unpack() == ord(b'o')
|
|
||||||
assert unpacker.unpack() == ord(b'b')
|
|
||||||
assert unpacker.unpack() == ord(b'a')
|
|
||||||
assert unpacker.unpack() == ord(b'r')
|
|
||||||
try:
|
|
||||||
o = unpacker.unpack()
|
|
||||||
print(("Oops!", o))
|
|
||||||
assert 0
|
|
||||||
except StopIteration:
|
|
||||||
assert 1
|
|
||||||
else:
|
|
||||||
assert 0
|
|
||||||
unpacker.feed(b'foo')
|
|
||||||
unpacker.feed(b'bar')
|
|
||||||
|
|
||||||
k = 0
|
|
||||||
for o, e in zip(unpacker, b'foobarbaz'):
|
|
||||||
assert o == e
|
|
||||||
k += 1
|
|
||||||
assert k == len(b'foobar')
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
test_foobar()
|
|
||||||
|
|
4
tox.ini
4
tox.ini
|
@ -3,7 +3,5 @@ envlist = py26,py27,py32
|
||||||
[testenv]
|
[testenv]
|
||||||
deps=
|
deps=
|
||||||
nose
|
nose
|
||||||
commands=nosetests -w test
|
|
||||||
|
|
||||||
[testenv:py32]
|
commands=nosetests -w test
|
||||||
commands=nosetests -w test3
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue