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