Converted test_readline to pytest

This commit is contained in:
Bruno Oliveira 2015-04-04 12:49:23 -03:00
parent 7e7a1b7b28
commit 6388ec4794

View File

@ -22,40 +22,42 @@
# pylint: disable=protected-access
import inspect
import unittest
from unittest import mock
from PyQt5.QtWidgets import QLineEdit
import pytest
from qutebrowser.misc import readline
from qutebrowser.test import stubs
@mock.patch('qutebrowser.misc.readline.QApplication',
new_callable=stubs.FakeQApplication)
class NoneWidgetTests(unittest.TestCase):
@pytest.fixture
def mocked_qapp(mocker, stubs):
"""
Fixture that mocks QApplication in the readline module and returns it.
"""
return mocker.patch('qutebrowser.misc.readline.QApplication',
new_callable=stubs.FakeQApplication)
class TestNoneWidget:
"""Tests when the focused widget is None."""
def setUp(self):
def test_none(self, mocked_qapp):
self.bridge = readline.ReadlineBridge()
def test_none(self, qapp):
"""Test if there are no exceptions when the widget is None."""
qapp.focusWidget = mock.Mock(return_value=None)
mocked_qapp.focusWidget = mock.Mock(return_value=None)
for name, method in inspect.getmembers(self.bridge, inspect.ismethod):
with self.subTest(name=name):
if name.startswith('rl_'):
method()
if name.startswith('rl_'):
method()
@mock.patch('qutebrowser.misc.readline.QApplication',
new_callable=stubs.FakeQApplication)
class ReadlineBridgeTest(unittest.TestCase):
class TestReadlineBridgeTest:
"""Tests for readline bridge."""
def setUp(self):
@pytest.fixture(autouse=True)
def setup(self):
self.qle = mock.Mock()
self.qle.__class__ = QLineEdit
self.bridge = readline.ReadlineBridge()
@ -64,104 +66,100 @@ class ReadlineBridgeTest(unittest.TestCase):
"""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):
def test_rl_backward_char(self, mocked_qapp):
"""Test rl_backward_char."""
qapp.focusWidget = mock.Mock(return_value=self.qle)
mocked_qapp.focusWidget = mock.Mock(return_value=self.qle)
self.bridge.rl_backward_char()
self.qle.cursorBackward.assert_called_with(False)
def test_rl_forward_char(self, qapp):
def test_rl_forward_char(self, mocked_qapp):
"""Test rl_forward_char."""
qapp.focusWidget = mock.Mock(return_value=self.qle)
mocked_qapp.focusWidget = mock.Mock(return_value=self.qle)
self.bridge.rl_forward_char()
self.qle.cursorForward.assert_called_with(False)
def test_rl_backward_word(self, qapp):
def test_rl_backward_word(self, mocked_qapp):
"""Test rl_backward_word."""
qapp.focusWidget = mock.Mock(return_value=self.qle)
mocked_qapp.focusWidget = mock.Mock(return_value=self.qle)
self.bridge.rl_backward_word()
self.qle.cursorWordBackward.assert_called_with(False)
def test_rl_forward_word(self, qapp):
def test_rl_forward_word(self, mocked_qapp):
"""Test rl_forward_word."""
qapp.focusWidget = mock.Mock(return_value=self.qle)
mocked_qapp.focusWidget = mock.Mock(return_value=self.qle)
self.bridge.rl_forward_word()
self.qle.cursorWordForward.assert_called_with(False)
def test_rl_beginning_of_line(self, qapp):
def test_rl_beginning_of_line(self, mocked_qapp):
"""Test rl_beginning_of_line."""
qapp.focusWidget = mock.Mock(return_value=self.qle)
mocked_qapp.focusWidget = mock.Mock(return_value=self.qle)
self.bridge.rl_beginning_of_line()
self.qle.home.assert_called_with(False)
def test_rl_end_of_line(self, qapp):
def test_rl_end_of_line(self, mocked_qapp):
"""Test rl_end_of_line."""
qapp.focusWidget = mock.Mock(return_value=self.qle)
mocked_qapp.focusWidget = mock.Mock(return_value=self.qle)
self.bridge.rl_end_of_line()
self.qle.end.assert_called_with(False)
def test_rl_delete_char(self, qapp):
def test_rl_delete_char(self, mocked_qapp):
"""Test rl_delete_char."""
qapp.focusWidget = mock.Mock(return_value=self.qle)
mocked_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):
def test_rl_backward_delete_char(self, mocked_qapp):
"""Test rl_backward_delete_char."""
qapp.focusWidget = mock.Mock(return_value=self.qle)
mocked_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):
def test_rl_unix_line_discard(self, mocked_qapp):
"""Set a selected text, delete it, see if it comes back with yank."""
qapp.focusWidget = mock.Mock(return_value=self.qle)
mocked_qapp.focusWidget = mock.Mock(return_value=self.qle)
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")
assert self.bridge._deleted[self.qle] == "delete test"
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):
def test_rl_kill_line(self, mocked_qapp):
"""Set a selected text, delete it, see if it comes back with yank."""
qapp.focusWidget = mock.Mock(return_value=self.qle)
mocked_qapp.focusWidget = mock.Mock(return_value=self.qle)
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")
assert self.bridge._deleted[self.qle] == "delete test"
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):
def test_rl_unix_word_rubout(self, mocked_qapp):
"""Set a selected text, delete it, see if it comes back with yank."""
qapp.focusWidget = mock.Mock(return_value=self.qle)
mocked_qapp.focusWidget = mock.Mock(return_value=self.qle)
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")
assert self.bridge._deleted[self.qle] == "delete test"
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):
def test_rl_kill_word(self, mocked_qapp):
"""Set a selected text, delete it, see if it comes back with yank."""
qapp.focusWidget = mock.Mock(return_value=self.qle)
mocked_qapp.focusWidget = mock.Mock(return_value=self.qle)
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")
assert self.bridge._deleted[self.qle] == "delete test"
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):
def test_rl_yank_no_text(self, mocked_qapp):
"""Test yank without having deleted anything."""
qapp.focusWidget = mock.Mock(return_value=self.qle)
mocked_qapp.focusWidget = mock.Mock(return_value=self.qle)
self.bridge.rl_yank()
self.assertFalse(self.qle.insert.called)
if __name__ == '__main__':
unittest.main()
assert not self.qle.insert.called