Skip to content

Commit f01b3e2

Browse files
committed
Merge pull request #80 from marcusrossel/patch-1
Various minor changes to XCTest-files.
2 parents 2037d73 + e0f34a7 commit f01b3e2

File tree

4 files changed

+34
-49
lines changed

4 files changed

+34
-49
lines changed

Sources/XCTest/TestFiltering.swift

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
//
99
//
1010
// XCTestFiltering.swift
11-
// This provides utilities for executing only a subset of the tests provided to XCTMain
11+
// This provides utilities for executing only a subset of the tests provided to `XCTMain`
1212
//
1313

1414
internal typealias TestFilter = (XCTestCase.Type, String) -> Bool
@@ -21,15 +21,10 @@ internal struct TestFiltering {
2121
}
2222

2323
var selectedTestFilter: TestFilter {
24-
if let selectedTestName = selectedTestName {
25-
if let selectedTest = SelectedTest(selectedTestName: selectedTestName) {
26-
return selectedTest.matches
27-
} else {
28-
return excludeAllFilter()
29-
}
30-
} else {
31-
return includeAllFilter()
32-
}
24+
guard let selectedTestName = selectedTestName else { return includeAllFilter() }
25+
guard let selectedTest = SelectedTest(selectedTestName: selectedTestName) else { return excludeAllFilter() }
26+
27+
return selectedTest.matches
3328
}
3429

3530
private func excludeAllFilter() -> TestFilter {
@@ -42,12 +37,12 @@ internal struct TestFiltering {
4237

4338
static func filterTests(entries: [XCTestCaseEntry], filter: TestFilter) -> [XCTestCaseEntry] {
4439
return entries
45-
.map({ testCase, tests in
46-
return (testCase, tests.filter({ filter(testCase, $0.0) }))
47-
})
48-
.filter({ testCase, tests in
40+
.map { testCase, tests in
41+
return (testCase, tests.filter { filter(testCase, $0.0) } )
42+
}
43+
.filter { testCase, tests in
4944
return !tests.isEmpty
50-
})
45+
}
5146
}
5247
}
5348

Sources/XCTest/XCTAssert.swift

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,25 +52,19 @@ private enum _XCTAssertionResult {
5252
case UnexpectedFailure(ErrorProtocol)
5353

5454
var expected: Bool {
55-
switch (self) {
56-
case .UnexpectedFailure(_):
57-
return false
58-
default:
59-
return true
55+
switch self {
56+
case .UnexpectedFailure(_): return false
57+
default: return true
6058
}
6159
}
6260

6361
func failureDescription(assertion: _XCTAssertion) -> String {
6462
let explanation: String
65-
switch (self) {
66-
case .Success:
67-
explanation = "passed"
68-
case .ExpectedFailure(let details?):
69-
explanation = "failed: \(details)"
70-
case .ExpectedFailure(_):
71-
explanation = "failed"
72-
case .UnexpectedFailure(let error):
73-
explanation = "threw error \"\(error)\""
63+
switch self {
64+
case .Success: explanation = "passed"
65+
case .ExpectedFailure(let details?): explanation = "failed: \(details)"
66+
case .ExpectedFailure(_): explanation = "failed"
67+
case .UnexpectedFailure(let error): explanation = "threw error \"\(error)\""
7468
}
7569

7670
if let name = assertion.name {
@@ -92,23 +86,22 @@ private func _XCTEvaluateAssertion(assertion: _XCTAssertion, @autoclosure messag
9286
switch result {
9387
case .Success:
9488
return
95-
9689
default:
9790
if let handler = XCTFailureHandler {
9891
handler(XCTFailure(message: message(), failureDescription: result.failureDescription(assertion), expected: result.expected, file: file, line: line))
9992
}
10093
}
10194
}
10295

103-
/// This function emits a test failure if the general Bool expression passed
104-
/// to it evaluates to false.
96+
/// This function emits a test failure if the general `Boolean` expression passed
97+
/// to it evaluates to `false`.
10598
///
10699
/// - Requires: This and all other XCTAssert* functions must be called from
107100
/// within a test method, as passed to `XCTMain`.
108101
/// Assertion failures that occur outside of a test method will *not* be
109102
/// reported as failures.
110103
///
111-
/// - Parameter expression: A boolean test. If it evaluates to false, the
104+
/// - Parameter expression: A boolean test. If it evaluates to `false`, the
112105
/// assertion fails and emits a test failure.
113106
/// - Parameter message: An optional message to use in the failure if the
114107
/// assertion fails. If no message is supplied a default message is used.

Sources/XCTest/XCTestCase.swift

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ public class XCTestCase {
4747
/// the signature required by `XCTMain`
4848
/// - seealso: `XCTMain`
4949
public func testCase<T: XCTestCase>(allTests: [(String, T -> () throws -> Void)]) -> XCTestCaseEntry {
50-
let tests: [(String, XCTestCase throws -> Void)] = allTests.map({ ($0.0, test($0.1)) })
50+
let tests: [(String, XCTestCase throws -> Void)] = allTests.map { ($0.0, test($0.1)) }
5151
return (T.self, tests)
5252
}
5353

5454
private func test<T: XCTestCase>(testFunc: T -> () throws -> Void) -> XCTestCase throws -> Void {
5555
return { testCaseType in
56-
guard let testCase: T = testCaseType as? T else {
56+
guard let testCase = testCaseType as? T else {
5757
fatalError("Attempt to invoke test on class \(T.self) with incompatible instance type \(testCaseType.dynamicType)")
5858
}
5959

@@ -62,7 +62,7 @@ private func test<T: XCTestCase>(testFunc: T -> () throws -> Void) -> XCTestCase
6262
}
6363

6464
// FIXME: Expectations should be stored in an instance variable defined on
65-
// XCTestCase, but when so defined Linux tests fail with "hidden symbol
65+
// `XCTestCase`, but when so defined Linux tests fail with "hidden symbol
6666
// isn't defined". Use a global for the time being, as this seems to
6767
// appease the Linux compiler.
6868
private var XCTAllExpectations = [XCTestExpectation]()
@@ -74,7 +74,10 @@ extension XCTestCase {
7474
return true
7575
}
7676
set {
77-
// TODO: When using the Objective-C runtime, XCTest is able to throw an exception from an assert and then catch it at the frame above the test method. This enables the framework to effectively stop all execution in the current test. There is no such facility in Swift. Until we figure out how to get a compatible behavior, we have decided to hard-code the value of 'true' for continue after failure.
77+
// TODO: When using the Objective-C runtime, XCTest is able to throw an exception from an assert and then catch it at the frame above the test method.
78+
// This enables the framework to effectively stop all execution in the current test.
79+
// There is no such facility in Swift. Until we figure out how to get a compatible behavior,
80+
// we have decided to hard-code the value of 'true' for continue after failure.
7881
}
7982
}
8083

@@ -143,14 +146,8 @@ extension XCTestCase {
143146
}
144147
}
145148

146-
var testCountSuffix = "s"
147-
if tests.count == 1 {
148-
testCountSuffix = ""
149-
}
150-
var failureSuffix = "s"
151-
if totalFailures == 1 {
152-
failureSuffix = ""
153-
}
149+
let testCountSuffix = (tests.count == 1) ? "" : "s"
150+
let failureSuffix = (totalFailures == 1) ? "" : "s"
154151

155152
XCTPrint("Executed \(tests.count) test\(testCountSuffix), with \(totalFailures) failure\(failureSuffix) (\(unexpectedFailures) unexpected) in \(printableStringForTimeInterval(totalDuration)) (\(printableStringForTimeInterval(overallDuration))) seconds")
156153
}

Sources/XCTest/XCTestMain.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
//
1414

1515
#if os(Linux) || os(FreeBSD)
16-
import Glibc
17-
import Foundation
16+
import Glibc
17+
import Foundation
1818
#else
19-
import Darwin
20-
import SwiftFoundation
19+
import Darwin
20+
import SwiftFoundation
2121
#endif
2222

2323
internal func XCTPrint(message: String) {

0 commit comments

Comments
 (0)