1# -*- coding: utf-8 -*- 2"""Tests for the byte stream operations.""" 3 4from __future__ import unicode_literals 5 6import unittest 7 8from dtfabric import errors 9from dtfabric.runtime import byte_operations 10 11from tests import test_lib 12 13 14class StructOperationTest(test_lib.BaseTestCase): 15 """Python struct-base byte stream operation tests.""" 16 17 def testInitialize(self): 18 """Tests the __init__ function.""" 19 byte_stream_operation = byte_operations.StructOperation('b') 20 self.assertIsNotNone(byte_stream_operation) 21 22 with self.assertRaises(errors.FormatError): 23 byte_operations.StructOperation(None) 24 25 with self.assertRaises(errors.FormatError): 26 byte_operations.StructOperation('z') 27 28 def testReadFrom(self): 29 """Tests the ReadFrom function.""" 30 byte_stream_operation = byte_operations.StructOperation('i') 31 32 value = byte_stream_operation.ReadFrom(b'\x12\x34\x56\x78') 33 self.assertEqual(value, tuple([0x78563412])) 34 35 with self.assertRaises(IOError): 36 byte_stream_operation.ReadFrom(None) 37 38 with self.assertRaises(IOError): 39 byte_stream_operation.ReadFrom(b'\x12\x34\x56') 40 41 def testWriteTo(self): 42 """Tests the WriteTo function.""" 43 byte_stream_operation = byte_operations.StructOperation('i') 44 45 byte_stream = byte_stream_operation.WriteTo(tuple([0x78563412])) 46 self.assertEqual(byte_stream, b'\x12\x34\x56\x78') 47 48 with self.assertRaises(IOError): 49 byte_stream_operation.WriteTo(None) 50 51 with self.assertRaises(IOError): 52 byte_stream_operation.WriteTo(0x78563412) 53 54 with self.assertRaises(IOError): 55 byte_stream_operation.WriteTo(tuple([0x9078563412])) 56 57 58if __name__ == '__main__': 59 unittest.main() 60