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,19 +20,32 @@
"""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.
Args:
args: One tuple for each row in the expected output.
Tuples are of the form: (prefix, color, suffix, command).
"""
text = '<table>'
for group in args:
print("group = {}".format(group))
text += ("<tr>"
"<td>{}</td>"
"<td style='color: {}'>{}</td>"
"<td style='padding-left: 2ex'>{}</td>"
"</tr>").format(*group)
return text + '</table>'
@pytest.fixture @pytest.fixture
def keyhint(self, qtbot, config_stub, key_config_stub): def keyhint(qtbot, config_stub, key_config_stub):
"""Fixture to initialize a KeyHintView.""" """Fixture to initialize a KeyHintView."""
config_stub.data = { config_stub.data = {
'colors': { 'colors': {
@ -44,11 +57,12 @@ class TestKeyHintView:
'ui': {'show-keyhints': True}, 'ui': {'show-keyhints': True},
} }
keyhint = KeyHintView(0, None) keyhint = KeyHintView(0, None)
#qtbot.add_widget(keyhint) qtbot.add_widget(keyhint)
assert keyhint.text() == '' assert keyhint.text() == ''
return keyhint return keyhint
def test_suggestions(self, qtbot, keyhint, key_config_stub):
def test_suggestions(keyhint, key_config_stub):
"""Test cursor position based on the prompt.""" """Test cursor position based on the prompt."""
# we want the dict to return sorted items() for reliable testing # we want the dict to return sorted items() for reliable testing
key_config_stub.set_bindings_for('normal', OrderedDict([ key_config_stub.set_bindings_for('normal', OrderedDict([
@ -60,37 +74,39 @@ class TestKeyHintView:
('xe', 'cmd-xe')])) ('xe', 'cmd-xe')]))
keyhint.update_keyhint('normal', 'a') keyhint.update_keyhint('normal', 'a')
line = "a<font color='{}'>{}</font> {}<br>" assert keyhint.text() == expected_text(
assert keyhint.text() == (line.format('yellow', 'a', 'cmd-aa') + ('a', 'yellow', 'a', 'cmd-aa'),
line.format('yellow', 'b', 'cmd-ab') + ('a', 'yellow', 'b', 'cmd-ab'),
line.format('yellow', 'ba', 'cmd-aba') + ('a', 'yellow', 'ba', 'cmd-aba'),
line.format('yellow', 'bb', 'cmd-abb')) ('a', 'yellow', 'bb', 'cmd-abb'))
def test_special_bindings(self, qtbot, keyhint, key_config_stub):
"""Ensure the a prefix of '<' doesn't suggest special keys""" 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 # we want the dict to return sorted items() for reliable testing
key_config_stub.set_bindings_for('normal', OrderedDict([ key_config_stub.set_bindings_for('normal', OrderedDict([
('<a', 'cmd-aa'), ('<a', 'cmd-<a'),
('<b', 'cmd-ab'), ('<b', 'cmd-<b'),
('<ctrl-a>', 'cmd-aba')])) ('<ctrl-a>', 'cmd-ctrla')]))
keyhint.update_keyhint('normal', '<') keyhint.update_keyhint('normal', '<')
line = "&lt;<font color='{}'>{}</font> {}<br>" assert keyhint.text() == expected_text(
assert keyhint.text() == (line.format('yellow', 'a', 'cmd-aa') + ('&lt;', 'yellow', 'a', 'cmd-&lt;a'),
line.format('yellow', 'b', 'cmd-ab')) ('&lt;', 'yellow', 'b', 'cmd-&lt;b'))
def test_disable(self, qtbot, keyhint, config_stub):
def test_disable(keyhint, config_stub):
"""Ensure the widget isn't visible if disabled.""" """Ensure the widget isn't visible if disabled."""
config_stub.set('ui', 'show-keyhints', False) config_stub.set('ui', 'show-keyhints', False)
keyhint.update_keyhint('normal', 'a') keyhint.update_keyhint('normal', 'a')
assert not keyhint.text() assert not keyhint.text()
assert not keyhint.isVisible() assert not keyhint.isVisible()
def test_color_switch(self, qtbot, keyhint, config_stub, key_config_stub):
def test_color_switch(keyhint, config_stub, key_config_stub):
"""Ensure the the keyhint suffix color can be updated at runtime.""" """Ensure the the keyhint suffix color can be updated at runtime."""
config_stub.set('colors', 'keyhint.fg.suffix', '#ABCDEF') config_stub.set('colors', 'keyhint.fg.suffix', '#ABCDEF')
key_config_stub.set_bindings_for('normal', OrderedDict([ key_config_stub.set_bindings_for('normal', OrderedDict([
('aa', 'cmd-aa')])) ('aa', 'cmd-aa')]))
keyhint.update_keyhint('normal', 'a') keyhint.update_keyhint('normal', 'a')
expected = "a<font color='#ABCDEF'>a</font> cmd-aa<br>" assert keyhint.text() == expected_text(('a', '#ABCDEF', 'a', 'cmd-aa'))
assert keyhint.text() == expected