2014-06-19 09:04:37 +02:00
|
|
|
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
|
|
|
|
|
2015-01-03 15:51:31 +01:00
|
|
|
# Copyright 2014-2015 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
|
2014-05-05 12:16:12 +02:00
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
2014-09-02 08:27:53 +02:00
|
|
|
"""Tests for qutebrowser.utils.utils."""
|
2014-05-05 12:16:12 +02:00
|
|
|
|
2015-05-17 18:59:40 +02:00
|
|
|
import sys
|
2014-09-03 10:47:27 +02:00
|
|
|
import enum
|
2015-03-22 22:39:56 +01:00
|
|
|
import datetime
|
2014-05-05 12:16:12 +02:00
|
|
|
import os.path
|
2015-05-19 16:24:43 +02:00
|
|
|
import io
|
|
|
|
import logging
|
|
|
|
import functools
|
2015-12-01 22:40:58 +01:00
|
|
|
import collections
|
2014-05-05 12:16:12 +02:00
|
|
|
|
2014-10-08 06:19:45 +02:00
|
|
|
from PyQt5.QtCore import Qt
|
2014-06-12 21:43:30 +02:00
|
|
|
from PyQt5.QtGui import QColor
|
2015-04-04 19:01:58 +02:00
|
|
|
import pytest
|
2014-05-09 08:43:31 +02:00
|
|
|
|
2015-05-19 16:24:43 +02:00
|
|
|
import qutebrowser
|
|
|
|
import qutebrowser.utils # for test_qualname
|
2014-08-26 20:25:11 +02:00
|
|
|
from qutebrowser.utils import utils, qtutils
|
2014-05-05 12:16:12 +02:00
|
|
|
|
|
|
|
|
2015-05-19 16:24:43 +02:00
|
|
|
ELLIPSIS = '\u2026'
|
|
|
|
|
|
|
|
|
2014-07-15 11:06:56 +02:00
|
|
|
class Color(QColor):
|
|
|
|
|
|
|
|
"""A QColor with a nicer repr()."""
|
|
|
|
|
|
|
|
def __repr__(self):
|
2014-09-26 15:48:24 +02:00
|
|
|
return utils.get_repr(self, constructor=True, red=self.red(),
|
|
|
|
green=self.green(), blue=self.blue(),
|
|
|
|
alpha=self.alpha())
|
2014-07-15 11:06:56 +02:00
|
|
|
|
|
|
|
|
2015-05-19 16:24:43 +02:00
|
|
|
class TestCompactText:
|
|
|
|
|
|
|
|
"""Test compact_text."""
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('text, expected', [
|
|
|
|
('foo\nbar', 'foobar'),
|
|
|
|
(' foo \n bar ', 'foobar'),
|
|
|
|
('\nfoo\n', 'foo'),
|
2015-09-16 20:25:02 +02:00
|
|
|
], ids=repr)
|
2015-05-19 16:24:43 +02:00
|
|
|
def test_compact_text(self, text, expected):
|
|
|
|
"""Test folding of newlines."""
|
|
|
|
assert utils.compact_text(text) == expected
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('elidelength, text, expected', [
|
|
|
|
(None, 'x' * 100, 'x' * 100),
|
|
|
|
(6, 'foobar', 'foobar'),
|
|
|
|
(5, 'foobar', 'foob' + ELLIPSIS),
|
|
|
|
(5, 'foo\nbar', 'foob' + ELLIPSIS),
|
|
|
|
(7, 'foo\nbar', 'foobar'),
|
2015-09-16 20:25:02 +02:00
|
|
|
], ids=lambda val: str(val)[:20])
|
2015-05-19 16:24:43 +02:00
|
|
|
def test_eliding(self, elidelength, text, expected):
|
|
|
|
"""Test eliding."""
|
|
|
|
assert utils.compact_text(text, elidelength) == expected
|
|
|
|
|
|
|
|
|
2015-04-04 19:01:58 +02:00
|
|
|
class TestEliding:
|
2014-05-16 07:46:56 +02:00
|
|
|
|
|
|
|
"""Test elide."""
|
|
|
|
|
|
|
|
def test_too_small(self):
|
2014-05-27 13:06:13 +02:00
|
|
|
"""Test eliding to 0 chars which should fail."""
|
2015-04-04 19:01:58 +02:00
|
|
|
with pytest.raises(ValueError):
|
2014-05-16 07:46:56 +02:00
|
|
|
utils.elide('foo', 0)
|
|
|
|
|
2015-05-19 16:24:43 +02:00
|
|
|
@pytest.mark.parametrize('text, length, expected', [
|
|
|
|
('foo', 1, ELLIPSIS),
|
|
|
|
('foo', 3, 'foo'),
|
|
|
|
('foobar', 3, 'fo' + ELLIPSIS),
|
|
|
|
])
|
|
|
|
def test_elided(self, text, length, expected):
|
|
|
|
assert utils.elide(text, length) == expected
|
2014-05-16 07:46:56 +02:00
|
|
|
|
|
|
|
|
2015-08-09 01:32:47 +02:00
|
|
|
@pytest.fixture(params=[True, False])
|
|
|
|
def freezer(request, monkeypatch):
|
|
|
|
if request.param and not getattr(sys, 'frozen', False):
|
|
|
|
monkeypatch.setattr(sys, 'frozen', True, raising=False)
|
|
|
|
monkeypatch.setattr('sys.executable', qutebrowser.__file__)
|
|
|
|
elif not request.param and getattr(sys, 'frozen', False):
|
|
|
|
# Want to test unfrozen tests, but we are frozen
|
|
|
|
pytest.skip("Can't run with sys.frozen = True!")
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.usefixtures('freezer')
|
2015-04-04 19:01:58 +02:00
|
|
|
class TestReadFile:
|
2014-05-05 12:16:12 +02:00
|
|
|
|
|
|
|
"""Test read_file."""
|
|
|
|
|
|
|
|
def test_readfile(self):
|
2015-03-31 20:49:29 +02:00
|
|
|
"""Read a test file."""
|
2015-04-05 00:14:34 +02:00
|
|
|
content = utils.read_file(os.path.join('utils', 'testfile'))
|
2015-04-04 19:01:58 +02:00
|
|
|
assert content.splitlines()[0] == "Hello World!"
|
2014-05-05 12:16:12 +02:00
|
|
|
|
2015-05-19 16:24:43 +02:00
|
|
|
def test_readfile_binary(self):
|
|
|
|
"""Read a test file in binary mode."""
|
|
|
|
content = utils.read_file(os.path.join('utils', 'testfile'),
|
|
|
|
binary=True)
|
|
|
|
assert content.splitlines()[0] == b"Hello World!"
|
|
|
|
|
|
|
|
|
2015-08-09 01:32:47 +02:00
|
|
|
@pytest.mark.usefixtures('freezer')
|
2015-08-14 01:54:23 +02:00
|
|
|
def test_resource_filename():
|
|
|
|
"""Read a test file."""
|
|
|
|
filename = utils.resource_filename(os.path.join('utils', 'testfile'))
|
|
|
|
with open(filename, 'r', encoding='utf-8') as f:
|
|
|
|
assert f.read().splitlines()[0] == "Hello World!"
|
2015-08-08 19:47:47 +02:00
|
|
|
|
|
|
|
|
2015-05-19 16:24:43 +02:00
|
|
|
class Patcher:
|
|
|
|
|
|
|
|
"""Helper for TestActuteWarning.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
monkeypatch: The pytest monkeypatch fixture.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, monkeypatch):
|
|
|
|
self.monkeypatch = monkeypatch
|
|
|
|
|
|
|
|
def patch_platform(self, platform='linux'):
|
|
|
|
"""Patch sys.platform."""
|
|
|
|
self.monkeypatch.setattr('sys.platform', platform)
|
|
|
|
|
|
|
|
def patch_exists(self, exists=True):
|
|
|
|
"""Patch os.path.exists."""
|
|
|
|
self.monkeypatch.setattr('qutebrowser.utils.utils.os.path.exists',
|
|
|
|
lambda path: exists)
|
|
|
|
|
|
|
|
def patch_version(self, version='5.2.0'):
|
|
|
|
"""Patch Qt version."""
|
|
|
|
self.monkeypatch.setattr(
|
|
|
|
'qutebrowser.utils.utils.qtutils.qVersion', lambda: version)
|
|
|
|
|
|
|
|
def patch_file(self, data):
|
|
|
|
"""Patch open() to return the given data."""
|
|
|
|
fake_file = io.StringIO(data)
|
|
|
|
self.monkeypatch.setattr(utils, 'open',
|
|
|
|
lambda filename, mode, encoding: fake_file,
|
|
|
|
raising=False)
|
|
|
|
|
|
|
|
def patch_all(self, data):
|
|
|
|
"""Patch everything so the issue would exist."""
|
|
|
|
self.patch_platform()
|
|
|
|
self.patch_exists()
|
|
|
|
self.patch_version()
|
|
|
|
self.patch_file(data)
|
|
|
|
|
|
|
|
|
|
|
|
class TestActuteWarning:
|
|
|
|
|
|
|
|
"""Test actute_warning."""
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def patcher(self, monkeypatch):
|
|
|
|
"""Fixture providing a Patcher helper."""
|
|
|
|
return Patcher(monkeypatch)
|
|
|
|
|
|
|
|
def test_non_linux(self, patcher, capsys):
|
|
|
|
"""Test with a non-Linux OS."""
|
|
|
|
patcher.patch_platform('toaster')
|
|
|
|
utils.actute_warning()
|
|
|
|
out, err = capsys.readouterr()
|
|
|
|
assert not out
|
|
|
|
assert not err
|
|
|
|
|
|
|
|
def test_no_compose(self, patcher, capsys):
|
|
|
|
"""Test with no compose file."""
|
|
|
|
patcher.patch_platform()
|
|
|
|
patcher.patch_exists(False)
|
|
|
|
utils.actute_warning()
|
|
|
|
out, err = capsys.readouterr()
|
|
|
|
assert not out
|
|
|
|
assert not err
|
|
|
|
|
|
|
|
def test_newer_qt(self, patcher, capsys):
|
|
|
|
"""Test with compose file but newer Qt version."""
|
|
|
|
patcher.patch_platform()
|
|
|
|
patcher.patch_exists()
|
|
|
|
patcher.patch_version('5.4')
|
|
|
|
utils.actute_warning()
|
|
|
|
out, err = capsys.readouterr()
|
|
|
|
assert not out
|
|
|
|
assert not err
|
|
|
|
|
|
|
|
def test_no_match(self, patcher, capsys):
|
|
|
|
"""Test with compose file and affected Qt but no match."""
|
|
|
|
patcher.patch_all('foobar')
|
|
|
|
utils.actute_warning()
|
|
|
|
out, err = capsys.readouterr()
|
|
|
|
assert not out
|
|
|
|
assert not err
|
|
|
|
|
|
|
|
def test_empty(self, patcher, capsys):
|
|
|
|
"""Test with empty compose file."""
|
|
|
|
patcher.patch_all(None)
|
|
|
|
utils.actute_warning()
|
|
|
|
out, err = capsys.readouterr()
|
|
|
|
assert not out
|
|
|
|
assert not err
|
|
|
|
|
|
|
|
def test_match(self, patcher, capsys):
|
|
|
|
"""Test with compose file and affected Qt and a match."""
|
|
|
|
patcher.patch_all('foobar\n<dead_actute>\nbaz')
|
|
|
|
utils.actute_warning()
|
|
|
|
out, err = capsys.readouterr()
|
|
|
|
assert out.startswith('Note: If you got a')
|
|
|
|
assert not err
|
|
|
|
|
|
|
|
def test_match_stdout_none(self, monkeypatch, patcher, capsys):
|
|
|
|
"""Test with a match and stdout being None."""
|
|
|
|
patcher.patch_all('foobar\n<dead_actute>\nbaz')
|
|
|
|
monkeypatch.setattr('sys.stdout', None)
|
|
|
|
utils.actute_warning()
|
|
|
|
|
|
|
|
def test_unreadable(self, mocker, patcher, capsys, caplog):
|
|
|
|
"""Test with an unreadable compose file."""
|
|
|
|
patcher.patch_platform()
|
|
|
|
patcher.patch_exists()
|
|
|
|
patcher.patch_version()
|
|
|
|
mocker.patch('qutebrowser.utils.utils.open', side_effect=OSError,
|
|
|
|
create=True)
|
|
|
|
|
2015-11-11 19:57:03 +01:00
|
|
|
with caplog.at_level(logging.ERROR, 'init'):
|
2015-05-19 16:24:43 +02:00
|
|
|
utils.actute_warning()
|
|
|
|
|
2015-11-11 19:57:03 +01:00
|
|
|
assert len(caplog.records) == 1
|
|
|
|
assert caplog.records[0].message == 'Failed to read Compose file'
|
2015-05-19 16:24:43 +02:00
|
|
|
out, _err = capsys.readouterr()
|
|
|
|
assert not out
|
|
|
|
|
2014-05-05 12:16:12 +02:00
|
|
|
|
2015-04-04 19:01:58 +02:00
|
|
|
class TestInterpolateColor:
|
2014-06-12 21:43:30 +02:00
|
|
|
|
|
|
|
"""Tests for interpolate_color.
|
|
|
|
|
|
|
|
Attributes:
|
2014-07-15 11:06:56 +02:00
|
|
|
white: The Color white as a valid Color for tests.
|
|
|
|
white: The Color black as a valid Color for tests.
|
2014-06-12 21:43:30 +02:00
|
|
|
"""
|
|
|
|
|
2015-12-01 22:40:58 +01:00
|
|
|
Colors = collections.namedtuple('Colors', ['white', 'black'])
|
2014-06-12 21:43:30 +02:00
|
|
|
|
2015-12-01 22:40:58 +01:00
|
|
|
@pytest.fixture
|
|
|
|
def colors(self):
|
|
|
|
"""Example colors to be used."""
|
|
|
|
return self.Colors(Color('white'), Color('black'))
|
|
|
|
|
|
|
|
def test_invalid_start(self, colors):
|
2014-06-12 21:43:30 +02:00
|
|
|
"""Test an invalid start color."""
|
2015-04-04 19:01:58 +02:00
|
|
|
with pytest.raises(qtutils.QtValueError):
|
2015-12-01 22:40:58 +01:00
|
|
|
utils.interpolate_color(Color(), colors.white, 0)
|
2014-06-12 21:43:30 +02:00
|
|
|
|
2015-12-01 22:40:58 +01:00
|
|
|
def test_invalid_end(self, colors):
|
2014-06-12 21:43:30 +02:00
|
|
|
"""Test an invalid end color."""
|
2015-04-04 19:01:58 +02:00
|
|
|
with pytest.raises(qtutils.QtValueError):
|
2015-12-01 22:40:58 +01:00
|
|
|
utils.interpolate_color(colors.white, Color(), 0)
|
2014-06-12 21:43:30 +02:00
|
|
|
|
2015-12-01 22:40:58 +01:00
|
|
|
def test_invalid_percentage(self, colors):
|
2014-06-12 21:43:30 +02:00
|
|
|
"""Test an invalid percentage."""
|
2015-04-04 19:01:58 +02:00
|
|
|
with pytest.raises(ValueError):
|
2015-12-01 22:40:58 +01:00
|
|
|
utils.interpolate_color(colors.white, colors.white, -1)
|
2015-04-04 19:01:58 +02:00
|
|
|
with pytest.raises(ValueError):
|
2015-12-01 22:40:58 +01:00
|
|
|
utils.interpolate_color(colors.white, colors.white, 101)
|
2014-06-12 21:43:30 +02:00
|
|
|
|
2015-12-01 22:40:58 +01:00
|
|
|
def test_invalid_colorspace(self, colors):
|
2014-06-12 21:43:30 +02:00
|
|
|
"""Test an invalid colorspace."""
|
2015-04-04 19:01:58 +02:00
|
|
|
with pytest.raises(ValueError):
|
2015-12-01 22:40:58 +01:00
|
|
|
utils.interpolate_color(colors.white, colors.black, 10,
|
|
|
|
QColor.Cmyk)
|
2014-06-12 21:43:30 +02:00
|
|
|
|
2015-12-01 22:40:58 +01:00
|
|
|
def test_valid_percentages_rgb(self, colors):
|
2014-06-12 21:43:30 +02:00
|
|
|
"""Test 0% and 100% in the RGB colorspace."""
|
2015-12-01 22:40:58 +01:00
|
|
|
white = utils.interpolate_color(colors.white, colors.black, 0,
|
|
|
|
QColor.Rgb)
|
|
|
|
black = utils.interpolate_color(colors.white, colors.black, 100,
|
2014-06-12 21:43:30 +02:00
|
|
|
QColor.Rgb)
|
2015-12-01 22:40:58 +01:00
|
|
|
assert Color(white) == colors.white
|
|
|
|
assert Color(black) == colors.black
|
2014-06-12 21:43:30 +02:00
|
|
|
|
2015-12-01 22:40:58 +01:00
|
|
|
def test_valid_percentages_hsv(self, colors):
|
2014-06-12 21:43:30 +02:00
|
|
|
"""Test 0% and 100% in the HSV colorspace."""
|
2015-12-01 22:40:58 +01:00
|
|
|
white = utils.interpolate_color(colors.white, colors.black, 0,
|
2014-06-12 21:43:30 +02:00
|
|
|
QColor.Hsv)
|
2015-12-01 22:40:58 +01:00
|
|
|
black = utils.interpolate_color(colors.white, colors.black, 100,
|
|
|
|
QColor.Hsv)
|
|
|
|
assert Color(white) == colors.white
|
|
|
|
assert Color(black) == colors.black
|
2014-06-12 21:43:30 +02:00
|
|
|
|
2015-12-01 22:40:58 +01:00
|
|
|
def test_valid_percentages_hsl(self, colors):
|
2014-06-12 21:43:30 +02:00
|
|
|
"""Test 0% and 100% in the HSL colorspace."""
|
2015-12-01 22:40:58 +01:00
|
|
|
white = utils.interpolate_color(colors.white, colors.black, 0,
|
|
|
|
QColor.Hsl)
|
|
|
|
black = utils.interpolate_color(colors.white, colors.black, 100,
|
2014-06-12 21:43:30 +02:00
|
|
|
QColor.Hsl)
|
2015-12-01 22:40:58 +01:00
|
|
|
assert Color(white) == colors.white
|
|
|
|
assert Color(black) == colors.black
|
2014-06-12 21:43:30 +02:00
|
|
|
|
|
|
|
def test_interpolation_rgb(self):
|
|
|
|
"""Test an interpolation in the RGB colorspace."""
|
2014-07-15 11:06:56 +02:00
|
|
|
color = utils.interpolate_color(Color(0, 40, 100), Color(0, 20, 200),
|
2014-06-12 21:43:30 +02:00
|
|
|
50, QColor.Rgb)
|
2015-04-04 19:01:58 +02:00
|
|
|
assert Color(color) == Color(0, 30, 150)
|
2014-06-12 21:43:30 +02:00
|
|
|
|
|
|
|
def test_interpolation_hsv(self):
|
|
|
|
"""Test an interpolation in the HSV colorspace."""
|
2014-07-15 11:06:56 +02:00
|
|
|
start = Color()
|
|
|
|
stop = Color()
|
2014-06-12 21:43:30 +02:00
|
|
|
start.setHsv(0, 40, 100)
|
|
|
|
stop.setHsv(0, 20, 200)
|
|
|
|
color = utils.interpolate_color(start, stop, 50, QColor.Hsv)
|
2014-07-15 11:06:56 +02:00
|
|
|
expected = Color()
|
2014-06-12 21:43:30 +02:00
|
|
|
expected.setHsv(0, 30, 150)
|
2015-04-04 19:01:58 +02:00
|
|
|
assert Color(color) == expected
|
2014-06-12 21:43:30 +02:00
|
|
|
|
|
|
|
def test_interpolation_hsl(self):
|
|
|
|
"""Test an interpolation in the HSL colorspace."""
|
2014-07-15 11:06:56 +02:00
|
|
|
start = Color()
|
|
|
|
stop = Color()
|
2014-06-12 21:43:30 +02:00
|
|
|
start.setHsl(0, 40, 100)
|
|
|
|
stop.setHsl(0, 20, 200)
|
|
|
|
color = utils.interpolate_color(start, stop, 50, QColor.Hsl)
|
2014-07-15 11:06:56 +02:00
|
|
|
expected = Color()
|
2014-06-12 21:43:30 +02:00
|
|
|
expected.setHsl(0, 30, 150)
|
2015-04-04 19:01:58 +02:00
|
|
|
assert Color(color) == expected
|
2014-06-12 21:43:30 +02:00
|
|
|
|
2015-10-24 16:01:32 +02:00
|
|
|
@pytest.mark.parametrize('percentage, expected', [
|
|
|
|
(0, (0, 0, 0)),
|
|
|
|
(99, (0, 0, 0)),
|
|
|
|
(100, (255, 255, 255)),
|
|
|
|
])
|
|
|
|
def test_interpolation_none(self, percentage, expected):
|
|
|
|
"""Test an interpolation with a gradient turned off."""
|
|
|
|
color = utils.interpolate_color(Color(0, 0, 0), Color(255, 255, 255),
|
|
|
|
percentage, None)
|
2015-10-26 06:45:36 +01:00
|
|
|
assert isinstance(color, QColor)
|
2015-10-24 16:01:32 +02:00
|
|
|
assert Color(color) == Color(*expected)
|
|
|
|
|
2014-06-12 21:43:30 +02:00
|
|
|
|
2015-05-19 16:24:43 +02:00
|
|
|
@pytest.mark.parametrize('seconds, out', [
|
|
|
|
(-1, '-0:01'),
|
|
|
|
(0, '0:00'),
|
|
|
|
(59, '0:59'),
|
|
|
|
(60, '1:00'),
|
|
|
|
(60.4, '1:00'),
|
|
|
|
(61, '1:01'),
|
|
|
|
(-61, '-1:01'),
|
|
|
|
(3599, '59:59'),
|
|
|
|
(3600, '1:00:00'),
|
|
|
|
(3601, '1:00:01'),
|
|
|
|
(36000, '10:00:00'),
|
|
|
|
])
|
|
|
|
def test_format_seconds(seconds, out):
|
|
|
|
assert utils.format_seconds(seconds) == out
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('td, out', [
|
|
|
|
(datetime.timedelta(seconds=-1), '-1s'),
|
|
|
|
(datetime.timedelta(seconds=0), '0s'),
|
|
|
|
(datetime.timedelta(seconds=59), '59s'),
|
|
|
|
(datetime.timedelta(seconds=120), '2m'),
|
|
|
|
(datetime.timedelta(seconds=60.4), '1m'),
|
|
|
|
(datetime.timedelta(seconds=63), '1m 3s'),
|
|
|
|
(datetime.timedelta(seconds=-64), '-1m 4s'),
|
|
|
|
(datetime.timedelta(seconds=3599), '59m 59s'),
|
|
|
|
(datetime.timedelta(seconds=3600), '1h'),
|
|
|
|
(datetime.timedelta(seconds=3605), '1h 5s'),
|
|
|
|
(datetime.timedelta(seconds=3723), '1h 2m 3s'),
|
|
|
|
(datetime.timedelta(seconds=3780), '1h 3m'),
|
|
|
|
(datetime.timedelta(seconds=36000), '10h'),
|
|
|
|
])
|
|
|
|
def test_format_timedelta(td, out):
|
|
|
|
assert utils.format_timedelta(td) == out
|
2015-03-22 22:39:56 +01:00
|
|
|
|
|
|
|
|
2015-04-04 19:01:58 +02:00
|
|
|
class TestFormatSize:
|
2014-06-13 07:13:47 +02:00
|
|
|
|
|
|
|
"""Tests for format_size.
|
|
|
|
|
|
|
|
Class attributes:
|
|
|
|
TESTS: A list of (input, output) tuples.
|
|
|
|
"""
|
|
|
|
|
|
|
|
TESTS = [
|
|
|
|
(-1024, '-1.00k'),
|
|
|
|
(-1, '-1.00'),
|
|
|
|
(0, '0.00'),
|
|
|
|
(1023, '1023.00'),
|
|
|
|
(1024, '1.00k'),
|
|
|
|
(1034.24, '1.01k'),
|
|
|
|
(1024 * 1024 * 2, '2.00M'),
|
|
|
|
(1024 ** 10, '1024.00Y'),
|
|
|
|
(None, '?.??'),
|
|
|
|
]
|
|
|
|
|
2015-04-04 19:01:58 +02:00
|
|
|
KILO_TESTS = [(999, '999.00'), (1000, '1.00k'), (1010, '1.01k')]
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('size, out', TESTS)
|
|
|
|
def test_format_size(self, size, out):
|
2014-06-13 07:13:47 +02:00
|
|
|
"""Test format_size with several tests."""
|
2015-04-04 19:01:58 +02:00
|
|
|
assert utils.format_size(size) == out
|
2014-06-13 07:13:47 +02:00
|
|
|
|
2015-04-04 19:01:58 +02:00
|
|
|
@pytest.mark.parametrize('size, out', TESTS)
|
|
|
|
def test_suffix(self, size, out):
|
2014-06-13 07:13:47 +02:00
|
|
|
"""Test the suffix option."""
|
2015-04-04 19:01:58 +02:00
|
|
|
assert utils.format_size(size, suffix='B') == out + 'B'
|
2014-06-13 07:13:47 +02:00
|
|
|
|
2015-04-04 19:01:58 +02:00
|
|
|
@pytest.mark.parametrize('size, out', KILO_TESTS)
|
|
|
|
def test_base(self, size, out):
|
2014-06-13 07:13:47 +02:00
|
|
|
"""Test with an alternative base."""
|
2015-04-04 19:01:58 +02:00
|
|
|
assert utils.format_size(size, base=1000) == out
|
2014-06-13 07:13:47 +02:00
|
|
|
|
|
|
|
|
2015-04-04 19:01:58 +02:00
|
|
|
class TestKeyToString:
|
2014-07-04 07:35:33 +02:00
|
|
|
|
2015-11-18 18:13:46 +01:00
|
|
|
"""Test key_to_string."""
|
2014-07-04 07:35:33 +02:00
|
|
|
|
2015-05-19 16:24:43 +02:00
|
|
|
@pytest.mark.parametrize('key, expected', [
|
|
|
|
(Qt.Key_Blue, 'Blue'),
|
|
|
|
(Qt.Key_Backtab, 'Tab'),
|
|
|
|
(Qt.Key_Escape, 'Escape'),
|
|
|
|
(Qt.Key_A, 'A'),
|
|
|
|
(Qt.Key_degree, '°'),
|
2015-06-12 16:37:07 +02:00
|
|
|
(Qt.Key_Meta, 'Meta'),
|
2015-05-19 16:24:43 +02:00
|
|
|
])
|
|
|
|
def test_normal(self, key, expected):
|
2014-07-04 07:35:33 +02:00
|
|
|
"""Test a special key where QKeyEvent::toString works incorrectly."""
|
2015-05-19 16:24:43 +02:00
|
|
|
assert utils.key_to_string(key) == expected
|
2014-07-04 07:35:33 +02:00
|
|
|
|
2015-05-19 16:24:43 +02:00
|
|
|
def test_missing(self, monkeypatch):
|
|
|
|
"""Test with a missing key."""
|
|
|
|
monkeypatch.delattr('qutebrowser.utils.utils.Qt.Key_Blue')
|
|
|
|
# We don't want to test the key which is actually missing - we only
|
|
|
|
# want to know if the mapping still behaves properly.
|
2015-04-04 19:01:58 +02:00
|
|
|
assert utils.key_to_string(Qt.Key_A) == 'A'
|
2014-07-04 07:35:33 +02:00
|
|
|
|
2015-11-18 18:13:46 +01:00
|
|
|
def test_all(self):
|
2015-06-12 16:37:07 +02:00
|
|
|
"""Make sure there's some sensible output for all keys."""
|
2015-11-18 18:13:46 +01:00
|
|
|
for name, value in sorted(vars(Qt).items()):
|
|
|
|
if not isinstance(value, Qt.Key):
|
|
|
|
continue
|
|
|
|
print(name)
|
|
|
|
string = utils.key_to_string(value)
|
|
|
|
assert string
|
|
|
|
string.encode('utf-8') # make sure it's encodable
|
2015-06-12 16:37:07 +02:00
|
|
|
|
2014-07-04 07:35:33 +02:00
|
|
|
|
2015-04-04 19:01:58 +02:00
|
|
|
class TestKeyEventToString:
|
2014-07-04 07:35:33 +02:00
|
|
|
|
|
|
|
"""Test keyevent_to_string."""
|
|
|
|
|
2015-04-04 19:01:58 +02:00
|
|
|
def test_only_control(self, fake_keyevent_factory):
|
2014-07-04 07:35:33 +02:00
|
|
|
"""Test keyeevent when only control is pressed."""
|
2015-04-04 19:01:58 +02:00
|
|
|
evt = fake_keyevent_factory(key=Qt.Key_Control,
|
2014-08-26 19:10:14 +02:00
|
|
|
modifiers=Qt.ControlModifier)
|
2015-04-04 19:01:58 +02:00
|
|
|
assert utils.keyevent_to_string(evt) is None
|
2014-07-04 07:35:33 +02:00
|
|
|
|
2015-04-04 19:01:58 +02:00
|
|
|
def test_only_hyper_l(self, fake_keyevent_factory):
|
2014-07-04 07:35:33 +02:00
|
|
|
"""Test keyeevent when only Hyper_L is pressed."""
|
2015-04-04 19:01:58 +02:00
|
|
|
evt = fake_keyevent_factory(key=Qt.Key_Hyper_L,
|
2014-08-26 19:10:14 +02:00
|
|
|
modifiers=Qt.MetaModifier)
|
2015-04-04 19:01:58 +02:00
|
|
|
assert utils.keyevent_to_string(evt) is None
|
2014-07-04 07:35:33 +02:00
|
|
|
|
2015-04-04 19:01:58 +02:00
|
|
|
def test_only_key(self, fake_keyevent_factory):
|
2014-07-04 07:35:33 +02:00
|
|
|
"""Test with a simple key pressed."""
|
2015-04-04 19:01:58 +02:00
|
|
|
evt = fake_keyevent_factory(key=Qt.Key_A)
|
|
|
|
assert utils.keyevent_to_string(evt) == 'A'
|
2014-07-04 07:35:33 +02:00
|
|
|
|
2015-04-04 19:01:58 +02:00
|
|
|
def test_key_and_modifier(self, fake_keyevent_factory):
|
2014-07-04 07:35:33 +02:00
|
|
|
"""Test with key and modifier pressed."""
|
2015-04-04 19:01:58 +02:00
|
|
|
evt = fake_keyevent_factory(key=Qt.Key_A, modifiers=Qt.ControlModifier)
|
2015-05-17 18:59:40 +02:00
|
|
|
expected = 'Meta+A' if sys.platform == 'darwin' else 'Ctrl+A'
|
|
|
|
assert utils.keyevent_to_string(evt) == expected
|
2014-07-04 07:35:33 +02:00
|
|
|
|
2015-04-04 19:01:58 +02:00
|
|
|
def test_key_and_modifiers(self, fake_keyevent_factory):
|
2015-05-17 18:59:40 +02:00
|
|
|
"""Test with key and multiple modifiers pressed."""
|
2015-04-04 19:01:58 +02:00
|
|
|
evt = fake_keyevent_factory(
|
2014-08-26 19:10:14 +02:00
|
|
|
key=Qt.Key_A, modifiers=(Qt.ControlModifier | Qt.AltModifier |
|
|
|
|
Qt.MetaModifier | Qt.ShiftModifier))
|
2015-05-17 18:52:25 +02:00
|
|
|
assert utils.keyevent_to_string(evt) == 'Ctrl+Alt+Meta+Shift+A'
|
2014-07-04 07:35:33 +02:00
|
|
|
|
2015-05-19 16:24:43 +02:00
|
|
|
def test_mac(self, monkeypatch, fake_keyevent_factory):
|
|
|
|
"""Test with a simulated mac."""
|
|
|
|
monkeypatch.setattr('sys.platform', 'darwin')
|
|
|
|
evt = fake_keyevent_factory(key=Qt.Key_A, modifiers=Qt.ControlModifier)
|
|
|
|
assert utils.keyevent_to_string(evt) == 'Meta+A'
|
|
|
|
|
|
|
|
|
2015-11-19 07:35:14 +01:00
|
|
|
@pytest.mark.parametrize('keystr, expected', [
|
|
|
|
('<Control-x>', utils.KeyInfo(Qt.Key_X, Qt.ControlModifier, '')),
|
|
|
|
('<Meta-x>', utils.KeyInfo(Qt.Key_X, Qt.MetaModifier, '')),
|
|
|
|
('<Ctrl-Alt-y>',
|
|
|
|
utils.KeyInfo(Qt.Key_Y, Qt.ControlModifier | Qt.AltModifier, '')),
|
|
|
|
('x', utils.KeyInfo(Qt.Key_X, Qt.NoModifier, 'x')),
|
|
|
|
('X', utils.KeyInfo(Qt.Key_X, Qt.ShiftModifier, 'X')),
|
|
|
|
('<Escape>', utils.KeyInfo(Qt.Key_Escape, Qt.NoModifier, '')),
|
|
|
|
|
|
|
|
('foobar', utils.KeyParseError),
|
|
|
|
('x, y', utils.KeyParseError),
|
|
|
|
('xyz', utils.KeyParseError),
|
|
|
|
('Escape', utils.KeyParseError),
|
|
|
|
('<Ctrl-x>, <Ctrl-y>', utils.KeyParseError),
|
|
|
|
])
|
|
|
|
def test_parse_single_key(keystr, expected):
|
|
|
|
if expected is utils.KeyParseError:
|
|
|
|
with pytest.raises(utils.KeyParseError):
|
|
|
|
utils._parse_single_key(keystr)
|
|
|
|
else:
|
|
|
|
assert utils._parse_single_key(keystr) == expected
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('keystr, expected', [
|
|
|
|
('<Control-x>', [utils.KeyInfo(Qt.Key_X, Qt.ControlModifier, '')]),
|
|
|
|
('x', [utils.KeyInfo(Qt.Key_X, Qt.NoModifier, 'x')]),
|
|
|
|
('xy', [utils.KeyInfo(Qt.Key_X, Qt.NoModifier, 'x'),
|
|
|
|
utils.KeyInfo(Qt.Key_Y, Qt.NoModifier, 'y')]),
|
|
|
|
|
|
|
|
('<Control-x><Meta-x>', utils.KeyParseError),
|
|
|
|
])
|
|
|
|
def test_parse_keystring(keystr, expected):
|
|
|
|
if expected is utils.KeyParseError:
|
|
|
|
with pytest.raises(utils.KeyParseError):
|
|
|
|
utils.parse_keystring(keystr)
|
|
|
|
else:
|
|
|
|
assert utils.parse_keystring(keystr) == expected
|
|
|
|
|
|
|
|
|
2015-05-19 16:24:43 +02:00
|
|
|
@pytest.mark.parametrize('orig, repl', [
|
|
|
|
('Control+x', 'ctrl+x'),
|
|
|
|
('Windows+x', 'meta+x'),
|
|
|
|
('Mod1+x', 'alt+x'),
|
|
|
|
('Mod4+x', 'meta+x'),
|
|
|
|
('Control--', 'ctrl+-'),
|
|
|
|
('Windows++', 'meta++'),
|
|
|
|
('ctrl-x', 'ctrl+x'),
|
|
|
|
('control+x', 'ctrl+x')
|
|
|
|
])
|
|
|
|
def test_normalize_keystr(orig, repl):
|
|
|
|
assert utils.normalize_keystr(orig) == repl
|
|
|
|
|
|
|
|
|
|
|
|
class TestFakeIOStream:
|
|
|
|
|
|
|
|
"""Test FakeIOStream."""
|
|
|
|
|
|
|
|
def _write_func(self, text):
|
|
|
|
return text
|
|
|
|
|
|
|
|
def test_flush(self):
|
|
|
|
"""Smoke-test to see if flushing works."""
|
|
|
|
s = utils.FakeIOStream(self._write_func)
|
|
|
|
s.flush()
|
|
|
|
|
|
|
|
def test_isatty(self):
|
|
|
|
"""Make sure isatty() is always false."""
|
|
|
|
s = utils.FakeIOStream(self._write_func)
|
|
|
|
assert not s.isatty()
|
|
|
|
|
|
|
|
def test_write(self):
|
|
|
|
"""Make sure writing works."""
|
|
|
|
s = utils.FakeIOStream(self._write_func)
|
|
|
|
assert s.write('echo') == 'echo'
|
|
|
|
|
|
|
|
|
|
|
|
class TestFakeIO:
|
2014-07-04 07:35:33 +02:00
|
|
|
|
2015-05-19 16:24:43 +02:00
|
|
|
"""Test FakeIO."""
|
2014-07-03 07:41:23 +02:00
|
|
|
|
2015-05-19 16:24:43 +02:00
|
|
|
@pytest.yield_fixture(autouse=True)
|
|
|
|
def restore_streams(self):
|
|
|
|
"""Restore sys.stderr/sys.stdout after tests."""
|
|
|
|
old_stdout = sys.stdout
|
|
|
|
old_stderr = sys.stderr
|
|
|
|
yield
|
|
|
|
sys.stdout = old_stdout
|
|
|
|
sys.stderr = old_stderr
|
2014-07-03 07:41:23 +02:00
|
|
|
|
2015-05-19 16:24:43 +02:00
|
|
|
def test_normal(self, capsys):
|
|
|
|
"""Test without changing sys.stderr/sys.stdout."""
|
|
|
|
data = io.StringIO()
|
|
|
|
with utils.fake_io(data.write):
|
|
|
|
sys.stdout.write('hello\n')
|
|
|
|
sys.stderr.write('world\n')
|
2015-04-04 19:01:58 +02:00
|
|
|
|
2015-05-19 16:24:43 +02:00
|
|
|
out, err = capsys.readouterr()
|
|
|
|
assert not out
|
|
|
|
assert not err
|
|
|
|
assert data.getvalue() == 'hello\nworld\n'
|
|
|
|
|
|
|
|
sys.stdout.write('back to\n')
|
|
|
|
sys.stderr.write('normal\n')
|
|
|
|
out, err = capsys.readouterr()
|
|
|
|
assert out == 'back to\n'
|
|
|
|
assert err == 'normal\n'
|
|
|
|
|
|
|
|
def test_stdout_replaced(self, capsys):
|
|
|
|
"""Test with replaced stdout."""
|
|
|
|
data = io.StringIO()
|
|
|
|
new_stdout = io.StringIO()
|
|
|
|
with utils.fake_io(data.write):
|
|
|
|
sys.stdout.write('hello\n')
|
|
|
|
sys.stderr.write('world\n')
|
|
|
|
sys.stdout = new_stdout
|
|
|
|
|
|
|
|
out, err = capsys.readouterr()
|
|
|
|
assert not out
|
|
|
|
assert not err
|
|
|
|
assert data.getvalue() == 'hello\nworld\n'
|
|
|
|
|
|
|
|
sys.stdout.write('still new\n')
|
|
|
|
sys.stderr.write('normal\n')
|
|
|
|
out, err = capsys.readouterr()
|
|
|
|
assert not out
|
|
|
|
assert err == 'normal\n'
|
|
|
|
assert new_stdout.getvalue() == 'still new\n'
|
|
|
|
|
|
|
|
def test_stderr_replaced(self, capsys):
|
|
|
|
"""Test with replaced stderr."""
|
|
|
|
data = io.StringIO()
|
|
|
|
new_stderr = io.StringIO()
|
|
|
|
with utils.fake_io(data.write):
|
|
|
|
sys.stdout.write('hello\n')
|
|
|
|
sys.stderr.write('world\n')
|
|
|
|
sys.stderr = new_stderr
|
|
|
|
|
|
|
|
out, err = capsys.readouterr()
|
|
|
|
assert not out
|
|
|
|
assert not err
|
|
|
|
assert data.getvalue() == 'hello\nworld\n'
|
|
|
|
|
|
|
|
sys.stdout.write('normal\n')
|
|
|
|
sys.stderr.write('still new\n')
|
|
|
|
out, err = capsys.readouterr()
|
|
|
|
assert out == 'normal\n'
|
|
|
|
assert not err
|
|
|
|
assert new_stderr.getvalue() == 'still new\n'
|
|
|
|
|
|
|
|
|
|
|
|
class GotException(Exception):
|
|
|
|
|
|
|
|
"""Exception used for TestDisabledExcepthook."""
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def excepthook(_exc, _val, _tb):
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
def excepthook_2(_exc, _val, _tb):
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
class TestDisabledExcepthook:
|
|
|
|
|
|
|
|
"""Test disabled_excepthook.
|
|
|
|
|
|
|
|
This doesn't test much as some things are untestable without triggering
|
|
|
|
the excepthook (which is hard to test).
|
|
|
|
"""
|
|
|
|
|
|
|
|
@pytest.yield_fixture(autouse=True)
|
|
|
|
def restore_excepthook(self):
|
|
|
|
"""Restore sys.excepthook and sys.__excepthook__ after tests."""
|
|
|
|
old_excepthook = sys.excepthook
|
|
|
|
old_dunder_excepthook = sys.__excepthook__
|
|
|
|
yield
|
|
|
|
sys.excepthook = old_excepthook
|
|
|
|
sys.__excepthook__ = old_dunder_excepthook
|
|
|
|
|
|
|
|
def test_normal(self):
|
|
|
|
"""Test without changing sys.excepthook."""
|
|
|
|
sys.excepthook = excepthook
|
|
|
|
assert sys.excepthook is excepthook
|
|
|
|
with utils.disabled_excepthook():
|
|
|
|
assert sys.excepthook is not excepthook
|
|
|
|
assert sys.excepthook is excepthook
|
|
|
|
|
|
|
|
def test_changed(self):
|
|
|
|
"""Test with changed sys.excepthook."""
|
|
|
|
sys.excepthook = excepthook
|
|
|
|
with utils.disabled_excepthook():
|
|
|
|
assert sys.excepthook is not excepthook
|
|
|
|
sys.excepthook = excepthook_2
|
|
|
|
assert sys.excepthook is excepthook_2
|
|
|
|
|
|
|
|
|
|
|
|
class TestPreventExceptions:
|
|
|
|
|
|
|
|
"""Test prevent_exceptions."""
|
|
|
|
|
|
|
|
@utils.prevent_exceptions(42)
|
|
|
|
def func_raising(self):
|
|
|
|
raise Exception
|
|
|
|
|
|
|
|
def test_raising(self, caplog):
|
|
|
|
"""Test with a raising function."""
|
2015-11-11 19:57:03 +01:00
|
|
|
with caplog.at_level(logging.ERROR, 'misc'):
|
2015-05-19 16:24:43 +02:00
|
|
|
ret = self.func_raising()
|
|
|
|
assert ret == 42
|
2015-11-11 19:57:03 +01:00
|
|
|
assert len(caplog.records) == 1
|
2015-05-19 16:24:43 +02:00
|
|
|
expected = 'Error in test_utils.TestPreventExceptions.func_raising'
|
2015-11-11 19:57:03 +01:00
|
|
|
actual = caplog.records[0].message
|
2015-05-19 16:24:43 +02:00
|
|
|
assert actual == expected
|
|
|
|
|
|
|
|
@utils.prevent_exceptions(42)
|
|
|
|
def func_not_raising(self):
|
|
|
|
return 23
|
|
|
|
|
|
|
|
def test_not_raising(self, caplog):
|
|
|
|
"""Test with a non-raising function."""
|
2015-11-11 19:57:03 +01:00
|
|
|
with caplog.at_level(logging.ERROR, 'misc'):
|
2015-05-19 16:24:43 +02:00
|
|
|
ret = self.func_not_raising()
|
|
|
|
assert ret == 23
|
2015-11-11 19:57:03 +01:00
|
|
|
assert not caplog.records
|
2015-05-19 16:24:43 +02:00
|
|
|
|
|
|
|
@utils.prevent_exceptions(42, True)
|
|
|
|
def func_predicate_true(self):
|
|
|
|
raise Exception
|
|
|
|
|
|
|
|
def test_predicate_true(self, caplog):
|
|
|
|
"""Test with a True predicate."""
|
2015-11-11 19:57:03 +01:00
|
|
|
with caplog.at_level(logging.ERROR, 'misc'):
|
2015-05-19 16:24:43 +02:00
|
|
|
ret = self.func_predicate_true()
|
|
|
|
assert ret == 42
|
2015-11-11 19:57:03 +01:00
|
|
|
assert len(caplog.records) == 1
|
2015-05-19 16:24:43 +02:00
|
|
|
|
|
|
|
@utils.prevent_exceptions(42, False)
|
|
|
|
def func_predicate_false(self):
|
|
|
|
raise Exception
|
|
|
|
|
|
|
|
def test_predicate_false(self, caplog):
|
|
|
|
"""Test with a False predicate."""
|
2015-11-11 19:57:03 +01:00
|
|
|
with caplog.at_level(logging.ERROR, 'misc'):
|
2015-05-19 16:24:43 +02:00
|
|
|
with pytest.raises(Exception):
|
|
|
|
self.func_predicate_false()
|
2015-11-11 19:57:03 +01:00
|
|
|
assert not caplog.records
|
2015-05-19 16:24:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Obj:
|
|
|
|
|
|
|
|
"""Test object for test_get_repr()."""
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('constructor, attrs, expected', [
|
|
|
|
(False, {}, '<test_utils.Obj>'),
|
|
|
|
(False, {'foo': None}, '<test_utils.Obj foo=None>'),
|
|
|
|
(False, {'foo': "b'ar", 'baz': 2}, '<test_utils.Obj baz=2 foo="b\'ar">'),
|
|
|
|
(True, {}, 'test_utils.Obj()'),
|
|
|
|
(True, {'foo': None}, 'test_utils.Obj(foo=None)'),
|
|
|
|
(True, {'foo': "te'st", 'bar': 2}, 'test_utils.Obj(bar=2, foo="te\'st")'),
|
|
|
|
])
|
|
|
|
def test_get_repr(constructor, attrs, expected):
|
|
|
|
"""Test get_repr()."""
|
|
|
|
assert utils.get_repr(Obj(), constructor, **attrs) == expected
|
|
|
|
|
|
|
|
|
|
|
|
class QualnameObj():
|
|
|
|
|
|
|
|
"""Test object for test_qualname."""
|
|
|
|
|
|
|
|
def func(self):
|
|
|
|
"""Test method for test_qualname."""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def qualname_func(_blah):
|
|
|
|
"""Test function for test_qualname."""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2015-06-17 06:46:03 +02:00
|
|
|
QUALNAME_OBJ = QualnameObj()
|
|
|
|
|
|
|
|
|
2015-05-19 16:24:43 +02:00
|
|
|
@pytest.mark.parametrize('obj, expected', [
|
2015-06-17 06:46:03 +02:00
|
|
|
(QUALNAME_OBJ, repr(QUALNAME_OBJ)), # instance - unknown
|
2015-05-19 16:24:43 +02:00
|
|
|
(QualnameObj, 'test_utils.QualnameObj'), # class
|
|
|
|
(QualnameObj.func, 'test_utils.QualnameObj.func'), # unbound method
|
|
|
|
(QualnameObj().func, 'test_utils.QualnameObj.func'), # bound method
|
|
|
|
(qualname_func, 'test_utils.qualname_func'), # function
|
|
|
|
(functools.partial(qualname_func, True), 'test_utils.qualname_func'),
|
|
|
|
(qutebrowser, 'qutebrowser'), # module
|
|
|
|
(qutebrowser.utils, 'qutebrowser.utils'), # submodule
|
|
|
|
(utils, 'qutebrowser.utils.utils'), # submodule (from-import)
|
|
|
|
])
|
|
|
|
def test_qualname(obj, expected):
|
|
|
|
assert utils.qualname(obj) == expected
|
2014-07-03 07:41:23 +02:00
|
|
|
|
|
|
|
|
2015-04-04 19:01:58 +02:00
|
|
|
class TestIsEnum:
|
2014-09-03 10:47:27 +02:00
|
|
|
|
|
|
|
"""Test is_enum."""
|
|
|
|
|
|
|
|
def test_enum(self):
|
|
|
|
"""Test is_enum with an enum."""
|
|
|
|
e = enum.Enum('Foo', 'bar, baz')
|
2015-04-04 19:01:58 +02:00
|
|
|
assert utils.is_enum(e)
|
2014-09-03 10:47:27 +02:00
|
|
|
|
|
|
|
def test_class(self):
|
|
|
|
"""Test is_enum with a non-enum class."""
|
2015-02-01 22:38:40 +01:00
|
|
|
class Test:
|
2015-03-26 07:08:13 +01:00
|
|
|
|
|
|
|
"""Test class for is_enum."""
|
|
|
|
|
2015-02-01 22:38:40 +01:00
|
|
|
pass
|
2015-04-04 19:01:58 +02:00
|
|
|
|
|
|
|
assert not utils.is_enum(Test)
|
2014-09-03 10:47:27 +02:00
|
|
|
|
|
|
|
def test_object(self):
|
|
|
|
"""Test is_enum with a non-enum object."""
|
2015-04-04 19:01:58 +02:00
|
|
|
assert not utils.is_enum(23)
|
2014-09-03 10:47:27 +02:00
|
|
|
|
|
|
|
|
2015-04-04 19:01:58 +02:00
|
|
|
class TestRaises:
|
2014-11-27 20:44:48 +01:00
|
|
|
|
|
|
|
"""Test raises."""
|
|
|
|
|
|
|
|
def do_raise(self):
|
2014-11-27 22:27:13 +01:00
|
|
|
"""Helper function which raises an exception."""
|
2014-11-27 20:44:48 +01:00
|
|
|
raise Exception
|
|
|
|
|
|
|
|
def do_nothing(self):
|
2014-11-27 22:27:13 +01:00
|
|
|
"""Helper function which does nothing."""
|
2014-11-27 20:44:48 +01:00
|
|
|
pass
|
|
|
|
|
|
|
|
def test_raises_single_exc_true(self):
|
|
|
|
"""Test raises with a single exception which gets raised."""
|
2015-04-04 19:01:58 +02:00
|
|
|
assert utils.raises(ValueError, int, 'a')
|
2014-11-27 20:44:48 +01:00
|
|
|
|
|
|
|
def test_raises_single_exc_false(self):
|
|
|
|
"""Test raises with a single exception which does not get raised."""
|
2015-04-04 19:01:58 +02:00
|
|
|
assert not utils.raises(ValueError, int, '1')
|
2014-11-27 20:44:48 +01:00
|
|
|
|
|
|
|
def test_raises_multiple_exc_true(self):
|
|
|
|
"""Test raises with multiple exceptions which get raised."""
|
2015-04-04 19:01:58 +02:00
|
|
|
assert utils.raises((ValueError, TypeError), int, 'a')
|
|
|
|
assert utils.raises((ValueError, TypeError), int, None)
|
2014-11-27 20:44:48 +01:00
|
|
|
|
|
|
|
def test_raises_multiple_exc_false(self):
|
|
|
|
"""Test raises with multiple exceptions which do not get raised."""
|
2015-04-04 19:01:58 +02:00
|
|
|
assert not utils.raises((ValueError, TypeError), int, '1')
|
2014-11-27 20:44:48 +01:00
|
|
|
|
2014-11-27 22:27:13 +01:00
|
|
|
def test_no_args_true(self):
|
2014-11-27 20:44:48 +01:00
|
|
|
"""Test with no args and an exception which gets raised."""
|
2015-04-04 19:01:58 +02:00
|
|
|
assert utils.raises(Exception, self.do_raise)
|
2014-11-27 20:44:48 +01:00
|
|
|
|
2014-11-27 22:27:13 +01:00
|
|
|
def test_no_args_false(self):
|
2014-11-27 20:44:48 +01:00
|
|
|
"""Test with no args and an exception which does not get raised."""
|
2015-04-04 19:01:58 +02:00
|
|
|
assert not utils.raises(Exception, self.do_nothing)
|
2014-11-27 20:44:48 +01:00
|
|
|
|
|
|
|
def test_unrelated_exception(self):
|
|
|
|
"""Test with an unrelated exception."""
|
2015-04-04 19:01:58 +02:00
|
|
|
with pytest.raises(Exception):
|
2014-11-27 20:44:48 +01:00
|
|
|
utils.raises(ValueError, self.do_raise)
|
|
|
|
|
|
|
|
|
2015-05-19 16:24:43 +02:00
|
|
|
@pytest.mark.parametrize('inp, enc, expected', [
|
|
|
|
('hello world', 'ascii', 'hello world'),
|
|
|
|
('hellö wörld', 'utf-8', 'hellö wörld'),
|
|
|
|
('hellö wörld', 'ascii', 'hell? w?rld'),
|
|
|
|
])
|
|
|
|
def test_force_encoding(inp, enc, expected):
|
|
|
|
assert utils.force_encoding(inp, enc) == expected
|
2015-01-03 17:50:59 +01:00
|
|
|
|
|
|
|
|
2015-10-30 21:08:37 +01:00
|
|
|
@pytest.mark.parametrize('inp, expected', [
|
|
|
|
('normal.txt', 'normal.txt'),
|
|
|
|
('user/repo issues.mht', 'user_repo issues.mht'),
|
|
|
|
('<Test\\File> - "*?:|', '_Test_File_ - _____'),
|
|
|
|
])
|
|
|
|
def test_sanitize_filename(inp, expected):
|
|
|
|
assert utils.sanitize_filename(inp) == expected
|
|
|
|
|
|
|
|
|
|
|
|
def test_sanitize_filename_empty_replacement():
|
|
|
|
name = '/<Bad File>/'
|
|
|
|
assert utils.sanitize_filename(name, replacement=None) == 'Bad File'
|
|
|
|
|
|
|
|
|
2015-04-04 19:01:58 +02:00
|
|
|
class TestNewestSlice:
|
2015-03-13 19:25:48 +01:00
|
|
|
|
|
|
|
"""Test newest_slice."""
|
|
|
|
|
|
|
|
def test_count_minus_two(self):
|
|
|
|
"""Test with a count of -2."""
|
2015-04-04 19:01:58 +02:00
|
|
|
with pytest.raises(ValueError):
|
2015-03-13 19:25:48 +01:00
|
|
|
utils.newest_slice([], -2)
|
|
|
|
|
2015-05-19 16:24:43 +02:00
|
|
|
@pytest.mark.parametrize('items, count, expected', [
|
|
|
|
# Count of -1 (all elements).
|
|
|
|
(range(20), -1, range(20)),
|
|
|
|
# Count of 0 (no elements).
|
|
|
|
(range(20), 0, []),
|
|
|
|
# Count which is much smaller than the iterable.
|
|
|
|
(range(20), 5, [15, 16, 17, 18, 19]),
|
|
|
|
# Count which is exactly one smaller."""
|
|
|
|
(range(5), 4, [1, 2, 3, 4]),
|
|
|
|
# Count which is just as large as the iterable."""
|
|
|
|
(range(5), 5, range(5)),
|
|
|
|
# Count which is one bigger than the iterable.
|
|
|
|
(range(5), 6, range(5)),
|
|
|
|
# Count which is much bigger than the iterable.
|
|
|
|
(range(5), 50, range(5)),
|
|
|
|
])
|
|
|
|
def test_good(self, items, count, expected):
|
|
|
|
"""Test slices which shouldn't raise an exception."""
|
|
|
|
sliced = utils.newest_slice(items, count)
|
|
|
|
assert list(sliced) == list(expected)
|