| 1 |
"""Test Unpacker's read_array_header and read_map_header methods""" |
| 2 |
|
| 3 |
from msgpack import OutOfData, Unpacker, packb |
| 4 |
|
| 5 |
UnexpectedTypeException = ValueError |
| 6 |
|
| 7 |
|
| 8 |
def test_read_array_header(): |
| 9 |
unpacker = Unpacker() |
| 10 |
unpacker.feed(packb(["a", "b", "c"])) |
| 11 |
assert unpacker.read_array_header() == 3 |
| 12 |
assert unpacker.unpack() == "a" |
| 13 |
assert unpacker.unpack() == "b" |
| 14 |
assert unpacker.unpack() == "c" |
| 15 |
try: |
| 16 |
unpacker.unpack() |
| 17 |
assert 0, "should raise exception" |
| 18 |
except OutOfData: |
| 19 |
assert 1, "okay" |
| 20 |
|
| 21 |
|
| 22 |
def test_read_map_header(): |
| 23 |
unpacker = Unpacker() |
| 24 |
unpacker.feed(packb({"a": "A"})) |
| 25 |
assert unpacker.read_map_header() == 1 |
| 26 |
assert unpacker.unpack() == "a" |
| 27 |
assert unpacker.unpack() == "A" |
| 28 |
try: |
| 29 |
unpacker.unpack() |
| 30 |
assert 0, "should raise exception" |
| 31 |
except OutOfData: |
| 32 |
assert 1, "okay" |
| 33 |
|
| 34 |
|
| 35 |
def test_incorrect_type_array(): |
| 36 |
unpacker = Unpacker() |
| 37 |
unpacker.feed(packb(1)) |
| 38 |
try: |
| 39 |
unpacker.read_array_header() |
| 40 |
assert 0, "should raise exception" |
| 41 |
except UnexpectedTypeException: |
| 42 |
assert 1, "okay" |
| 43 |
|
| 44 |
|
| 45 |
def test_incorrect_type_map(): |
| 46 |
unpacker = Unpacker() |
| 47 |
unpacker.feed(packb(1)) |
| 48 |
try: |
| 49 |
unpacker.read_map_header() |
| 50 |
assert 0, "should raise exception" |
| 51 |
except UnexpectedTypeException: |
| 52 |
assert 1, "okay" |
| 53 |
|
| 54 |
|
| 55 |
def test_correct_type_nested_array(): |
| 56 |
unpacker = Unpacker() |
| 57 |
unpacker.feed(packb({"a": ["b", "c", "d"]})) |
| 58 |
try: |
| 59 |
unpacker.read_array_header() |
| 60 |
assert 0, "should raise exception" |
| 61 |
except UnexpectedTypeException: |
| 62 |
assert 1, "okay" |
| 63 |
|
| 64 |
|
| 65 |
def test_incorrect_type_nested_map(): |
| 66 |
unpacker = Unpacker() |
| 67 |
unpacker.feed(packb([{"a": "b"}])) |
| 68 |
try: |
| 69 |
unpacker.read_map_header() |
| 70 |
assert 0, "should raise exception" |
| 71 |
except UnexpectedTypeException: |
| 72 |
assert 1, "okay" |