Skip to content

Commit 96803e0

Browse files
authored
Add meta test for new diff logic (#16211)
Follow up to #16112
1 parent bcd4ff2 commit 96803e0

File tree

2 files changed

+61
-9
lines changed

2 files changed

+61
-9
lines changed

mypy/test/helpers.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import shutil
99
import sys
1010
import time
11-
from typing import Any, Callable, Iterable, Iterator, Pattern
11+
from typing import IO, Any, Callable, Iterable, Iterator, Pattern
1212

1313
# Exporting Suite as alias to TestCase for backwards compatibility
1414
# TODO: avoid aliasing - import and subclass TestCase directly
@@ -70,7 +70,12 @@ def diff_ranges(
7070

7171

7272
def render_diff_range(
73-
ranges: list[tuple[int, int]], content: list[str], colour: str | None = None
73+
ranges: list[tuple[int, int]],
74+
content: list[str],
75+
*,
76+
colour: str | None = None,
77+
output: IO[str] = sys.stderr,
78+
indent: int = 2,
7479
) -> None:
7580
for i, line_range in enumerate(ranges):
7681
is_matching = i % 2 == 1
@@ -83,20 +88,20 @@ def render_diff_range(
8388
and j < len(lines) - 3
8489
):
8590
if j == 3:
86-
sys.stderr.write(" ...\n")
91+
output.write(" " * indent + "...\n")
8792
continue
8893

8994
if not is_matching and colour:
90-
sys.stderr.write(colour)
95+
output.write(colour)
9196

92-
sys.stderr.write(" " + line)
97+
output.write(" " * indent + line)
9398

9499
if not is_matching:
95100
if colour:
96-
sys.stderr.write("\033[0m")
97-
sys.stderr.write(" (diff)")
101+
output.write("\033[0m")
102+
output.write(" (diff)")
98103

99-
sys.stderr.write("\n")
104+
output.write("\n")
100105

101106

102107
def assert_string_arrays_equal(expected: list[str], actual: list[str], msg: str) -> None:
@@ -129,7 +134,7 @@ def assert_string_arrays_equal(expected: list[str], actual: list[str], msg: str)
129134

130135
sys.stderr.write(
131136
"Update the test output using --update-data -n0 "
132-
"(you can additionally use the -k selector to update only specific tests)"
137+
"(you can additionally use the -k selector to update only specific tests)\n"
133138
)
134139
pytest.fail(msg, pytrace=False)
135140

mypy/test/meta/test_diff_helper.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import io
2+
3+
from mypy.test.helpers import Suite, diff_ranges, render_diff_range
4+
5+
6+
class DiffHelperSuite(Suite):
7+
def test_render_diff_range(self) -> None:
8+
expected = ["hello", "world"]
9+
actual = ["goodbye", "world"]
10+
11+
expected_ranges, actual_ranges = diff_ranges(expected, actual)
12+
13+
output = io.StringIO()
14+
render_diff_range(expected_ranges, expected, output=output)
15+
assert output.getvalue() == " hello (diff)\n world\n"
16+
output = io.StringIO()
17+
render_diff_range(actual_ranges, actual, output=output)
18+
assert output.getvalue() == " goodbye (diff)\n world\n"
19+
20+
expected = ["a", "b", "c", "d", "e", "f", "g", "h", "circle", "i", "j"]
21+
actual = ["a", "b", "c", "d", "e", "f", "g", "h", "square", "i", "j"]
22+
23+
expected_ranges, actual_ranges = diff_ranges(expected, actual)
24+
25+
output = io.StringIO()
26+
render_diff_range(expected_ranges, expected, output=output, indent=0)
27+
assert output.getvalue() == "a\nb\nc\n...\nf\ng\nh\ncircle (diff)\ni\nj\n"
28+
output = io.StringIO()
29+
render_diff_range(actual_ranges, actual, output=output, indent=0)
30+
assert output.getvalue() == "a\nb\nc\n...\nf\ng\nh\nsquare (diff)\ni\nj\n"
31+
32+
def test_diff_ranges(self) -> None:
33+
a = ["hello", "world"]
34+
b = ["hello", "world"]
35+
36+
assert diff_ranges(a, b) == (
37+
[(0, 0), (0, 2), (2, 2), (2, 2)],
38+
[(0, 0), (0, 2), (2, 2), (2, 2)],
39+
)
40+
41+
a = ["hello", "world"]
42+
b = ["goodbye", "world"]
43+
44+
assert diff_ranges(a, b) == (
45+
[(0, 1), (1, 2), (2, 2), (2, 2)],
46+
[(0, 1), (1, 2), (2, 2), (2, 2)],
47+
)

0 commit comments

Comments
 (0)