<s> import unittest <EOL> from minecraft . networking . types import ( <EOL> Type , Boolean , UnsignedByte , Byte , Short , UnsignedShort , <EOL> Integer , VarInt , Long , Float , Double , ShortPrefixedByteArray , <EOL> VarIntPrefixedByteArray , String as StringType <EOL> ) <EOL> from minecraft . networking . packets import PacketBuffer <EOL> TEST_DATA = { <EOL> Boolean : [ True , False ] , <EOL> UnsignedByte : [ <NUM_LIT:0> , <NUM_LIT> ] , <EOL> Byte : [ - <NUM_LIT> , <NUM_LIT> ] , <EOL> Short : [ - <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> UnsignedShort : [ <NUM_LIT:0> , <NUM_LIT> ] , <EOL> Integer : [ - <NUM_LIT:1000> , <NUM_LIT:1000> ] , <EOL> VarInt : [ <NUM_LIT:1> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> Long : [ <NUM_LIT> ] , <EOL> Float : [ <NUM_LIT> ] , <EOL> Double : [ <NUM_LIT> ] , <EOL> ShortPrefixedByteArray : [ bytes ( <NUM_LIT> ) ] , <EOL> VarIntPrefixedByteArray : [ bytes ( <NUM_LIT> ) ] , <EOL> StringType : [ "<STR_LIT>" ] <EOL> } <EOL> class SerializationTest ( unittest . TestCase ) : <EOL> def test_serialization ( self ) : <EOL> for data_type in Type . __subclasses__ ( ) : <EOL> if data_type in TEST_DATA : <EOL> test_cases = TEST_DATA [ data_type ] <EOL> for test_data in test_cases : <EOL> packet_buffer = PacketBuffer ( ) <EOL> data_type . send ( test_data , packet_buffer ) <EOL> packet_buffer . reset_cursor ( ) <EOL> deserialized = data_type . read ( packet_buffer ) <EOL> if data_type is Float or data_type is Double : <EOL> self . assertAlmostEquals ( test_data , deserialized , <NUM_LIT:3> ) <EOL> else : <EOL> self . assertEqual ( test_data , deserialized ) <EOL> def test_exceptions ( self ) : <EOL> base_type = Type ( ) <EOL> with self . assertRaises ( NotImplementedError ) : <EOL> base_type . read ( None ) <EOL> with self . assertRaises ( NotImplementedError ) : <EOL> base_type . send ( None , None ) <EOL> empty_socket = PacketBuffer ( ) <EOL> with self . assertRaises ( RuntimeError ) : <EOL> VarInt . read_socket ( empty_socket ) <EOL> def test_varint ( self ) : <EOL> self . assertEqual ( VarInt . size ( <NUM_LIT:2> ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( VarInt . size ( <NUM_LIT> ) , <NUM_LIT:2> ) <EOL> packet_buffer = PacketBuffer ( ) <EOL> VarInt . send ( <NUM_LIT> , packet_buffer ) <EOL> packet_buffer . reset_cursor ( ) <EOL> self . assertEqual ( VarInt . read_socket ( packet_buffer ) , <NUM_LIT> ) </s>
