From 555bdb75b537ffc7d4cd39a6828b589c1f9b87a0 Mon Sep 17 00:00:00 2001 From: Ryan Roden-Corrent Date: Sat, 11 Jun 2016 12:43:37 -0400 Subject: [PATCH] Add unit test for CommandCompletionModel. This establishes a pattern that can probably be used to test any of the completion models. See #999. --- tests/helpers/stubs.py | 7 ++- tests/unit/completion/test_models.py | 82 +++++++++++++++++++++++++++ tests/unit/config/test_configtypes.py | 5 +- 3 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 tests/unit/completion/test_models.py diff --git a/tests/helpers/stubs.py b/tests/helpers/stubs.py index 87c5594c5..7d22f4359 100644 --- a/tests/helpers/stubs.py +++ b/tests/helpers/stubs.py @@ -283,8 +283,13 @@ class FakeCommand: """A simple command stub which has a description.""" - def __init__(self, desc): + def __init__(self, name='', desc='', hide=False, debug=False, + deprecated=False): self.desc = desc + self.name = name + self.hide = hide + self.debug = debug + self.deprecated = deprecated class FakeTimer(QObject): diff --git a/tests/unit/completion/test_models.py b/tests/unit/completion/test_models.py new file mode 100644 index 000000000..e08f82b73 --- /dev/null +++ b/tests/unit/completion/test_models.py @@ -0,0 +1,82 @@ +# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et: + +# Copyright 2016 Ryan Roden-Corrent (rcorre) +# +# This file is part of qutebrowser. +# +# qutebrowser is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# qutebrowser is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with qutebrowser. If not, see . + +"""Tests for completion models.""" + +from qutebrowser.completion.models import miscmodels + + +def test_command_completion(monkeypatch, stubs, config_stub, key_config_stub): + """Test the results of command completion. + + Validates that: + - only non-hidden and non-deprecated commands are included + - commands are sorted by name + - the command description is shown in the desc column + - the binding (if any) is shown in the misc column + - aliases are included + """ + _patch_cmdutils(monkeypatch, stubs, + 'qutebrowser.completion.models.miscmodels.cmdutils') + config_stub.data['aliases'] = {'rock': 'roll'} + key_config_stub.set_bindings_for('normal', {'s': 'stop', 'rr': 'roll'}) + actual = _get_completions(miscmodels.CommandCompletionModel()) + assert actual == [ + ("Commands", [ + ('drop', 'drop all user data', ''), + ('rock', "Alias for 'roll'", ''), + ('roll', 'never gonna give you up', 'rr'), + ('stop', 'stop qutebrowser', 's') + ]) + ] + + +def _get_completions(model): + """Collect all the completion entries of a model, organized by category. + + The result is a list of form: + [ + (CategoryName: [(name, desc, misc), ...]), + (CategoryName: [(name, desc, misc), ...]), + ... + ] + """ + completions = [] + for i in range(0, model.rowCount()): + category = model.item(i) + entries = [] + for j in range(0, category.rowCount()): + name = category.child(j, 0) + desc = category.child(j, 1) + misc = category.child(j, 2) + entries.append((name.text(), desc.text(), misc.text())) + completions.append((category.text(), entries)) + return completions + + +def _patch_cmdutils(monkeypatch, stubs, symbol): + """Patch the cmdutils module to provide fake commands.""" + cmd_utils = stubs.FakeCmdUtils({ + 'stop': stubs.FakeCommand(name='stop', desc='stop qutebrowser'), + 'drop': stubs.FakeCommand(name='drop', desc='drop all user data'), + 'roll': stubs.FakeCommand(name='roll', desc='never gonna give you up'), + 'hide': stubs.FakeCommand(name='hide', hide=True), + 'depr': stubs.FakeCommand(name='depr', deprecated=True), + }) + monkeypatch.setattr(symbol, cmd_utils) diff --git a/tests/unit/config/test_configtypes.py b/tests/unit/config/test_configtypes.py index 729256f68..6998d86cd 100644 --- a/tests/unit/config/test_configtypes.py +++ b/tests/unit/config/test_configtypes.py @@ -823,8 +823,9 @@ class TestCommand: @pytest.fixture(autouse=True) def patch(self, monkeypatch, stubs): """Patch the cmdutils module to provide fake commands.""" - cmd_utils = stubs.FakeCmdUtils({'cmd1': stubs.FakeCommand("desc 1"), - 'cmd2': stubs.FakeCommand("desc 2")}) + cmd_utils = stubs.FakeCmdUtils({ + 'cmd1': stubs.FakeCommand(desc="desc 1"), + 'cmd2': stubs.FakeCommand(desc="desc 2")}) monkeypatch.setattr('qutebrowser.config.configtypes.cmdutils', cmd_utils)