mirror of
https://github.com/msgpack/msgpack-python.git
synced 2026-02-07 02:09:59 +00:00
commit
ff263dfee8
5 changed files with 29 additions and 33 deletions
|
|
@ -1,7 +1,7 @@
|
|||
#!/usr/bin/env python
|
||||
# coding: utf-8
|
||||
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||
|
||||
import six
|
||||
import struct
|
||||
from pytest import raises, xfail
|
||||
|
||||
|
|
@ -28,24 +28,22 @@ def testPack():
|
|||
check(td)
|
||||
|
||||
def testPackUnicode():
|
||||
test_data = [
|
||||
six.u(""), six.u("abcd"), [six.u("defgh")], six.u("Русский текст"),
|
||||
]
|
||||
test_data = ["", "abcd", ["defgh"], "Русский текст"]
|
||||
for td in test_data:
|
||||
re = unpackb(packb(td, encoding='utf-8'), use_list=1, encoding='utf-8')
|
||||
assert re == td
|
||||
packer = Packer(encoding='utf-8')
|
||||
data = packer.pack(td)
|
||||
re = Unpacker(BytesIO(data), encoding='utf-8', use_list=1).unpack()
|
||||
re = Unpacker(BytesIO(data), encoding=str('utf-8'), use_list=1).unpack()
|
||||
assert re == td
|
||||
|
||||
def testPackUTF32():
|
||||
try:
|
||||
test_data = [
|
||||
six.u(""),
|
||||
six.u("abcd"),
|
||||
[six.u("defgh")],
|
||||
six.u("Русский текст"),
|
||||
"",
|
||||
"abcd",
|
||||
["defgh"],
|
||||
"Русский текст",
|
||||
]
|
||||
for td in test_data:
|
||||
re = unpackb(packb(td, encoding='utf-32'), use_list=1, encoding='utf-32')
|
||||
|
|
@ -70,26 +68,26 @@ def testStrictUnicodeUnpack():
|
|||
|
||||
def testStrictUnicodePack():
|
||||
with raises(UnicodeEncodeError):
|
||||
packb(six.u("abc\xeddef"), encoding='ascii', unicode_errors='strict')
|
||||
packb("abc\xeddef", encoding='ascii', unicode_errors='strict')
|
||||
|
||||
def testIgnoreErrorsPack():
|
||||
re = unpackb(packb(six.u("abcФФФdef"), encoding='ascii', unicode_errors='ignore'), encoding='utf-8', use_list=1)
|
||||
assert re == six.u("abcdef")
|
||||
re = unpackb(packb("abcФФФdef", encoding='ascii', unicode_errors='ignore'), encoding='utf-8', use_list=1)
|
||||
assert re == "abcdef"
|
||||
|
||||
def testNoEncoding():
|
||||
with raises(TypeError):
|
||||
packb(six.u("abc"), encoding=None)
|
||||
packb("abc", encoding=None)
|
||||
|
||||
def testDecodeBinary():
|
||||
re = unpackb(packb("abc"), encoding=None, use_list=1)
|
||||
re = unpackb(packb(b"abc"), encoding=None, use_list=1)
|
||||
assert re == b"abc"
|
||||
|
||||
def testPackFloat():
|
||||
assert packb(1.0, use_single_float=True) == b'\xca' + struct.pack('>f', 1.0)
|
||||
assert packb(1.0, use_single_float=False) == b'\xcb' + struct.pack('>d', 1.0)
|
||||
assert packb(1.0, use_single_float=True) == b'\xca' + struct.pack(str('>f'), 1.0)
|
||||
assert packb(1.0, use_single_float=False) == b'\xcb' + struct.pack(str('>d'), 1.0)
|
||||
|
||||
def testArraySize(sizes=[0, 5, 50, 1000]):
|
||||
bio = six.BytesIO()
|
||||
bio = BytesIO()
|
||||
packer = Packer()
|
||||
for size in sizes:
|
||||
bio.write(packer.pack_array_header(size))
|
||||
|
|
@ -108,7 +106,7 @@ def test_manualreset(sizes=[0, 5, 50, 1000]):
|
|||
for i in range(size):
|
||||
packer.pack(i)
|
||||
|
||||
bio = six.BytesIO(packer.bytes())
|
||||
bio = BytesIO(packer.bytes())
|
||||
unpacker = Unpacker(bio, use_list=1)
|
||||
for size in sizes:
|
||||
assert unpacker.unpack() == list(range(size))
|
||||
|
|
@ -117,7 +115,7 @@ def test_manualreset(sizes=[0, 5, 50, 1000]):
|
|||
assert packer.bytes() == b''
|
||||
|
||||
def testMapSize(sizes=[0, 5, 50, 1000]):
|
||||
bio = six.BytesIO()
|
||||
bio = BytesIO()
|
||||
packer = Packer()
|
||||
for size in sizes:
|
||||
bio.write(packer.pack_map_header(size))
|
||||
|
|
|
|||
|
|
@ -1,16 +1,15 @@
|
|||
#!/usr/bin/env python
|
||||
# coding: utf-8
|
||||
|
||||
import six
|
||||
import io
|
||||
import msgpack
|
||||
|
||||
binarydata = [chr(i) for i in range(256)]
|
||||
binarydata = six.b("".join(binarydata))
|
||||
|
||||
binarydata = bytes(bytearray(range(256)))
|
||||
|
||||
def gen_binary_data(idx):
|
||||
data = binarydata[:idx % 300]
|
||||
return data
|
||||
return binarydata[:idx % 300]
|
||||
|
||||
|
||||
def test_exceeding_unpacker_read_size():
|
||||
dumpf = io.BytesIO()
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
#!/usr/bin/env python
|
||||
# coding: utf-8
|
||||
|
||||
import six
|
||||
import io
|
||||
from msgpack import Unpacker, BufferFull
|
||||
from msgpack.exceptions import OutOfData
|
||||
from pytest import raises
|
||||
|
|
@ -79,7 +79,7 @@ def test_readbytes():
|
|||
assert unpacker.unpack() == ord(b'r')
|
||||
|
||||
# Test buffer refill
|
||||
unpacker = Unpacker(six.BytesIO(b'foobar'), read_size=3)
|
||||
unpacker = Unpacker(io.BytesIO(b'foobar'), read_size=3)
|
||||
assert unpacker.unpack() == ord(b'f')
|
||||
assert unpacker.read_bytes(3) == b'oob'
|
||||
assert unpacker.unpack() == ord(b'a')
|
||||
|
|
|
|||
|
|
@ -1,19 +1,19 @@
|
|||
"""Tests for cases where the user seeks to obtain packed msgpack objects"""
|
||||
|
||||
import six
|
||||
import io
|
||||
from msgpack import Unpacker, packb
|
||||
|
||||
|
||||
def test_write_bytes():
|
||||
unpacker = Unpacker()
|
||||
unpacker.feed(b'abc')
|
||||
f = six.BytesIO()
|
||||
f = io.BytesIO()
|
||||
assert unpacker.unpack(f.write) == ord('a')
|
||||
assert f.getvalue() == b'a'
|
||||
f = six.BytesIO()
|
||||
f = io.BytesIO()
|
||||
assert unpacker.skip(f.write) is None
|
||||
assert f.getvalue() == b'b'
|
||||
f = six.BytesIO()
|
||||
f = io.BytesIO()
|
||||
assert unpacker.skip() is None
|
||||
assert f.getvalue() == b''
|
||||
|
||||
|
|
@ -21,9 +21,9 @@ def test_write_bytes():
|
|||
def test_write_bytes_multi_buffer():
|
||||
long_val = (5) * 100
|
||||
expected = packb(long_val)
|
||||
unpacker = Unpacker(six.BytesIO(expected), read_size=3, max_buffer_size=3)
|
||||
unpacker = Unpacker(io.BytesIO(expected), read_size=3, max_buffer_size=3)
|
||||
|
||||
f = six.BytesIO()
|
||||
f = io.BytesIO()
|
||||
unpacked = unpacker.unpack(f.write)
|
||||
assert unpacked == long_val
|
||||
assert f.getvalue() == expected
|
||||
|
|
|
|||
1
tox.ini
1
tox.ini
|
|
@ -4,6 +4,5 @@ envlist = py26,py27,py32,py33,pypy
|
|||
[testenv]
|
||||
deps=
|
||||
pytest
|
||||
six
|
||||
|
||||
commands=py.test test
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue