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