From b381148e06c7e62647c2b8e0ecd7b09e590bf471 Mon Sep 17 00:00:00 2001 From: Ryan Roden-Corrent Date: Sun, 12 Feb 2017 18:03:00 -0500 Subject: [PATCH] Unittest CompletionView.completion_item_del. There were no unit tests for this and the various ways it can fail, and I'm about to screw with it a bit. --- .../unit/completion/test_completionwidget.py | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/tests/unit/completion/test_completionwidget.py b/tests/unit/completion/test_completionwidget.py index 88f4aed20..2c8d96713 100644 --- a/tests/unit/completion/test_completionwidget.py +++ b/tests/unit/completion/test_completionwidget.py @@ -26,6 +26,7 @@ from PyQt5.QtGui import QStandardItem, QColor from qutebrowser.completion import completionwidget from qutebrowser.completion.models import base, sortfilter +from qutebrowser.commands import cmdexc @pytest.fixture @@ -69,6 +70,16 @@ def completionview(qtbot, status_command_stub, config_stub, win_registry, return view +@pytest.fixture +def simplemodel(completionview): + """A filter model wrapped around a completion model with one item.""" + model = base.CompletionModel() + cat = QStandardItem() + cat.appendRow(QStandardItem('foo')) + model.appendRow(cat) + return sortfilter.CompletionFilterModel(model, parent=completionview) + + def test_set_model(completionview): """Ensure set_model actually sets the model and expands all categories.""" model = base.CompletionModel() @@ -218,3 +229,29 @@ def test_completion_show(show, rows, quick_complete, completionview, completionview.set_model(None) completionview.completion_item_focus('next') assert not completionview.isVisible() + + +def test_completion_item_del(completionview, simplemodel): + """Test that completion_item_del invokes delete_cur_item in the model.""" + simplemodel.srcmodel.delete_cur_item = unittest.mock.Mock() + completionview.set_model(simplemodel) + completionview.completion_item_focus('next') + completionview.completion_item_del() + assert simplemodel.srcmodel.delete_cur_item.called + + +def test_completion_item_del_no_selection(completionview, simplemodel): + """Test that completion_item_del with no selected index.""" + simplemodel.srcmodel.delete_cur_item = unittest.mock.Mock() + completionview.set_model(simplemodel) + with pytest.raises(cmdexc.CommandError): + completionview.completion_item_del() + assert not simplemodel.srcmodel.delete_cur_item.called + + +def test_completion_item_del_no_func(completionview, simplemodel): + """Test completion_item_del with no delete_cur_item in the model.""" + completionview.set_model(simplemodel) + completionview.completion_item_focus('next') + with pytest.raises(cmdexc.CommandError): + completionview.completion_item_del()