2012-09-25 01:18:33 +10:00
|
|
|
"""Test Unpacker's read_array_header and read_map_header methods"""
|
2012-12-10 00:31:19 +09:00
|
|
|
from msgpack import packb, Unpacker, OutOfData
|
2019-12-05 18:51:45 +09:00
|
|
|
|
2012-09-25 01:18:33 +10:00
|
|
|
UnexpectedTypeException = ValueError
|
|
|
|
|
2019-12-05 18:51:45 +09:00
|
|
|
|
2012-09-25 01:18:33 +10:00
|
|
|
def test_read_array_header():
|
|
|
|
unpacker = Unpacker()
|
2019-12-05 18:51:45 +09:00
|
|
|
unpacker.feed(packb(["a", "b", "c"]))
|
2012-09-25 01:18:33 +10:00
|
|
|
assert unpacker.read_array_header() == 3
|
2019-12-05 18:51:45 +09:00
|
|
|
assert unpacker.unpack() == b"a"
|
|
|
|
assert unpacker.unpack() == b"b"
|
|
|
|
assert unpacker.unpack() == b"c"
|
2012-09-25 01:18:33 +10:00
|
|
|
try:
|
|
|
|
unpacker.unpack()
|
2019-12-05 18:51:45 +09:00
|
|
|
assert 0, "should raise exception"
|
2012-12-10 00:31:19 +09:00
|
|
|
except OutOfData:
|
2019-12-05 18:51:45 +09:00
|
|
|
assert 1, "okay"
|
2012-09-25 01:18:33 +10:00
|
|
|
|
|
|
|
|
|
|
|
def test_read_map_header():
|
|
|
|
unpacker = Unpacker()
|
2019-12-05 18:51:45 +09:00
|
|
|
unpacker.feed(packb({"a": "A"}))
|
2012-09-25 01:18:33 +10:00
|
|
|
assert unpacker.read_map_header() == 1
|
2019-12-05 18:51:45 +09:00
|
|
|
assert unpacker.unpack() == b"a"
|
|
|
|
assert unpacker.unpack() == b"A"
|
2012-09-25 01:18:33 +10:00
|
|
|
try:
|
|
|
|
unpacker.unpack()
|
2019-12-05 18:51:45 +09:00
|
|
|
assert 0, "should raise exception"
|
2012-12-10 00:31:19 +09:00
|
|
|
except OutOfData:
|
2019-12-05 18:51:45 +09:00
|
|
|
assert 1, "okay"
|
|
|
|
|
2012-09-25 01:18:33 +10:00
|
|
|
|
|
|
|
def test_incorrect_type_array():
|
|
|
|
unpacker = Unpacker()
|
|
|
|
unpacker.feed(packb(1))
|
|
|
|
try:
|
|
|
|
unpacker.read_array_header()
|
2019-12-05 18:51:45 +09:00
|
|
|
assert 0, "should raise exception"
|
2012-09-25 01:18:33 +10:00
|
|
|
except UnexpectedTypeException:
|
2019-12-05 18:51:45 +09:00
|
|
|
assert 1, "okay"
|
|
|
|
|
2012-09-25 01:18:33 +10:00
|
|
|
|
|
|
|
def test_incorrect_type_map():
|
|
|
|
unpacker = Unpacker()
|
|
|
|
unpacker.feed(packb(1))
|
|
|
|
try:
|
|
|
|
unpacker.read_map_header()
|
2019-12-05 18:51:45 +09:00
|
|
|
assert 0, "should raise exception"
|
2012-09-25 01:18:33 +10:00
|
|
|
except UnexpectedTypeException:
|
2019-12-05 18:51:45 +09:00
|
|
|
assert 1, "okay"
|
|
|
|
|
2012-09-25 01:18:33 +10:00
|
|
|
|
|
|
|
def test_correct_type_nested_array():
|
|
|
|
unpacker = Unpacker()
|
2019-12-05 18:51:45 +09:00
|
|
|
unpacker.feed(packb({"a": ["b", "c", "d"]}))
|
2012-09-25 01:18:33 +10:00
|
|
|
try:
|
|
|
|
unpacker.read_array_header()
|
2019-12-05 18:51:45 +09:00
|
|
|
assert 0, "should raise exception"
|
2012-09-25 01:18:33 +10:00
|
|
|
except UnexpectedTypeException:
|
2019-12-05 18:51:45 +09:00
|
|
|
assert 1, "okay"
|
|
|
|
|
2012-09-25 01:18:33 +10:00
|
|
|
|
|
|
|
def test_incorrect_type_nested_map():
|
|
|
|
unpacker = Unpacker()
|
2019-12-05 18:51:45 +09:00
|
|
|
unpacker.feed(packb([{"a": "b"}]))
|
2012-09-25 01:18:33 +10:00
|
|
|
try:
|
|
|
|
unpacker.read_map_header()
|
2019-12-05 18:51:45 +09:00
|
|
|
assert 0, "should raise exception"
|
2012-09-25 01:18:33 +10:00
|
|
|
except UnexpectedTypeException:
|
2019-12-05 18:51:45 +09:00
|
|
|
assert 1, "okay"
|