| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
from msgpack import unpackb |
| 4 |
|
| 5 |
|
| 6 |
def check(src, should, use_list=0, raw=True): |
| 7 |
assert unpackb(src, use_list=use_list, raw=raw, strict_map_key=False) == should |
| 8 |
|
| 9 |
|
| 10 |
def testSimpleValue(): |
| 11 |
check(b"\x93\xc0\xc2\xc3", (None, False, True)) |
| 12 |
|
| 13 |
|
| 14 |
def testFixnum(): |
| 15 |
check(b"\x92\x93\x00\x40\x7f\x93\xe0\xf0\xff", ((0, 64, 127), (-32, -16, -1))) |
| 16 |
|
| 17 |
|
| 18 |
def testFixArray(): |
| 19 |
check(b"\x92\x90\x91\x91\xc0", ((), ((None,),))) |
| 20 |
|
| 21 |
|
| 22 |
def testFixRaw(): |
| 23 |
check(b"\x94\xa0\xa1a\xa2bc\xa3def", (b"", b"a", b"bc", b"def")) |
| 24 |
|
| 25 |
|
| 26 |
def testFixMap(): |
| 27 |
check(b"\x82\xc2\x81\xc0\xc0\xc3\x81\xc0\x80", {False: {None: None}, True: {None: {}}}) |
| 28 |
|
| 29 |
|
| 30 |
def testUnsignedInt(): |
| 31 |
check( |
| 32 |
b"\x99\xcc\x00\xcc\x80\xcc\xff\xcd\x00\x00\xcd\x80\x00" |
| 33 |
b"\xcd\xff\xff\xce\x00\x00\x00\x00\xce\x80\x00\x00\x00" |
| 34 |
b"\xce\xff\xff\xff\xff", |
| 35 |
(0, 128, 255, 0, 32768, 65535, 0, 2147483648, 4294967295), |
| 36 |
) |
| 37 |
|
| 38 |
|
| 39 |
def testSignedInt(): |
| 40 |
check( |
| 41 |
b"\x99\xd0\x00\xd0\x80\xd0\xff\xd1\x00\x00\xd1\x80\x00" |
| 42 |
b"\xd1\xff\xff\xd2\x00\x00\x00\x00\xd2\x80\x00\x00\x00" |
| 43 |
b"\xd2\xff\xff\xff\xff", |
| 44 |
(0, -128, -1, 0, -32768, -1, 0, -2147483648, -1), |
| 45 |
) |
| 46 |
|
| 47 |
|
| 48 |
def testRaw(): |
| 49 |
check( |
| 50 |
b"\x96\xda\x00\x00\xda\x00\x01a\xda\x00\x02ab\xdb\x00\x00" |
| 51 |
b"\x00\x00\xdb\x00\x00\x00\x01a\xdb\x00\x00\x00\x02ab", |
| 52 |
(b"", b"a", b"ab", b"", b"a", b"ab"), |
| 53 |
) |
| 54 |
check( |
| 55 |
b"\x96\xda\x00\x00\xda\x00\x01a\xda\x00\x02ab\xdb\x00\x00" |
| 56 |
b"\x00\x00\xdb\x00\x00\x00\x01a\xdb\x00\x00\x00\x02ab", |
| 57 |
("", "a", "ab", "", "a", "ab"), |
| 58 |
raw=False, |
| 59 |
) |
| 60 |
|
| 61 |
|
| 62 |
def testArray(): |
| 63 |
check( |
| 64 |
b"\x96\xdc\x00\x00\xdc\x00\x01\xc0\xdc\x00\x02\xc2\xc3\xdd\x00" |
| 65 |
b"\x00\x00\x00\xdd\x00\x00\x00\x01\xc0\xdd\x00\x00\x00\x02" |
| 66 |
b"\xc2\xc3", |
| 67 |
((), (None,), (False, True), (), (None,), (False, True)), |
| 68 |
) |
| 69 |
|
| 70 |
|
| 71 |
def testMap(): |
| 72 |
check( |
| 73 |
b"\x96" |
| 74 |
b"\xde\x00\x00" |
| 75 |
b"\xde\x00\x01\xc0\xc2" |
| 76 |
b"\xde\x00\x02\xc0\xc2\xc3\xc2" |
| 77 |
b"\xdf\x00\x00\x00\x00" |
| 78 |
b"\xdf\x00\x00\x00\x01\xc0\xc2" |
| 79 |
b"\xdf\x00\x00\x00\x02\xc0\xc2\xc3\xc2", |
| 80 |
( |
| 81 |
{}, |
| 82 |
{None: False}, |
| 83 |
{True: False, None: False}, |
| 84 |
{}, |
| 85 |
{None: False}, |
| 86 |
{True: False, None: False}, |
| 87 |
), |
| 88 |
) |