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
|
|
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
2014-08-26 20:25:11 +02:00
|
|
|
from qutebrowser.utils import utils, qtutils
|
2014-05-05 12:16:12 +02:00
|
|
|
|
|
|
|
|
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-04-04 19:01:58 +02:00
|
|
|
class TestEliding:
|
2014-05-16 07:46:56 +02:00
|
|
|
|
|
|
|
"""Test elide."""
|
|
|
|
|
|
|
|
ELLIPSIS = '\u2026'
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
def test_length_one(self):
|
2014-05-27 13:06:13 +02:00
|
|
|
"""Test eliding to 1 char which should yield ..."""
|
2015-04-04 19:01:58 +02:00
|
|
|
assert utils.elide('foo', 1) == self.ELLIPSIS
|
2014-05-16 07:46:56 +02:00
|
|
|
|
|
|
|
def test_fits(self):
|
2014-05-27 13:06:13 +02:00
|
|
|
"""Test eliding with a string which fits exactly."""
|
2015-04-04 19:01:58 +02:00
|
|
|
assert utils.elide('foo', 3) == 'foo'
|
2014-05-16 07:46:56 +02:00
|
|
|
|
|
|
|
def test_elided(self):
|
2014-05-27 13:06:13 +02:00
|
|
|
"""Test eliding with a string which should get elided."""
|
2015-04-04 19:01:58 +02:00
|
|
|
assert utils.elide('foobar', 3) == 'fo' + self.ELLIPSIS
|
2014-05-16 07:46:56 +02:00
|
|
|
|
|
|
|
|
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-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-04-04 19:01:58 +02:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def setup(self):
|
2014-07-15 11:06:56 +02:00
|
|
|
self.white = Color('white')
|
|
|
|
self.black = Color('black')
|
2014-06-12 21:43:30 +02:00
|
|
|
|
|
|
|
def test_invalid_start(self):
|
|
|
|
"""Test an invalid start color."""
|
2015-04-04 19:01:58 +02:00
|
|
|
with pytest.raises(qtutils.QtValueError):
|
2014-07-15 11:06:56 +02:00
|
|
|
utils.interpolate_color(Color(), self.white, 0)
|
2014-06-12 21:43:30 +02:00
|
|
|
|
|
|
|
def test_invalid_end(self):
|
|
|
|
"""Test an invalid end color."""
|
2015-04-04 19:01:58 +02:00
|
|
|
with pytest.raises(qtutils.QtValueError):
|
2014-07-15 11:06:56 +02:00
|
|
|
utils.interpolate_color(self.white, Color(), 0)
|
2014-06-12 21:43:30 +02:00
|
|
|
|
|
|
|
def test_invalid_percentage(self):
|
|
|
|
"""Test an invalid percentage."""
|
2015-04-04 19:01:58 +02:00
|
|
|
with pytest.raises(ValueError):
|
2014-06-12 21:43:30 +02:00
|
|
|
utils.interpolate_color(self.white, self.white, -1)
|
2015-04-04 19:01:58 +02:00
|
|
|
with pytest.raises(ValueError):
|
2014-06-12 21:43:30 +02:00
|
|
|
utils.interpolate_color(self.white, self.white, 101)
|
|
|
|
|
|
|
|
def test_invalid_colorspace(self):
|
|
|
|
"""Test an invalid colorspace."""
|
2015-04-04 19:01:58 +02:00
|
|
|
with pytest.raises(ValueError):
|
2014-06-12 21:43:30 +02:00
|
|
|
utils.interpolate_color(self.white, self.black, 10, QColor.Cmyk)
|
|
|
|
|
|
|
|
def test_valid_percentages_rgb(self):
|
|
|
|
"""Test 0% and 100% in the RGB colorspace."""
|
|
|
|
white = utils.interpolate_color(self.white, self.black, 0, QColor.Rgb)
|
|
|
|
black = utils.interpolate_color(self.white, self.black, 100,
|
|
|
|
QColor.Rgb)
|
2015-04-04 19:01:58 +02:00
|
|
|
assert Color(white) == self.white
|
|
|
|
assert Color(black) == self.black
|
2014-06-12 21:43:30 +02:00
|
|
|
|
|
|
|
def test_valid_percentages_hsv(self):
|
|
|
|
"""Test 0% and 100% in the HSV colorspace."""
|
|
|
|
white = utils.interpolate_color(self.white, self.black, 0, QColor.Hsv)
|
|
|
|
black = utils.interpolate_color(self.white, self.black, 100,
|
|
|
|
QColor.Hsv)
|
2015-04-04 19:01:58 +02:00
|
|
|
assert Color(white) == self.white
|
|
|
|
assert Color(black) == self.black
|
2014-06-12 21:43:30 +02:00
|
|
|
|
|
|
|
def test_valid_percentages_hsl(self):
|
|
|
|
"""Test 0% and 100% in the HSL colorspace."""
|
|
|
|
white = utils.interpolate_color(self.white, self.black, 0, QColor.Hsl)
|
|
|
|
black = utils.interpolate_color(self.white, self.black, 100,
|
|
|
|
QColor.Hsl)
|
2015-04-04 19:01:58 +02:00
|
|
|
assert Color(white) == self.white
|
|
|
|
assert Color(black) == self.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-04-04 19:01:58 +02:00
|
|
|
class TestFormatSeconds:
|
2014-06-12 23:29:34 +02:00
|
|
|
|
|
|
|
"""Tests for format_seconds.
|
|
|
|
|
|
|
|
Class attributes:
|
|
|
|
TESTS: A list of (input, output) tuples.
|
|
|
|
"""
|
|
|
|
|
|
|
|
TESTS = [
|
|
|
|
(-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'),
|
|
|
|
]
|
|
|
|
|
2015-04-04 19:01:58 +02:00
|
|
|
@pytest.mark.parametrize('seconds, out', TESTS)
|
|
|
|
def test_format_seconds(self, seconds, out):
|
2014-06-12 23:29:34 +02:00
|
|
|
"""Test format_seconds with several tests."""
|
2015-04-04 19:01:58 +02:00
|
|
|
assert utils.format_seconds(seconds) == out
|
2014-06-12 23:29:34 +02:00
|
|
|
|
|
|
|
|
2015-04-04 19:01:58 +02:00
|
|
|
class TestFormatTimedelta:
|
2015-03-22 22:39:56 +01:00
|
|
|
|
|
|
|
"""Tests for format_timedelta.
|
|
|
|
|
|
|
|
Class attributes:
|
|
|
|
TESTS: A list of (input, output) tuples.
|
|
|
|
"""
|
|
|
|
|
|
|
|
TESTS = [
|
|
|
|
(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'),
|
|
|
|
]
|
|
|
|
|
2015-04-04 19:01:58 +02:00
|
|
|
@pytest.mark.parametrize('td, out', TESTS)
|
|
|
|
def test_format_seconds(self, td, out):
|
2015-03-22 22:39:56 +01:00
|
|
|
"""Test format_seconds with several tests."""
|
2015-04-04 19:01:58 +02:00
|
|
|
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
|
|
|
|
|
|
|
"""Test key_to_string."""
|
|
|
|
|
|
|
|
def test_unicode_garbage_keys(self):
|
|
|
|
"""Test a special key where QKeyEvent::toString works incorrectly."""
|
2015-04-04 19:01:58 +02:00
|
|
|
assert utils.key_to_string(Qt.Key_Blue) == 'Blue'
|
2014-07-04 07:35:33 +02:00
|
|
|
|
|
|
|
def test_backtab(self):
|
|
|
|
"""Test if backtab is normalized to tab correctly."""
|
2015-04-04 19:01:58 +02:00
|
|
|
assert utils.key_to_string(Qt.Key_Backtab) == 'Tab'
|
2014-07-04 07:35:33 +02:00
|
|
|
|
|
|
|
def test_escape(self):
|
|
|
|
"""Test if escape is normalized to escape correctly."""
|
2015-04-04 19:01:58 +02:00
|
|
|
assert utils.key_to_string(Qt.Key_Escape) == 'Escape'
|
2014-07-04 07:35:33 +02:00
|
|
|
|
|
|
|
def test_letter(self):
|
|
|
|
"""Test a simple letter key."""
|
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
|
|
|
|
|
|
|
def test_unicode(self):
|
|
|
|
"""Test a printable unicode key."""
|
2015-04-04 19:01:58 +02:00
|
|
|
assert utils.key_to_string(Qt.Key_degree) == '°'
|
2014-07-04 07:35:33 +02:00
|
|
|
|
|
|
|
def test_special(self):
|
|
|
|
"""Test a non-printable key handled by QKeyEvent::toString."""
|
2015-04-04 19:01:58 +02:00
|
|
|
assert utils.key_to_string(Qt.Key_F1) == 'F1'
|
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)
|
|
|
|
assert utils.keyevent_to_string(evt) == 'Ctrl+A'
|
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):
|
2014-07-04 07:35:33 +02:00
|
|
|
"""Test with key and multiple modifier 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))
|
2014-09-26 07:14:44 +02:00
|
|
|
if sys.platform == 'darwin':
|
2015-04-04 19:01:58 +02:00
|
|
|
assert utils.keyevent_to_string(evt) == 'Ctrl+Alt+Shift+A'
|
2014-09-26 07:14:44 +02:00
|
|
|
else:
|
2015-04-04 19:01:58 +02:00
|
|
|
assert utils.keyevent_to_string(evt) == 'Ctrl+Alt+Meta+Shift+A'
|
2014-07-04 07:35:33 +02:00
|
|
|
|
|
|
|
|
2015-04-04 19:01:58 +02:00
|
|
|
class TestNormalize:
|
2014-07-03 07:41:23 +02:00
|
|
|
|
2014-07-04 07:35:33 +02:00
|
|
|
"""Test normalize_keystr."""
|
2014-07-03 07:41:23 +02:00
|
|
|
|
2015-04-04 19:01:58 +02:00
|
|
|
STRINGS = (
|
|
|
|
('Control+x', 'ctrl+x'),
|
|
|
|
('Windows+x', 'meta+x'),
|
|
|
|
('Mod1+x', 'alt+x'),
|
|
|
|
('Mod4+x', 'meta+x'),
|
|
|
|
('Control--', 'ctrl+-'),
|
|
|
|
('Windows++', 'meta++'),
|
|
|
|
)
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('orig, repl', STRINGS)
|
|
|
|
def test_normalize(self, orig, repl):
|
2014-07-03 07:41:23 +02:00
|
|
|
"""Test normalize with some strings."""
|
2015-04-04 19:01:58 +02:00
|
|
|
assert utils.normalize_keystr(orig) == repl
|
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-04-04 19:01:58 +02:00
|
|
|
class TestForceEncoding:
|
2015-01-03 17:50:59 +01:00
|
|
|
|
|
|
|
"""Test force_encoding."""
|
|
|
|
|
2015-04-04 19:01:58 +02:00
|
|
|
TESTS = [
|
|
|
|
('hello world', 'ascii', 'hello world'),
|
|
|
|
('hellö wörld', 'utf-8', 'hellö wörld'),
|
|
|
|
('hellö wörld', 'ascii', 'hell? w?rld'),
|
|
|
|
]
|
2015-01-03 17:50:59 +01:00
|
|
|
|
2015-04-04 19:01:58 +02:00
|
|
|
@pytest.mark.parametrize('inp, enc, expected', TESTS)
|
|
|
|
def test_fitting_ascii(self, inp, enc, expected):
|
|
|
|
"""Test force_encoding will yield expected text."""
|
|
|
|
assert utils.force_encoding(inp, enc) == expected
|
2015-01-03 17:50:59 +01:00
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
def test_count_minus_one(self):
|
|
|
|
"""Test with a count of -1 (all elements)."""
|
|
|
|
items = range(20)
|
|
|
|
sliced = utils.newest_slice(items, -1)
|
2015-04-04 19:01:58 +02:00
|
|
|
assert list(sliced) == list(items)
|
2015-03-13 19:25:48 +01:00
|
|
|
|
|
|
|
def test_count_zero(self):
|
|
|
|
"""Test with a count of 0 (no elements)."""
|
|
|
|
items = range(20)
|
|
|
|
sliced = utils.newest_slice(items, 0)
|
2015-04-04 19:01:58 +02:00
|
|
|
assert list(sliced) == []
|
2015-03-13 19:25:48 +01:00
|
|
|
|
|
|
|
def test_count_much_smaller(self):
|
|
|
|
"""Test with a count which is much smaller than the iterable."""
|
|
|
|
items = range(20)
|
|
|
|
sliced = utils.newest_slice(items, 5)
|
2015-04-04 19:01:58 +02:00
|
|
|
assert list(sliced) == [15, 16, 17, 18, 19]
|
2015-03-13 19:25:48 +01:00
|
|
|
|
|
|
|
def test_count_smaller(self):
|
|
|
|
"""Test with a count which is exactly one smaller."""
|
|
|
|
items = range(5)
|
|
|
|
sliced = utils.newest_slice(items, 4)
|
2015-04-04 19:01:58 +02:00
|
|
|
assert list(sliced) == [1, 2, 3, 4]
|
2015-03-13 19:25:48 +01:00
|
|
|
|
|
|
|
def test_count_equal(self):
|
|
|
|
"""Test with a count which is just as large as the iterable."""
|
|
|
|
items = range(5)
|
|
|
|
sliced = utils.newest_slice(items, 5)
|
2015-04-04 19:01:58 +02:00
|
|
|
assert list(sliced) == list(items)
|
2015-03-13 19:25:48 +01:00
|
|
|
|
|
|
|
def test_count_bigger(self):
|
|
|
|
"""Test with a count which is one bigger than the iterable."""
|
|
|
|
items = range(5)
|
|
|
|
sliced = utils.newest_slice(items, 6)
|
2015-04-04 19:01:58 +02:00
|
|
|
assert list(sliced) == list(items)
|
2015-03-13 19:25:48 +01:00
|
|
|
|
|
|
|
def test_count_much_bigger(self):
|
|
|
|
"""Test with a count which is much bigger than the iterable."""
|
|
|
|
items = range(5)
|
|
|
|
sliced = utils.newest_slice(items, 50)
|
2015-04-04 19:01:58 +02:00
|
|
|
assert list(sliced) == list(items)
|