def test_exception_wrong_type(self): """ should throw an error if resource type is a number which is not defined in resource type dictionary """ buff = bytearray([0x7f, 0xff, 0xff]) res = { 'type': 99, } with self.assertRaises(Exception): decode_resource_value(buff, res)
def test_integer_wrong_buff_length(self): """ should raise an error if given buffer length does not meet 8, 16, 32 bit integer """ buff = bytearray([0x7f, 0xff, 0xff]) res = { 'type': RESOURCE_TYPE['INTEGER'], } with self.assertRaises(Exception): decode_resource_value(buff, res)
def test_float_double_wrong_length(self): """ should raise an error if given buffer length does not meet float or double """ buff = bytearray([0x7f, 0xff, 0xff]) res = { 'type': RESOURCE_TYPE['FLOAT'], } with self.assertRaises(Exception): decode_resource_value(buff, res)
def test_decode_opaque_pass_buffer(self): """ should handle opaque resource type and return bytearray """ buff = bytearray([0x02]) res = { 'type': RESOURCE_TYPE['OPAQUE'], } decoded = decode_resource_value(buff, res) self.assertTrue(decoded == buff)
def test_decode_boolean(self): """ should decode buffer to boolean """ buff = bytearray([0x01]) res = { 'type': RESOURCE_TYPE['BOOLEAN'], } decoded = decode_resource_value(buff, res) self.assertTrue(decoded)
def test_decode_string(self): """ should decode buffer to string """ buff = bytearray([0x74, 0x65, 0x78, 0x74]) res = { 'type': RESOURCE_TYPE['STRING'], } decoded = decode_resource_value(buff, res) self.assertTrue(decoded == 'text')
def test_decode_double(self): """ should decode buffer to double """ buff = bytearray([0x3f, 0xf3, 0xae, 0x14, 0x7a, 0xe1, 0x47, 0xae]) res = { 'type': RESOURCE_TYPE['FLOAT'], } decoded = decode_resource_value(buff, res) self.assertTrue(decoded == 1.23)
def test_decode_float(self): """ should decode buffer to float """ buff = bytearray([0x3f, 0x9d, 0x70, 0xa4]) res = { 'type': RESOURCE_TYPE['FLOAT'], } decoded = decode_resource_value(buff, res) self.assertTrue(round(decoded, 7) == 1.23)
def test_decode_32_bit_integer(self): """ should decode buffer to 32 bit integer """ buff = bytearray([0x7f, 0xff, 0xff, 0xff]) res = { 'type': RESOURCE_TYPE['INTEGER'], } decoded = decode_resource_value(buff, res) self.assertTrue(decoded == 0x7FFFFFFF)
def test_decode_16_bit_integer(self): """ should decode buffer to 16 bit integer """ buff = bytearray([0x00, 0x80]) res = { 'type': RESOURCE_TYPE['INTEGER'], } decoded = decode_resource_value(buff, res) self.assertTrue(decoded == 128)
def test_return_0_empty_buff_given(self): """ return 0 if given buffer is empty """ buff = bytearray() res = { 'type': RESOURCE_TYPE['INTEGER'], } decoded = decode_resource_value(buff, res) self.assertTrue(decoded == 0)