Skip to content

Commit 264d26b

Browse files
committed
Merge pull request #71 from briancroom/allow-leading-trailing-whitespace
Allow differing leading/trailing whitespace when checking test output
2 parents 82986c7 + 2b32c53 commit 264d26b

File tree

6 files changed

+30
-16
lines changed

6 files changed

+30
-16
lines changed

Tests/Functional/ErrorHandling/main.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class ErrorHandling: XCTestCase {
3737
}
3838

3939
// CHECK: Test Case 'ErrorHandling.test_shouldButDoesNotThrowErrorInAssertion' started.
40-
// CHECK: .*/ErrorHandling/main.swift:\d+: error: ErrorHandling.test_shouldButDoesNotThrowErrorInAssertion : XCTAssertThrowsError failed: did not throw error -
40+
// CHECK: .*/ErrorHandling/main.swift:\d+: error: ErrorHandling.test_shouldButDoesNotThrowErrorInAssertion : XCTAssertThrowsError failed: did not throw error -
4141
// CHECK: Test Case 'ErrorHandling.test_shouldButDoesNotThrowErrorInAssertion' failed \(\d+\.\d+ seconds\).
4242
func test_shouldButDoesNotThrowErrorInAssertion() {
4343
XCTAssertThrowsError(try functionThatDoesNotThrowError())
@@ -60,7 +60,7 @@ class ErrorHandling: XCTestCase {
6060
}
6161

6262
// CHECK: Test Case 'ErrorHandling.test_throwsErrorInAssertionButFailsWhenCheckingError' started.
63-
// CHECK: .*/ErrorHandling/main.swift:\d+: error: ErrorHandling.test_throwsErrorInAssertionButFailsWhenCheckingError : XCTAssertEqual failed: \("Optional\("an error message"\)"\) is not equal to \("Optional\(""\)"\) -
63+
// CHECK: .*/ErrorHandling/main.swift:\d+: error: ErrorHandling.test_throwsErrorInAssertionButFailsWhenCheckingError : XCTAssertEqual failed: \("Optional\("an error message"\)"\) is not equal to \("Optional\(""\)"\) -
6464
// CHECK: Test Case 'ErrorHandling.test_throwsErrorInAssertionButFailsWhenCheckingError' failed \(\d+\.\d+ seconds\).
6565
func test_throwsErrorInAssertionButFailsWhenCheckingError() {
6666
XCTAssertThrowsError(try functionThatDoesThrowError()) { error in
@@ -77,7 +77,7 @@ class ErrorHandling: XCTestCase {
7777
}
7878

7979
// CHECK: Test Case 'ErrorHandling.test_canAndDoesThrowErrorFromTestMethod' started.
80-
// CHECK: \<EXPR\>:0: unexpected error: ErrorHandling.test_canAndDoesThrowErrorFromTestMethod : threw error "AnError\("an error message"\)" -
80+
// CHECK: \<EXPR\>:0: unexpected error: ErrorHandling.test_canAndDoesThrowErrorFromTestMethod : threw error "AnError\("an error message"\)" -
8181
// CHECK: Test Case 'ErrorHandling.test_canAndDoesThrowErrorFromTestMethod' failed \(\d+\.\d+ seconds\).
8282
func test_canAndDoesThrowErrorFromTestMethod() throws {
8383
try functionThatDoesThrowError()
@@ -94,7 +94,7 @@ class ErrorHandling: XCTestCase {
9494
}
9595

9696
// CHECK: Test Case 'ErrorHandling.test_assertionExpressionCanThrow' started.
97-
// CHECK: .*/ErrorHandling/main.swift:\d+: unexpected error: ErrorHandling.test_assertionExpressionCanThrow : XCTAssertEqual threw error "AnError\("did not actually return"\)" -
97+
// CHECK: .*/ErrorHandling/main.swift:\d+: unexpected error: ErrorHandling.test_assertionExpressionCanThrow : XCTAssertEqual threw error "AnError\("did not actually return"\)" -
9898
// CHECK: Test Case 'ErrorHandling.test_assertionExpressionCanThrow' failed \(\d+\.\d+ seconds\).
9999
func test_assertionExpressionCanThrow() {
100100
XCTAssertEqual(try functionThatShouldReturnButThrows(), 1)

Tests/Functional/SelectedTest/main.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
// RUN: %{built_tests_dir}/SelectedTest SelectedTest.ExecutedTestCase/test_foo > %T/one_test_method || true
33
// RUN: %{built_tests_dir}/SelectedTest SelectedTest.ExecutedTestCase > %T/one_test_case || true
44
// RUN: %{built_tests_dir}/SelectedTest > %T/all || true
5-
// RUN: %{xctest_checker} -p "// CHECK-METHOD: " %T/one_test_method %s
6-
// RUN: %{xctest_checker} -p "// CHECK-TESTCASE: " %T/one_test_case %s
7-
// RUN: %{xctest_checker} -p "// CHECK-ALL: " %T/all %s
5+
// RUN: %{xctest_checker} -p "// CHECK-METHOD:" %T/one_test_method %s
6+
// RUN: %{xctest_checker} -p "// CHECK-TESTCASE:" %T/one_test_case %s
7+
// RUN: %{xctest_checker} -p "// CHECK-ALL:" %T/all %s
88

99
#if os(Linux) || os(FreeBSD)
1010
import XCTest
@@ -44,11 +44,11 @@ class SkippedTestCase: XCTestCase {
4444
return [("test_baz", test_baz)]
4545
}
4646

47-
// CHECK-ALL: Test Case 'SkippedTestCase.test_baz' started.
48-
// CHECK-ALL: Test Case 'SkippedTestCase.test_baz' passed \(\d+\.\d+ seconds\).
47+
// CHECK-ALL: Test Case 'SkippedTestCase.test_baz' started.
48+
// CHECK-ALL: Test Case 'SkippedTestCase.test_baz' passed \(\d+\.\d+ seconds\).
4949
func test_baz() {}
5050
}
51-
// CHECK-ALL: Executed 1 test, with 0 failures \(0 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
51+
// CHECK-ALL: Executed 1 test, with 0 failures \(0 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
5252

5353
XCTMain([
5454
testCase(ExecutedTestCase.allTests),

Tests/Functional/SingleFailingTestCase/main.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class SingleFailingTestCase: XCTestCase {
1616
}
1717

1818
// CHECK: Test Case 'SingleFailingTestCase.test_fails' started.
19-
// CHECK: .*/SingleFailingTestCase/main.swift:22: error: SingleFailingTestCase.test_fails : XCTAssertTrue failed -
19+
// CHECK: .*/SingleFailingTestCase/main.swift:22: error: SingleFailingTestCase.test_fails : XCTAssertTrue failed -
2020
// CHECK: Test Case 'SingleFailingTestCase.test_fails' failed \(\d+\.\d+ seconds\).
2121
func test_fails() {
2222
XCTAssert(false)

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)