Skip to content
This repository has been archived by the owner on Sep 10, 2019. It is now read-only.

Commit

Permalink
Merge pull request #16 from ixje/fix_binaryreader_tests
Browse files Browse the repository at this point in the history
up test coverage of BinaryReader to 100%
  • Loading branch information
metachris committed Jan 4, 2018
2 parents e29f022 + 99f87d3 commit 6db4659
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
4 changes: 2 additions & 2 deletions neocore/IO/BinaryReader.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,8 @@ def ReadVarInt(self, max=sys.maxsize):
int:
"""
fb = self.ReadByte()
if fb is None:
return 0
if fb is 0:
return fb
value = 0
if hex(fb) == '0xfd':
value = self.ReadUInt16()
Expand Down
38 changes: 35 additions & 3 deletions tests/test_io.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
import os
from io import BytesIO
from unittest import TestCase

from neocore.Fixed8 import Fixed8
from neocore.UInt160 import UInt160
from neocore.UInt256 import UInt256

from neocore.IO.Mixins import SerializableMixin
import neocore.IO.BinaryWriter as BinaryWriter
from neocore.IO.BinaryReader import BinaryReader


class TestObject(SerializableMixin):
test_value = None

def __init__(self, test_value=None):
self.test_value = test_value

def Serialize(self, writer):
writer.WriteUInt32(self.test_value)

def Deserialize(self, reader):
self.test_value = reader.ReadUInt32()

def ToArray(self):
pass


class SerializableMixinTestCase(TestCase):
def test_serializable_mixin(self):
m = SerializableMixin()
Expand Down Expand Up @@ -95,6 +108,25 @@ def test_Read2000256List(self):
for item in res:
self.assertEqual(item, val)

def test_readserializable_success(self):
stream = BytesIO(b"\x04\x01\x02\x03\x04")
reader = BinaryReader(stream)
test_object_list = reader.ReadSerializableArray('tests.test_io.TestObject')
self.assertEqual(test_object_list[0].test_value, 0x4030201)

def test_readserializable_fail(self):
# fails because input stream is too short
stream = BytesIO(b"\x04\x01\x02\x03")
reader = BinaryReader(stream)
test_object_list = reader.ReadSerializableArray('tests.test_io.TestObject')
self.assertEqual(len(test_object_list), 0)

def test_readvarint_fail(self):
stream = BytesIO(b"")
reader = BinaryReader(stream)
result = reader.ReadVarInt()
self.assertEqual(result, 0)


class BinaryWriterTestCase(TestCase):
def test_various(self):
Expand Down

0 comments on commit 6db4659

Please sign in to comment.