From 08e126188a0e77f8cee1b21fe1331db136d8b100 Mon Sep 17 00:00:00 2001 From: Ammar Askar Date: Fri, 3 Apr 2015 08:04:59 +0500 Subject: [PATCH] Add a serialization test --- MANIFEST.in | 1 + minecraft/networking/types.py | 2 +- tests/test_serialization.py | 42 +++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 tests/test_serialization.py diff --git a/MANIFEST.in b/MANIFEST.in index 0077ebb..ef709c6 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -4,6 +4,7 @@ include requirements.txt include tox.ini include start.py recursive-include tests *.py +recursive-include tests *.bin recursive-include minecraft *.py recursive-include docs *.rst include docs/Makefile diff --git a/minecraft/networking/types.py b/minecraft/networking/types.py index af4779f..099fd07 100644 --- a/minecraft/networking/types.py +++ b/minecraft/networking/types.py @@ -104,7 +104,7 @@ class VarInt(Type): @staticmethod def send(value, socket): - out = "" + out = bytes() while True: byte = value & 0x7F value >>= 7 diff --git a/tests/test_serialization.py b/tests/test_serialization.py new file mode 100644 index 0000000..bb5b292 --- /dev/null +++ b/tests/test_serialization.py @@ -0,0 +1,42 @@ +import unittest +from minecraft.networking.types import ( + Type, Boolean, UnsignedByte, Byte, Short, UnsignedShort, + Integer, VarInt, Long, Float, Double, ShortPrefixedByteArray, + VarIntPrefixedByteArray, String +) +from minecraft.networking.packets import PacketBuffer + +TEST_DATA = { + Boolean: [True, False], + UnsignedByte: [0, 125], + Byte: [-22, 22], + Short: [-340, 22, 350], + UnsignedShort: [0, 400], + Integer: [-1000, 1000], + VarInt: [1, 250, 50000, 10000000], + Long: [50000000], + Float: [21.000301], + Double: [36.004002], + ShortPrefixedByteArray: [bytes(245)], + VarIntPrefixedByteArray: [bytes(1234)], + String: ["hello world"] +} + + +class SerializationTest(unittest.TestCase): + + def test_serialization(self): + for data_type in Type.__subclasses__(): + if data_type in TEST_DATA: + test_cases = TEST_DATA[data_type] + + for test_data in test_cases: + packet_buffer = PacketBuffer() + data_type.send(test_data, packet_buffer) + packet_buffer.reset_cursor() + + deserialized = data_type.read(packet_buffer) + if data_type is Float or data_type is Double: + self.assertAlmostEquals(test_data, deserialized, 3) + else: + self.assertEqual(test_data, deserialized)