# Path: robel/utils/testing/mock_dynamixel_sdk.py
# def patch_dynamixel(**devices):
#     """Decorator that patches the DynamixelSDK for the function context."""
# 
#     def decorator(fn):
#         def wrapped_fn(*args):
#             sdk = MockDynamixelSdk()
#             for key, motor_ids in devices.items():
#                 sdk.create_device(key, motor_ids)
#             sys.modules['dynamixel_sdk'] = sdk
#             fn(*args, sdk)
#             del sys.modules['dynamixel_sdk']
# 
#         return wrapped_fn
# 
#     return decorator


from absl.testing import absltest
from robel.utils.testing.mock_dynamixel_sdk import patch_dynamixel
    """Tests MockDynamixelSdk."""

    @patch_dynamixel(test=[1, 2, 3, 4])
    def test_port_handler(self, sdk):
        handler = sdk.PortHandler('test')

        self.assertTrue(handler.openPort())
        self.assertTrue(handler.setBaudRate(1000000))
        handler.closePort()

    @patch_dynamixel(test=[1, 2, 3, 4], test1=[1, 2])
    def test_port_handler_fault(self, sdk):
        with self.assertRaises(ValueError):
            sdk.PortHandler('another')

        handler = sdk.PortHandler('test')

        with self.assertRaises(ValueError):
            sdk.PortHandler('test')

        handler.faulty = True
        self.assertFalse(handler.openPort())

        with self.assertRaises(ValueError):
            sdk.PortHandler('test1')

        self.assertFalse(handler.setBaudRate(1000000))
        self.assertIn('test', sdk.used_ports)
        self.assertNotIn('test1', sdk.used_ports)


# Path: robel/utils/testing/mock_dynamixel_sdk.py
# def patch_dynamixel(**devices):
#     """Decorator that patches the DynamixelSDK for the function context."""
# 
#     def decorator(fn):
#         def wrapped_fn(*args):
#             sdk = MockDynamixelSdk()
#             for key, motor_ids in devices.items():
#                 sdk.create_device(key, motor_ids)
#             sys.modules['dynamixel_sdk'] = sdk
#             fn(*args, sdk)
#             del sys.modules['dynamixel_sdk']
# 
#         return wrapped_fn
# 
#     return decorator


# Path: robel/utils/testing/mock_dynamixel_sdk_test.py
from absl.testing import absltest
from robel.utils.testing.mock_dynamixel_sdk import patch_dynamixel

    """Tests MockDynamixelSdk."""

    @patch_dynamixel(test=[1, 2, 3, 4])
    def test_port_handler(self, sdk):
        handler = sdk.PortHandler('test')

        self.assertTrue(handler.openPort())
        self.assertTrue(handler.setBaudRate(1000000))
        handler.closePort()

    @patch_dynamixel(test=[1, 2, 3, 4], test1=[1, 2])
    def test_port_handler_fault(self, sdk):
        with self.assertRaises(ValueError):
            sdk.PortHandler('another')

        handler = sdk.PortHandler('test')

        with self.assertRaises(ValueError):
            sdk.PortHandler('test')

        handler.faulty = True
        self.assertFalse(handler.openPort())

        with self.assertRaises(ValueError):
            sdk.PortHandler('test1')

        self.assertFalse(handler.setBaudRate(1000000))
        self.assertIn('test', sdk.used_ports)
        self.assertNotIn('test1', sdk.used_ports)


    @patch_dynamixel(test1=[1, 2, 3, 4], test2=[1, 2, 3, 4, 5, 6])