def test_pretty_hex_str_items_value_hi(self): """Check that the utils.pretty_hex_str() function fails when the "byte_seq" items's value is wrong (too high).""" byte_seq = (0xffff, 0, 10) # the first item is too high with self.assertRaises(ValueError): utils.pretty_hex_str(byte_seq)
def test_pretty_hex_str_wrong_arg_type(self): """Check that the utils.pretty_hex_str() function fails when the "byte_seq" argument's type is wrong (int).""" with self.assertRaises(TypeError): utils.pretty_hex_str(1.0) with self.assertRaises(TypeError): utils.pretty_hex_str("hello")
def test_pretty_hex_str_2(self): """Check the returned value of utils.pretty_hex_str().""" byte_seq = (0xff,) * 10 hex_str = utils.pretty_hex_str(byte_seq) expected_str = "ff," * 9 + "ff" self.assertEqual(hex_str, expected_str)
def test_pretty_hex_str_2(self): """Check the returned value of utils.pretty_hex_str().""" byte_seq = (0xff, ) * 10 hex_str = utils.pretty_hex_str(byte_seq) expected_str = "ff," * 9 + "ff" self.assertEqual(hex_str, expected_str)
def test_pretty_hex_str_good_arg_type(self): """Check the returned value of utils.pretty_hex_str() when various correct arguments are given.""" # Test with a tuple of bytes byte_seq = (0xff, 0x00, 0x0a) hex_str = utils.pretty_hex_str(byte_seq) expected_str = "ff,00,0a" self.assertEqual(hex_str, expected_str) # Test with a list of bytes byte_seq = [0xff, 0x00, 0x0a] hex_str = utils.pretty_hex_str(byte_seq) expected_str = "ff,00,0a" self.assertEqual(hex_str, expected_str) # Test with a bytes string byte_seq = bytes((0xff, 0x00, 0x0a)) hex_str = utils.pretty_hex_str(byte_seq) expected_str = "ff,00,0a" self.assertEqual(hex_str, expected_str) # Test with a bytearray byte_seq = bytearray((0xff, 0x00, 0x0a)) hex_str = utils.pretty_hex_str(byte_seq) expected_str = "ff,00,0a" self.assertEqual(hex_str, expected_str) # Test with an integer byte_seq = 0xff hex_str = utils.pretty_hex_str(byte_seq) expected_str = "ff" self.assertEqual(hex_str, expected_str)
def test_pretty_hex_str_items_type(self): """Check that the utils.pretty_hex_str() function fails when the "byte_seq" items have wrong type.""" byte_seq = (1.0, 0, 10) # wrong type (float) with self.assertRaises(TypeError): utils.pretty_hex_str(byte_seq) byte_seq = ("hello", 0, 10) # wrong type (str) with self.assertRaises(TypeError): utils.pretty_hex_str(byte_seq) byte_seq = ((1, ), 0, 10) # wrong type (tuple) with self.assertRaises(TypeError): utils.pretty_hex_str(byte_seq)
def __init__(self, dynamixel_id, instruction, parameters=None): # Check the parameters byte. # "TypeError" and "ValueError" are raised by the "bytes" constructor if # necessary. # The statement "tuple(parameters)" implicitely rejects integers (and # all non-iterable objects) to compensate the fact that the bytes # constructor doesn't reject them: bytes(3) is valid and returns # b'\x00\x00\x00'. if parameters is None: parameters = bytes() else: parameters = bytes(tuple(parameters)) # Add the header bytes. self._bytes = bytearray((0xff, 0xff)) # Check and add the Dynamixel ID byte. # "TypeError" and "ValueError" are raised by the "bytearray.append()" # if necessary. if 0x00 <= dynamixel_id <= 0xfe: self._bytes.append(dynamixel_id) else: if isinstance(dynamixel_id, int): msg = ("Wrong dynamixel_id value, " "an integer in range(0x00, 0xfe) is required.") raise ValueError(msg) else: raise TypeError("Wrong dynamixel_id type (integer required).") # Add the length byte. self._bytes.append(len(parameters) + 2) # Check and add the instruction byte. # "TypeError" and "ValueError" are raised by the "bytearray.append()" # if necessary. if instruction in INSTRUCTIONS: self._bytes.append(instruction) else: if isinstance(instruction, int): msg = "Wrong instruction, should be in ({})." instructions_str = utils.pretty_hex_str(INSTRUCTIONS) raise ValueError(msg.format(instructions_str)) else: raise TypeError("Wrong instruction type (integer required).") # Check and add the parameter bytes. nb_param_min = NUMBER_OF_PARAMETERS[self.instruction]['min'] nb_param_max = NUMBER_OF_PARAMETERS[self.instruction]['max'] if nb_param_min <= len(parameters) <= nb_param_max: self._bytes.extend(parameters) else: msg = ("Wrong number of parameters: {} parameters " "(min expected={}; max expected={}).") nb_param = len(parameters) raise ValueError(msg.format(nb_param, nb_param_min, nb_param_max)) # Add the checksum byte. computed_checksum = pk.compute_checksum(self._bytes[2:]) self._bytes.append(computed_checksum)