Clean up completiondelegate tests.

Respond to code review comments to reduce mocking and clean up comments.
This commit is contained in:
Ryan Roden-Corrent 2018-09-18 20:40:06 -04:00
parent 4f733333c3
commit 2eacf4bd94
No known key found for this signature in database
GPG Key ID: 4E5072F68872BC04

View File

@ -16,13 +16,10 @@
# #
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>. # along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
"""Tests for the CompletionView Object."""
from unittest import mock from unittest import mock
import pytest import pytest
from PyQt5.QtCore import QModelIndex, QObject
from PyQt5.QtGui import QPainter from PyQt5.QtGui import QPainter
from qutebrowser.completion import completiondelegate from qutebrowser.completion import completiondelegate
@ -30,32 +27,32 @@ from qutebrowser.completion import completiondelegate
@pytest.fixture @pytest.fixture
def painter(): def painter():
"""Create the CompletionView used for testing."""
return mock.Mock(spec=QPainter) return mock.Mock(spec=QPainter)
def _qt_mock(klass, mocker): def _qt_mock(klass, mocker):
m = mocker.patch( m = mocker.patch.object(completiondelegate, klass, autospec=True)
'qutebrowser.completion.completiondelegate.{}'.format(klass),
autospec=True)
return m return m
@pytest.fixture @pytest.fixture
def mock_style_option(mocker): def mock_style_option(mocker):
"""Create the CompletionView used for testing."""
return _qt_mock('QStyleOptionViewItem', mocker) return _qt_mock('QStyleOptionViewItem', mocker)
@pytest.fixture @pytest.fixture
def mock_text_document(mocker): def mock_text_document(mocker):
"""Create the CompletionView used for testing."""
return _qt_mock('QTextDocument', mocker) return _qt_mock('QTextDocument', mocker)
@pytest.fixture @pytest.fixture
def view(): def view():
return mock.Mock() class FakeView(QObject):
def __init__(self):
super().__init__()
self.pattern = None
return FakeView()
@pytest.fixture @pytest.fixture
@ -63,13 +60,9 @@ def delegate(mock_style_option, mock_text_document, config_stub, mocker, view):
_qt_mock('QStyle', mocker) _qt_mock('QStyle', mocker)
_qt_mock('QTextOption', mocker) _qt_mock('QTextOption', mocker)
_qt_mock('QAbstractTextDocumentLayout', mocker) _qt_mock('QAbstractTextDocumentLayout', mocker)
completiondelegate._cached_stylesheet = mock.Mock() completiondelegate._cached_stylesheet = ''
delegate = completiondelegate.CompletionItemDelegate() delegate = completiondelegate.CompletionItemDelegate(parent=view)
parent = mock.Mock()
parent.return_value = view
delegate.parent = parent
delegate.initStyleOption = mock.Mock() delegate.initStyleOption = mock.Mock()
delegate.setTextDirection = mock.Mock()
return delegate return delegate
@ -92,10 +85,9 @@ def delegate(mock_style_option, mock_text_document, config_stub, mocker, view):
]) ])
def test_paint(delegate, painter, view, mock_style_option, mock_text_document, def test_paint(delegate, painter, view, mock_style_option, mock_text_document,
pat, txt_in, txt_out): pat, txt_in, txt_out):
"""Ensure set_model actually sets the model and expands all categories."""
view.pattern = pat view.pattern = pat
mock_style_option().text = txt_in mock_style_option().text = txt_in
index = mock.Mock() index = mock.Mock(spec=QModelIndex)
index.column.return_value = 0 index.column.return_value = 0
index.model.return_value.columns_to_filter.return_value = [0] index.model.return_value.columns_to_filter.return_value = [0]
opt = mock_style_option() opt = mock_style_option()