Merge branch 'pytest-rewrites'

This commit is contained in:
Florian Bruhin 2015-05-18 21:42:15 +02:00
commit 8e417970c3
5 changed files with 466 additions and 497 deletions

View File

@ -19,8 +19,6 @@
"""Tests for qutebrowser.misc.crashdialog.""" """Tests for qutebrowser.misc.crashdialog."""
import unittest
from qutebrowser.misc import crashdialog from qutebrowser.misc import crashdialog
@ -52,7 +50,7 @@ Hello world!
""" """
class ParseFatalStacktraceTests(unittest.TestCase): class TestParseFatalStacktrace:
"""Tests for parse_fatal_stacktrace.""" """Tests for parse_fatal_stacktrace."""
@ -60,30 +58,22 @@ class ParseFatalStacktraceTests(unittest.TestCase):
"""Test parse_fatal_stacktrace with a valid text.""" """Test parse_fatal_stacktrace with a valid text."""
text = VALID_CRASH_TEXT.strip().replace('_', ' ') text = VALID_CRASH_TEXT.strip().replace('_', ' ')
typ, func = crashdialog.parse_fatal_stacktrace(text) typ, func = crashdialog.parse_fatal_stacktrace(text)
self.assertEqual(typ, "Segmentation fault") assert (typ, func) == ("Segmentation fault", 'testfunc')
self.assertEqual(func, 'testfunc')
def test_valid_text_thread(self): def test_valid_text_thread(self):
"""Test parse_fatal_stacktrace with a valid text #2.""" """Test parse_fatal_stacktrace with a valid text #2."""
text = VALID_CRASH_TEXT_THREAD.strip().replace('_', ' ') text = VALID_CRASH_TEXT_THREAD.strip().replace('_', ' ')
typ, func = crashdialog.parse_fatal_stacktrace(text) typ, func = crashdialog.parse_fatal_stacktrace(text)
self.assertEqual(typ, "Segmentation fault") assert (typ, func) == ("Segmentation fault", 'testfunc')
self.assertEqual(func, 'testfunc')
def test_valid_text_empty(self): def test_valid_text_empty(self):
"""Test parse_fatal_stacktrace with a valid text but empty function.""" """Test parse_fatal_stacktrace with a valid text but empty function."""
text = VALID_CRASH_TEXT_EMPTY.strip().replace('_', ' ') text = VALID_CRASH_TEXT_EMPTY.strip().replace('_', ' ')
typ, func = crashdialog.parse_fatal_stacktrace(text) typ, func = crashdialog.parse_fatal_stacktrace(text)
self.assertEqual(typ, 'Aborted') assert (typ, func) == ('Aborted', '')
self.assertEqual(func, '')
def test_invalid_text(self): def test_invalid_text(self):
"""Test parse_fatal_stacktrace with an invalid text.""" """Test parse_fatal_stacktrace with an invalid text."""
text = INVALID_CRASH_TEXT.strip().replace('_', ' ') text = INVALID_CRASH_TEXT.strip().replace('_', ' ')
typ, func = crashdialog.parse_fatal_stacktrace(text) typ, func = crashdialog.parse_fatal_stacktrace(text)
self.assertEqual(typ, '') assert (typ, func) == ('', '')
self.assertEqual(func, '')
if __name__ == '__main__':
unittest.main()

View File

@ -23,10 +23,10 @@
import io import io
import os import os
import unittest
from unittest import mock
from qutebrowser.misc import lineparser import pytest
from qutebrowser.misc import lineparser as lineparsermod
class LineParserWrapper: class LineParserWrapper:
@ -72,116 +72,126 @@ class LineParserWrapper:
return True return True
class TestableAppendLineParser(LineParserWrapper, lineparser.AppendLineParser): class TestableAppendLineParser(LineParserWrapper,
lineparsermod.AppendLineParser):
"""Wrapper over AppendLineParser to make it testable.""" """Wrapper over AppendLineParser to make it testable."""
pass pass
class TestableLineParser(LineParserWrapper, lineparser.LineParser): class TestableLineParser(LineParserWrapper, lineparsermod.LineParser):
"""Wrapper over LineParser to make it testable.""" """Wrapper over LineParser to make it testable."""
pass pass
class TestableLimitLineParser(LineParserWrapper, lineparser.LimitLineParser): class TestableLimitLineParser(LineParserWrapper,
lineparsermod.LimitLineParser):
"""Wrapper over LimitLineParser to make it testable.""" """Wrapper over LimitLineParser to make it testable."""
pass pass
@mock.patch('qutebrowser.misc.lineparser.os.path') class TestBaseLineParser:
@mock.patch('qutebrowser.misc.lineparser.os')
class BaseLineParserTests(unittest.TestCase):
"""Tests for BaseLineParser.""" """Tests for BaseLineParser."""
def setUp(self): CONFDIR = "this really doesn't matter"
self._confdir = "this really doesn't matter" FILENAME = "and neither does this"
self._fname = "and neither does this"
self._lineparser = lineparser.BaseLineParser(
self._confdir, self._fname)
def test_prepare_save_existing(self, os_mock, os_path_mock): @pytest.fixture
def lineparser(self):
"""Fixture providing a BaseLineParser."""
return lineparsermod.BaseLineParser(self.CONFDIR, self.FILENAME)
def test_prepare_save_existing(self, mocker, lineparser):
"""Test if _prepare_save does what it's supposed to do.""" """Test if _prepare_save does what it's supposed to do."""
os_path_mock.exists.return_value = True exists_mock = mocker.patch(
self._lineparser._prepare_save() 'qutebrowser.misc.lineparser.os.path.exists')
self.assertFalse(os_mock.makedirs.called) makedirs_mock = mocker.patch('qutebrowser.misc.lineparser.os.makedirs')
exists_mock.return_value = True
def test_prepare_save_missing(self, os_mock, os_path_mock): lineparser._prepare_save()
assert not makedirs_mock.called
def test_prepare_save_missing(self, mocker, lineparser):
"""Test if _prepare_save does what it's supposed to do.""" """Test if _prepare_save does what it's supposed to do."""
os_path_mock.exists.return_value = False exists_mock = mocker.patch(
self._lineparser._prepare_save() 'qutebrowser.misc.lineparser.os.path.exists')
os_mock.makedirs.assert_called_with(self._confdir, 0o755) exists_mock.return_value = False
makedirs_mock = mocker.patch('qutebrowser.misc.lineparser.os.makedirs')
lineparser._prepare_save()
makedirs_mock.assert_called_with(self.CONFDIR, 0o755)
class AppendLineParserTests(unittest.TestCase): class TestAppendLineParser:
"""Tests for AppendLineParser.""" """Tests for AppendLineParser."""
def setUp(self): BASE_DATA = ['old data 1', 'old data 2']
self._lineparser = TestableAppendLineParser('this really',
'does not matter')
self._lineparser.new_data = ['old data 1', 'old data 2']
self._expected_data = self._lineparser.new_data
self._lineparser.save()
def _get_expected(self): @pytest.fixture
def lineparser(self):
"""Fixture to get an AppendLineParser for tests."""
lp = TestableAppendLineParser('this really', 'does not matter')
lp.new_data = self.BASE_DATA
lp.save()
return lp
def _get_expected(self, new_data):
"""Get the expected data with newlines.""" """Get the expected data with newlines."""
return '\n'.join(self._expected_data) + '\n' return '\n'.join(self.BASE_DATA + new_data) + '\n'
def test_save(self): def test_save(self, lineparser):
"""Test save().""" """Test save()."""
self._lineparser.new_data = ['new data 1', 'new data 2'] new_data = ['new data 1', 'new data 2']
self._expected_data += self._lineparser.new_data lineparser.new_data = new_data
self._lineparser.save() lineparser.save()
self.assertEqual(self._lineparser._data, self._get_expected()) assert lineparser._data == self._get_expected(new_data)
def test_iter_without_open(self): def test_iter_without_open(self, lineparser):
"""Test __iter__ without having called open().""" """Test __iter__ without having called open()."""
with self.assertRaises(ValueError): with pytest.raises(ValueError):
iter(self._lineparser) iter(lineparser)
def test_iter(self): def test_iter(self, lineparser):
"""Test __iter__.""" """Test __iter__."""
self._lineparser.new_data = ['new data 1', 'new data 2'] new_data = ['new data 1', 'new data 2']
self._expected_data += self._lineparser.new_data lineparser.new_data = new_data
with self._lineparser.open(): with lineparser.open():
self.assertEqual(list(self._lineparser), self._expected_data) assert list(lineparser) == self.BASE_DATA + new_data
@mock.patch('qutebrowser.misc.lineparser.AppendLineParser._open') def test_iter_not_found(self, mocker):
def test_iter_not_found(self, open_mock):
"""Test __iter__ with no file.""" """Test __iter__ with no file."""
open_mock = mocker.patch(
'qutebrowser.misc.lineparser.AppendLineParser._open')
open_mock.side_effect = FileNotFoundError open_mock.side_effect = FileNotFoundError
linep = lineparser.AppendLineParser('foo', 'bar') new_data = ['new data 1', 'new data 2']
linep.new_data = ['new data 1', 'new data 2'] linep = lineparsermod.AppendLineParser('foo', 'bar')
expected_data = linep.new_data linep.new_data = new_data
with linep.open(): with linep.open():
self.assertEqual(list(linep), expected_data) assert list(linep) == new_data
def test_get_recent_none(self): def test_get_recent_none(self):
"""Test get_recent with no data.""" """Test get_recent with no data."""
linep = TestableAppendLineParser('this really', 'does not matter') linep = TestableAppendLineParser('this really', 'does not matter')
self.assertEqual(linep.get_recent(), []) assert linep.get_recent() == []
def test_get_recent_little(self): def test_get_recent_little(self, lineparser):
"""Test get_recent with little data.""" """Test get_recent with little data."""
data = [e + '\n' for e in self._expected_data] data = [e + '\n' for e in self.BASE_DATA]
self.assertEqual(self._lineparser.get_recent(), data) assert lineparser.get_recent() == data
def test_get_recent_much(self): def test_get_recent_much(self, lineparser):
"""Test get_recent with much data.""" """Test get_recent with much data."""
size = 64 size = 64
new_data = ['new data {}'.format(i) for i in range(size)] new_data = ['new data {}'.format(i) for i in range(size)]
self._lineparser.new_data = new_data lineparser.new_data = new_data
self._lineparser.save() lineparser.save()
data = '\n'.join(self._expected_data + new_data) data = '\n'.join(self.BASE_DATA + new_data)
data = [e + '\n' for e in data[-(size - 1):].splitlines()] data = [e + '\n' for e in data[-(size - 1):].splitlines()]
self.assertEqual(self._lineparser.get_recent(size), data) assert lineparser.get_recent(size) == data
if __name__ == '__main__':
unittest.main()

View File

@ -22,232 +22,241 @@
"""Tests for qutebrowser.utils.log.""" """Tests for qutebrowser.utils.log."""
import logging import logging
import unittest
import argparse import argparse
import itertools
import sys import sys
from unittest import mock
import pytest
from PyQt5.QtCore import qWarning
from qutebrowser.utils import log from qutebrowser.utils import log
from PyQt5.QtCore import qWarning
@pytest.yield_fixture(autouse=True)
class BaseTest(unittest.TestCase): def restore_loggers():
"""Fixture to save/restore the logging state.
"""Base class for logging tests.
Based on CPython's Lib/test/test_logging.py. Based on CPython's Lib/test/test_logging.py.
""" """
def setUp(self):
"""Save the old logging configuration."""
logger_dict = logging.getLogger().manager.loggerDict logger_dict = logging.getLogger().manager.loggerDict
logging._acquireLock() logging._acquireLock()
try: try:
self.saved_handlers = logging._handlers.copy() saved_handlers = logging._handlers.copy()
self.saved_handler_list = logging._handlerList[:] saved_handler_list = logging._handlerList[:]
self.saved_loggers = saved_loggers = logger_dict.copy() saved_loggers = saved_loggers = logger_dict.copy()
self.saved_name_to_level = logging._nameToLevel.copy() saved_name_to_level = logging._nameToLevel.copy()
self.saved_level_to_name = logging._levelToName.copy() saved_level_to_name = logging._levelToName.copy()
self.logger_states = {} logger_states = {}
for name in saved_loggers: for name in saved_loggers:
self.logger_states[name] = getattr(saved_loggers[name], logger_states[name] = getattr(saved_loggers[name], 'disabled',
'disabled', None) None)
finally: finally:
logging._releaseLock() logging._releaseLock()
self.root_logger = logging.getLogger("") root_logger = logging.getLogger("")
self.root_handlers = self.root_logger.handlers[:] root_handlers = root_logger.handlers[:]
self.original_logging_level = self.root_logger.getEffectiveLevel() original_logging_level = root_logger.getEffectiveLevel()
def tearDown(self): yield
"""Restore the original logging configuration."""
while self.root_logger.handlers: while root_logger.handlers:
h = self.root_logger.handlers[0] h = root_logger.handlers[0]
self.root_logger.removeHandler(h) root_logger.removeHandler(h)
h.close() h.close()
self.root_logger.setLevel(self.original_logging_level) root_logger.setLevel(original_logging_level)
for h in self.root_handlers: for h in root_handlers:
self.root_logger.addHandler(h) root_logger.addHandler(h)
logging._acquireLock() logging._acquireLock()
try: try:
logging._levelToName.clear() logging._levelToName.clear()
logging._levelToName.update(self.saved_level_to_name) logging._levelToName.update(saved_level_to_name)
logging._nameToLevel.clear() logging._nameToLevel.clear()
logging._nameToLevel.update(self.saved_name_to_level) logging._nameToLevel.update(saved_name_to_level)
logging._handlers.clear() logging._handlers.clear()
logging._handlers.update(self.saved_handlers) logging._handlers.update(saved_handlers)
logging._handlerList[:] = self.saved_handler_list logging._handlerList[:] = saved_handler_list
logger_dict = logging.getLogger().manager.loggerDict logger_dict = logging.getLogger().manager.loggerDict
logger_dict.clear() logger_dict.clear()
logger_dict.update(self.saved_loggers) logger_dict.update(saved_loggers)
logger_states = self.logger_states logger_states = logger_states
for name in self.logger_states: for name in logger_states:
if logger_states[name] is not None: if logger_states[name] is not None:
self.saved_loggers[name].disabled = logger_states[name] saved_loggers[name].disabled = logger_states[name]
finally: finally:
logging._releaseLock() logging._releaseLock()
class LogFilterTests(unittest.TestCase): @pytest.fixture(scope='session')
def log_counter():
"""Counter for logger fixture to get unique loggers."""
return itertools.count()
"""Tests for LogFilter.
Attributes: @pytest.fixture
logger: The logger we use to create records. def logger(log_counter):
"""Fixture which provides a logger for tests.
Unique throwaway loggers are used to make sure the tests don't influence
each other.
""" """
i = next(log_counter)
return logging.getLogger('qutebrowser-unittest-logger-{}'.format(i))
def setUp(self):
self.logger = logging.getLogger("foo")
def _make_record(self, name, level=logging.DEBUG): class TestLogFilter:
"""Tests for LogFilter."""
def _make_record(self, logger, name, level=logging.DEBUG):
"""Create a bogus logging record with the supplied logger name.""" """Create a bogus logging record with the supplied logger name."""
return self.logger.makeRecord(name, level=level, fn=None, lno=0, return logger.makeRecord(name, level=level, fn=None, lno=0, msg="",
msg="", args=None, exc_info=None) args=None, exc_info=None)
def test_empty(self): def test_empty(self, logger):
"""Test if an empty filter lets all messages through.""" """Test if an empty filter lets all messages through."""
logfilter = log.LogFilter(None) logfilter = log.LogFilter(None)
record = self._make_record("eggs.bacon.spam") record = self._make_record(logger, "eggs.bacon.spam")
self.assertTrue(logfilter.filter(record)) assert logfilter.filter(record)
record = self._make_record("eggs") record = self._make_record(logger, "eggs")
self.assertTrue(logfilter.filter(record)) assert logfilter.filter(record)
def test_matching(self): def test_matching(self, logger):
"""Test if a filter lets an exactly matching log record through.""" """Test if a filter lets an exactly matching log record through."""
logfilter = log.LogFilter(["eggs", "bacon"]) logfilter = log.LogFilter(["eggs", "bacon"])
record = self._make_record("eggs") record = self._make_record(logger, "eggs")
self.assertTrue(logfilter.filter(record)) assert logfilter.filter(record)
record = self._make_record("bacon") record = self._make_record(logger, "bacon")
self.assertTrue(logfilter.filter(record)) assert logfilter.filter(record)
record = self._make_record("spam") record = self._make_record(logger, "spam")
self.assertFalse(logfilter.filter(record)) assert not logfilter.filter(record)
logfilter = log.LogFilter(["eggs.bacon"]) logfilter = log.LogFilter(["eggs.bacon"])
record = self._make_record("eggs.bacon") record = self._make_record(logger, "eggs.bacon")
self.assertTrue(logfilter.filter(record)) assert logfilter.filter(record)
def test_equal_start(self): def test_equal_start(self, logger):
"""Test if a filter blocks a logger which looks equal but isn't.""" """Test if a filter blocks a logger which looks equal but isn't."""
logfilter = log.LogFilter(["eggs"]) logfilter = log.LogFilter(["eggs"])
record = self._make_record("eggsauce") record = self._make_record(logger, "eggsauce")
self.assertFalse(logfilter.filter(record)) assert not logfilter.filter(record)
logfilter = log.LogFilter("eggs.bacon") logfilter = log.LogFilter("eggs.bacon")
record = self._make_record("eggs.baconstrips") record = self._make_record(logger, "eggs.baconstrips")
self.assertFalse(logfilter.filter(record)) assert not logfilter.filter(record)
def test_child(self): def test_child(self, logger):
"""Test if a filter lets through a logger which is a child.""" """Test if a filter lets through a logger which is a child."""
logfilter = log.LogFilter(["eggs.bacon", "spam.ham"]) logfilter = log.LogFilter(["eggs.bacon", "spam.ham"])
record = self._make_record("eggs.bacon.spam") record = self._make_record(logger, "eggs.bacon.spam")
self.assertTrue(logfilter.filter(record)) assert logfilter.filter(record)
record = self._make_record("spam.ham.salami") record = self._make_record(logger, "spam.ham.salami")
self.assertTrue(logfilter.filter(record)) assert logfilter.filter(record)
def test_debug(self): def test_debug(self, logger):
"""Test if messages more important than debug are never filtered.""" """Test if messages more important than debug are never filtered."""
logfilter = log.LogFilter(["eggs"]) logfilter = log.LogFilter(["eggs"])
# First check if the filter works as intended with debug messages # First check if the filter works as intended with debug messages
record = self._make_record("eggs") record = self._make_record(logger, "eggs")
self.assertTrue(logfilter.filter(record)) assert logfilter.filter(record)
record = self._make_record("bacon") record = self._make_record(logger, "bacon")
self.assertFalse(logfilter.filter(record)) assert not logfilter.filter(record)
# Then check if info is not filtered # Then check if info is not filtered
record = self._make_record("eggs", level=logging.INFO) record = self._make_record(logger, "eggs", level=logging.INFO)
self.assertTrue(logfilter.filter(record)) assert logfilter.filter(record)
record = self._make_record("bacon", level=logging.INFO) record = self._make_record(logger, "bacon", level=logging.INFO)
self.assertTrue(logfilter.filter(record)) assert logfilter.filter(record)
class RAMHandlerTests(BaseTest): class TestRAMHandler:
"""Tests for RAMHandler. """Tests for RAMHandler."""
Attributes: @pytest.fixture
logger: The logger we use to log to the handler. def handler(self, logger):
handler: The RAMHandler we're testing. """Fixture providing a RAMHandler."""
old_level: The level the root logger had before executing the test. handler = log.RAMHandler(capacity=2)
old_handlers: The handlers the root logger had before executing the handler.setLevel(logging.NOTSET)
test. logger.addHandler(handler)
""" return handler
def setUp(self): def test_filled(self, handler, logger):
super().setUp()
self.logger = logging.getLogger()
self.logger.handlers = []
self.logger.setLevel(logging.NOTSET)
self.handler = log.RAMHandler(capacity=2)
self.handler.setLevel(logging.NOTSET)
self.logger.addHandler(self.handler)
def test_filled(self):
"""Test handler with exactly as much records as it can hold.""" """Test handler with exactly as much records as it can hold."""
self.logger.debug("One") logger.debug("One")
self.logger.debug("Two") logger.debug("Two")
self.assertEqual(len(self.handler._data), 2) assert len(handler._data) == 2
self.assertEqual(self.handler._data[0].msg, "One") assert handler._data[0].msg == "One"
self.assertEqual(self.handler._data[1].msg, "Two") assert handler._data[1].msg == "Two"
def test_overflow(self): def test_overflow(self, handler, logger):
"""Test handler with more records as it can hold.""" """Test handler with more records as it can hold."""
self.logger.debug("One") logger.debug("One")
self.logger.debug("Two") logger.debug("Two")
self.logger.debug("Three") logger.debug("Three")
self.assertEqual(len(self.handler._data), 2) assert len(handler._data) == 2
self.assertEqual(self.handler._data[0].msg, "Two") assert handler._data[0].msg == "Two"
self.assertEqual(self.handler._data[1].msg, "Three") assert handler._data[1].msg == "Three"
def test_dump_log(self): def test_dump_log(self, handler, logger):
"""Test dump_log().""" """Test dump_log()."""
self.logger.debug("One") logger.debug("One")
self.logger.debug("Two") logger.debug("Two")
self.logger.debug("Three") logger.debug("Three")
self.assertEqual(self.handler.dump_log(), "Two\nThree") assert handler.dump_log() == "Two\nThree"
@mock.patch('qutebrowser.utils.log.QtCore.qInstallMessageHandler', class TestInitLog:
autospec=True)
class InitLogTests(BaseTest):
"""Tests for init_log.""" """Tests for init_log."""
def setUp(self): @pytest.fixture(autouse=True)
super().setUp() def setup(self, mocker):
self.args = argparse.Namespace(debug=True, loglevel=logging.DEBUG, """Mock out qInstallMessageHandler."""
mocker.patch('qutebrowser.utils.log.QtCore.qInstallMessageHandler',
autospec=True)
@pytest.fixture
def args(self):
"""Fixture providing an argparse namespace."""
return argparse.Namespace(debug=True, loglevel=logging.DEBUG,
color=True, loglines=10, logfilter="") color=True, loglines=10, logfilter="")
def test_stderr_none(self, _mock): def test_stderr_none(self, args):
"""Test init_log with sys.stderr = None.""" """Test init_log with sys.stderr = None."""
old_stderr = sys.stderr old_stderr = sys.stderr
sys.stderr = None sys.stderr = None
log.init_log(self.args) log.init_log(args)
sys.stderr = old_stderr sys.stderr = old_stderr
class HideQtWarningTests(BaseTest): class TestHideQtWarning:
"""Tests for hide_qt_warning/QtWarningFilter.""" """Tests for hide_qt_warning/QtWarningFilter."""
def test_unfiltered(self): def test_unfiltered(self, caplog):
"""Test a message which is not filtered.""" """Test a message which is not filtered."""
with log.hide_qt_warning("World", logger='qt-tests'): with log.hide_qt_warning("World", logger='qt-tests'):
with self.assertLogs('qt-tests', logging.WARNING): with caplog.atLevel(logging.WARNING, logger='qt-tests'):
qWarning("Hello World") qWarning("Hello World")
assert len(caplog.records()) == 1
record = caplog.records()[0]
assert record.levelname == 'WARNING'
assert record.message == "Hello World"
def test_filtered_exact(self): def test_filtered_exact(self, caplog):
"""Test a message which is filtered (exact match).""" """Test a message which is filtered (exact match)."""
with log.hide_qt_warning("Hello", logger='qt-tests'): with log.hide_qt_warning("Hello", logger='qt-tests'):
with caplog.atLevel(logging.WARNING, logger='qt-tests'):
qWarning("Hello") qWarning("Hello")
assert not caplog.records()
def test_filtered_start(self): def test_filtered_start(self, caplog):
"""Test a message which is filtered (match at line start).""" """Test a message which is filtered (match at line start)."""
with log.hide_qt_warning("Hello", logger='qt-tests'): with log.hide_qt_warning("Hello", logger='qt-tests'):
with caplog.atLevel(logging.WARNING, logger='qt-tests'):
qWarning("Hello World") qWarning("Hello World")
assert not caplog.records()
def test_filtered_whitespace(self): def test_filtered_whitespace(self, caplog):
"""Test a message which is filtered (match with whitespace).""" """Test a message which is filtered (match with whitespace)."""
with log.hide_qt_warning("Hello", logger='qt-tests'): with log.hide_qt_warning("Hello", logger='qt-tests'):
with caplog.atLevel(logging.WARNING, logger='qt-tests'):
qWarning(" Hello World ") qWarning(" Hello World ")
assert not caplog.records()
if __name__ == '__main__':
unittest.main()

View File

@ -19,45 +19,38 @@
"""Tests for the Enum class.""" """Tests for the Enum class."""
import unittest
from qutebrowser.utils import usertypes from qutebrowser.utils import usertypes
import pytest
# FIXME: Add some more tests, e.g. for is_int # FIXME: Add some more tests, e.g. for is_int
class EnumTests(unittest.TestCase): @pytest.fixture
def enum():
return usertypes.enum('Enum', ['one', 'two'])
"""Test simple enums.
Attributes: def test_values(enum):
enum: The enum we're testing.
"""
def setUp(self):
self.enum = usertypes.enum('Enum', ['one', 'two'])
def test_values(self):
"""Test if enum members resolve to the right values.""" """Test if enum members resolve to the right values."""
self.assertEqual(self.enum.one.value, 1) assert enum.one.value == 1
self.assertEqual(self.enum.two.value, 2) assert enum.two.value == 2
def test_name(self):
def test_name(enum):
"""Test .name mapping.""" """Test .name mapping."""
self.assertEqual(self.enum.one.name, 'one') assert enum.one.name == 'one'
self.assertEqual(self.enum.two.name, 'two') assert enum.two.name == 'two'
def test_unknown(self):
def test_unknown(enum):
"""Test invalid values which should raise an AttributeError.""" """Test invalid values which should raise an AttributeError."""
with self.assertRaises(AttributeError): with pytest.raises(AttributeError):
_ = self.enum.three _ = enum.three
def test_start(self):
def test_start():
"""Test the start= argument.""" """Test the start= argument."""
e = usertypes.enum('Enum', ['three', 'four'], start=3) e = usertypes.enum('Enum', ['three', 'four'], start=3)
self.assertEqual(e.three.value, 3) assert e.three.value == 3
self.assertEqual(e.four.value, 4) assert e.four.value == 4
if __name__ == '__main__':
unittest.main()

View File

@ -21,364 +21,331 @@
"""Tests for the NeighborList class.""" """Tests for the NeighborList class."""
import unittest
from qutebrowser.utils import usertypes from qutebrowser.utils import usertypes
import pytest
class InitTests(unittest.TestCase):
"""Just try to init some neighborlists. class TestInit:
Attributes: """Just try to init some neighborlists."""
nl: The NeighborList we're testing.
"""
def test_empty(self): def test_empty(self):
"""Test constructing an empty NeighborList.""" """Test constructing an empty NeighborList."""
nl = usertypes.NeighborList() nl = usertypes.NeighborList()
self.assertEqual(nl.items, []) assert nl.items == []
def test_items(self): def test_items(self):
"""Test constructing an NeighborList with items.""" """Test constructing an NeighborList with items."""
nl = usertypes.NeighborList([1, 2, 3]) nl = usertypes.NeighborList([1, 2, 3])
self.assertEqual(nl.items, [1, 2, 3]) assert nl.items == [1, 2, 3]
def test_len(self): def test_len(self):
"""Test len() on NeighborList.""" """Test len() on NeighborList."""
nl = usertypes.NeighborList([1, 2, 3]) nl = usertypes.NeighborList([1, 2, 3])
self.assertEqual(len(nl), 3) assert len(nl) == 3
def test_contains(self): def test_contains(self):
"""Test 'in' on NeighborList.""" """Test 'in' on NeighborList."""
nl = usertypes.NeighborList([1, 2, 3]) nl = usertypes.NeighborList([1, 2, 3])
self.assertIn(2, nl) assert 2 in nl
self.assertNotIn(4, nl) assert 4 not in nl
class DefaultTests(unittest.TestCase): class TestDefaultArg:
"""Test the default argument. """Test the default argument."""
Attributes:
nl: The NeighborList we're testing.
"""
def test_simple(self): def test_simple(self):
"""Test default with a numeric argument.""" """Test default with a numeric argument."""
nl = usertypes.NeighborList([1, 2, 3], default=2) nl = usertypes.NeighborList([1, 2, 3], default=2)
self.assertEqual(nl._idx, 1) assert nl._idx == 1
def test_none(self): def test_none(self):
"""Test default 'None'.""" """Test default 'None'."""
nl = usertypes.NeighborList([1, 2, None], default=None) nl = usertypes.NeighborList([1, 2, None], default=None)
self.assertEqual(nl._idx, 2) assert nl._idx == 2
def test_unset(self): def test_unset(self):
"""Test unset default value.""" """Test unset default value."""
nl = usertypes.NeighborList([1, 2, 3]) nl = usertypes.NeighborList([1, 2, 3])
self.assertIsNone(nl._idx) assert nl._idx is None
class EmptyTests(unittest.TestCase): class TestEmpty:
"""Tests with no items. """Tests with no items."""
Attributes: @pytest.fixture
nl: The NeighborList we're testing. def neighborlist(self):
""" return usertypes.NeighborList()
def setUp(self): def test_curitem(self, neighborlist):
self.nl = usertypes.NeighborList()
def test_curitem(self):
"""Test curitem with no item.""" """Test curitem with no item."""
with self.assertRaises(IndexError): with pytest.raises(IndexError):
self.nl.curitem() neighborlist.curitem()
def test_firstitem(self): def test_firstitem(self, neighborlist):
"""Test firstitem with no item.""" """Test firstitem with no item."""
with self.assertRaises(IndexError): with pytest.raises(IndexError):
self.nl.firstitem() neighborlist.firstitem()
def test_lastitem(self): def test_lastitem(self, neighborlist):
"""Test lastitem with no item.""" """Test lastitem with no item."""
with self.assertRaises(IndexError): with pytest.raises(IndexError):
self.nl.lastitem() neighborlist.lastitem()
def test_getitem(self): def test_getitem(self, neighborlist):
"""Test getitem with no item.""" """Test getitem with no item."""
with self.assertRaises(IndexError): with pytest.raises(IndexError):
self.nl.getitem(1) neighborlist.getitem(1)
class ItemTests(unittest.TestCase): class TestItems:
"""Tests with items. """Tests with items."""
Attributes: @pytest.fixture
nl: The NeighborList we're testing. def neighborlist(self):
""" return usertypes.NeighborList([1, 2, 3, 4, 5], default=3)
def setUp(self): def test_curitem(self, neighborlist):
self.nl = usertypes.NeighborList([1, 2, 3, 4, 5], default=3)
def test_curitem(self):
"""Test curitem().""" """Test curitem()."""
self.assertEqual(self.nl._idx, 2) assert neighborlist._idx == 2
self.assertEqual(self.nl.curitem(), 3) assert neighborlist.curitem() == 3
self.assertEqual(self.nl._idx, 2) assert neighborlist._idx == 2
def test_nextitem(self): def test_nextitem(self, neighborlist):
"""Test nextitem().""" """Test nextitem()."""
self.assertEqual(self.nl.nextitem(), 4) assert neighborlist.nextitem() == 4
self.assertEqual(self.nl._idx, 3) assert neighborlist._idx == 3
self.assertEqual(self.nl.nextitem(), 5) assert neighborlist.nextitem() == 5
self.assertEqual(self.nl._idx, 4) assert neighborlist._idx == 4
def test_previtem(self): def test_previtem(self, neighborlist):
"""Test previtem().""" """Test previtem()."""
self.assertEqual(self.nl.previtem(), 2) assert neighborlist.previtem() == 2
self.assertEqual(self.nl._idx, 1) assert neighborlist._idx == 1
self.assertEqual(self.nl.previtem(), 1) assert neighborlist.previtem() == 1
self.assertEqual(self.nl._idx, 0) assert neighborlist._idx == 0
def test_firstitem(self): def test_firstitem(self, neighborlist):
"""Test firstitem().""" """Test firstitem()."""
self.assertEqual(self.nl.firstitem(), 1) assert neighborlist.firstitem() == 1
self.assertEqual(self.nl._idx, 0) assert neighborlist._idx == 0
def test_lastitem(self): def test_lastitem(self, neighborlist):
"""Test lastitem().""" """Test lastitem()."""
self.assertEqual(self.nl.lastitem(), 5) assert neighborlist.lastitem() == 5
self.assertEqual(self.nl._idx, 4) assert neighborlist._idx == 4
def test_reset(self): def test_reset(self, neighborlist):
"""Test reset().""" """Test reset()."""
self.nl.nextitem() neighborlist.nextitem()
self.assertEqual(self.nl._idx, 3) assert neighborlist._idx == 3
self.nl.reset() neighborlist.reset()
self.assertEqual(self.nl._idx, 2) assert neighborlist._idx == 2
def test_getitem(self): def test_getitem(self, neighborlist):
"""Test getitem().""" """Test getitem()."""
self.assertEqual(self.nl.getitem(2), 5) assert neighborlist.getitem(2) == 5
self.assertEqual(self.nl._idx, 4) assert neighborlist._idx == 4
self.nl.reset() neighborlist.reset()
self.assertEqual(self.nl.getitem(-2), 1) assert neighborlist.getitem(-2) == 1
self.assertEqual(self.nl._idx, 0) assert neighborlist._idx == 0
class OneTests(unittest.TestCase): class TestSingleItem:
"""Tests with a list with only one item. """Tests with a list with only one item."""
Attributes: @pytest.fixture
nl: The NeighborList we're testing. def neighborlist(self):
""" return usertypes.NeighborList([1], default=1)
def setUp(self): def test_first_wrap(self, neighborlist):
self.nl = usertypes.NeighborList([1], default=1)
def test_first_wrap(self):
"""Test out of bounds previtem() with mode=wrap.""" """Test out of bounds previtem() with mode=wrap."""
self.nl._mode = usertypes.NeighborList.Modes.wrap neighborlist._mode = usertypes.NeighborList.Modes.wrap
self.nl.firstitem() neighborlist.firstitem()
self.assertEqual(self.nl._idx, 0) assert neighborlist._idx == 0
self.assertEqual(self.nl.previtem(), 1) assert neighborlist.previtem() == 1
self.assertEqual(self.nl._idx, 0) assert neighborlist._idx == 0
def test_first_block(self): def test_first_block(self, neighborlist):
"""Test out of bounds previtem() with mode=block.""" """Test out of bounds previtem() with mode=block."""
self.nl._mode = usertypes.NeighborList.Modes.block neighborlist._mode = usertypes.NeighborList.Modes.block
self.nl.firstitem() neighborlist.firstitem()
self.assertEqual(self.nl._idx, 0) assert neighborlist._idx == 0
self.assertEqual(self.nl.previtem(), 1) assert neighborlist.previtem() == 1
self.assertEqual(self.nl._idx, 0) assert neighborlist._idx == 0
def test_first_raise(self): def test_first_raise(self, neighborlist):
"""Test out of bounds previtem() with mode=raise.""" """Test out of bounds previtem() with mode=raise."""
self.nl._mode = usertypes.NeighborList.Modes.exception neighborlist._mode = usertypes.NeighborList.Modes.exception
self.nl.firstitem() neighborlist.firstitem()
self.assertEqual(self.nl._idx, 0) assert neighborlist._idx == 0
with self.assertRaises(IndexError): with pytest.raises(IndexError):
self.nl.previtem() neighborlist.previtem()
self.assertEqual(self.nl._idx, 0) assert neighborlist._idx == 0
def test_last_wrap(self): def test_last_wrap(self, neighborlist):
"""Test out of bounds nextitem() with mode=wrap.""" """Test out of bounds nextitem() with mode=wrap."""
self.nl._mode = usertypes.NeighborList.Modes.wrap neighborlist._mode = usertypes.NeighborList.Modes.wrap
self.nl.lastitem() neighborlist.lastitem()
self.assertEqual(self.nl._idx, 0) assert neighborlist._idx == 0
self.assertEqual(self.nl.nextitem(), 1) assert neighborlist.nextitem() == 1
self.assertEqual(self.nl._idx, 0) assert neighborlist._idx == 0
def test_last_block(self): def test_last_block(self, neighborlist):
"""Test out of bounds nextitem() with mode=block.""" """Test out of bounds nextitem() with mode=block."""
self.nl._mode = usertypes.NeighborList.Modes.block neighborlist._mode = usertypes.NeighborList.Modes.block
self.nl.lastitem() neighborlist.lastitem()
self.assertEqual(self.nl._idx, 0) assert neighborlist._idx == 0
self.assertEqual(self.nl.nextitem(), 1) assert neighborlist.nextitem() == 1
self.assertEqual(self.nl._idx, 0) assert neighborlist._idx == 0
def test_last_raise(self): def test_last_raise(self, neighborlist):
"""Test out of bounds nextitem() with mode=raise.""" """Test out of bounds nextitem() with mode=raise."""
self.nl._mode = usertypes.NeighborList.Modes.exception neighborlist._mode = usertypes.NeighborList.Modes.exception
self.nl.lastitem() neighborlist.lastitem()
self.assertEqual(self.nl._idx, 0) assert neighborlist._idx == 0
with self.assertRaises(IndexError): with pytest.raises(IndexError):
self.nl.nextitem() neighborlist.nextitem()
self.assertEqual(self.nl._idx, 0) assert neighborlist._idx == 0
class BlockTests(unittest.TestCase): class TestBlockMode:
"""Tests with mode=block. """Tests with mode=block."""
Attributes: @pytest.fixture
nl: The NeighborList we're testing. def neighborlist(self):
""" return usertypes.NeighborList(
def setUp(self):
self.nl = usertypes.NeighborList(
[1, 2, 3, 4, 5], default=3, [1, 2, 3, 4, 5], default=3,
mode=usertypes.NeighborList.Modes.block) mode=usertypes.NeighborList.Modes.block)
def test_first(self): def test_first(self, neighborlist):
"""Test out of bounds previtem().""" """Test out of bounds previtem()."""
self.nl.firstitem() neighborlist.firstitem()
self.assertEqual(self.nl._idx, 0) assert neighborlist._idx == 0
self.assertEqual(self.nl.previtem(), 1) assert neighborlist.previtem() == 1
self.assertEqual(self.nl._idx, 0) assert neighborlist._idx == 0
def test_last(self): def test_last(self, neighborlist):
"""Test out of bounds nextitem().""" """Test out of bounds nextitem()."""
self.nl.lastitem() neighborlist.lastitem()
self.assertEqual(self.nl._idx, 4) assert neighborlist._idx == 4
self.assertEqual(self.nl.nextitem(), 5) assert neighborlist.nextitem() == 5
self.assertEqual(self.nl._idx, 4) assert neighborlist._idx == 4
class WrapTests(unittest.TestCase): class TestWrapMode:
"""Tests with mode=wrap. """Tests with mode=wrap."""
Attributes: @pytest.fixture
nl: The NeighborList we're testing. def neighborlist(self):
""" return usertypes.NeighborList(
def setUp(self):
self.nl = usertypes.NeighborList(
[1, 2, 3, 4, 5], default=3, mode=usertypes.NeighborList.Modes.wrap) [1, 2, 3, 4, 5], default=3, mode=usertypes.NeighborList.Modes.wrap)
def test_first(self): def test_first(self, neighborlist):
"""Test out of bounds previtem().""" """Test out of bounds previtem()."""
self.nl.firstitem() neighborlist.firstitem()
self.assertEqual(self.nl._idx, 0) assert neighborlist._idx == 0
self.assertEqual(self.nl.previtem(), 5) assert neighborlist.previtem() == 5
self.assertEqual(self.nl._idx, 4) assert neighborlist._idx == 4
def test_last(self): def test_last(self, neighborlist):
"""Test out of bounds nextitem().""" """Test out of bounds nextitem()."""
self.nl.lastitem() neighborlist.lastitem()
self.assertEqual(self.nl._idx, 4) assert neighborlist._idx == 4
self.assertEqual(self.nl.nextitem(), 1) assert neighborlist.nextitem() == 1
self.assertEqual(self.nl._idx, 0) assert neighborlist._idx == 0
class RaiseTests(unittest.TestCase): class TestExceptionMode:
"""Tests with mode=exception. """Tests with mode=exception."""
Attributes: @pytest.fixture
nl: The NeighborList we're testing. def neighborlist(self):
""" return usertypes.NeighborList(
def setUp(self):
self.nl = usertypes.NeighborList(
[1, 2, 3, 4, 5], default=3, [1, 2, 3, 4, 5], default=3,
mode=usertypes.NeighborList.Modes.exception) mode=usertypes.NeighborList.Modes.exception)
def test_first(self): def test_first(self, neighborlist):
"""Test out of bounds previtem().""" """Test out of bounds previtem()."""
self.nl.firstitem() neighborlist.firstitem()
self.assertEqual(self.nl._idx, 0) assert neighborlist._idx == 0
with self.assertRaises(IndexError): with pytest.raises(IndexError):
self.nl.previtem() neighborlist.previtem()
self.assertEqual(self.nl._idx, 0) assert neighborlist._idx == 0
def test_last(self): def test_last(self, neighborlist):
"""Test out of bounds nextitem().""" """Test out of bounds nextitem()."""
self.nl.lastitem() neighborlist.lastitem()
self.assertEqual(self.nl._idx, 4) assert neighborlist._idx == 4
with self.assertRaises(IndexError): with pytest.raises(IndexError):
self.nl.nextitem() neighborlist.nextitem()
self.assertEqual(self.nl._idx, 4) assert neighborlist._idx == 4
class SnapInTests(unittest.TestCase): class TestSnapIn:
"""Tests for the fuzzyval/_snap_in features. """Tests for the fuzzyval/_snap_in features."""
Attributes: @pytest.fixture
nl: The NeighborList we're testing. def neighborlist(self):
""" return usertypes.NeighborList([20, 9, 1, 5])
def setUp(self): def test_bigger(self, neighborlist):
self.nl = usertypes.NeighborList([20, 9, 1, 5])
def test_bigger(self):
"""Test fuzzyval with snapping to a bigger value.""" """Test fuzzyval with snapping to a bigger value."""
self.nl.fuzzyval = 7 neighborlist.fuzzyval = 7
self.assertEqual(self.nl.nextitem(), 9) assert neighborlist.nextitem() == 9
self.assertEqual(self.nl._idx, 1) assert neighborlist._idx == 1
self.assertEqual(self.nl.nextitem(), 1) assert neighborlist.nextitem() == 1
self.assertEqual(self.nl._idx, 2) assert neighborlist._idx == 2
def test_smaller(self): def test_smaller(self, neighborlist):
"""Test fuzzyval with snapping to a smaller value.""" """Test fuzzyval with snapping to a smaller value."""
self.nl.fuzzyval = 7 neighborlist.fuzzyval = 7
self.assertEqual(self.nl.previtem(), 5) assert neighborlist.previtem() == 5
self.assertEqual(self.nl._idx, 3) assert neighborlist._idx == 3
self.assertEqual(self.nl.previtem(), 1) assert neighborlist.previtem() == 1
self.assertEqual(self.nl._idx, 2) assert neighborlist._idx == 2
def test_equal_bigger(self): def test_equal_bigger(self, neighborlist):
"""Test fuzzyval with matching value, snapping to a bigger value.""" """Test fuzzyval with matching value, snapping to a bigger value."""
self.nl.fuzzyval = 20 neighborlist.fuzzyval = 20
self.assertEqual(self.nl.nextitem(), 9) assert neighborlist.nextitem() == 9
self.assertEqual(self.nl._idx, 1) assert neighborlist._idx == 1
def test_equal_smaller(self): def test_equal_smaller(self, neighborlist):
"""Test fuzzyval with matching value, snapping to a smaller value.""" """Test fuzzyval with matching value, snapping to a smaller value."""
self.nl.fuzzyval = 5 neighborlist.fuzzyval = 5
self.assertEqual(self.nl.previtem(), 1) assert neighborlist.previtem() == 1
self.assertEqual(self.nl._idx, 2) assert neighborlist._idx == 2
def test_too_big_next(self): def test_too_big_next(self, neighborlist):
"""Test fuzzyval/next with a value bigger than any in the list.""" """Test fuzzyval/next with a value bigger than any in the list."""
self.nl.fuzzyval = 30 neighborlist.fuzzyval = 30
self.assertEqual(self.nl.nextitem(), 20) assert neighborlist.nextitem() == 20
self.assertEqual(self.nl._idx, 0) assert neighborlist._idx == 0
def test_too_big_prev(self): def test_too_big_prev(self, neighborlist):
"""Test fuzzyval/prev with a value bigger than any in the list.""" """Test fuzzyval/prev with a value bigger than any in the list."""
self.nl.fuzzyval = 30 neighborlist.fuzzyval = 30
self.assertEqual(self.nl.previtem(), 20) assert neighborlist.previtem() == 20
self.assertEqual(self.nl._idx, 0) assert neighborlist._idx == 0
def test_too_small_next(self): def test_too_small_next(self, neighborlist):
"""Test fuzzyval/next with a value smaller than any in the list.""" """Test fuzzyval/next with a value smaller than any in the list."""
self.nl.fuzzyval = 0 neighborlist.fuzzyval = 0
self.assertEqual(self.nl.nextitem(), 1) assert neighborlist.nextitem() == 1
self.assertEqual(self.nl._idx, 2) assert neighborlist._idx == 2
def test_too_small_prev(self): def test_too_small_prev(self, neighborlist):
"""Test fuzzyval/prev with a value smaller than any in the list.""" """Test fuzzyval/prev with a value smaller than any in the list."""
self.nl.fuzzyval = 0 neighborlist.fuzzyval = 0
self.assertEqual(self.nl.previtem(), 1) assert neighborlist.previtem() == 1
self.assertEqual(self.nl._idx, 2) assert neighborlist._idx == 2
if __name__ == '__main__':
unittest.main()