def test_decode_predefined_atoms(self): self.assertEqual((True, b""), decode(b"\x83d\0\4true")) self.assertEqual((True, b"tail"), decode(b"\x83d\0\4truetail")) self.assertEqual((False, b""), decode(b"\x83d\0\5false")) self.assertEqual((False, b"tail"), decode(b"\x83d\0\5falsetail")) self.assertEqual((None, b""), decode(b"\x83d\0\11undefined")) self.assertEqual((None, b"tail"), decode(b"\x83d\0\11undefinedtail"))
def test_decode_small_tuple(self): self.assertRaises(IncompleteData, decode, "\x83h") self.assertRaises(IncompleteData, decode, "\x83h\1") self.assertEqual(((), ""), decode("\x83h\0")) self.assertEqual(((), "tail"), decode("\x83h\0tail")) self.assertEqual((([], []), ""), decode("\x83h\2jj")) self.assertEqual((([], []), "tail"), decode("\x83h\2jjtail"))
def test_decode_python_opaque_object(self): data, tail = decode(b"\x83h\3d\0\x0f$erlport.opaqued\0\6python" b"m\0\0\0\14S'test'\np0\n.") self.assertEqual("test", data) self.assertEqual(b"", tail) data, tail = decode(b"\x83h\3d\0\x0f$erlport.opaqued\0\6python" b"m\0\0\0\14S'test'\np0\n.tail") self.assertEqual("test", data) self.assertEqual(b"tail", tail)
def test_decode_small_tuple(self): self.assertRaises(IncompleteData, decode, b"\x83h") self.assertRaises(IncompleteData, decode, b"\x83h\1") self.assertEqual(((), b""), decode(b"\x83h\0")) self.assertEqual(((), b"tail"), decode(b"\x83h\0tail")) self.assertEqual((([], []), b""), decode(b"\x83h\2jj")) self.assertEqual((([], []), b"tail"), decode(b"\x83h\2jjtail"))
def test_decode_atom(self): self.assertRaises(IncompleteData, decode, b"\x83d") self.assertRaises(IncompleteData, decode, b"\x83d\0") self.assertRaises(IncompleteData, decode, b"\x83d\0\1") self.assertEqual((Atom(b""), b""), decode(b"\x83d\0\0")) self.assertEqual((Atom(b""), b"tail"), decode(b"\x83d\0\0tail")) self.assertEqual((Atom(b"test"), b""), decode(b"\x83d\0\4test")) self.assertEqual((Atom(b"test"), b"tail"), decode(b"\x83d\0\4testtail"))
def test_decode_python_opaque_object(self): data, tail = decode("\x83h\3d\0\x0f$erlport.opaqued\0\6python" "m\0\0\0\14S'test'\np0\n.") self.assertEqual("test", data) self.assertEqual("", tail) data, tail = decode("\x83h\3d\0\x0f$erlport.opaqued\0\6python" "m\0\0\0\14S'test'\np0\n.tail") self.assertEqual("test", data) self.assertEqual("tail", tail)
def test_decode_binary(self): self.assertRaises(IncompleteData, decode, b"\x83m") self.assertRaises(IncompleteData, decode, b"\x83m\0") self.assertRaises(IncompleteData, decode, b"\x83m\0\0") self.assertRaises(IncompleteData, decode, b"\x83m\0\0\0") self.assertRaises(IncompleteData, decode, b"\x83m\0\0\0\1") self.assertEqual((b"", b""), decode(b"\x83m\0\0\0\0")) self.assertEqual((b"", b"tail"), decode(b"\x83m\0\0\0\0tail")) self.assertEqual((b"data", b""), decode(b"\x83m\0\0\0\4data")) self.assertEqual((b"data", b"tail"), decode(b"\x83m\0\0\0\4datatail"))
def test_decode_string_list(self): self.assertRaises(IncompleteData, decode, b"\x83k") self.assertRaises(IncompleteData, decode, b"\x83k\0") self.assertRaises(IncompleteData, decode, b"\x83k\0\1") # Erlang use 'j' tag for empty lists self.assertEqual(([], b""), decode(b"\x83k\0\0")) self.assertEqual(([], b"tail"), decode(b"\x83k\0\0tail")) self.assertEqual(([116, 101, 115, 116], b""), decode(b"\x83k\0\4test")) self.assertTrue(isinstance(decode(b"\x83k\0\4test")[0], List)) self.assertEqual(([116, 101, 115, 116], b"tail"), decode(b"\x83k\0\4testtail"))
def test_decode_binary(self): self.assertRaises(IncompleteData, decode, "\x83m") self.assertRaises(IncompleteData, decode, "\x83m\0") self.assertRaises(IncompleteData, decode, "\x83m\0\0") self.assertRaises(IncompleteData, decode, "\x83m\0\0\0") self.assertRaises(IncompleteData, decode, "\x83m\0\0\0\1") self.assertEqual(("", ""), decode("\x83m\0\0\0\0")) self.assertEqual(("", "tail"), decode("\x83m\0\0\0\0tail")) self.assertEqual(("data", ""), decode("\x83m\0\0\0\4data")) self.assertEqual(("data", "tail"), decode("\x83m\0\0\0\4datatail"))
def test_decode_small_big_integer(self): self.assertRaises(IncompleteData, decode, b"\x83n") self.assertRaises(IncompleteData, decode, b"\x83n\0") self.assertRaises(IncompleteData, decode, b"\x83n\1\0") # Erlang use 'a' tag for small integers self.assertEqual((0, b""), decode(b"\x83n\0\0")) self.assertEqual((0, b"tail"), decode(b"\x83n\0\0tail")) self.assertEqual((6618611909121, b""), decode(b"\x83n\6\0\1\2\3\4\5\6")) self.assertEqual((-6618611909121, b""), decode(b"\x83n\6\1\1\2\3\4\5\6")) self.assertEqual((6618611909121, b"tail"), decode(b"\x83n\6\0\1\2\3\4\5\6tail"))
def test_decode_large_tuple(self): self.assertRaises(IncompleteData, decode, b"\x83i") self.assertRaises(IncompleteData, decode, b"\x83i\0") self.assertRaises(IncompleteData, decode, b"\x83i\0\0") self.assertRaises(IncompleteData, decode, b"\x83i\0\0\0") self.assertRaises(IncompleteData, decode, b"\x83i\0\0\0\1") # Erlang use 'h' tag for small tuples self.assertEqual(((), b""), decode(b"\x83i\0\0\0\0")) self.assertEqual(((), b"tail"), decode(b"\x83i\0\0\0\0tail")) self.assertEqual((([], []), b""), decode(b"\x83i\0\0\0\2jj")) self.assertEqual((([], []), b"tail"), decode(b"\x83i\0\0\0\2jjtail"))
def test_decode_opaque_object(self): opaque, tail = decode(b"\x83h\3d\0\x0f$erlport.opaqued\0\10language" b"m\0\0\0\4data") self.assertEqual(OpaqueObject, type(opaque)) self.assertEqual(b"data", opaque.data) self.assertEqual(b"language", opaque.language) self.assertEqual(b"", tail) opaque, tail = decode(b"\x83h\3d\0\x0f$erlport.opaqued\0\10language" b"m\0\0\0\4datatail") self.assertEqual(OpaqueObject, type(opaque)) self.assertEqual(b"data", opaque.data) self.assertEqual(b"language", opaque.language) self.assertEqual(b"tail", tail)
def test_decode_compressed_term(self): self.assertRaises(IncompleteData, decode, "\x83P") self.assertRaises(IncompleteData, decode, "\x83P\0") self.assertRaises(IncompleteData, decode, "\x83P\0\0") self.assertRaises(IncompleteData, decode, "\x83P\0\0\0") self.assertRaises(IncompleteData, decode, "\x83P\0\0\0\0") self.assertRaises(ValueError, decode, "\x83P\0\0\0\x16" "\x78\xda\xcb\x66\x10\x49\xc1\2\0\x5d\x60\x08\x50") self.assertEqual(([100] * 20, ""), decode("\x83P\0\0\0\x17" "\x78\xda\xcb\x66\x10\x49\xc1\2\0\x5d\x60\x08\x50")) self.assertEqual( ([100] * 20, "tail"), decode("\x83P\0\0\0\x17" "\x78\xda\xcb\x66\x10\x49\xc1\2\0\x5d\x60\x08\x50tail") )
def test_decode_large_tuple(self): self.assertRaises(IncompleteData, decode, "\x83i") self.assertRaises(IncompleteData, decode, "\x83i\0") self.assertRaises(IncompleteData, decode, "\x83i\0\0") self.assertRaises(IncompleteData, decode, "\x83i\0\0\0") self.assertRaises(IncompleteData, decode, "\x83i\0\0\0\1") # Erlang use 'h' tag for small tuples self.assertEqual(((), ""), decode("\x83i\0\0\0\0")) self.assertEqual(((), "tail"), decode("\x83i\0\0\0\0tail")) self.assertEqual((([], []), ""), decode("\x83i\0\0\0\2jj")) self.assertEqual((([], []), "tail"), decode("\x83i\0\0\0\2jjtail"))
def test_decode_small_big_integer(self): self.assertRaises(IncompleteData, decode, "\x83n") self.assertRaises(IncompleteData, decode, "\x83n\0") self.assertRaises(IncompleteData, decode, "\x83n\1\0") # Erlang use 'a' tag for small integers self.assertEqual((0, ""), decode("\x83n\0\0")) self.assertEqual((0, "tail"), decode("\x83n\0\0tail")) self.assertEqual((6618611909121, ""), decode("\x83n\6\0\1\2\3\4\5\6")) self.assertEqual((-6618611909121, ""), decode("\x83n\6\1\1\2\3\4\5\6")) self.assertEqual((6618611909121, "tail"), decode("\x83n\6\0\1\2\3\4\5\6tail"))
def test_decode_string_list(self): self.assertRaises(IncompleteData, decode, "\x83k") self.assertRaises(IncompleteData, decode, "\x83k\0") self.assertRaises(IncompleteData, decode, "\x83k\0\1") # Erlang use 'j' tag for empty lists self.assertEqual(([], ""), decode("\x83k\0\0")) self.assertEqual(([], "tail"), decode("\x83k\0\0tail")) self.assertEqual(([116, 101, 115, 116], ""), decode("\x83k\0\4test")) self.assertTrue(isinstance(decode("\x83k\0\4test")[0], List)) self.assertEqual(([116, 101, 115, 116], "tail"), decode("\x83k\0\4testtail"))
def test_decode_compressed_term(self): self.assertRaises(IncompleteData, decode, b"\x83P") self.assertRaises(IncompleteData, decode, b"\x83P\0") self.assertRaises(IncompleteData, decode, b"\x83P\0\0") self.assertRaises(IncompleteData, decode, b"\x83P\0\0\0") self.assertRaises(IncompleteData, decode, b"\x83P\0\0\0\0") self.assertRaises(ValueError, decode, b"\x83P\0\0\0\x16" b"\x78\xda\xcb\x66\x10\x49\xc1\2\0\x5d\x60\x08\x50") self.assertEqual(([100] * 20, b""), decode(b"\x83P\0\0\0\x17" b"\x78\xda\xcb\x66\x10\x49\xc1\2\0\x5d\x60\x08\x50")) self.assertEqual(([100] * 20, b"tail"), decode(b"\x83P\0\0\0\x17" b"\x78\xda\xcb\x66\x10\x49\xc1\2\0\x5d\x60\x08\x50tail"))
def test_decode_improper_list(self): self.assertRaises(IncompleteData, decode, "\x83l\0\0\0\0k") improper, tail = decode("\x83l\0\0\0\1jd\0\4tail") self.assertEqual(ImproperList, type(improper)) self.assertEqual([[]], list(improper)) self.assertEqual(Atom("tail"), improper.tail) self.assertEqual("", tail) improper, tail = decode("\x83l\0\0\0\1jd\0\4tailtail") self.assertEqual(ImproperList, type(improper)) self.assertEqual([[]], list(improper)) self.assertEqual(Atom("tail"), improper.tail) self.assertEqual("tail", tail)
def test_decode_list(self): self.assertRaises(IncompleteData, decode, "\x83l") self.assertRaises(IncompleteData, decode, "\x83l\0") self.assertRaises(IncompleteData, decode, "\x83l\0\0") self.assertRaises(IncompleteData, decode, "\x83l\0\0\0") self.assertRaises(IncompleteData, decode, "\x83l\0\0\0\0") # Elang use 'j' tag for empty lists self.assertEqual(([], ""), decode("\x83l\0\0\0\0j")) self.assertEqual(([], "tail"), decode("\x83l\0\0\0\0jtail")) self.assertEqual(([[], []], ""), decode("\x83l\0\0\0\2jjj")) self.assertTrue(isinstance(decode("\x83l\0\0\0\2jjj")[0], List)) self.assertEqual(([[], []], "tail"), decode("\x83l\0\0\0\2jjjtail"))
def test_decode_list(self): self.assertRaises(IncompleteData, decode, b"\x83l") self.assertRaises(IncompleteData, decode, b"\x83l\0") self.assertRaises(IncompleteData, decode, b"\x83l\0\0") self.assertRaises(IncompleteData, decode, b"\x83l\0\0\0") self.assertRaises(IncompleteData, decode, b"\x83l\0\0\0\0") # Elang use 'j' tag for empty lists self.assertEqual(([], b""), decode(b"\x83l\0\0\0\0j")) self.assertEqual(([], b"tail"), decode(b"\x83l\0\0\0\0jtail")) self.assertEqual(([[], []], b""), decode(b"\x83l\0\0\0\2jjj")) self.assertTrue(isinstance(decode(b"\x83l\0\0\0\2jjj")[0], List)) self.assertEqual(([[], []], b"tail"), decode(b"\x83l\0\0\0\2jjjtail"))
def test_decode_improper_list(self): self.assertRaises(IncompleteData, decode, b"\x83l\0\0\0\0k") improper, tail = decode(b"\x83l\0\0\0\1jd\0\4tail") self.assertEqual(ImproperList, type(improper)) self.assertEqual([[]], list(improper)) self.assertEqual(Atom(b"tail"), improper.tail) self.assertEqual(b"", tail) improper, tail = decode(b"\x83l\0\0\0\1jd\0\4tailtail") self.assertEqual(ImproperList, type(improper)) self.assertEqual([[]], list(improper)) self.assertEqual(Atom(b"tail"), improper.tail) self.assertEqual(b"tail", tail)
def test_empty_map(self): input = Map() self.assertEquals( (input, b""), decode(encode(input)) ) input = {} self.assertEquals( (input, b""), decode(encode(input)) )
def test_decode_float(self): self.assertRaises(IncompleteData, decode, b"\x83F") self.assertRaises(IncompleteData, decode, b"\x83F\0") self.assertRaises(IncompleteData, decode, b"\x83F\0\0") self.assertRaises(IncompleteData, decode, b"\x83F\0\0\0") self.assertRaises(IncompleteData, decode, b"\x83F\0\0\0\0") self.assertRaises(IncompleteData, decode, b"\x83F\0\0\0\0\0") self.assertRaises(IncompleteData, decode, b"\x83F\0\0\0\0\0\0") self.assertRaises(IncompleteData, decode, b"\x83F\0\0\0\0\0\0\0") self.assertEqual((0.0, b""), decode(b"\x83F\0\0\0\0\0\0\0\0")) self.assertEqual((0.0, b"tail"), decode(b"\x83F\0\0\0\0\0\0\0\0tail")) self.assertEqual((1.5, b""), decode(b"\x83F?\xf8\0\0\0\0\0\0")) self.assertEqual((1.5, b"tail"), decode(b"\x83F?\xf8\0\0\0\0\0\0tail"))
def test_decode_opaque_object(self): opaque, tail = decode("\x83h\3d\0\x0f$erlport.opaqued\0\10language" "m\0\0\0\4data") self.assertEqual(OpaqueObject, type(opaque)) self.assertEqual("data", opaque.data) self.assertEqual("language", opaque.language) self.assertEqual("", tail) opaque, tail = decode("\x83h\3d\0\x0f$erlport.opaqued\0\10language" "m\0\0\0\4datatail") self.assertEqual(OpaqueObject, type(opaque)) self.assertEqual("data", opaque.data) self.assertEqual("language", opaque.language) self.assertEqual("tail", tail)
def test_decode_float(self): self.assertRaises(IncompleteData, decode, "\x83F") self.assertRaises(IncompleteData, decode, "\x83F\0") self.assertRaises(IncompleteData, decode, "\x83F\0\0") self.assertRaises(IncompleteData, decode, "\x83F\0\0\0") self.assertRaises(IncompleteData, decode, "\x83F\0\0\0\0") self.assertRaises(IncompleteData, decode, "\x83F\0\0\0\0\0") self.assertRaises(IncompleteData, decode, "\x83F\0\0\0\0\0\0") self.assertRaises(IncompleteData, decode, "\x83F\0\0\0\0\0\0\0") self.assertEqual((0.0, ""), decode("\x83F\0\0\0\0\0\0\0\0")) self.assertEqual((0.0, "tail"), decode("\x83F\0\0\0\0\0\0\0\0tail")) self.assertEqual((1.5, ""), decode("\x83F?\xf8\0\0\0\0\0\0")) self.assertEqual((1.5, "tail"), decode("\x83F?\xf8\0\0\0\0\0\0tail"))
def test_big_map(self): input = {b"hello_" + str(i): b"world" + str(i) for i in range(10000)} self.assertEquals( (input, b""), decode(encode(input)) )
def test_decode_map(self): self.assertEqual( (Map({"hello": "world"}), b""), decode( b"\x83t\x00\x00\x00\x01m\x00\x00\x00\x05hellom\x00\x00\x00\x05world" )) self.assertEqual( (Map({"hello": "world"}), b"rest"), decode( b"\x83t\x00\x00\x00\x01m\x00\x00\x00\x05hellom\x00\x00\x00\x05worldrest" )) self.assertEqual( (Map({"hello": List()}), b""), decode(b"\x83t\x00\x00\x00\x01m\x00\x00\x00\x05helloj")) self.assertRaises(IncompleteData, decode, b"\x83t\x00\x00\x00")
def test_decode_atom(self): # ATOM_EXT: self.assertRaises(IncompleteData, decode, b"\x83d") self.assertRaises(IncompleteData, decode, b"\x83d\0") self.assertRaises(IncompleteData, decode, b"\x83d\0\1") self.assertEqual((Atom(b""), b""), decode(b"\x83d\0\0")) self.assertEqual((Atom(b""), b"tail"), decode(b"\x83d\0\0tail")) self.assertEqual((Atom(b"test"), b""), decode(b"\x83d\0\4test")) self.assertEqual((Atom(b"test"), b"tail"), decode(b"\x83d\0\4testtail")) self.assertEqual((Atom(b"\xe4"), b""), decode(b"\x83d\0\1\xe4")) # SMALL_ATOM_UTF8_EXT: self.assertRaises(IncompleteData, decode, b"\x83w") self.assertRaises(IncompleteData, decode, b"\x83w\1") self.assertEqual((Atom(b""), b""), decode(b"\x83w\0")) self.assertEqual((Atom(b""), b"tail"), decode(b"\x83w\0tail")) self.assertEqual((Atom(b"test"), b""), decode(b"\x83w\4test")) self.assertEqual((Atom(b"test"), b"tail"), decode(b"\x83w\4testtail")) self.assertEqual((Atom(b"\xc3\xa4"), b""), decode(b"\x83w\2\xc3\xa4"))
def test_decode_atom(self): # ATOM_EXT: self.assertRaises(IncompleteData, decode, "\x83d") self.assertRaises(IncompleteData, decode, "\x83d\0") self.assertRaises(IncompleteData, decode, "\x83d\0\1") self.assertEqual((Atom(""), ""), decode("\x83d\0\0")) self.assertEqual((Atom(""), "tail"), decode("\x83d\0\0tail")) self.assertEqual((Atom("test"), ""), decode("\x83d\0\4test")) self.assertEqual((Atom("test"), "tail"), decode("\x83d\0\4testtail")) self.assertEqual((Atom("\xe4"), ""), decode("\x83d\0\1\xe4")) # SMALL_ATOM_UTF8_EXT: self.assertRaises(IncompleteData, decode, "\x83w") self.assertRaises(IncompleteData, decode, "\x83w\1") self.assertEqual((Atom(""), ""), decode("\x83w\0")) self.assertEqual((Atom(""), "tail"), decode("\x83w\0tail")) self.assertEqual((Atom("test"), ""), decode("\x83w\4test")) self.assertEqual((Atom("test"), "tail"), decode("\x83w\4testtail")) self.assertEqual((Atom("\xc3\xa4"), ""), decode("\x83w\2\xc3\xa4"))
def read_message(self): """Read incoming message.""" data = self._fin.read(self.packet) if not data: raise EOFError() length, = unpack(self._format, data) data = self._fin.read(length) if not data: raise EOFError() return decode(data)[0]
def test_hash_list(self): input = Map({List([1, 2, 3]): [4, 5, 6]}) self.assertEquals( input[List([1, 2, 3])], List([4, 5, 6]) ) self.assertEquals( (input, b""), decode(encode(input)) )
def read(self): """Read incoming message.""" packet = self.packet with self.__read_lock: buffer = self.__buffer while len(buffer) < packet: buffer += self._read_data() length = self.__unpack(buffer[:packet])[0] + packet while len(buffer) < length: buffer += self._read_data() term, self.__buffer = decode(buffer[packet:]) return term
def test_hash_improper_list(self): input = Map({ ImproperList([1, 2, 3], 1000): b"xxx" }) self.assertEquals( (input, ""), decode(encode(input)) ) self.assertEquals( input[ImproperList([1, 2, 3], 1000)], b"xxx")
def test_decode_map(self): self.assertEqual( (Map(hello="world"), ""), decode( "\x83t\x00\x00\x00\x01m\x00\x00\x00\x05" "hellom\x00\x00\x00\x05world") ) self.assertEqual( (Map(hello="world"), "rest"), decode( "\x83t\x00\x00\x00\x01m\x00\x00\x00\x05" "hellom\x00\x00\x00\x05worldrest") ) self.assertEqual( (Map(hello=List()), ""), decode("\x83t\x00\x00\x00\x01m\x00\x00\x00\x05helloj") ) self.assertRaises(IncompleteData, decode, "\x83t\x00\x00\x00")
def test_decode_integer(self): self.assertRaises(IncompleteData, decode, b"\x83b") self.assertRaises(IncompleteData, decode, b"\x83b\0") self.assertRaises(IncompleteData, decode, b"\x83b\0\0") self.assertRaises(IncompleteData, decode, b"\x83b\0\0\0") # Erlang use 'a' tag for small integers self.assertEqual((0, b""), decode(b"\x83b\0\0\0\0")) self.assertEqual((0, b"tail"), decode(b"\x83b\0\0\0\0tail")) self.assertEqual((2147483647, b""), decode(b"\x83b\x7f\xff\xff\xff")) self.assertEqual((2147483647, b"tail"), decode(b"\x83b\x7f\xff\xff\xfftail")) self.assertEqual((-1, b""), decode(b"\x83b\xff\xff\xff\xff")) self.assertEqual((-1, b"tail"), decode(b"\x83b\xff\xff\xff\xfftail"))
def test_decode_integer(self): self.assertRaises(IncompleteData, decode, "\x83b") self.assertRaises(IncompleteData, decode, "\x83b\0") self.assertRaises(IncompleteData, decode, "\x83b\0\0") self.assertRaises(IncompleteData, decode, "\x83b\0\0\0") # Erlang use 'a' tag for small integers self.assertEqual((0, ""), decode("\x83b\0\0\0\0")) self.assertEqual((0, "tail"), decode("\x83b\0\0\0\0tail")) self.assertEqual((2147483647, ""), decode("\x83b\x7f\xff\xff\xff")) self.assertEqual((2147483647, "tail"), decode("\x83b\x7f\xff\xff\xfftail")) self.assertEqual((-1, ""), decode("\x83b\xff\xff\xff\xff")) self.assertEqual((-1, "tail"), decode("\x83b\xff\xff\xff\xfftail"))
def test_decode_predefined_atoms(self): # ATOM_EXT: self.assertEqual((True, b""), decode(b"\x83d\0\4true")) self.assertEqual((True, b"tail"), decode(b"\x83d\0\4truetail")) self.assertEqual((False, b""), decode(b"\x83d\0\5false")) self.assertEqual((False, b"tail"), decode(b"\x83d\0\5falsetail")) self.assertEqual((None, b""), decode(b"\x83d\0\11undefined")) self.assertEqual((None, b"tail"), decode(b"\x83d\0\11undefinedtail")) # SMALL_ATOM_UTF8_EXT: self.assertEqual((True, b""), decode(b"\x83w\4true")) self.assertEqual((True, b"tail"), decode(b"\x83w\4truetail")) self.assertEqual((False, b""), decode(b"\x83w\5false")) self.assertEqual((False, b"tail"), decode(b"\x83w\5falsetail")) self.assertEqual((None, b""), decode(b"\x83w\11undefined")) self.assertEqual((None, b"tail"), decode(b"\x83w\11undefinedtail"))
def test_empty_list(self): input = List() self.assertEquals( (input, b""), decode(encode(input)) )
def test_decode_small_integer(self): self.assertRaises(IncompleteData, decode, b"\x83a") self.assertEqual((0, b""), decode(b"\x83a\0")) self.assertEqual((0, b"tail"), decode(b"\x83a\0tail")) self.assertEqual((255, b""), decode(b"\x83a\xff")) self.assertEqual((255, b"tail"), decode(b"\x83a\xfftail"))
def test_simple_list(self): input = List([1, 2, 3]) self.assertEquals( (input, b""), decode(encode(input)) )
def test_nested_list(self): input = List([[[[[[]]]]]]) self.assertEquals( (input, b""), decode(encode(input)) )
def test_simple_map(self): input = Map({b"hello": b"world"}) self.assertEquals( (input, b""), decode(encode(input)) )
def test_opaque(self): input = _TestObj(100) output, _ = decode(encode(input)) self.assertEquals(input.v, input.v)
def test_decode_predefined_atoms(self): # ATOM_EXT: self.assertEqual((True, ""), decode("\x83d\0\4true")) self.assertEqual((True, "tail"), decode("\x83d\0\4truetail")) self.assertEqual((False, ""), decode("\x83d\0\5false")) self.assertEqual((False, "tail"), decode("\x83d\0\5falsetail")) self.assertEqual((None, ""), decode("\x83d\0\11undefined")) self.assertEqual((None, "tail"), decode("\x83d\0\11undefinedtail")) # SMALL_ATOM_UTF8_EXT: self.assertEqual((True, ""), decode("\x83w\4true")) self.assertEqual((True, "tail"), decode("\x83w\4truetail")) self.assertEqual((False, ""), decode("\x83w\5false")) self.assertEqual((False, "tail"), decode("\x83w\5falsetail")) self.assertEqual((None, ""), decode("\x83w\11undefined")) self.assertEqual((None, "tail"), decode("\x83w\11undefinedtail"))
def test_decode_empty_list(self): self.assertEqual(([], ""), decode("\x83j")) self.assertTrue(isinstance(decode("\x83j")[0], List)) self.assertEqual(([], "tail"), decode("\x83jtail"))
def test_decode_small_integer(self): self.assertRaises(IncompleteData, decode, "\x83a") self.assertEqual((0, ""), decode("\x83a\0")) self.assertEqual((0, "tail"), decode("\x83a\0tail")) self.assertEqual((255, ""), decode("\x83a\xff")) self.assertEqual((255, "tail"), decode("\x83a\xfftail"))
def test_decode_empty_list(self): self.assertEqual(([], b""), decode(b"\x83j")) self.assertTrue(isinstance(decode(b"\x83j")[0], List)) self.assertEqual(([], b"tail"), decode(b"\x83jtail"))
def read(self): """Read incoming message.""" data = self._read_data(self.packet) length, = unpack(self._format, data) data = self._read_data(length) return decode(data)[0]