Added tests for LongLong and UnsignedLongLong.

Implemented compatibility layer for the long type (has been removed in
py3)

Added test for long-type compat.
This commit is contained in:
Jeppe Klitgaard 2015-04-16 17:05:44 +02:00
parent 474aa5858a
commit cc184f3e4d
4 changed files with 50 additions and 3 deletions

View File

@ -3,4 +3,10 @@ This module stores code used for making pyCraft compatible with
both Python2 and Python3 while using the same codebase.
"""
# Currently empty, this is sure to grow in time.
import six
# ### LONG ###
if six.PY3:
long = int
else:
long = long

View File

@ -19,6 +19,7 @@ __all__ = ["ENDIANNESS",
"String"]
from minecraft.exceptions import DeserializationError, SerializationError
from minecraft.compat import long
from io import BytesIO
import struct
import collections
@ -161,7 +162,7 @@ class NumberDatatype(Datatype):
MIN_NUMBER_VALUE = None
MAX_NUMBER_VALUE = None
ALLOWED_SERIALIZATION_TYPES = (int,)
ALLOWED_SERIALIZATION_TYPES = (int, long)
DISALLOWED_SERIALIZATION_TYPES = (bool,)
@classmethod

View File

@ -2,4 +2,10 @@ from minecraft import compat # noqa unused-import
import unittest # noqa unused-import
# Currently empty, eventually it should grow.
class TestCompatLong(unittest.TestCase):
def test_import_long(self):
from minecraft.compat import long # noqa unused-import
def test_long(self):
compat.long(45)

View File

@ -248,6 +248,40 @@ class UnsignedLongTest(UnsignedInteger):
DATATYPE_CLS = UnsignedLong
class LongLongTest(BaseNumberDatatypeTester):
DATATYPE_CLS = LongLong
VALID_VALUES = [
(-9223372036854775808, b"\x80\x00\x00\x00\x00\x00\x00\x00"),
(-1000000, b"\xff\xff\xff\xff\xff\xf0\xbd\xc0"),
(0, b"\x00\x00\x00\x00\x00\x00\x00\x00"),
(10000000, b"\x00\x00\x00\x00\x00\x98\x96\x80"),
(9223372036854775807, b"\x7f\xff\xff\xff\xff\xff\xff\xff")
]
INVALID_DESERIALIZATION_VALUES = list(BASE_INVALID_DESERIALIZATION_VALUES)
INVALID_DESERIALIZATION_VALUES.extend([
(b"\xff", ValueError),
(b"\x00\x01", ValueError),
(b"\x76\x80\x80\x10\xff", ValueError),
(b"\x55\x44\x33\x22\x11\x66\x77\x88\x99", ValueError)
])
class UnsignedLongLongTest(BaseNumberDatatypeTester):
DATATYPE_CLS = UnsignedLongLong
VALID_VALUES = [
(0, b"\x00\x00\x00\x00\x00\x00\x00\x00"),
(10000000, b"\x00\x00\x00\x00\x00\x98\x96\x80"),
(9223372036854775807, b"\x7f\xff\xff\xff\xff\xff\xff\xff"),
(18446744073709551615, b"\xff\xff\xff\xff\xff\xff\xff\xff")
]
INVALID_DESERIALIZATION_VALUES = \
LongLongTest.INVALID_DESERIALIZATION_VALUES
# def _bin(binstr):
# """
# Accepts a pretty looking string of binary numbers and