diff --git a/qutebrowser/utils/usertypes.py b/qutebrowser/utils/usertypes.py index 5a9ca4ad1..d1d6883a8 100644 --- a/qutebrowser/utils/usertypes.py +++ b/qutebrowser/utils/usertypes.py @@ -147,7 +147,7 @@ class NeighborList(collections.abc.Sequence): self._idx += offset self._idx %= len(self.items) new = self.curitem() - elif self._mode == self.Modes.exception: + elif self._mode == self.Modes.exception: # pragma: no branch raise else: self._idx += offset diff --git a/tests/utils/usertypes/test_enum.py b/tests/utils/usertypes/test_enum.py index 7298b2861..38fbb3167 100644 --- a/tests/utils/usertypes/test_enum.py +++ b/tests/utils/usertypes/test_enum.py @@ -23,8 +23,6 @@ from qutebrowser.utils import usertypes import pytest -# FIXME: Add some more tests, e.g. for is_int - @pytest.fixture def enum(): @@ -60,3 +58,17 @@ def test_exit(): """Make sure the exit status enum is correct.""" assert usertypes.Exit.ok == 0 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']) diff --git a/tests/utils/usertypes/test_neighborlist.py b/tests/utils/usertypes/test_neighborlist.py index 41e4f547a..ffce4723e 100644 --- a/tests/utils/usertypes/test_neighborlist.py +++ b/tests/utils/usertypes/test_neighborlist.py @@ -51,6 +51,11 @@ class TestInit: assert 2 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: @@ -71,6 +76,12 @@ class TestDefaultArg: nl = usertypes.NeighborList([1, 2, 3]) 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: diff --git a/tests/utils/usertypes/test_question.py b/tests/utils/usertypes/test_question.py new file mode 100644 index 000000000..b63106513 --- /dev/null +++ b/tests/utils/usertypes/test_question.py @@ -0,0 +1,91 @@ +# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et: + +# Copyright 2015 Florian Bruhin (The Compiler) +# +# 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 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' diff --git a/tests/utils/usertypes/test_timer.py b/tests/utils/usertypes/test_timer.py new file mode 100644 index 000000000..04e163ed1 --- /dev/null +++ b/tests/utils/usertypes/test_timer.py @@ -0,0 +1,86 @@ +# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et: + +# Copyright 2015 Florian Bruhin (The Compiler) +# +# 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 . + +# 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) == "" + + +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) == "" + + +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()