2009-06-29 09:31:43 +09:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# coding: utf-8
|
|
|
|
|
|
|
|
from nose import main
|
|
|
|
from nose.tools import *
|
2011-06-01 18:30:43 +09:00
|
|
|
from nose.plugins.skip import SkipTest
|
2009-06-29 09:31:43 +09:00
|
|
|
|
|
|
|
from msgpack import packs, unpacks
|
|
|
|
|
|
|
|
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,
|
2011-06-01 18:30:43 +09:00
|
|
|
"", "a", "a"*31, "a"*32,
|
2009-06-29 09:31:43 +09:00
|
|
|
None, True, False,
|
2011-04-15 17:36:17 +03:00
|
|
|
(), ((),), ((), None,),
|
|
|
|
{None: 0},
|
|
|
|
(1<<23),
|
2009-06-29 09:31:43 +09:00
|
|
|
]
|
|
|
|
for td in test_data:
|
|
|
|
check(td)
|
|
|
|
|
2011-04-15 17:36:17 +03:00
|
|
|
def testPackUnicode():
|
|
|
|
test_data = [
|
|
|
|
u"", u"abcd", (u"defgh",), u"Русский текст",
|
|
|
|
]
|
|
|
|
for td in test_data:
|
|
|
|
re = unpacks(packs(td, encoding='utf-8'), encoding='utf-8')
|
|
|
|
assert_equal(re, td)
|
|
|
|
|
|
|
|
def testPackUTF32():
|
2011-06-01 18:30:43 +09:00
|
|
|
try:
|
|
|
|
test_data = [
|
|
|
|
u"", u"abcd", (u"defgh",), u"Русский текст",
|
|
|
|
]
|
|
|
|
for td in test_data:
|
|
|
|
re = unpacks(packs(td, encoding='utf-32'), encoding='utf-32')
|
|
|
|
assert_equal(re, td)
|
|
|
|
except LookupError:
|
|
|
|
raise SkipTest
|
2011-04-15 17:36:17 +03:00
|
|
|
|
|
|
|
def testPackBytes():
|
|
|
|
test_data = [
|
2011-06-01 18:30:43 +09:00
|
|
|
"", "abcd", ("defgh",),
|
2011-04-15 17:36:17 +03:00
|
|
|
]
|
|
|
|
for td in test_data:
|
|
|
|
check(td)
|
|
|
|
|
|
|
|
def testIgnoreUnicodeErrors():
|
2011-06-01 18:30:43 +09:00
|
|
|
re = unpacks(packs('abc\xeddef'),
|
|
|
|
encoding='ascii', unicode_errors='ignore')
|
2011-04-15 17:36:17 +03:00
|
|
|
assert_equal(re, "abcdef")
|
|
|
|
|
|
|
|
@raises(UnicodeDecodeError)
|
|
|
|
def testStrictUnicodeUnpack():
|
2011-06-01 18:30:43 +09:00
|
|
|
unpacks(packs('abc\xeddef'), encoding='utf-8')
|
2011-04-15 17:36:17 +03:00
|
|
|
|
|
|
|
@raises(UnicodeEncodeError)
|
|
|
|
def testStrictUnicodePack():
|
|
|
|
packs(u"abc\xeddef", encoding='ascii', unicode_errors='strict')
|
|
|
|
|
|
|
|
def testIgnoreErrorsPack():
|
2011-06-01 18:30:43 +09:00
|
|
|
re = unpacks(
|
|
|
|
packs(u"abcФФФdef", encoding='ascii', unicode_errors='ignore'),
|
|
|
|
encoding='utf-8')
|
2011-04-15 17:36:17 +03:00
|
|
|
assert_equal(re, u"abcdef")
|
|
|
|
|
|
|
|
@raises(TypeError)
|
|
|
|
def testNoEncoding():
|
|
|
|
packs(u"abc", encoding=None)
|
|
|
|
|
|
|
|
def testDecodeBinary():
|
|
|
|
re = unpacks(packs(u"abc"), encoding=None)
|
2011-06-01 18:30:43 +09:00
|
|
|
assert_equal(re, "abc")
|
2011-04-15 17:36:17 +03:00
|
|
|
|
2009-06-29 09:31:43 +09:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|