-
Notifications
You must be signed in to change notification settings - Fork 263
[SR-5643] Adopt Swift 4 API adjustments from Apple XCTest #198
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
briancroom
merged 4 commits into
swiftlang:master
from
briancroom:swift4-API-compatibility
Aug 21, 2017
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,8 +11,30 @@ | |
// Methods on XCTestCase for testing the performance of code blocks. | ||
// | ||
|
||
/// Records wall clock time in seconds between `startMeasuring`/`stopMeasuring`. | ||
public let XCTPerformanceMetric_WallClockTime = WallClockTimeMetric.name | ||
public struct XCTPerformanceMetric : RawRepresentable, Equatable, Hashable { | ||
public private(set) var rawValue: String | ||
|
||
public init(_ rawValue: String) { | ||
self.rawValue = rawValue | ||
} | ||
|
||
public init(rawValue: String) { | ||
self.rawValue = rawValue | ||
} | ||
|
||
public var hashValue: Int { | ||
return rawValue.hashValue | ||
} | ||
|
||
public static func ==(_ lhs: XCTPerformanceMetric, _ rhs: XCTPerformanceMetric) -> Bool { | ||
return lhs.rawValue == rhs.rawValue | ||
} | ||
} | ||
|
||
public extension XCTPerformanceMetric { | ||
/// Records wall clock time in seconds between `startMeasuring`/`stopMeasuring`. | ||
public static let wallClockTime = XCTPerformanceMetric(rawValue: WallClockTimeMetric.name) | ||
} | ||
|
||
/// The following methods are called from within a test method to carry out | ||
/// performance testing on blocks of code. | ||
|
@@ -21,11 +43,11 @@ public extension XCTestCase { | |
/// The names of the performance metrics to measure when invoking `measure(block:)`. | ||
/// Returns `XCTPerformanceMetric_WallClockTime` by default. Subclasses can | ||
/// override this to change the behavior of `measure(block:)` | ||
class func defaultPerformanceMetrics() -> [String] { | ||
return [XCTPerformanceMetric_WallClockTime] | ||
class var defaultPerformanceMetrics: [XCTPerformanceMetric] { | ||
return [.wallClockTime] | ||
} | ||
|
||
/// Call from a test method to measure resources (`defaultPerformanceMetrics()`) | ||
/// Call from a test method to measure resources (`defaultPerformanceMetrics`) | ||
/// used by the block in the current process. | ||
/// | ||
/// func testPerformanceOfMyFunction() { | ||
|
@@ -49,8 +71,8 @@ public extension XCTestCase { | |
/// these methods are not exactly identical between these environments. To | ||
/// ensure compatibility of tests between swift-corelibs-xctest and Apple | ||
/// XCTest, it is not recommended to pass explicit values for `file` and `line`. | ||
func measure(file: StaticString = #file, line: UInt = #line, block: () -> Void) { | ||
measureMetrics(type(of: self).defaultPerformanceMetrics(), | ||
func measure(file: StaticString = #file, line: Int = #line, block: () -> Void) { | ||
measureMetrics(type(of: self).defaultPerformanceMetrics, | ||
automaticallyStartMeasuring: true, | ||
file: file, | ||
line: line, | ||
|
@@ -66,7 +88,7 @@ public extension XCTestCase { | |
/// may interfere the API will measure them separately. | ||
/// | ||
/// func testMyFunction2_WallClockTime() { | ||
/// measureMetrics(type(of: self).defaultPerformanceMetrics(), automaticallyStartMeasuring: false) { | ||
/// measureMetrics(type(of: self).defaultPerformanceMetrics, automaticallyStartMeasuring: false) { | ||
/// | ||
/// // Do setup work that needs to be done for every iteration but | ||
/// // you don't want to measure before the call to `startMeasuring()` | ||
|
@@ -103,12 +125,12 @@ public extension XCTestCase { | |
/// these methods are not exactly identical between these environments. To | ||
/// ensure compatibility of tests between swift-corelibs-xctest and Apple | ||
/// XCTest, it is not recommended to pass explicit values for `file` and `line`. | ||
func measureMetrics(_ metrics: [String], automaticallyStartMeasuring: Bool, file: StaticString = #file, line: UInt = #line, for block: () -> Void) { | ||
func measureMetrics(_ metrics: [XCTPerformanceMetric], automaticallyStartMeasuring: Bool, file: StaticString = #file, line: Int = #line, for block: () -> Void) { | ||
guard _performanceMeter == nil else { | ||
return recordAPIViolation(description: "Can only record one set of metrics per test method.", file: file, line: line) | ||
} | ||
|
||
PerformanceMeter.measureMetrics(metrics, delegate: self, file: file, line: line) { meter in | ||
PerformanceMeter.measureMetrics(metrics.map({ $0.rawValue }), delegate: self, file: file, line: line) { meter in | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could get rid of this map if we move to |
||
self._performanceMeter = meter | ||
if automaticallyStartMeasuring { | ||
meter.startMeasuring(file: file, line: line) | ||
|
@@ -125,7 +147,7 @@ public extension XCTestCase { | |
/// these methods are not exactly identical between these environments. To | ||
/// ensure compatibility of tests between swift-corelibs-xctest and Apple | ||
/// XCTest, it is not recommended to pass explicit values for `file` and `line`. | ||
func startMeasuring(file: StaticString = #file, line: UInt = #line) { | ||
func startMeasuring(file: StaticString = #file, line: Int = #line) { | ||
guard let performanceMeter = _performanceMeter, !performanceMeter.didFinishMeasuring else { | ||
return recordAPIViolation(description: "Cannot start measuring. startMeasuring() is only supported from a block passed to measureMetrics(...).", file: file, line: line) | ||
} | ||
|
@@ -140,7 +162,7 @@ public extension XCTestCase { | |
/// these methods are not exactly identical between these environments. To | ||
/// ensure compatibility of tests between swift-corelibs-xctest and Apple | ||
/// XCTest, it is not recommended to pass explicit values for `file` and `line`. | ||
func stopMeasuring(file: StaticString = #file, line: UInt = #line) { | ||
func stopMeasuring(file: StaticString = #file, line: Int = #line) { | ||
guard let performanceMeter = _performanceMeter, !performanceMeter.didFinishMeasuring else { | ||
return recordAPIViolation(description: "Cannot stop measuring. stopMeasuring() is only supported from a block passed to measureMetrics(...).", file: file, line: line) | ||
} | ||
|
@@ -149,18 +171,18 @@ public extension XCTestCase { | |
} | ||
|
||
extension XCTestCase: PerformanceMeterDelegate { | ||
internal func recordAPIViolation(description: String, file: StaticString, line: UInt) { | ||
internal func recordAPIViolation(description: String, file: StaticString, line: Int) { | ||
recordFailure(withDescription: "API violation - \(description)", | ||
inFile: String(describing: file), | ||
atLine: line, | ||
expected: false) | ||
} | ||
|
||
internal func recordMeasurements(results: String, file: StaticString, line: UInt) { | ||
XCTestObservationCenter.shared().testCase(self, didMeasurePerformanceResults: results, file: file, line: line) | ||
internal func recordMeasurements(results: String, file: StaticString, line: Int) { | ||
XCTestObservationCenter.shared.testCase(self, didMeasurePerformanceResults: results, file: file, line: line) | ||
} | ||
|
||
internal func recordFailure(description: String, file: StaticString, line: UInt) { | ||
internal func recordFailure(description: String, file: StaticString, line: Int) { | ||
recordFailure(withDescription: "failed: " + description, inFile: String(describing: file), atLine: line, expected: true) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not make
metricNames: [PerformanceMetric]
too? As they're strings, we have to map fromPerformanceMetric
toString
down below.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm..
One of the design goals with the PerformanceMeter and WallClockTimeMetric classes was for them to be entirely decoupled from the XCTest-specific stuff. i.e. they are conceptually a lower layer upon which the XCTest API sits. Ideally they would exist in their own separate module, but it's not currently possible to do that while still keeping XCTest library product and module standalone. See #109 for earlier discussion.
How do you feel about this given that context?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense. A possible (future) improvement could be to create a similar enum at the PerformanceMeter layer.