Converted test_modeparsers to pytest

This commit is contained in:
Bruno Oliveira 2015-04-04 12:57:54 -03:00
parent 065c3fcd9d
commit 12903a34f4

View File

@ -21,11 +21,10 @@
from PyQt5.QtCore import Qt from PyQt5.QtCore import Qt
import unittest
from unittest import mock from unittest import mock
import pytest
from qutebrowser.keyinput import modeparsers from qutebrowser.keyinput import modeparsers
from qutebrowser.test import stubs, helpers
from qutebrowser.utils import objreg from qutebrowser.utils import objreg
@ -39,11 +38,7 @@ fake_keyconfig = mock.Mock(spec=['get_bindings_for'])
fake_keyconfig.get_bindings_for.side_effect = lambda s: BINDINGS[s] fake_keyconfig.get_bindings_for.side_effect = lambda s: BINDINGS[s]
@mock.patch('qutebrowser.keyinput.basekeyparser.usertypes.Timer', class TestsNormalKeyParser:
new=stubs.FakeTimer)
@mock.patch('qutebrowser.keyinput.modeparsers.config',
new=stubs.ConfigStub(CONFIG))
class NormalKeyParserTests(unittest.TestCase):
"""Tests for NormalKeyParser. """Tests for NormalKeyParser.
@ -53,46 +48,48 @@ class NormalKeyParserTests(unittest.TestCase):
# pylint: disable=protected-access # pylint: disable=protected-access
def setUp(self): @pytest.yield_fixture(autouse=True)
def setup(self, mocker, stubs):
"""Set up mocks and read the test config.""" """Set up mocks and read the test config."""
mocker.patch('qutebrowser.keyinput.basekeyparser.usertypes.Timer',
new=stubs.FakeTimer)
mocker.patch('qutebrowser.keyinput.modeparsers.config',
new=stubs.ConfigStub(CONFIG))
objreg.register('key-config', fake_keyconfig) objreg.register('key-config', fake_keyconfig)
self.kp = modeparsers.NormalKeyParser(0) self.kp = modeparsers.NormalKeyParser(0)
self.kp.execute = mock.Mock() self.kp.execute = mock.Mock()
yield
def tearDown(self):
objreg.delete('key-config') objreg.delete('key-config')
def test_keychain(self): def test_keychain(self, fake_keyevent_factory):
"""Test valid keychain.""" """Test valid keychain."""
# Press 'x' which is ignored because of no match # Press 'x' which is ignored because of no match
self.kp.handle(helpers.fake_keyevent(Qt.Key_X, text='x')) self.kp.handle(fake_keyevent_factory(Qt.Key_X, text='x'))
# Then start the real chain # Then start the real chain
self.kp.handle(helpers.fake_keyevent(Qt.Key_B, text='b')) self.kp.handle(fake_keyevent_factory(Qt.Key_B, text='b'))
self.kp.handle(helpers.fake_keyevent(Qt.Key_A, text='a')) self.kp.handle(fake_keyevent_factory(Qt.Key_A, text='a'))
self.kp.execute.assert_called_once_with('ba', self.kp.Type.chain, None) self.kp.execute.assert_called_once_with('ba', self.kp.Type.chain, None)
self.assertEqual(self.kp._keystring, '') assert self.kp._keystring == ''
def test_partial_keychain_timeout(self): def test_partial_keychain_timeout(self, fake_keyevent_factory):
"""Test partial keychain timeout.""" """Test partial keychain timeout."""
timer = self.kp._partial_timer timer = self.kp._partial_timer
self.assertFalse(timer.isActive()) assert not timer.isActive()
# Press 'b' for a partial match. # Press 'b' for a partial match.
# Then we check if the timer has been set up correctly # Then we check if the timer has been set up correctly
self.kp.handle(helpers.fake_keyevent(Qt.Key_B, text='b')) self.kp.handle(fake_keyevent_factory(Qt.Key_B, text='b'))
self.assertTrue(timer.isSingleShot()) assert timer.isSingleShot()
self.assertEqual(timer.interval(), 100) assert timer.interval() == 100
self.assertTrue(timer.isActive()) assert timer.isActive()
self.assertFalse(self.kp.execute.called) assert not self.kp.execute.called
self.assertEqual(self.kp._keystring, 'b') assert self.kp._keystring == 'b'
# Now simulate a timeout and check the keystring has been cleared. # Now simulate a timeout and check the keystring has been cleared.
keystring_updated_mock = mock.Mock() keystring_updated_mock = mock.Mock()
self.kp.keystring_updated.connect(keystring_updated_mock) self.kp.keystring_updated.connect(keystring_updated_mock)
timer.timeout.emit() timer.timeout.emit()
self.assertFalse(self.kp.execute.called) assert not self.kp.execute.called
self.assertEqual(self.kp._keystring, '') assert self.kp._keystring == ''
keystring_updated_mock.assert_called_once_with('') keystring_updated_mock.assert_called_once_with('')
if __name__ == '__main__':
unittest.main()