Skip to content

Commit 82ccd73

Browse files
committed
Add non-throwing testCase() wrapper function.
- This is useful in clients which would like to use Swift type inference to define the `allTests` array. If none of the tests in the class throw, Swift will infer the type of that array as `(String, (T) -> () -> ())` which is not convertible to the type expected by the `testCase` method. - Resolves: https://bugs.swift.org/browse/SR-1589
1 parent 2ce4b57 commit 82ccd73

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

Sources/XCTest/XCTestCase.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,20 @@ private func test<T: XCTestCase>(_ testFunc: (T) -> () throws -> Void) -> (XCTes
178178
try testFunc(testCase)()
179179
}
180180
}
181+
182+
/// Wrapper function for the non-throwing variant of tests.
183+
/// - seealso: `XCTMain`
184+
public func testCase<T: XCTestCase>(_ allTests: [(String, (T) -> () -> Void)]) -> XCTestCaseEntry {
185+
let tests: [(String, (XCTestCase) throws -> Void)] = allTests.map { ($0.0, test($0.1)) }
186+
return (T.self, tests)
187+
}
188+
189+
private func test<T: XCTestCase>(_ testFunc: (T) -> () -> Void) -> (XCTestCase) throws -> Void {
190+
return { testCaseType in
191+
guard let testCase = testCaseType as? T else {
192+
fatalError("Attempt to invoke test on class \(T.self) with incompatible instance type \(testCaseType.dynamicType)")
193+
}
194+
195+
try testFunc(testCase)()
196+
}
197+
}

0 commit comments

Comments
 (0)