Update key hint tests for new format.

Change the unit tests to expect the new tabular format.
Also generally clean up the tests -- refactor from a class to
module-level functions as there was no need for a class here.
This commit is contained in:
Ryan Roden-Corrent 2016-05-16 08:36:01 -04:00
parent acb60a1bf4
commit 822d148713
2 changed files with 80 additions and 62 deletions

View File

@ -136,6 +136,8 @@ PERFECT_FILES = [
'qutebrowser/utils/jinja.py'), 'qutebrowser/utils/jinja.py'),
('tests/unit/utils/test_error.py', ('tests/unit/utils/test_error.py',
'qutebrowser/utils/error.py'), 'qutebrowser/utils/error.py'),
('tests/unit/utils/test_keyhints.py',
'qutebrowser/misc/keyhintwidget.py'),
] ]

View File

@ -20,77 +20,93 @@
"""Test the keyhint widget.""" """Test the keyhint widget."""
from collections import OrderedDict from collections import OrderedDict
from unittest import mock
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication
import pytest import pytest
from qutebrowser.misc.keyhintwidget import KeyHintView from qutebrowser.misc.keyhintwidget import KeyHintView
class TestKeyHintView: def expected_text(*args):
"""Tests for KeyHintView widget.""" """Helper to format text we expect the KeyHintView to generate.
@pytest.fixture Args:
def keyhint(self, qtbot, config_stub, key_config_stub): args: One tuple for each row in the expected output.
"""Fixture to initialize a KeyHintView.""" Tuples are of the form: (prefix, color, suffix, command).
config_stub.data = { """
'colors': { text = '<table>'
'keyhint.fg': 'white', for group in args:
'keyhint.fg.suffix': 'yellow', print("group = {}".format(group))
'keyhint.bg': 'black' text += ("<tr>"
}, "<td>{}</td>"
'fonts': {'keyhint': 'Comic Sans'}, "<td style='color: {}'>{}</td>"
'ui': {'show-keyhints': True}, "<td style='padding-left: 2ex'>{}</td>"
} "</tr>").format(*group)
keyhint = KeyHintView(0, None)
#qtbot.add_widget(keyhint)
assert keyhint.text() == ''
return keyhint
def test_suggestions(self, qtbot, keyhint, key_config_stub): return text + '</table>'
"""Test cursor position based on the prompt."""
# we want the dict to return sorted items() for reliable testing
key_config_stub.set_bindings_for('normal', OrderedDict([
('aa', 'cmd-aa'),
('ab', 'cmd-ab'),
('aba', 'cmd-aba'),
('abb', 'cmd-abb'),
('xd', 'cmd-xd'),
('xe', 'cmd-xe')]))
keyhint.update_keyhint('normal', 'a')
line = "a<font color='{}'>{}</font> {}<br>"
assert keyhint.text() == (line.format('yellow', 'a', 'cmd-aa') +
line.format('yellow', 'b', 'cmd-ab') +
line.format('yellow', 'ba', 'cmd-aba') +
line.format('yellow', 'bb', 'cmd-abb'))
def test_special_bindings(self, qtbot, keyhint, key_config_stub): @pytest.fixture
"""Ensure the a prefix of '<' doesn't suggest special keys""" def keyhint(qtbot, config_stub, key_config_stub):
# we want the dict to return sorted items() for reliable testing """Fixture to initialize a KeyHintView."""
key_config_stub.set_bindings_for('normal', OrderedDict([ config_stub.data = {
('<a', 'cmd-aa'), 'colors': {
('<b', 'cmd-ab'), 'keyhint.fg': 'white',
('<ctrl-a>', 'cmd-aba')])) 'keyhint.fg.suffix': 'yellow',
'keyhint.bg': 'black'
},
'fonts': {'keyhint': 'Comic Sans'},
'ui': {'show-keyhints': True},
}
keyhint = KeyHintView(0, None)
qtbot.add_widget(keyhint)
assert keyhint.text() == ''
return keyhint
keyhint.update_keyhint('normal', '<')
line = "&lt;<font color='{}'>{}</font> {}<br>"
assert keyhint.text() == (line.format('yellow', 'a', 'cmd-aa') +
line.format('yellow', 'b', 'cmd-ab'))
def test_disable(self, qtbot, keyhint, config_stub): def test_suggestions(keyhint, key_config_stub):
"""Ensure the widget isn't visible if disabled.""" """Test cursor position based on the prompt."""
config_stub.set('ui', 'show-keyhints', False) # we want the dict to return sorted items() for reliable testing
keyhint.update_keyhint('normal', 'a') key_config_stub.set_bindings_for('normal', OrderedDict([
assert not keyhint.text() ('aa', 'cmd-aa'),
assert not keyhint.isVisible() ('ab', 'cmd-ab'),
('aba', 'cmd-aba'),
('abb', 'cmd-abb'),
('xd', 'cmd-xd'),
('xe', 'cmd-xe')]))
def test_color_switch(self, qtbot, keyhint, config_stub, key_config_stub): keyhint.update_keyhint('normal', 'a')
"""Ensure the the keyhint suffix color can be updated at runtime.""" assert keyhint.text() == expected_text(
config_stub.set('colors', 'keyhint.fg.suffix', '#ABCDEF') ('a', 'yellow', 'a', 'cmd-aa'),
key_config_stub.set_bindings_for('normal', OrderedDict([ ('a', 'yellow', 'b', 'cmd-ab'),
('aa', 'cmd-aa')])) ('a', 'yellow', 'ba', 'cmd-aba'),
keyhint.update_keyhint('normal', 'a') ('a', 'yellow', 'bb', 'cmd-abb'))
expected = "a<font color='#ABCDEF'>a</font> cmd-aa<br>"
assert keyhint.text() == expected
def test_special_bindings(keyhint, key_config_stub):
"""Ensure the a prefix of '<' doesn't suggest special keys."""
# we want the dict to return sorted items() for reliable testing
key_config_stub.set_bindings_for('normal', OrderedDict([
('<a', 'cmd-<a'),
('<b', 'cmd-<b'),
('<ctrl-a>', 'cmd-ctrla')]))
keyhint.update_keyhint('normal', '<')
assert keyhint.text() == expected_text(
('&lt;', 'yellow', 'a', 'cmd-&lt;a'),
('&lt;', 'yellow', 'b', 'cmd-&lt;b'))
def test_disable(keyhint, config_stub):
"""Ensure the widget isn't visible if disabled."""
config_stub.set('ui', 'show-keyhints', False)
keyhint.update_keyhint('normal', 'a')
assert not keyhint.text()
assert not keyhint.isVisible()
def test_color_switch(keyhint, config_stub, key_config_stub):
"""Ensure the the keyhint suffix color can be updated at runtime."""
config_stub.set('colors', 'keyhint.fg.suffix', '#ABCDEF')
key_config_stub.set_bindings_for('normal', OrderedDict([
('aa', 'cmd-aa')]))
keyhint.update_keyhint('normal', 'a')
assert keyhint.text() == expected_text(('a', '#ABCDEF', 'a', 'cmd-aa'))