def test_micropython_mode_add_plotter_exception(): """ Ensure that any non-IOError based exceptions are logged. """ editor = mock.MagicMock() view = mock.MagicMock() ex = Exception('BOOM') view.add_micropython_plotter = mock.MagicMock(side_effect=ex) mm = MicroPythonMode(editor, view) mm.find_device = mock.MagicMock(return_value='COM0') with mock.patch('mu.modes.base.logger', return_value=None) as logger: mm.add_plotter() logger.error.assert_called_once_with(ex)
def test_micropython_mode_add_plotter_exception(): """ Ensure that any non-IOError based exceptions are logged. """ editor = mock.MagicMock() view = mock.MagicMock() ex = Exception('BOOM') view.add_micropython_plotter = mock.MagicMock(side_effect=ex) mm = MicroPythonMode(editor, view) mm.find_device = mock.MagicMock(return_value=('COM0', '12345')) with mock.patch('mu.modes.base.logger', return_value=None) as logger: mm.add_plotter() logger.error.assert_called_once_with(ex)
def test_micropython_mode_add_plotter_no_port(): """ If it's not possible to find a connected micro:bit then ensure a helpful message is enacted. """ editor = mock.MagicMock() view = mock.MagicMock() view.show_message = mock.MagicMock() mm = MicroPythonMode(editor, view) mm.find_device = mock.MagicMock(return_value=(None, None)) mm.add_plotter() assert view.show_message.call_count == 1 message = 'Could not find an attached device.' assert view.show_message.call_args[0][0] == message
def test_micropython_mode_add_plotter_no_port(): """ If it's not possible to find a connected micro:bit then ensure a helpful message is enacted. """ editor = mock.MagicMock() view = mock.MagicMock() view.show_message = mock.MagicMock() mm = MicroPythonMode(editor, view) mm.find_device = mock.MagicMock(return_value=False) mm.add_plotter() assert view.show_message.call_count == 1 message = 'Could not find an attached device.' assert view.show_message.call_args[0][0] == message
def test_micropython_mode_add_plotter(): """ Nothing goes wrong so check the _view.add_micropython_plotter gets the expected argument. """ editor = mock.MagicMock() view = mock.MagicMock() view.show_message = mock.MagicMock() view.add_micropython_plotter = mock.MagicMock() mm = MicroPythonMode(editor, view) mm.find_device = mock.MagicMock(return_value=('COM0', '12345')) with mock.patch('os.name', 'nt'): mm.add_plotter() assert view.show_message.call_count == 0 assert view.add_micropython_plotter.call_args[0][0] == 'COM0'
def test_micropython_mode_add_plotter(): """ Nothing goes wrong so check the _view.add_micropython_plotter gets the expected argument. """ editor = mock.MagicMock() view = mock.MagicMock() view.show_message = mock.MagicMock() view.add_micropython_plotter = mock.MagicMock() mm = MicroPythonMode(editor, view) mm.find_device = mock.MagicMock(return_value='COM0') with mock.patch('os.name', 'nt'): mm.add_plotter() assert view.show_message.call_count == 0 assert view.add_micropython_plotter.call_args[0][0] == 'COM0'
def test_micropython_mode_add_plotter_ioerror(): """ Sometimes when attempting to connect to the device there is an IOError because it's still booting up or connecting to the host computer. In this case, ensure a useful message is displayed. """ editor = mock.MagicMock() view = mock.MagicMock() view.show_message = mock.MagicMock() ex = IOError('BOOM') view.add_micropython_plotter = mock.MagicMock(side_effect=ex) mm = MicroPythonMode(editor, view) mm.find_device = mock.MagicMock(return_value=('COM0', '123456')) mm.add_plotter() assert view.show_message.call_count == 1 assert view.show_message.call_args[0][0] == str(ex)
def test_micropython_mode_add_plotter_exception(microbit): """ Ensure that any non-IOError based exceptions are logged. """ editor = mock.MagicMock() editor.current_device = microbit view = mock.MagicMock() ex = Exception("BOOM") mm = MicroPythonMode(editor, view) mock_repl_connection = mock.MagicMock() mock_repl_connection.open = mock.MagicMock(side_effect=ex) mock_connection_class = mock.MagicMock(return_value=mock_repl_connection) with mock.patch("mu.modes.base.logger", return_value=None) as logger: with mock.patch("mu.modes.base.REPLConnection", mock_connection_class): mm.add_plotter() logger.error.assert_called_once_with(ex)
def test_micropython_mode_add_plotter_ioerror(): """ Sometimes when attempting to connect to the device there is an IOError because it's still booting up or connecting to the host computer. In this case, ensure a useful message is displayed. """ editor = mock.MagicMock() view = mock.MagicMock() view.show_message = mock.MagicMock() ex = IOError('BOOM') view.add_micropython_plotter = mock.MagicMock(side_effect=ex) mm = MicroPythonMode(editor, view) mm.find_device = mock.MagicMock(return_value='COM0') mm.add_plotter() assert view.show_message.call_count == 1 assert view.show_message.call_args[0][0] == str(ex)
def test_micropython_mode_add_plotter(microbit): """ Nothing goes wrong so check the _view.add_micropython_plotter gets the expected argument. """ editor = mock.MagicMock() editor.current_device = microbit view = mock.MagicMock() view.show_message = mock.MagicMock() view.add_micropython_plotter = mock.MagicMock() mm = MicroPythonMode(editor, view) mock_repl_connection = mock.MagicMock() mock_connection_class = mock.MagicMock(return_value=mock_repl_connection) with mock.patch("mu.modes.base.REPLConnection", mock_connection_class): mm.add_plotter() view.show_message.assert_not_called() assert view.add_micropython_plotter.call_args[0][1] == mock_repl_connection mock_repl_connection.open.assert_called_once_with()
def test_micropython_mode_add_plotter_ioerror(microbit): """ Sometimes when attempting to connect to the device there is an IOError because it's still booting up or connecting to the host computer. In this case, ensure a useful message is displayed. """ editor = mock.MagicMock() editor.current_device = microbit view = mock.MagicMock() view.show_message = mock.MagicMock() ex = IOError("BOOM") mm = MicroPythonMode(editor, view) mock_repl_connection = mock.MagicMock() mock_repl_connection.open = mock.MagicMock(side_effect=ex) mock_connection_class = mock.MagicMock(return_value=mock_repl_connection) with mock.patch("mu.modes.base.REPLConnection", mock_connection_class): mm.add_plotter() assert view.show_message.call_count == 1 assert view.show_message.call_args[0][0] == str(ex)
def test_micropython_mode_toggle_plotter_on(): """ There is no plotter, so toggle on. """ editor = mock.MagicMock() view = mock.MagicMock() mm = MicroPythonMode(editor, view) mm.add_plotter = mock.MagicMock() mm.plotter = None mm.toggle_plotter(None) assert mm.add_plotter.call_count == 1
def test_micropython_device_changed(microbit): """ Ensure REPL/Plotter and connection are reconnected, when the user changes device. """ editor = mock.MagicMock() view = mock.MagicMock() view.show_device_selector = mock.MagicMock() view.hide_device_selector = mock.MagicMock() mm = MicroPythonMode(editor, view) mm.add_repl = mock.MagicMock() mm.add_plotter = mock.MagicMock() mm.remove_repl = mock.MagicMock() mm.remove_plotter = mock.MagicMock() mm.repl = True mm.plotter = True mm.connection = mock.MagicMock() mm.activate() mm.device_changed(microbit) mm.add_repl.assert_called_once_with() mm.add_plotter.assert_called_once_with() mm.remove_repl.assert_called_once_with() mm.remove_plotter.assert_called_once_with() mm.connection.send_interrupt.assert_called_once_with()