From f5e6091ff654838143a52e7b996ce60c75b9d4a0 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Wed, 15 Apr 2015 20:16:57 -0300 Subject: [PATCH 1/6] Add tests for CommandLineEdit --- tests/misc/test_miscwidgets.py | 98 ++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 tests/misc/test_miscwidgets.py diff --git a/tests/misc/test_miscwidgets.py b/tests/misc/test_miscwidgets.py new file mode 100644 index 000000000..b73bab050 --- /dev/null +++ b/tests/misc/test_miscwidgets.py @@ -0,0 +1,98 @@ +# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et: + +# Copyright 2014-2015 Florian Bruhin (The Compiler) +# +# This file is part of qutebrowser. +# +# qutebrowser is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# qutebrowser is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with qutebrowser. If not, see . + +"""Test widgets in miscwidgets module.""" +from unittest import mock +from PyQt5.QtCore import Qt +from PyQt5.QtWidgets import QApplication +import pytest + +from qutebrowser.misc import lineparser +from qutebrowser.misc.miscwidgets import CommandLineEdit +from qutebrowser.utils import objreg + + +class TestCommandLineEdit: + """Tests for CommandLineEdit widget.""" + + @pytest.yield_fixture + def cmd_edit(self, qtbot): + """ + Fixture to initialize a CommandLineEdit and its dependencies, + cleaning up afterwards. + """ + command_history = lineparser.LimitLineParser('', '', limit=None) + objreg.register('command-history', command_history) + cmd_edit = CommandLineEdit(None) + cmd_edit.set_prompt(':') + qtbot.add_widget(cmd_edit) + assert cmd_edit.text() == '' + + yield cmd_edit + + objreg.delete('command-history') + + @pytest.yield_fixture + def mock_clipboard(self): + """ + Fixture installs a MagicMock into QApplication.clipboard() and + returns it. + """ + with mock.patch.object(QApplication, 'clipboard'): + clipboard = mock.MagicMock() + clipboard.supportsSelection.return_value = True + QApplication.clipboard.return_value = clipboard + yield clipboard + + def test_position(self, qtbot, cmd_edit): + """Test cursor position based on the prompt.""" + qtbot.keyClicks(cmd_edit, ':hello') + assert cmd_edit.text() == ':hello' + assert cmd_edit.cursorPosition() == len(':hello') + + cmd_edit.home(mark=True) + assert cmd_edit.cursorPosition() == len(':hello') + qtbot.keyClick(cmd_edit, Qt.Key_Delete) + assert cmd_edit.text() == ':' + qtbot.keyClick(cmd_edit, Qt.Key_Backspace) + assert cmd_edit.text() == ':' + + qtbot.keyClicks(cmd_edit, 'hey again') + assert cmd_edit.text() == ':hey again' + + def test_invalid_prompt(self, qtbot, cmd_edit): + """Test prevent invalid prompt to be entered.""" + qtbot.keyClicks(cmd_edit, '$hello') + assert cmd_edit.text() == '' + + def test_clipboard_paste(self, qtbot, cmd_edit, mock_clipboard): + """Test pasting commands from clipboard.""" + mock_clipboard.text.return_value = ':command' + qtbot.keyClick(cmd_edit, Qt.Key_Insert, Qt.ShiftModifier) + assert cmd_edit.text() == ':command' + + mock_clipboard.text.return_value = ' param1' + qtbot.keyClick(cmd_edit, Qt.Key_Insert, Qt.ShiftModifier) + assert cmd_edit.text() == ':command param1' + + cmd_edit.clear() + mock_clipboard.text.return_value = '$ command' + qtbot.keyClick(cmd_edit, Qt.Key_Insert, Qt.ShiftModifier) + assert cmd_edit.text() == ':command param1' + From 6c97a4a6e0e642d83fd600662b749aa2545e3dba Mon Sep 17 00:00:00 2001 From: Raphael Pierzina Date: Sun, 19 Apr 2015 21:10:27 +0200 Subject: [PATCH 2/6] Remove blank line at end of file to fix flake8 --- tests/misc/test_miscwidgets.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/misc/test_miscwidgets.py b/tests/misc/test_miscwidgets.py index b73bab050..ad94f2613 100644 --- a/tests/misc/test_miscwidgets.py +++ b/tests/misc/test_miscwidgets.py @@ -95,4 +95,3 @@ class TestCommandLineEdit: mock_clipboard.text.return_value = '$ command' qtbot.keyClick(cmd_edit, Qt.Key_Insert, Qt.ShiftModifier) assert cmd_edit.text() == ':command param1' - From 2d19708a419fff804e6395ba9d7041818ef80345 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Sun, 19 Apr 2015 17:11:29 -0300 Subject: [PATCH 3/6] Play nice with other plugins in conftest.py Some plugins might create their own Item subclasses without a `fixturenames` attribute. Discovered while taking pytest-flakes for a spin. --- tests/conftest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/conftest.py b/tests/conftest.py index cf34f75d5..892a91912 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -107,7 +107,7 @@ def pytest_collection_modifyitems(items): http://pytest.org/latest/plugins.html """ for item in items: - if 'qtbot' in item.fixturenames: + if 'qtbot' in getattr(item, 'fixturenames', ()): item.add_marker('gui') From f55242ad93c9927a50611b666aa9bcdb1b0bafad Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Sun, 19 Apr 2015 17:13:47 -0300 Subject: [PATCH 4/6] Use pytest-mock to install QApplication.clipboard mock --- tests/misc/test_miscwidgets.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/misc/test_miscwidgets.py b/tests/misc/test_miscwidgets.py index ad94f2613..1bd19541a 100644 --- a/tests/misc/test_miscwidgets.py +++ b/tests/misc/test_miscwidgets.py @@ -48,17 +48,17 @@ class TestCommandLineEdit: objreg.delete('command-history') - @pytest.yield_fixture - def mock_clipboard(self): + @pytest.fixture + def mock_clipboard(self, mocker): """ Fixture installs a MagicMock into QApplication.clipboard() and returns it. """ - with mock.patch.object(QApplication, 'clipboard'): - clipboard = mock.MagicMock() - clipboard.supportsSelection.return_value = True - QApplication.clipboard.return_value = clipboard - yield clipboard + mocker.patch.object(QApplication, 'clipboard') + clipboard = mock.MagicMock() + clipboard.supportsSelection.return_value = True + QApplication.clipboard.return_value = clipboard + return clipboard def test_position(self, qtbot, cmd_edit): """Test cursor position based on the prompt.""" From 69061c5629c15e0b5b0d21790f42d14c55bd1739 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Mon, 20 Apr 2015 12:51:36 -0300 Subject: [PATCH 5/6] Remove LimitLineParser from test As suggested by @The-Compiler, this is not really necessary --- tests/misc/test_miscwidgets.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/tests/misc/test_miscwidgets.py b/tests/misc/test_miscwidgets.py index 1bd19541a..ec7a6bf15 100644 --- a/tests/misc/test_miscwidgets.py +++ b/tests/misc/test_miscwidgets.py @@ -37,17 +37,12 @@ class TestCommandLineEdit: Fixture to initialize a CommandLineEdit and its dependencies, cleaning up afterwards. """ - command_history = lineparser.LimitLineParser('', '', limit=None) - objreg.register('command-history', command_history) cmd_edit = CommandLineEdit(None) cmd_edit.set_prompt(':') qtbot.add_widget(cmd_edit) assert cmd_edit.text() == '' - yield cmd_edit - objreg.delete('command-history') - @pytest.fixture def mock_clipboard(self, mocker): """ From a7dfdd48e01718024d797b7c50ce9f16f09cd30d Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Mon, 20 Apr 2015 22:59:35 +0200 Subject: [PATCH 6/6] Fix lint. --- tests/misc/test_miscwidgets.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/tests/misc/test_miscwidgets.py b/tests/misc/test_miscwidgets.py index ec7a6bf15..bd316935f 100644 --- a/tests/misc/test_miscwidgets.py +++ b/tests/misc/test_miscwidgets.py @@ -18,25 +18,22 @@ # along with qutebrowser. If not, see . """Test widgets in miscwidgets module.""" + from unittest import mock from PyQt5.QtCore import Qt from PyQt5.QtWidgets import QApplication import pytest -from qutebrowser.misc import lineparser from qutebrowser.misc.miscwidgets import CommandLineEdit -from qutebrowser.utils import objreg class TestCommandLineEdit: + """Tests for CommandLineEdit widget.""" @pytest.yield_fixture def cmd_edit(self, qtbot): - """ - Fixture to initialize a CommandLineEdit and its dependencies, - cleaning up afterwards. - """ + """Fixture to initialize a CommandLineEdit.""" cmd_edit = CommandLineEdit(None) cmd_edit.set_prompt(':') qtbot.add_widget(cmd_edit) @@ -45,9 +42,10 @@ class TestCommandLineEdit: @pytest.fixture def mock_clipboard(self, mocker): - """ - Fixture installs a MagicMock into QApplication.clipboard() and - returns it. + """Fixture to mock QApplication.clipboard. + + Return: + The mocked QClipboard object. """ mocker.patch.object(QApplication, 'clipboard') clipboard = mock.MagicMock() @@ -72,7 +70,7 @@ class TestCommandLineEdit: assert cmd_edit.text() == ':hey again' def test_invalid_prompt(self, qtbot, cmd_edit): - """Test prevent invalid prompt to be entered.""" + """Test preventing of an invalid prompt being entered.""" qtbot.keyClicks(cmd_edit, '$hello') assert cmd_edit.text() == ''