コード例 #1
0
 def test_type_unrecognizable(self):
     """
     should raise an error if resource type is not recognized or supported
     """
     res = {
         'type': RESOURCE_TYPE.get("UNRECOGNIZABLE"),
         'value': 'NAN'
     }
     with self.assertRaises(Exception):
         encode_resource_value(res)
コード例 #2
0
 def test_opaque_type_not_match(self):
     """
     should throw an error if resource type is set
     to opaque and value is not a bytearray
     """
     res = {
         'type': RESOURCE_TYPE['OPAQUE'],
         'value': 'NOT OPAQUE'
     }
     with self.assertRaises(Exception):
         encode_resource_value(res)
コード例 #3
0
 def test_encode_str_type_not_match(self):
     """
     should throw an error if resource type is set
     to string and value is not a string
     """
     res = {
         'type': RESOURCE_TYPE['STRING'],
         'value': 1324
     }
     with self.assertRaises(Exception):
         encode_resource_value(res)
コード例 #4
0
 def test_encode_bool_type_not_match(self):
     """
     should raise an error if resource type is set
     to boolean and value is not a boolean
     """
     res = {
         'type': RESOURCE_TYPE['BOOLEAN'],
         'value': "NOT BOOLEAN"
     }
     with self.assertRaises(Exception):
         encode_resource_value(res)
コード例 #5
0
 def test_encode_undefined_type(self):
     """
     should raise an error if resource type is a number
     which is not defined in resource type dictionary
     """
     res = {
         'type': 99,
         'value': 'NAN'
     }
     with self.assertRaises(Exception):
         encode_resource_value(res)
コード例 #6
0
 def test_encode_type_not_match(self):
     """
     should throw an error if resource type
     is set to float and value is not a number'
     """
     res = {
         'type': RESOURCE_TYPE['FLOAT'],
         'value': 'NAN'
     }
     with self.assertRaises(Exception):
         encode_resource_value(res)
コード例 #7
0
 def test_encode_int_minus_2_pow_15(self):
     """
     should encode integer (-2^15)
     """
     res = {
         'type': RESOURCE_TYPE['INTEGER'],
         'value': -0x8000
     }
     encoded = encode_resource_value(res)
     self.assertTrue(encoded == bytearray([0x80, 0x00]))
コード例 #8
0
 def test_encode_int_2_pow_15_min_1(self):
     """
     should encode integer (2^15-1)
     """
     res = {
         'type': RESOURCE_TYPE['INTEGER'],
         'value': 32767
     }
     encoded = encode_resource_value(res)
     self.assertTrue(encoded == bytearray([0x7f, 0xff]))
コード例 #9
0
 def test_encode_int__minus_1(self):
     """
     should encode integer (-1)
     """
     res = {
         'type': RESOURCE_TYPE['INTEGER'],
         'value': -1
     }
     encoded = encode_resource_value(res)
     self.assertTrue(encoded == bytearray([0xff]))
コード例 #10
0
 def test_encode_value_integer_0(self):
     """
     should encode integer (0)
     """
     res = {
         'type': RESOURCE_TYPE['INTEGER'],
         'value': 0
     }
     encoded = encode_resource_value(res)
     self.assertTrue(encoded == bytearray([0x00]))
コード例 #11
0
 def test_encode_value_empty_buff(self):
     """
     should return empty bytearray if resource type is set to none
     """
     res = {
         'type': RESOURCE_TYPE['NONE'],
         'value': 0x80000000
     }
     encoded = encode_resource_value(res)
     self.assertTrue(encoded == bytearray())
コード例 #12
0
 def test_encode_value_string(self):
     """
     should encode string (text)
     """
     res = {
         'type': RESOURCE_TYPE['STRING'],
         'value': 'text'
     }
     encoded = encode_resource_value(res)
     self.assertTrue(encoded == bytearray([0x74, 0x65, 0x78, 0x74]))
コード例 #13
0
 def test_encode_value_boolean(self):
     """
     should encode boolean (true)
     """
     res = {
         'type': RESOURCE_TYPE['BOOLEAN'],
         'value': True
     }
     encoded = encode_resource_value(res)
     self.assertTrue(encoded == bytearray([0x01]))
コード例 #14
0
 def test_encode_float_1_point_23(self):
     """
     should encode float (1.23)
     """
     res = {
         'type': RESOURCE_TYPE['FLOAT'],
         'value': 1.23
     }
     encoded = encode_resource_value(res)
     self.assertTrue(encoded == bytearray([0x3f, 0x9d, 0x70, 0xa4]))
コード例 #15
0
 def test_encode_opaque_pass_bytes(self):
     """
     should handle opaque (bytearray <0x74, 0x65, 0x78, 0x74>)
     """
     buff = bytearray([0x74, 0x65, 0x78, 0x74])
     res = {
         'type': RESOURCE_TYPE['OPAQUE'],
         'value': buff
     }
     encoded = encode_resource_value(res)
     self.assertTrue(encoded == buff)