def test_REPLConnection_send_commands(): """ Ensure the list of commands is correctly encoded and bound by control commands to put the board into and out of raw mode. """ mock_serial_class = mock.MagicMock() with mock.patch("mu.modes.base.QSerialPort", mock_serial_class): conn = REPLConnection("COM0") conn.execute = mock.MagicMock() commands = ["import os", "print(os.listdir())"] conn.send_commands(commands) expected = [ b"\x03", # Keyboard interrupt b"\x03", # Keyboard interrupt b"\x01", # Put the board into raw mode. b"\x04", # Soft-reboot b"\x03", # Keyboard interrupt b"\x03", # Keyboard interrupt b'print("\\n");', # Ensure a newline at the start of output. b"import os\r", # The commands to run. b"print(os.listdir())\r", b"\r", # Ensure newline after commands. b"\x04", # Evaluate the commands. b"\x02", # Leave raw mode. ] conn.execute.assert_called_once_with(expected)
def test_REPLConnection_execute(): """ Ensure the first command is sent via serial to the connected device, and further commands are scheduled for the future. """ mock_serial_class = mock.MagicMock() with mock.patch("mu.modes.base.QSerialPort", mock_serial_class): conn = REPLConnection("COM0") conn.write = mock.MagicMock() # Mocks QTimer, so only first command will be sent commands = [b"A", b"B"] with mock.patch("mu.modes.base.QTimer") as mock_timer: conn.execute(commands) conn.write.assert_called_once_with(b"A") assert mock_timer.singleShot.call_count == 1