Merge remote-tracking branch 'origin/usertypes-tests'

This commit is contained in:
Florian Bruhin 2015-07-06 17:58:13 +02:00
commit c5b75b0b16
5 changed files with 203 additions and 3 deletions

View File

@ -147,7 +147,7 @@ class NeighborList(collections.abc.Sequence):
self._idx += offset self._idx += offset
self._idx %= len(self.items) self._idx %= len(self.items)
new = self.curitem() new = self.curitem()
elif self._mode == self.Modes.exception: elif self._mode == self.Modes.exception: # pragma: no branch
raise raise
else: else:
self._idx += offset self._idx += offset

View File

@ -23,8 +23,6 @@ from qutebrowser.utils import usertypes
import pytest import pytest
# FIXME: Add some more tests, e.g. for is_int
@pytest.fixture @pytest.fixture
def enum(): def enum():
@ -60,3 +58,17 @@ def test_exit():
"""Make sure the exit status enum is correct.""" """Make sure the exit status enum is correct."""
assert usertypes.Exit.ok == 0 assert usertypes.Exit.ok == 0
assert usertypes.Exit.reserved == 1 assert usertypes.Exit.reserved == 1
def test_is_int():
"""Test the is_int argument."""
int_enum = usertypes.enum('Enum', ['item'], is_int=True)
no_int_enum = usertypes.enum('Enum', ['item'])
assert isinstance(int_enum.item, int)
assert not isinstance(no_int_enum.item, int)
def test_unique():
"""Make sure elements need to be unique."""
with pytest.raises(TypeError):
usertypes.enum('Enum', ['item', 'item'])

View File

@ -51,6 +51,11 @@ class TestInit:
assert 2 in nl assert 2 in nl
assert 4 not in nl assert 4 not in nl
def test_invalid_mode(self):
"""Test with an invalid mode."""
with pytest.raises(TypeError):
usertypes.NeighborList(mode='blah')
class TestDefaultArg: class TestDefaultArg:
@ -71,6 +76,12 @@ class TestDefaultArg:
nl = usertypes.NeighborList([1, 2, 3]) nl = usertypes.NeighborList([1, 2, 3])
assert nl._idx is None assert nl._idx is None
def test_invalid_reset(self):
"""Test reset without default."""
nl = usertypes.NeighborList([1, 2, 3, 4, 5])
with pytest.raises(ValueError):
nl.reset()
class TestEmpty: class TestEmpty:

View File

@ -0,0 +1,91 @@
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
# Copyright 2015 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
#
# 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 <http://www.gnu.org/licenses/>.
"""Tests for usertypes.Question."""
import logging
import pytest
from qutebrowser.utils import usertypes
@pytest.fixture
def question():
return usertypes.Question()
def test_attributes(question):
"""Test setting attributes."""
question.default = True
question.text = "foo"
def test_mode(question):
"""Test setting mode to valid members."""
question.mode = usertypes.PromptMode.yesno
assert question.mode == usertypes.PromptMode.yesno
def test_mode_invalid(question):
"""Test setting mode to something which is not a PromptMode member."""
with pytest.raises(TypeError):
question.mode = 42
@pytest.mark.parametrize('mode, answer, signal_names', [
(usertypes.PromptMode.text, 'foo', ['answered', 'completed']),
(usertypes.PromptMode.yesno, True, ['answered', 'completed',
'answered_yes']),
(usertypes.PromptMode.yesno, False, ['answered', 'completed',
'answered_no']),
])
def test_done(mode, answer, signal_names, question, qtbot):
"""Test the 'done' method and completed/answered signals."""
question.mode = mode
question.answer = answer
signals = [getattr(question, name) for name in signal_names]
with qtbot.waitSignals(signals, raising=True):
question.done()
assert not question.is_aborted
def test_cancel(question, qtbot):
"""Test Question.cancel()."""
with qtbot.waitSignals([question.cancelled, question.completed],
raising=True):
question.cancel()
assert not question.is_aborted
def test_abort(question, qtbot):
"""Test Question.abort()."""
with qtbot.waitSignals([question.aborted, question.completed],
raising=True):
question.abort()
assert question.is_aborted
def test_abort_typeerror(question, qtbot, mocker, caplog):
"""Test Question.abort() with .emit() raising a TypeError."""
signal_mock = mocker.patch('qutebrowser.utils.usertypes.Question.aborted')
signal_mock.emit.side_effect = TypeError
with caplog.atLevel(logging.ERROR):
question.abort()
assert caplog.records()[0].message == 'Error while aborting question'

View File

@ -0,0 +1,86 @@
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
# Copyright 2015 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
#
# 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 <http://www.gnu.org/licenses/>.
# pylint: disable=protected-access
"""Tests for Timer."""
from qutebrowser.utils import usertypes
import pytest
from PyQt5.QtCore import QObject
class Parent(QObject):
"""Class for test_parent()."""
pass
def test_parent():
"""Make sure the parent is set correctly."""
parent = Parent()
t = usertypes.Timer(parent)
assert t.parent() is parent
def test_named():
"""Make sure the name is set correctly."""
t = usertypes.Timer(name='foobar')
assert t._name == 'foobar'
assert t.objectName() == 'foobar'
assert repr(t) == "<qutebrowser.utils.usertypes.Timer name='foobar'>"
def test_unnamed():
"""Make sure an unnamed Timer is named correctly."""
t = usertypes.Timer()
assert not t.objectName()
assert t._name == 'unnamed'
assert repr(t) == "<qutebrowser.utils.usertypes.Timer name='unnamed'>"
def test_set_interval_overflow():
"""Make sure setInterval raises OverflowError with very big numbers."""
t = usertypes.Timer()
with pytest.raises(OverflowError):
t.setInterval(2 ** 64)
def test_start_overflow():
"""Make sure start raises OverflowError with very big numbers."""
t = usertypes.Timer()
with pytest.raises(OverflowError):
t.start(2 ** 64)
def test_timeout_start(qtbot):
"""Make sure the timer works with start()."""
t = usertypes.Timer()
with qtbot.waitSignal(t.timeout, raising=True):
t.start(200)
def test_timeout_set_interval(qtbot):
"""Make sure the timer works with setInterval()."""
t = usertypes.Timer()
with qtbot.waitSignal(t.timeout, raising=True):
t.setInterval(200)
t.start()