def _negative(n): # pack packed=pymsgpack.pack(n) self.assertEqual(struct.pack('B', 0x100+n), packed) # parse parsed=pymsgpack.Parser(packed) self.assertEqual(n, parsed.get_number())
def test_false(self): # pack packed=pymsgpack.pack(False) self.assertEqual(struct.pack('B', 0xc2), packed) # parse parsed=pymsgpack.Parser(packed) self.assertFalse(parsed.get_bool())
def test_true(self): # pack packed=pymsgpack.pack(True) self.assertEqual(struct.pack('B', 0xc3), packed) # parse parsed=pymsgpack.Parser(packed) self.assertTrue(parsed.get_bool())
def test_array(self): def _test_array(src, dst): # pack packed=pymsgpack.pack(src) self.assertEqual(dst, bytes(packed)) # parse parsed=pymsgpack.Parser(packed) self.assertTrue(parsed.is_array()) self.assertEqual(src, parsed.get()) # fixarray _test_array([1, 2, 3] , b'\x93\x01\x02\x03') _test_array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] , b'\x9F\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F') # array16 n=0xF+1 _test_array([1 for x in range(n)] , b''.join([b'\xdc', struct.pack('>H', n), b'\x01'*n])) # array32 n=0xFFFF+1 _test_array([1 for x in range(n)] , b''.join([b'\xdd', struct.pack('>I', n), b'\x01'*n])) # iter packed=pymsgpack.pack([1, {2: 2}, 3]) parsed=[x for x in pymsgpack.Parser(packed)] self.assertEqual(3, len(parsed))
def _test_map(src, dst): # pack packed=pymsgpack.pack(src) self.assertEqual(dst, packed) # parse parsed=pymsgpack.Parser(packed) self.assertTrue(parsed.is_map()) self.assertEqual(src, parsed.get())
def _test_array(src, dst): # pack packed=pymsgpack.pack(src) self.assertEqual(dst, bytes(packed)) # parse parsed=pymsgpack.Parser(packed) self.assertTrue(parsed.is_array()) self.assertEqual(src, parsed.get())
def test_construct(self): partial=pymsgpack.pack([1, 2, 3]) parsed=pymsgpack.Parser(partial) packed=pymsgpack.pack({ 'key': parsed }) self.assertEqual(b'\x81\xa3\x6b\x65\x79\x93\x01\x02\x03',bytes(packed))
def _test_bin(src, fmt, head): # pack packed=pymsgpack.pack(src) src_len=len(src) _fmt=fmt % src_len self.assertEqual(struct.pack(_fmt, head, src_len, src), packed) # parse parsed=pymsgpack.Parser(packed) self.assertEqual(src, parsed.get_bin())
def _test_str(src): # pack packed=pymsgpack.pack(src) utf8=src.encode('utf-8') fmt='B%ds' % len(utf8) self.assertEqual(struct.pack(fmt, 0xa0 + len(utf8), utf8), packed) # parse parsed=pymsgpack.Parser(packed) self.assertEqual(src, parsed.get_str())
def test_nil(self): # pack packed=pymsgpack.pack(None) self.assertEqual(struct.pack('B', 0xc0), packed) # parse parsed=pymsgpack.Parser(packed) self.assertTrue(parsed.is_nil()) self.assertEqual(1, len(parsed.get_bytes()))
def _test_longstr(src, fmt, head): # pack packed=pymsgpack.pack(src) utf8=src.encode('utf-8') utf8_len=len(utf8) _fmt=fmt % utf8_len self.assertEqual(struct.pack(_fmt, head, utf8_len, utf8), packed) # parse parsed=pymsgpack.Parser(packed) self.assertEqual(src, parsed.get_str())
def _number(src, fmt, head): dst=struct.pack(fmt, head, src) packed=pymsgpack.pack(src) if isinstance(src, float): self.assertAlmostEqual(dst, packed) else: self.assertEqual(dst, packed) parsed=pymsgpack.Parser(packed) if isinstance(src, float): self.assertAlmostEqual(src, parsed.get_number()) else: self.assertEqual(src, parsed.get_number())
def test_nest(self): src={ 'nest': { 'hoge': 1 } } # pack packed=pymsgpack.pack(src) #self.assertEqual(dst, packed) # parse parsed=pymsgpack.Parser(packed) #self.assertTrue(parsed.is_map()) self.assertEqual(src, parsed.get())