Esempio n. 1
0
def test_micropython_mode_remove_plotter():
    """
    Ensure the plotter is removed and data is saved as a CSV file in the
    expected directory.
    """
    editor = mock.MagicMock()
    view = mock.MagicMock()
    view.plotter_pane.raw_data = [1, 2, 3]
    mm = MicroPythonMode(editor, view)
    mm.plotter = mock.MagicMock()
    mock_mkdir = mock.MagicMock()
    mock_open = mock.mock_open()
    mock_csv_writer = mock.MagicMock()
    mock_csv = mock.MagicMock()
    mock_csv.writer.return_value = mock_csv_writer
    with mock.patch('mu.modes.base.os.path.exists', return_value=False), \
            mock.patch('mu.modes.base.os.makedirs', mock_mkdir), \
            mock.patch('builtins.open', mock_open), \
            mock.patch('mu.modes.base.csv', mock_csv):
        mm.remove_plotter()
    assert mm.plotter is None
    view.remove_plotter.assert_called_once_with()
    dd = os.path.join(mm.workspace_dir(), 'data_capture')
    mock_mkdir.assert_called_once_with(dd)
    mock_csv_writer.writerows.\
        assert_called_once_with(view.plotter_pane.raw_data)
Esempio n. 2
0
def test_micropython_mode_remove_plotter():
    """
    Ensure the plotter is removed and data is saved as a CSV file in the
    expected directory.
    """
    editor = mock.MagicMock()
    view = mock.MagicMock()
    view.plotter_pane.raw_data = [1, 2, 3]
    mm = MicroPythonMode(editor, view)
    mm.plotter = mock.MagicMock()
    mock_mkdir = mock.MagicMock()
    mock_open = mock.mock_open()
    mock_csv_writer = mock.MagicMock()
    mock_csv = mock.MagicMock()
    mock_csv.writer.return_value = mock_csv_writer
    with mock.patch('mu.modes.base.os.path.exists', return_value=False), \
            mock.patch('mu.modes.base.os.makedirs', mock_mkdir), \
            mock.patch('builtins.open', mock_open), \
            mock.patch('mu.modes.base.csv', mock_csv):
        mm.remove_plotter()
    assert mm.plotter is None
    view.remove_plotter.assert_called_once_with()
    dd = os.path.join(mm.workspace_dir(), 'data_capture')
    mock_mkdir.assert_called_once_with(dd)
    mock_csv_writer.writerows.\
        assert_called_once_with(view.plotter_pane.raw_data)
Esempio n. 3
0
def test_micropython_mode_toggle_plotter_off():
    """
    There is a plotter, so toggle off.
    """
    editor = mock.MagicMock()
    view = mock.MagicMock()
    mm = MicroPythonMode(editor, view)
    mm.remove_plotter = mock.MagicMock()
    mm.plotter = True
    mm.toggle_plotter(None)
    assert mm.remove_plotter.call_count == 1
Esempio n. 4
0
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
Esempio n. 5
0
def test_micropython_mode_toggle_plotter_off():
    """
    There is a plotter, so toggle off.
    """
    editor = mock.MagicMock()
    view = mock.MagicMock()
    mm = MicroPythonMode(editor, view)
    mm.remove_plotter = mock.MagicMock()
    mm.plotter = True
    mm.toggle_plotter(None)
    assert mm.remove_plotter.call_count == 1
Esempio n. 6
0
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
Esempio n. 7
0
def test_micropython_mode_remove_repl_active_plotter():
    """
    When removing the repl, if the plotter is active, retain the
    connection.
    """
    editor = mock.MagicMock()
    view = mock.MagicMock()
    mm = MicroPythonMode(editor, view)
    mm.connection = mock.MagicMock()
    mm.plotter = True
    mm.remove_repl()
    assert mm.repl is False
    assert mm.connection is not None
Esempio n. 8
0
def test_micropython_mode_remove_plotter_disconnects():
    """
    Ensure that connections are closed when plotter is closed.
    """
    editor = mock.MagicMock()
    view = mock.MagicMock()
    mm = MicroPythonMode(editor, view)
    mm.repl = False
    mm.plotter = True
    mock_repl_connection = mock.MagicMock()
    mm.connection = mock_repl_connection
    mm.remove_plotter()
    mock_repl_connection.close.assert_called_once_with()
    assert mm.connection is None
Esempio n. 9
0
def test_micropython_mode_remove_repl_and_disconnect():
    """
    If there is a repl, make sure it's removed as expected and the state is
    updated. Disconnect any open serial connection.
    """
    editor = mock.MagicMock()
    view = mock.MagicMock()
    mm = MicroPythonMode(editor, view)
    mm.repl = True
    mm.plotter = False
    mock_repl_connection = mock.MagicMock()
    mm.connection = mock_repl_connection
    mm.remove_repl()
    mock_repl_connection.close.assert_called_once_with()
    assert mm.connection is None
Esempio n. 10
0
def test_micropython_deactivate():
    """
    Ensure REPL/Plotter and device_selector is hidden, when
    MicroPython-mode is deactivated.
    """
    editor = mock.MagicMock()
    view = mock.MagicMock()
    view.show_device_selector = mock.MagicMock()
    view.hide_device_selector = mock.MagicMock()
    mm = MicroPythonMode(editor, view)
    mm.remove_repl = mock.MagicMock()
    mm.remove_plotter = mock.MagicMock()
    mm.activate()
    mm.repl = True
    mm.plotter = True
    mm.deactivate()
    view.hide_device_selector.assert_called_once_with()
    mm.remove_repl.assert_called_once_with()
    mm.remove_plotter.assert_called_once_with()
Esempio n. 11
0
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()