Skip to content

Commit 2e96829

Browse files
authored
Merge pull request #198 from briancroom/swift4-API-compatibility
[SR-5643] Adopt Swift 4 API adjustments from Apple XCTest
2 parents 0f7f725 + 22e1c07 commit 2e96829

24 files changed

+127
-86
lines changed

Sources/XCTest/Private/PerformanceMeter.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,22 @@ internal protocol PerformanceMeterDelegate {
4040
/// as average, and standard deviation
4141
/// - Parameter file: The source file name where the measurement was invoked
4242
/// - Parameter line: The source line number where the measurement was invoked
43-
func recordMeasurements(results: String, file: StaticString, line: UInt)
43+
func recordMeasurements(results: String, file: StaticString, line: Int)
4444

4545
/// Reports a test failure from the analysis of performance measurements.
4646
/// This can currently be caused by an unexpectedly large standard deviation
4747
/// calculated over the data.
4848
/// - Parameter description: An explanation of the failure
4949
/// - Parameter file: The source file name where the measurement was invoked
5050
/// - Parameter line: The source line number where the measurement was invoked
51-
func recordFailure(description: String, file: StaticString, line: UInt)
51+
func recordFailure(description: String, file: StaticString, line: Int)
5252

5353
/// Reports a misuse of the `PerformanceMeter` API, such as calling `
5454
/// startMeasuring` multiple times.
5555
/// - Parameter description: An explanation of the misuse
5656
/// - Parameter file: The source file name where the misuse occurred
5757
/// - Parameter line: The source line number where the misuse occurred
58-
func recordAPIViolation(description: String, file: StaticString, line: UInt)
58+
func recordAPIViolation(description: String, file: StaticString, line: Int)
5959
}
6060

6161
/// - Bug: This class is intended to be `internal` but is public to work around
@@ -97,16 +97,16 @@ public final class PerformanceMeter {
9797
private let metrics: [PerformanceMetric]
9898
private let delegate: PerformanceMeterDelegate
9999
private let invocationFile: StaticString
100-
private let invocationLine: UInt
100+
private let invocationLine: Int
101101

102-
private init(metrics: [PerformanceMetric], delegate: PerformanceMeterDelegate, file: StaticString, line: UInt) {
102+
private init(metrics: [PerformanceMetric], delegate: PerformanceMeterDelegate, file: StaticString, line: Int) {
103103
self.metrics = metrics
104104
self.delegate = delegate
105105
self.invocationFile = file
106106
self.invocationLine = line
107107
}
108108

109-
static func measureMetrics(_ metricNames: [String], delegate: PerformanceMeterDelegate, file: StaticString = #file, line: UInt = #line, for block: (PerformanceMeter) -> Void) {
109+
static func measureMetrics(_ metricNames: [String], delegate: PerformanceMeterDelegate, file: StaticString = #file, line: Int = #line, for block: (PerformanceMeter) -> Void) {
110110
do {
111111
let metrics = try self.metrics(forNames: metricNames)
112112
let meter = PerformanceMeter(metrics: metrics, delegate: delegate, file: file, line: line)
@@ -116,15 +116,15 @@ public final class PerformanceMeter {
116116
}
117117
}
118118

119-
func startMeasuring(file: StaticString = #file, line: UInt = #line) {
119+
func startMeasuring(file: StaticString = #file, line: Int = #line) {
120120
guard state == .iterationUnstarted else {
121121
return recordAPIViolation(.startMeasuringAlreadyCalled, file: file, line: line)
122122
}
123123
state = .iterationStarted
124124
metrics.forEach { $0.startMeasuring() }
125125
}
126126

127-
func stopMeasuring(file: StaticString = #file, line: UInt = #line) {
127+
func stopMeasuring(file: StaticString = #file, line: Int = #line) {
128128
guard state != .iterationUnstarted else {
129129
return recordAPIViolation(.stopBeforeStarting, file: file, line: line)
130130
}
@@ -195,7 +195,7 @@ public final class PerformanceMeter {
195195
}
196196
}
197197

198-
private func recordAPIViolation(_ error: Error, file: StaticString, line: UInt) {
198+
private func recordAPIViolation(_ error: Error, file: StaticString, line: Int) {
199199
state = .measurementAborted
200200
delegate.recordAPIViolation(description: String(describing: error), file: file, line: line)
201201
}

Sources/XCTest/Private/PrintObserver.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ internal class PrintObserver: XCTestObservation {
2424
printAndFlush("Test Case '\(testCase.name)' started at \(dateFormatter.string(from: testCase.testRun!.startDate!))")
2525
}
2626

27-
func testCase(_ testCase: XCTestCase, didFailWithDescription description: String, inFile filePath: String?, atLine lineNumber: UInt) {
27+
func testCase(_ testCase: XCTestCase, didFailWithDescription description: String, inFile filePath: String?, atLine lineNumber: Int) {
2828
let file = filePath ?? "<unknown>"
2929
printAndFlush("\(file):\(lineNumber): error: \(testCase.name) : \(description)")
3030
}
@@ -68,7 +68,7 @@ internal class PrintObserver: XCTestObservation {
6868
}
6969

7070
extension PrintObserver: XCTestInternalObservation {
71-
func testCase(_ testCase: XCTestCase, didMeasurePerformanceResults results: String, file: StaticString, line: UInt) {
71+
func testCase(_ testCase: XCTestCase, didMeasurePerformanceResults results: String, file: StaticString, line: Int) {
7272
printAndFlush("\(file):\(line): Test Case '\(testCase.name)' measured \(results)")
7373
}
7474
}

Sources/XCTest/Private/XCPredicateExpectation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ internal class XCPredicateExpectation: XCTestExpectation {
1818
internal let handler: XCPredicateExpectationHandler?
1919
private let evaluationInterval = 0.01
2020

21-
internal init(predicate: NSPredicate, object: AnyObject, description: String, file: StaticString, line: UInt, testCase: XCTestCase, handler: XCPredicateExpectationHandler? = nil) {
21+
internal init(predicate: NSPredicate, object: AnyObject, description: String, file: StaticString, line: Int, testCase: XCTestCase, handler: XCPredicateExpectationHandler? = nil) {
2222
self.predicate = predicate
2323
self.object = object
2424
self.handler = handler

Sources/XCTest/Private/XCTestInternalObservation.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ internal protocol XCTestInternalObservation: XCTestObservation {
2323
/// reported, if available.
2424
/// - Parameter line: The line number in the source file where the failure
2525
/// was reported.
26-
func testCase(_ testCase: XCTestCase, didMeasurePerformanceResults results: String, file: StaticString, line: UInt)
26+
func testCase(_ testCase: XCTestCase, didMeasurePerformanceResults results: String, file: StaticString, line: Int)
2727
}
2828

2929
// All `XCInternalTestObservation` methods are optional, so empty default implementations are provided
3030
internal extension XCTestInternalObservation {
31-
func testCase(_ testCase: XCTestCase, didMeasurePerformanceResults results: String, file: StaticString, line: UInt) {}
31+
func testCase(_ testCase: XCTestCase, didMeasurePerformanceResults results: String, file: StaticString, line: Int) {}
3232
}

Sources/XCTest/Public/Asynchronous/XCTestCase+Asynchronous.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public extension XCTestCase {
3333
/// between these environments. To ensure compatibility of tests between
3434
/// swift-corelibs-xctest and Apple XCTest, it is not recommended to pass
3535
/// explicit values for `file` and `line`.
36-
@discardableResult func expectation(description: String, file: StaticString = #file, line: UInt = #line) -> XCTestExpectation {
36+
@discardableResult func expectation(description: String, file: StaticString = #file, line: Int = #line) -> XCTestExpectation {
3737
let expectation = XCTestExpectation(
3838
description: description,
3939
file: file,
@@ -68,7 +68,7 @@ public extension XCTestCase {
6868
/// these environments. To ensure compatibility of tests between
6969
/// swift-corelibs-xctest and Apple XCTest, it is not recommended to pass
7070
/// explicit values for `file` and `line`.
71-
func waitForExpectations(timeout: TimeInterval, file: StaticString = #file, line: UInt = #line, handler: XCWaitCompletionHandler? = nil) {
71+
func waitForExpectations(timeout: TimeInterval, file: StaticString = #file, line: Int = #line, handler: XCWaitCompletionHandler? = nil) {
7272
// Mirror Objective-C XCTest behavior; display an unexpected test
7373
// failure when users wait without having first set expectations.
7474
// FIXME: Objective-C XCTest raises an exception for most "API
@@ -139,7 +139,7 @@ public extension XCTestCase {
139139
// If the test failed, send an error object.
140140
error = NSError(
141141
domain: XCTestErrorDomain,
142-
code: XCTestErrorCode.timeoutWhileWaiting.rawValue,
142+
code: XCTestError.Code.timeoutWhileWaiting.rawValue,
143143
userInfo: [:])
144144
}
145145
completionHandler(error)

Sources/XCTest/Public/Asynchronous/XCTestCase+PredicateExpectation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public extension XCTestCase {
3333
/// first successful evaluation will fulfill the expectation. If provided,
3434
/// the handler can override that behavior which leaves the caller
3535
/// responsible for fulfilling the expectation.
36-
@discardableResult func expectation(for predicate: NSPredicate, evaluatedWith object: AnyObject, file: StaticString = #file, line: UInt = #line, handler: XCPredicateExpectationHandler? = nil) -> XCTestExpectation {
36+
@discardableResult func expectation(for predicate: NSPredicate, evaluatedWith object: AnyObject, file: StaticString = #file, line: Int = #line, handler: XCPredicateExpectationHandler? = nil) -> XCTestExpectation {
3737
let expectation = XCPredicateExpectation(
3838
predicate: predicate,
3939
object: object,

Sources/XCTest/Public/Asynchronous/XCTestExpectation.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
public class XCTestExpectation {
1616
internal let description: String
1717
internal let file: StaticString
18-
internal let line: UInt
18+
internal let line: Int
1919

2020
internal var isFulfilled = false
2121
internal weak var testCase: XCTestCase?
2222

23-
internal init(description: String, file: StaticString, line: UInt, testCase: XCTestCase) {
23+
internal init(description: String, file: StaticString, line: Int, testCase: XCTestCase) {
2424
self.description = description
2525
self.file = file
2626
self.line = line
@@ -47,7 +47,7 @@ public class XCTestExpectation {
4747
/// between these environments. To ensure compatibility of tests between
4848
/// swift-corelibs-xctest and Apple XCTest, it is not recommended to pass
4949
/// explicit values for `file` and `line`.
50-
public func fulfill(_ file: StaticString = #file, line: UInt = #line) {
50+
public func fulfill(_ file: StaticString = #file, line: Int = #line) {
5151
// FIXME: Objective-C XCTest emits failures when expectations are
5252
// fulfilled after the test cases that generated those
5353
// expectations have completed. Similarly, this should cause an

Sources/XCTest/Public/XCAbstractTest.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ open class XCTest {
2222
}
2323

2424
/// Number of test cases. Must be overridden by subclasses.
25-
open var testCaseCount: UInt {
25+
open var testCaseCount: Int {
2626
fatalError("Must be overridden by subclasses.")
2727
}
2828

Sources/XCTest/Public/XCTAssert.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ private func _XCTEvaluateAssertion(_ assertion: _XCTAssertion, message: @autoclo
9393
currentTestCase.recordFailure(
9494
withDescription: "\(result.failureDescription(assertion)) - \(message())",
9595
inFile: String(describing: file),
96-
atLine: line,
96+
atLine: Int(line),
9797
expected: result.isExpected)
9898
}
9999
}

Sources/XCTest/Public/XCTestCase+Performance.swift

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,30 @@
1111
// Methods on XCTestCase for testing the performance of code blocks.
1212
//
1313

14-
/// Records wall clock time in seconds between `startMeasuring`/`stopMeasuring`.
15-
public let XCTPerformanceMetric_WallClockTime = WallClockTimeMetric.name
14+
public struct XCTPerformanceMetric : RawRepresentable, Equatable, Hashable {
15+
public private(set) var rawValue: String
16+
17+
public init(_ rawValue: String) {
18+
self.rawValue = rawValue
19+
}
20+
21+
public init(rawValue: String) {
22+
self.rawValue = rawValue
23+
}
24+
25+
public var hashValue: Int {
26+
return rawValue.hashValue
27+
}
28+
29+
public static func ==(_ lhs: XCTPerformanceMetric, _ rhs: XCTPerformanceMetric) -> Bool {
30+
return lhs.rawValue == rhs.rawValue
31+
}
32+
}
33+
34+
public extension XCTPerformanceMetric {
35+
/// Records wall clock time in seconds between `startMeasuring`/`stopMeasuring`.
36+
public static let wallClockTime = XCTPerformanceMetric(rawValue: WallClockTimeMetric.name)
37+
}
1638

1739
/// The following methods are called from within a test method to carry out
1840
/// performance testing on blocks of code.
@@ -21,11 +43,11 @@ public extension XCTestCase {
2143
/// The names of the performance metrics to measure when invoking `measure(block:)`.
2244
/// Returns `XCTPerformanceMetric_WallClockTime` by default. Subclasses can
2345
/// override this to change the behavior of `measure(block:)`
24-
class func defaultPerformanceMetrics() -> [String] {
25-
return [XCTPerformanceMetric_WallClockTime]
46+
class var defaultPerformanceMetrics: [XCTPerformanceMetric] {
47+
return [.wallClockTime]
2648
}
2749

28-
/// Call from a test method to measure resources (`defaultPerformanceMetrics()`)
50+
/// Call from a test method to measure resources (`defaultPerformanceMetrics`)
2951
/// used by the block in the current process.
3052
///
3153
/// func testPerformanceOfMyFunction() {
@@ -49,8 +71,8 @@ public extension XCTestCase {
4971
/// these methods are not exactly identical between these environments. To
5072
/// ensure compatibility of tests between swift-corelibs-xctest and Apple
5173
/// XCTest, it is not recommended to pass explicit values for `file` and `line`.
52-
func measure(file: StaticString = #file, line: UInt = #line, block: () -> Void) {
53-
measureMetrics(type(of: self).defaultPerformanceMetrics(),
74+
func measure(file: StaticString = #file, line: Int = #line, block: () -> Void) {
75+
measureMetrics(type(of: self).defaultPerformanceMetrics,
5476
automaticallyStartMeasuring: true,
5577
file: file,
5678
line: line,
@@ -66,7 +88,7 @@ public extension XCTestCase {
6688
/// may interfere the API will measure them separately.
6789
///
6890
/// func testMyFunction2_WallClockTime() {
69-
/// measureMetrics(type(of: self).defaultPerformanceMetrics(), automaticallyStartMeasuring: false) {
91+
/// measureMetrics(type(of: self).defaultPerformanceMetrics, automaticallyStartMeasuring: false) {
7092
///
7193
/// // Do setup work that needs to be done for every iteration but
7294
/// // you don't want to measure before the call to `startMeasuring()`
@@ -103,12 +125,12 @@ public extension XCTestCase {
103125
/// these methods are not exactly identical between these environments. To
104126
/// ensure compatibility of tests between swift-corelibs-xctest and Apple
105127
/// XCTest, it is not recommended to pass explicit values for `file` and `line`.
106-
func measureMetrics(_ metrics: [String], automaticallyStartMeasuring: Bool, file: StaticString = #file, line: UInt = #line, for block: () -> Void) {
128+
func measureMetrics(_ metrics: [XCTPerformanceMetric], automaticallyStartMeasuring: Bool, file: StaticString = #file, line: Int = #line, for block: () -> Void) {
107129
guard _performanceMeter == nil else {
108130
return recordAPIViolation(description: "Can only record one set of metrics per test method.", file: file, line: line)
109131
}
110132

111-
PerformanceMeter.measureMetrics(metrics, delegate: self, file: file, line: line) { meter in
133+
PerformanceMeter.measureMetrics(metrics.map({ $0.rawValue }), delegate: self, file: file, line: line) { meter in
112134
self._performanceMeter = meter
113135
if automaticallyStartMeasuring {
114136
meter.startMeasuring(file: file, line: line)
@@ -125,7 +147,7 @@ public extension XCTestCase {
125147
/// these methods are not exactly identical between these environments. To
126148
/// ensure compatibility of tests between swift-corelibs-xctest and Apple
127149
/// XCTest, it is not recommended to pass explicit values for `file` and `line`.
128-
func startMeasuring(file: StaticString = #file, line: UInt = #line) {
150+
func startMeasuring(file: StaticString = #file, line: Int = #line) {
129151
guard let performanceMeter = _performanceMeter, !performanceMeter.didFinishMeasuring else {
130152
return recordAPIViolation(description: "Cannot start measuring. startMeasuring() is only supported from a block passed to measureMetrics(...).", file: file, line: line)
131153
}
@@ -140,7 +162,7 @@ public extension XCTestCase {
140162
/// these methods are not exactly identical between these environments. To
141163
/// ensure compatibility of tests between swift-corelibs-xctest and Apple
142164
/// XCTest, it is not recommended to pass explicit values for `file` and `line`.
143-
func stopMeasuring(file: StaticString = #file, line: UInt = #line) {
165+
func stopMeasuring(file: StaticString = #file, line: Int = #line) {
144166
guard let performanceMeter = _performanceMeter, !performanceMeter.didFinishMeasuring else {
145167
return recordAPIViolation(description: "Cannot stop measuring. stopMeasuring() is only supported from a block passed to measureMetrics(...).", file: file, line: line)
146168
}
@@ -149,18 +171,18 @@ public extension XCTestCase {
149171
}
150172

151173
extension XCTestCase: PerformanceMeterDelegate {
152-
internal func recordAPIViolation(description: String, file: StaticString, line: UInt) {
174+
internal func recordAPIViolation(description: String, file: StaticString, line: Int) {
153175
recordFailure(withDescription: "API violation - \(description)",
154176
inFile: String(describing: file),
155177
atLine: line,
156178
expected: false)
157179
}
158180

159-
internal func recordMeasurements(results: String, file: StaticString, line: UInt) {
160-
XCTestObservationCenter.shared().testCase(self, didMeasurePerformanceResults: results, file: file, line: line)
181+
internal func recordMeasurements(results: String, file: StaticString, line: Int) {
182+
XCTestObservationCenter.shared.testCase(self, didMeasurePerformanceResults: results, file: file, line: line)
161183
}
162184

163-
internal func recordFailure(description: String, file: StaticString, line: UInt) {
185+
internal func recordFailure(description: String, file: StaticString, line: Int) {
164186
recordFailure(withDescription: "failed: " + description, inFile: String(describing: file), atLine: line, expected: true)
165187
}
166188
}

Sources/XCTest/Public/XCTestCase.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ open class XCTestCase: XCTest {
3737
/// A private setter for the name of this test case.
3838
private var _name: String
3939

40-
open override var testCaseCount: UInt {
40+
open override var testCaseCount: Int {
4141
return 1
4242
}
4343

@@ -99,7 +99,7 @@ open class XCTestCase: XCTest {
9999
/// - Parameter expected: `true` if the failure being reported was the
100100
/// result of a failed assertion, `false` if it was the result of an
101101
/// uncaught exception.
102-
open func recordFailure(withDescription description: String, inFile filePath: String, atLine lineNumber: UInt, expected: Bool) {
102+
open func recordFailure(withDescription description: String, inFile filePath: String, atLine lineNumber: Int, expected: Bool) {
103103
testRun?.recordFailure(
104104
withDescription: description,
105105
inFile: filePath,

Sources/XCTest/Public/XCTestCaseRun.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,21 @@
1515
open class XCTestCaseRun: XCTestRun {
1616
open override func start() {
1717
super.start()
18-
XCTestObservationCenter.shared().testCaseWillStart(testCase)
18+
XCTestObservationCenter.shared.testCaseWillStart(testCase)
1919
}
2020

2121
open override func stop() {
2222
super.stop()
23-
XCTestObservationCenter.shared().testCaseDidFinish(testCase)
23+
XCTestObservationCenter.shared.testCaseDidFinish(testCase)
2424
}
2525

26-
open override func recordFailure(withDescription description: String, inFile filePath: String?, atLine lineNumber: UInt, expected: Bool) {
26+
open override func recordFailure(withDescription description: String, inFile filePath: String?, atLine lineNumber: Int, expected: Bool) {
2727
super.recordFailure(
2828
withDescription: "\(test.name) : \(description)",
2929
inFile: filePath,
3030
atLine: lineNumber,
3131
expected: expected)
32-
XCTestObservationCenter.shared().testCase(
32+
XCTestObservationCenter.shared.testCase(
3333
testCase,
3434
didFailWithDescription: description,
3535
inFile: filePath,

0 commit comments

Comments
 (0)