Skip to content

Commit 8053a15

Browse files
committed
Use multiline repr when tests fail
1 parent f04f262 commit 8053a15

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

mypy/myunit/__init__.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,25 @@ def assert_false(b: bool, msg: str = None) -> None:
3939
if b:
4040
raise AssertionFailure(msg)
4141

42+
def good_repr(obj: object) -> str:
43+
if isinstance(obj, str):
44+
if obj.count('\n') > 1:
45+
bits = ["'''\\"]
46+
for line in obj.split('\n'):
47+
# force repr to use ' not ", then cut it off
48+
bits.append(repr('"' + line)[2:-1])
49+
bits[-1] += "'''"
50+
return '\n'.join(bits)
51+
return repr(obj)
4252

4353
def assert_equal(a: object, b: object, fmt: str = '{} != {}') -> None:
4454
if a != b:
45-
raise AssertionFailure(fmt.format(repr(a), repr(b)))
55+
raise AssertionFailure(fmt.format(good_repr(a), good_repr(b)))
4656

4757

4858
def assert_not_equal(a: object, b: object, fmt: str = '{} == {}') -> None:
4959
if a == b:
50-
raise AssertionFailure(fmt.format(repr(a), repr(b)))
60+
raise AssertionFailure(fmt.format(good_repr(a), good_repr(b)))
5161

5262

5363
def assert_raises(typ: type, *rest: Any) -> None:

0 commit comments

Comments
 (0)