Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,23 @@ module = [
]
ignore_missing_imports = true

[[tool.mypy.overrides]]
module = "fixtures.*"
ignore_missing_imports = true
follow_imports = "skip"

[[tool.mypy.overrides]]
module = [
# FIXME(stephenfin): We would like to remove all modules from this list
# except tests (we're not sadists)
"testtools.assertions",
"testtools.compat",
"testtools.content",
"testtools.content_type",
"testtools.matchers.*",
"testtools.monkey",
"testtools.run",
"testtools.runtest",
"testtools.testcase",
"testtools.testresult.*",
"testtools.testsuite",
"testtools.twistedsupport.*",
"tests.*",
]
Expand Down
31 changes: 14 additions & 17 deletions tests/matchers/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
from typing import ClassVar

from testtools import TestCase
from testtools.compat import (
_b,
text_repr,
)
from testtools.compat import text_repr
from testtools.matchers._basic import (
Contains,
DoesNotEndWith,
Expand Down Expand Up @@ -36,7 +33,7 @@ class Test_BinaryMismatch(TestCase):
"""Mismatches from binary comparisons need useful describe output"""

_long_string = "This is a longish multiline non-ascii string\n\xa7"
_long_b = _b(_long_string)
_long_b = _long_string.encode("utf-8")
_long_u = _long_string

class CustomRepr:
Expand All @@ -52,12 +49,12 @@ def test_short_objects(self):
self.assertEqual(mismatch.describe(), f"{o1!r} !~ {o2!r}")

def test_short_mixed_strings(self):
b, u = _b("\xa7"), "\xa7"
b, u = b"\xa7", "\xa7"
mismatch = _BinaryMismatch(b, "!~", u)
self.assertEqual(mismatch.describe(), f"{b!r} !~ {u!r}")

def test_long_bytes(self):
one_line_b = self._long_b.replace(_b("\n"), _b(" "))
one_line_b = self._long_b.replace(b"\n", b" ")
mismatch = _BinaryMismatch(one_line_b, "!~", self._long_b)
self.assertEqual(
mismatch.describe(),
Expand Down Expand Up @@ -249,8 +246,8 @@ def test_describe_non_ascii_unicode(self):
)

def test_describe_non_ascii_bytes(self):
string = _b("A\xa7")
suffix = _b("B\xa7")
string = b"A\xa7"
suffix = b"B\xa7"
mismatch = DoesNotStartWith(string, suffix)
self.assertEqual(
f"{string!r} does not start with {suffix!r}.", mismatch.describe()
Expand All @@ -265,7 +262,7 @@ def test_str(self):
self.assertEqual("StartsWith('bar')", str(matcher))

def test_str_with_bytes(self):
b = _b("\xa7")
b = b"\xa7"
matcher = StartsWith(b)
self.assertEqual(f"StartsWith({b!r})", str(matcher))

Expand Down Expand Up @@ -310,8 +307,8 @@ def test_describe_non_ascii_unicode(self):
)

def test_describe_non_ascii_bytes(self):
string = _b("A\xa7")
suffix = _b("B\xa7")
string = b"A\xa7"
suffix = b"B\xa7"
mismatch = DoesNotEndWith(string, suffix)
self.assertEqual(
f"{string!r} does not end with {suffix!r}.", mismatch.describe()
Expand All @@ -326,7 +323,7 @@ def test_str(self):
self.assertEqual("EndsWith('bar')", str(matcher))

def test_str_with_bytes(self):
b = _b("\xa7")
b = b"\xa7"
matcher = EndsWith(b)
self.assertEqual(f"EndsWith({b!r})", str(matcher))

Expand Down Expand Up @@ -416,17 +413,17 @@ class TestMatchesRegex(TestCase, TestMatchersInterface):
("MatchesRegex('a|b')", MatchesRegex("a|b")),
("MatchesRegex('a|b', re.M)", MatchesRegex("a|b", re.M)),
("MatchesRegex('a|b', re.I|re.M)", MatchesRegex("a|b", re.I | re.M)),
("MatchesRegex({!r})".format(_b("\xa7")), MatchesRegex(_b("\xa7"))),
("MatchesRegex({!r})".format(b"\xa7"), MatchesRegex(b"\xa7")),
("MatchesRegex({!r})".format("\xa7"), MatchesRegex("\xa7")),
]

describe_examples: ClassVar = [
("'c' does not match /a|b/", "c", MatchesRegex("a|b")),
("'c' does not match /a\\d/", "c", MatchesRegex(r"a\d")),
(
"{!r} does not match /\\s+\\xa7/".format(_b("c")),
_b("c"),
MatchesRegex(_b("\\s+\xa7")),
"{!r} does not match /\\s+\\xa7/".format(b"c"),
b"c",
MatchesRegex(b"\\s+\xa7"),
),
("{!r} does not match /\\s+\\xa7/".format("c"), "c", MatchesRegex("\\s+\xa7")),
]
Expand Down
5 changes: 1 addition & 4 deletions tests/matchers/test_doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
from typing import ClassVar

from testtools import TestCase
from testtools.compat import (
_b,
)
from testtools.matchers._doctest import DocTestMatches

from ..helpers import FullStackRunTest
Expand Down Expand Up @@ -75,7 +72,7 @@ def test_describe_non_ascii_bytes(self):
permits arbitrary binary inputs. This is a slightly bogus thing to do,
and under Python 3 using bytes objects will reasonably raise an error.
"""
header = _b("\x89PNG\r\n\x1a\n...")
header = b"\x89PNG\r\n\x1a\n..."
self.assertRaises(TypeError, DocTestMatches, header, doctest.ELLIPSIS)


Expand Down
66 changes: 13 additions & 53 deletions tests/test_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@
import ast
import io
import sys
import traceback

import testtools
from testtools.compat import (
_b,
reraise,
text_repr,
unicode_output_stream,
)
Expand Down Expand Up @@ -45,28 +42,28 @@ def test_no_encoding_becomes_ascii(self):
"""A stream with no encoding attribute gets ascii/replace strings"""
sout = _FakeOutputStream()
unicode_output_stream(sout).write(self.uni)
self.assertEqual([_b("pa???n")], sout.writelog)
self.assertEqual([b"pa???n"], sout.writelog)

def test_encoding_as_none_becomes_ascii(self):
"""A stream with encoding value of None gets ascii/replace strings"""
sout = _FakeOutputStream()
sout.encoding = None
unicode_output_stream(sout).write(self.uni)
self.assertEqual([_b("pa???n")], sout.writelog)
self.assertEqual([b"pa???n"], sout.writelog)

def test_bogus_encoding_becomes_ascii(self):
"""A stream with a bogus encoding gets ascii/replace strings"""
sout = _FakeOutputStream()
sout.encoding = "bogus"
unicode_output_stream(sout).write(self.uni)
self.assertEqual([_b("pa???n")], sout.writelog)
self.assertEqual([b"pa???n"], sout.writelog)

def test_partial_encoding_replace(self):
"""A string which can be partly encoded correctly should be"""
sout = _FakeOutputStream()
sout.encoding = "iso-8859-7"
unicode_output_stream(sout).write(self.uni)
self.assertEqual([_b("pa?\xe8?n")], sout.writelog)
self.assertEqual([b"pa?\xe8?n"], sout.writelog)

def test_stringio(self):
"""A StringIO object should maybe get an ascii native str type"""
Expand Down Expand Up @@ -126,11 +123,11 @@ class TestTextRepr(testtools.TestCase):

# Bytes with the high bit set should always be escaped
bytes_examples = (
(_b("\x80"), "'\\x80'", "'''\\\n\\x80'''"),
(_b("\xa0"), "'\\xa0'", "'''\\\n\\xa0'''"),
(_b("\xc0"), "'\\xc0'", "'''\\\n\\xc0'''"),
(_b("\xff"), "'\\xff'", "'''\\\n\\xff'''"),
(_b("\xc2\xa7"), "'\\xc2\\xa7'", "'''\\\n\\xc2\\xa7'''"),
(b"\x80", "'\\x80'", "'''\\\n\\x80'''"),
(b"\xa0", "'\\xa0'", "'''\\\n\\xa0'''"),
(b"\xc0", "'\\xc0'", "'''\\\n\\xc0'''"),
(b"\xff", "'\\xff'", "'''\\\n\\xff'''"),
(b"\xc2\xa7", "'\\xc2\\xa7'", "'''\\\n\\xc2\\xa7'''"),
)

# Unicode doesn't escape printable characters as per the Python 3 model
Expand All @@ -153,12 +150,12 @@ class TestTextRepr(testtools.TestCase):
# Unprintable general categories not fully tested: Cc, Cf, Co, Cn, Zs
)

b_prefix = repr(_b(""))[:-2]
b_prefix = repr(b"")[:-2]
u_prefix = repr("")[:-2]

def test_ascii_examples_oneline_bytes(self):
for s, expected, _ in self.ascii_examples:
b = _b(s)
b = s.encode("utf-8")
actual = text_repr(b, multiline=False)
# Add self.assertIsInstance check?
self.assertEqual(actual, self.b_prefix + expected)
Expand All @@ -173,7 +170,7 @@ def test_ascii_examples_oneline_unicode(self):

def test_ascii_examples_multiline_bytes(self):
for s, _, expected in self.ascii_examples:
b = _b(s)
b = s.encode("utf-8")
actual = text_repr(b, multiline=True)
self.assertEqual(actual, self.b_prefix + expected)
self.assertEqual(ast.literal_eval(actual), b)
Expand All @@ -187,7 +184,7 @@ def test_ascii_examples_multiline_unicode(self):
def test_ascii_examples_defaultline_bytes(self):
for s, one, multi in self.ascii_examples:
expected = ("\n" in s and multi) or one
self.assertEqual(text_repr(_b(s)), self.b_prefix + expected)
self.assertEqual(text_repr(s.encode("utf-8")), self.b_prefix + expected)

def test_ascii_examples_defaultline_unicode(self):
for s, one, multi in self.ascii_examples:
Expand Down Expand Up @@ -219,43 +216,6 @@ def test_unicode_examples_multiline(self):
self.assertEqual(ast.literal_eval(actual), u)


class TestReraise(testtools.TestCase):
"""Tests for trivial reraise wrapper needed for Python 2/3 changes"""

def test_exc_info(self):
"""After reraise exc_info matches plus some extra traceback"""
try:
raise ValueError("Bad value")
except ValueError:
_exc_info = sys.exc_info()
try:
reraise(*_exc_info)
except ValueError:
_new_exc_info = sys.exc_info()
self.assertIs(_exc_info[0], _new_exc_info[0])
self.assertIs(_exc_info[1], _new_exc_info[1])
expected_tb = traceback.extract_tb(_exc_info[2])
self.assertEqual(
expected_tb, traceback.extract_tb(_new_exc_info[2])[-len(expected_tb) :]
)

def test_custom_exception_no_args(self):
"""Reraising does not require args attribute to contain params"""

class CustomException(Exception):
"""Exception that expects and sets attrs but not args"""

def __init__(self, value):
Exception.__init__(self)
self.value = value

try:
raise CustomException("Some value")
except CustomException:
_exc_info = sys.exc_info()
self.assertRaises(CustomException, reraise, *_exc_info)


def test_suite():
from unittest import TestLoader

Expand Down
Loading