Skip to content

Commit 0503412

Browse files
committed
Allow differing leading/trailing whitespace when checking test output
1 parent 82986c7 commit 0503412

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

Tests/Functional/xctest_checker/tests/test_compare.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,15 @@ def test_includes_file_name_and_line_of_expected_in_error(self):
5454

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

57+
def test_matching_ignores_leading_and_trailing_whitespace(self):
58+
actual = _tmpfile('foo\nbar\nbaz\n')
59+
expected = _tmpfile('c: foo\nc: bar \nc: baz\n')
60+
compare.compare(actual, expected, check_prefix='c:')
61+
62+
def test_can_explicitly_match_leading_and_trailing_whitespace(self):
63+
actual = _tmpfile('foo\n bar\nbaz \n')
64+
expected = _tmpfile('c: foo\nc: ^ bar \nc: baz $\n')
65+
compare.compare(actual, expected, check_prefix='c:')
66+
5767
if __name__ == "__main__":
5868
unittest.main()

Tests/Functional/xctest_checker/xctest_checker/compare.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ def _expected_lines_and_line_numbers(path, check_prefix):
2828
with open(path) as f:
2929
for line_number, line in enumerate(f):
3030
if line.startswith(check_prefix):
31-
yield line[len(check_prefix):], line_number+1
31+
yield line[len(check_prefix):].strip(), line_number+1
3232

33+
def _add_whitespace_leniency(original_regex):
34+
return "^ *" + original_regex + " *$"
3335

3436
def compare(actual, expected, check_prefix):
3537
"""
@@ -52,7 +54,7 @@ def compare(actual, expected, check_prefix):
5254

5355
(expected_line, expectation_source_line_number) = expected_line_and_line_number
5456

55-
if not re.match(expected_line, actual_line):
57+
if not re.match(_add_whitespace_leniency(expected_line), actual_line):
5658
raise AssertionError('Actual line did not match the expected '
5759
'regular expression.\n'
5860
'{}:{}: Actual: {}\n'

Tests/Functional/xctest_checker/xctest_checker/main.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@ def main():
2323
parser.add_argument('expected', help='A path to a file containing the '
2424
'expected output of an XCTest run.')
2525
parser.add_argument('-p', '--check-prefix',
26-
default='// CHECK: ',
26+
default='// CHECK:',
2727
help='{prog} checks actual output against expected '
2828
'output. By default, {prog} only checks lines '
29-
'that are prefixed with "// CHECK: ". This '
29+
'that are prefixed with "// CHECK:". This '
3030
'option can be used to change that '
31-
'prefix.'.format(prog=parser.prog))
31+
'prefix. Leading and trailing whitespace is '
32+
'ignored unless the check line contains explicit '
33+
'^ or $ characters'.format(prog=parser.prog))
3234
args = parser.parse_args()
3335
compare.compare(args.actual, args.expected, args.check_prefix)
3436

0 commit comments

Comments
 (0)