Skip to content

Commit 4c01ccf

Browse files
committed
Emit the file name and line number of failing expectations in the functional tests.
This allows Xcode to highlight the failing line in the editor.
1 parent 5a564c5 commit 4c01ccf

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

Tests/Functional/xctest_checker/tests/test_compare.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ def test_match_does_not_raise(self):
3636
expected = _tmpfile('c: foo\nc: bar\nc: baz\n')
3737
compare.compare(actual, expected, check_prefix='c: ')
3838

39+
def test_includes_file_name_and_line_of_expected_in_error(self):
40+
actual = _tmpfile('foo\nbar\nbaz\n')
41+
expected = _tmpfile('c: foo\nc: baz\nc: bar\n')
42+
with self.assertRaises(AssertionError) as cm:
43+
compare.compare(actual, expected, check_prefix='c: ')
44+
45+
self.assertIn("{}:{}:".format(expected, 2), cm.exception.message)
3946

4047
if __name__ == "__main__":
4148
unittest.main()

Tests/Functional/xctest_checker/xctest_checker/compare.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ def _actual_lines(path):
1010
yield line
1111

1212

13-
def _expected_lines(path, check_prefix):
13+
def _expected_lines_and_line_numbers(path, check_prefix):
1414
"""
1515
Returns a generator that yields each line in the file at the given path
1616
that begins with the given prefix.
1717
"""
1818
with open(path) as f:
19-
for line in f:
19+
for line_number, line in enumerate(f):
2020
if line.startswith(check_prefix):
21-
yield line[len(check_prefix):]
21+
yield line[len(check_prefix):], line_number+1
2222

2323

2424
def compare(actual, expected, check_prefix):
@@ -28,19 +28,23 @@ def compare(actual, expected, check_prefix):
2828
file, raises an AssertionError. Also raises an AssertionError if the number
2929
of lines in the two files differ.
3030
"""
31-
for actual_line, expected_line in map(
31+
for actual_line, expected_line_and_line_number in map(
3232
None,
3333
_actual_lines(actual),
34-
_expected_lines(expected, check_prefix)):
34+
_expected_lines_and_line_numbers(expected, check_prefix)):
35+
3536
if actual_line is None:
3637
raise AssertionError('There were more lines expected to appear '
3738
'than there were lines in the actual input.')
38-
if expected_line is None:
39+
if expected_line_and_line_number is None:
3940
raise AssertionError('There were more lines than expected to '
4041
'appear.')
42+
43+
(expected_line, expectation_source_line_number) = expected_line_and_line_number
44+
4145
if not re.match(expected_line, actual_line):
4246
raise AssertionError('Actual line did not match the expected '
4347
'regular expression.\n'
44-
'Actual: {}\n'
48+
'{}:{}: Actual: {}\n'
4549
'Expected: {}\n'.format(
46-
repr(actual_line), repr(expected_line)))
50+
expected, expectation_source_line_number, repr(actual_line), repr(expected_line)))

0 commit comments

Comments
 (0)