Skip to content

Allow Xcode to highlight failing functional test expectations #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 13, 2016
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
7 changes: 7 additions & 0 deletions Tests/Functional/xctest_checker/tests/test_compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ def test_match_does_not_raise(self):
expected = _tmpfile('c: foo\nc: bar\nc: baz\n')
compare.compare(actual, expected, check_prefix='c: ')

def test_includes_file_name_and_line_of_expected_in_error(self):
actual = _tmpfile('foo\nbar\nbaz\n')
expected = _tmpfile('c: foo\nc: baz\nc: bar\n')
with self.assertRaises(AssertionError) as cm:
compare.compare(actual, expected, check_prefix='c: ')

self.assertIn("{}:{}:".format(expected, 2), cm.exception.message)

if __name__ == "__main__":
unittest.main()
20 changes: 12 additions & 8 deletions Tests/Functional/xctest_checker/xctest_checker/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ def _actual_lines(path):
yield line


def _expected_lines(path, check_prefix):
def _expected_lines_and_line_numbers(path, check_prefix):
"""
Returns a generator that yields each line in the file at the given path
that begins with the given prefix.
"""
with open(path) as f:
for line in f:
for line_number, line in enumerate(f):
if line.startswith(check_prefix):
yield line[len(check_prefix):]
yield line[len(check_prefix):], line_number+1


def compare(actual, expected, check_prefix):
Expand All @@ -28,19 +28,23 @@ def compare(actual, expected, check_prefix):
file, raises an AssertionError. Also raises an AssertionError if the number
of lines in the two files differ.
"""
for actual_line, expected_line in map(
for actual_line, expected_line_and_line_number in map(
None,
_actual_lines(actual),
_expected_lines(expected, check_prefix)):
_expected_lines_and_line_numbers(expected, check_prefix)):

if actual_line is None:
raise AssertionError('There were more lines expected to appear '
'than there were lines in the actual input.')
if expected_line is None:
if expected_line_and_line_number is None:
raise AssertionError('There were more lines than expected to '
'appear.')

(expected_line, expectation_source_line_number) = expected_line_and_line_number

if not re.match(expected_line, actual_line):
raise AssertionError('Actual line did not match the expected '
'regular expression.\n'
'Actual: {}\n'
'{}:{}: Actual: {}\n'
'Expected: {}\n'.format(
repr(actual_line), repr(expected_line)))
expected, expectation_source_line_number, repr(actual_line), repr(expected_line)))