diff --git a/test/keyinput/test_modeparsers.py b/test/keyinput/test_modeparsers.py index 910b2bea9..c59bfe163 100644 --- a/test/keyinput/test_modeparsers.py +++ b/test/keyinput/test_modeparsers.py @@ -21,11 +21,10 @@ from PyQt5.QtCore import Qt -import unittest from unittest import mock +import pytest from qutebrowser.keyinput import modeparsers -from qutebrowser.test import stubs, helpers 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] -@mock.patch('qutebrowser.keyinput.basekeyparser.usertypes.Timer', - new=stubs.FakeTimer) -@mock.patch('qutebrowser.keyinput.modeparsers.config', - new=stubs.ConfigStub(CONFIG)) -class NormalKeyParserTests(unittest.TestCase): +class TestsNormalKeyParser: """Tests for NormalKeyParser. @@ -53,46 +48,48 @@ class NormalKeyParserTests(unittest.TestCase): # 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.""" + 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) self.kp = modeparsers.NormalKeyParser(0) self.kp.execute = mock.Mock() - - def tearDown(self): + yield objreg.delete('key-config') - def test_keychain(self): + def test_keychain(self, fake_keyevent_factory): """Test valid keychain.""" # 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 - self.kp.handle(helpers.fake_keyevent(Qt.Key_B, text='b')) - self.kp.handle(helpers.fake_keyevent(Qt.Key_A, text='a')) + self.kp.handle(fake_keyevent_factory(Qt.Key_B, text='b')) + 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.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.""" timer = self.kp._partial_timer - self.assertFalse(timer.isActive()) + assert not timer.isActive() # Press 'b' for a partial match. # Then we check if the timer has been set up correctly - self.kp.handle(helpers.fake_keyevent(Qt.Key_B, text='b')) - self.assertTrue(timer.isSingleShot()) - self.assertEqual(timer.interval(), 100) - self.assertTrue(timer.isActive()) + self.kp.handle(fake_keyevent_factory(Qt.Key_B, text='b')) + assert timer.isSingleShot() + assert timer.interval() == 100 + assert timer.isActive() - self.assertFalse(self.kp.execute.called) - self.assertEqual(self.kp._keystring, 'b') + assert not self.kp.execute.called + assert self.kp._keystring == 'b' # Now simulate a timeout and check the keystring has been cleared. keystring_updated_mock = mock.Mock() self.kp.keystring_updated.connect(keystring_updated_mock) timer.timeout.emit() - self.assertFalse(self.kp.execute.called) - self.assertEqual(self.kp._keystring, '') + assert not self.kp.execute.called + assert self.kp._keystring == '' keystring_updated_mock.assert_called_once_with('') - -if __name__ == '__main__': - unittest.main()