qutebrowser/test/misc/test_readline.py

168 lines
6.3 KiB
Python
Raw Normal View History

2014-06-19 09:04:37 +02:00
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
2015-01-03 15:51:31 +01:00
# Copyright 2014-2015 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
2014-05-27 14:58:07 +02:00
#
# 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 <http://www.gnu.org/licenses/>.
"""Tests for qutebrowser.misc.readline."""
2014-05-27 14:58:07 +02:00
# pylint: disable=protected-access
2014-05-27 14:58:07 +02:00
import inspect
import unittest
2014-08-26 19:10:14 +02:00
from unittest import mock
2014-05-27 14:58:07 +02:00
from PyQt5.QtWidgets import QLineEdit
from qutebrowser.misc import readline
2014-08-26 19:10:14 +02:00
from qutebrowser.test import stubs
2014-05-27 14:58:07 +02:00
@mock.patch('qutebrowser.misc.readline.QApplication',
new_callable=stubs.FakeQApplication)
2014-06-23 19:44:21 +02:00
class NoneWidgetTests(unittest.TestCase):
2014-05-27 14:58:07 +02:00
"""Tests when the focused widget is None."""
def setUp(self):
self.bridge = readline.ReadlineBridge()
def test_none(self, qapp):
2014-05-27 14:58:07 +02:00
"""Test if there are no exceptions when the widget is None."""
qapp.focusWidget = mock.Mock(return_value=None)
2014-05-27 14:58:07 +02:00
for name, method in inspect.getmembers(self.bridge, inspect.ismethod):
with self.subTest(name=name):
if name.startswith('rl_'):
method()
2014-05-27 14:58:07 +02:00
@mock.patch('qutebrowser.misc.readline.QApplication',
new_callable=stubs.FakeQApplication)
2014-06-23 19:44:21 +02:00
class ReadlineBridgeTest(unittest.TestCase):
2014-05-27 14:58:07 +02:00
"""Tests for readline bridge."""
def setUp(self):
2014-08-26 19:10:14 +02:00
self.qle = mock.Mock()
2014-05-27 14:58:07 +02:00
self.qle.__class__ = QLineEdit
self.bridge = readline.ReadlineBridge()
def _set_selected_text(self, text):
"""Set the value the fake QLineEdit should return for selectedText."""
self.qle.configure_mock(**{'selectedText.return_value': text})
def test_rl_backward_char(self, qapp):
2014-06-23 19:44:21 +02:00
"""Test rl_backward_char."""
qapp.focusWidget = mock.Mock(return_value=self.qle)
2014-05-27 14:58:07 +02:00
self.bridge.rl_backward_char()
self.qle.cursorBackward.assert_called_with(False)
def test_rl_forward_char(self, qapp):
2014-06-23 19:44:21 +02:00
"""Test rl_forward_char."""
qapp.focusWidget = mock.Mock(return_value=self.qle)
2014-05-27 14:58:07 +02:00
self.bridge.rl_forward_char()
self.qle.cursorForward.assert_called_with(False)
def test_rl_backward_word(self, qapp):
2014-06-23 19:44:21 +02:00
"""Test rl_backward_word."""
qapp.focusWidget = mock.Mock(return_value=self.qle)
2014-05-27 14:58:07 +02:00
self.bridge.rl_backward_word()
self.qle.cursorWordBackward.assert_called_with(False)
def test_rl_forward_word(self, qapp):
2014-06-23 19:44:21 +02:00
"""Test rl_forward_word."""
qapp.focusWidget = mock.Mock(return_value=self.qle)
2014-05-27 14:58:07 +02:00
self.bridge.rl_forward_word()
self.qle.cursorWordForward.assert_called_with(False)
def test_rl_beginning_of_line(self, qapp):
2014-06-23 19:44:21 +02:00
"""Test rl_beginning_of_line."""
qapp.focusWidget = mock.Mock(return_value=self.qle)
2014-05-27 14:58:07 +02:00
self.bridge.rl_beginning_of_line()
self.qle.home.assert_called_with(False)
def test_rl_end_of_line(self, qapp):
2014-06-23 19:44:21 +02:00
"""Test rl_end_of_line."""
qapp.focusWidget = mock.Mock(return_value=self.qle)
2014-05-27 14:58:07 +02:00
self.bridge.rl_end_of_line()
self.qle.end.assert_called_with(False)
def test_rl_delete_char(self, qapp):
2014-06-23 19:44:21 +02:00
"""Test rl_delete_char."""
qapp.focusWidget = mock.Mock(return_value=self.qle)
self.bridge.rl_delete_char()
self.qle.del_.assert_called_with()
def test_rl_backward_delete_char(self, qapp):
2014-06-23 19:44:21 +02:00
"""Test rl_backward_delete_char."""
qapp.focusWidget = mock.Mock(return_value=self.qle)
self.bridge.rl_backward_delete_char()
self.qle.backspace.assert_called_with()
def test_rl_unix_line_discard(self, qapp):
2014-05-27 14:58:07 +02:00
"""Set a selected text, delete it, see if it comes back with yank."""
qapp.focusWidget = mock.Mock(return_value=self.qle)
2014-05-27 14:58:07 +02:00
self._set_selected_text("delete test")
self.bridge.rl_unix_line_discard()
self.qle.home.assert_called_with(True)
self.assertEqual(self.bridge._deleted[self.qle], "delete test")
2014-05-27 14:58:07 +02:00
self.qle.del_.assert_called_with()
self.bridge.rl_yank()
self.qle.insert.assert_called_with("delete test")
def test_rl_kill_line(self, qapp):
2014-05-27 14:58:07 +02:00
"""Set a selected text, delete it, see if it comes back with yank."""
qapp.focusWidget = mock.Mock(return_value=self.qle)
2014-05-27 14:58:07 +02:00
self._set_selected_text("delete test")
self.bridge.rl_kill_line()
self.qle.end.assert_called_with(True)
self.assertEqual(self.bridge._deleted[self.qle], "delete test")
2014-05-27 14:58:07 +02:00
self.qle.del_.assert_called_with()
self.bridge.rl_yank()
self.qle.insert.assert_called_with("delete test")
def test_rl_unix_word_rubout(self, qapp):
2014-05-27 14:58:07 +02:00
"""Set a selected text, delete it, see if it comes back with yank."""
qapp.focusWidget = mock.Mock(return_value=self.qle)
2014-05-27 14:58:07 +02:00
self._set_selected_text("delete test")
self.bridge.rl_unix_word_rubout()
self.qle.cursorWordBackward.assert_called_with(True)
self.assertEqual(self.bridge._deleted[self.qle], "delete test")
2014-05-27 14:58:07 +02:00
self.qle.del_.assert_called_with()
self.bridge.rl_yank()
self.qle.insert.assert_called_with("delete test")
def test_rl_kill_word(self, qapp):
2014-05-27 14:58:07 +02:00
"""Set a selected text, delete it, see if it comes back with yank."""
qapp.focusWidget = mock.Mock(return_value=self.qle)
2014-05-27 14:58:07 +02:00
self._set_selected_text("delete test")
self.bridge.rl_kill_word()
self.qle.cursorWordForward.assert_called_with(True)
self.assertEqual(self.bridge._deleted[self.qle], "delete test")
2014-05-27 14:58:07 +02:00
self.qle.del_.assert_called_with()
self.bridge.rl_yank()
self.qle.insert.assert_called_with("delete test")
def test_rl_yank_no_text(self, qapp):
2014-05-27 14:58:07 +02:00
"""Test yank without having deleted anything."""
qapp.focusWidget = mock.Mock(return_value=self.qle)
2014-05-27 14:58:07 +02:00
self.bridge.rl_yank()
self.assertFalse(self.qle.insert.called)
if __name__ == '__main__':
unittest.main()