Skip to content

Additional Swift 3 API naming updates #88

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
merged 3 commits into from
Apr 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
210 changes: 105 additions & 105 deletions Sources/XCTest/XCTAssert.swift

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions Sources/XCTest/XCTestCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ extension XCTestCase {
/// 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`.
public func expectationWithDescription(description: String, file: StaticString = #file, line: UInt = #line) -> XCTestExpectation {
public func expectation(withDescription description: String, file: StaticString = #file, line: UInt = #line) -> XCTestExpectation {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"With" is usually a vacuous preposition that does not carry any semantics. I think this is one of those cases. (Other cases in XCTest may also be like that.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. However this is the method name as generated by the importer with the March 24 toolchain from XCTest.framework. Do you expect additional changes to the importer which would automatically remove "with"? Or is this a situation where the Objective-C header should be annotated with the swift_name attribute?

(Interestingly, it appears that https://github.com/apple/swift-3-api-guidelines-review doesn't include XCTest.framework)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should match exactly what the importer does, which is leaving the with in place (and this is very much an intentional choice).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@parkera is there any additional discussion around this particular choice you could point me to? I'm curious about the context on this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was a discussion we had in our Swift 3 naming 'working group'.

The summary is that we felt that the importer could not automatically make the right decision about if a 'with' was important or not in an API. It is used to mean different things in different APIs. Therefore we decided to stick with a default of leaving it in place for imported API.

We can of course change our minds about the names, but then we're talking about changing it for all clients on all platforms. Since this version of XCTest is attempting to keep the same API, and there isn't a plan to change the Darwin version of this method, we should keep the name the same for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, that's exactly the info I was looking for.

let expectation = XCTestExpectation(
description: description,
file: file,
Expand Down Expand Up @@ -225,7 +225,7 @@ extension XCTestCase {
/// 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`.
public func waitForExpectationsWithTimeout(timeout: NSTimeInterval, file: StaticString = #file, line: UInt = #line, handler: XCWaitCompletionHandler?) {
public func waitForExpectations(withTimeout timeout: NSTimeInterval, file: StaticString = #file, line: UInt = #line, handler: XCWaitCompletionHandler? = nil) {
// Mirror Objective-C XCTest behavior; display an unexpected test
// failure when users wait without having first set expectations.
// FIXME: Objective-C XCTest raises an exception for most "API
Expand Down Expand Up @@ -263,7 +263,7 @@ extension XCTestCase {
repeat {
unfulfilledDescriptions = []
for expectation in _allExpectations {
if !expectation.fulfilled {
if !expectation.isFulfilled {
unfulfilledDescriptions.append(expectation.description)
}
}
Expand Down Expand Up @@ -323,9 +323,9 @@ extension XCTestCase {
/// notification is observed. It will not be invoked on timeout. Use the
/// handler to further investigate if the notification fulfills the
/// expectation.
public func expectationForNotification(notificationName: String, object objectToObserve: AnyObject?, handler: XCNotificationExpectationHandler?) -> XCTestExpectation {
public func expectation(forNotification notificationName: String, object objectToObserve: AnyObject?, handler: XCNotificationExpectationHandler? = nil) -> XCTestExpectation {
let objectDescription = objectToObserve == nil ? "any object" : "\(objectToObserve!)"
let expectation = self.expectationWithDescription("Expect notification '\(notificationName)' from " + objectDescription)
let expectation = self.expectation(withDescription: "Expect notification '\(notificationName)' from " + objectDescription)
// Start observing the notification with specified name and object.
var observer: NSObjectProtocol? = nil
func removeObserver() {
Expand Down
6 changes: 3 additions & 3 deletions Sources/XCTest/XCTestExpectation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class XCTestExpectation {
internal let file: StaticString
internal let line: UInt

internal var fulfilled = false
internal var isFulfilled = false
internal weak var testCase: XCTestCase?

internal init(description: String, file: StaticString, line: UInt, testCase: XCTestCase) {
Expand Down Expand Up @@ -52,7 +52,7 @@ public class XCTestExpectation {
// fulfilled after the test cases that generated those
// expectations have completed. Similarly, this should cause an
// error as well.
if fulfilled {
if isFulfilled {
// Mirror Objective-C XCTest behavior: treat multiple calls to
// fulfill() as an unexpected failure.
let failure = XCTFailure(
Expand All @@ -65,7 +65,7 @@ public class XCTestExpectation {
failureHandler(failure)
}
} else {
fulfilled = true
isFulfilled = true
}
}
}
30 changes: 15 additions & 15 deletions Tests/Functional/Asynchronous/Expectations/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,64 +15,64 @@ class ExpectationsTestCase: XCTestCase {
// CHECK: .*/Tests/Functional/Asynchronous/Expectations/main.swift:19: error: ExpectationsTestCase.test_waitingForAnUnfulfilledExpectation_fails : Asynchronous wait failed - Exceeded timeout of 0.2 seconds, with unfulfilled expectations: foo
// CHECK: Test Case 'ExpectationsTestCase.test_waitingForAnUnfulfilledExpectation_fails' failed \(\d+\.\d+ seconds\).
func test_waitingForAnUnfulfilledExpectation_fails() {
expectationWithDescription("foo")
waitForExpectationsWithTimeout(0.2, handler: nil)
expectation(withDescription: "foo")
waitForExpectations(withTimeout: 0.2)
}

// CHECK: Test Case 'ExpectationsTestCase.test_waitingForUnfulfilledExpectations_outputsAllExpectations_andFails' started.
// CHECK: .*/Tests/Functional/Asynchronous/Expectations/main.swift:28: error: ExpectationsTestCase.test_waitingForUnfulfilledExpectations_outputsAllExpectations_andFails : Asynchronous wait failed - Exceeded timeout of 0.2 seconds, with unfulfilled expectations: bar, baz
// CHECK: Test Case 'ExpectationsTestCase.test_waitingForUnfulfilledExpectations_outputsAllExpectations_andFails' failed \(\d+\.\d+ seconds\).
func test_waitingForUnfulfilledExpectations_outputsAllExpectations_andFails() {
expectationWithDescription("bar")
expectationWithDescription("baz")
waitForExpectationsWithTimeout(0.2, handler: nil)
expectation(withDescription: "bar")
expectation(withDescription: "baz")
waitForExpectations(withTimeout: 0.2)
}

// CHECK: Test Case 'ExpectationsTestCase.test_waitingForAnImmediatelyFulfilledExpectation_passes' started.
// CHECK: Test Case 'ExpectationsTestCase.test_waitingForAnImmediatelyFulfilledExpectation_passes' passed \(\d+\.\d+ seconds\).
func test_waitingForAnImmediatelyFulfilledExpectation_passes() {
let expectation = expectationWithDescription("flim")
let expectation = self.expectation(withDescription: "flim")
expectation.fulfill()
waitForExpectationsWithTimeout(0.2, handler: nil)
waitForExpectations(withTimeout: 0.2)
}

// CHECK: Test Case 'ExpectationsTestCase.test_waitingForAnEventuallyFulfilledExpectation_passes' started.
// CHECK: Test Case 'ExpectationsTestCase.test_waitingForAnEventuallyFulfilledExpectation_passes' passed \(\d+\.\d+ seconds\).
func test_waitingForAnEventuallyFulfilledExpectation_passes() {
let expectation = expectationWithDescription("flam")
let expectation = self.expectation(withDescription: "flam")
let timer = NSTimer.scheduledTimer(0.1, repeats: false) { _ in
expectation.fulfill()
}
NSRunLoop.currentRunLoop().addTimer(timer, forMode: NSDefaultRunLoopMode)
waitForExpectationsWithTimeout(1.0, handler: nil)
waitForExpectations(withTimeout: 1.0)
}

// CHECK: Test Case 'ExpectationsTestCase.test_waitingForAnExpectationFulfilledAfterTheTimeout_fails' started.
// CHECK: .*/Tests/Functional/Asynchronous/Expectations/main.swift:59: error: ExpectationsTestCase.test_waitingForAnExpectationFulfilledAfterTheTimeout_fails : Asynchronous wait failed - Exceeded timeout of 0.1 seconds, with unfulfilled expectations: hog
// CHECK: Test Case 'ExpectationsTestCase.test_waitingForAnExpectationFulfilledAfterTheTimeout_fails' failed \(\d+\.\d+ seconds\).
func test_waitingForAnExpectationFulfilledAfterTheTimeout_fails() {
let expectation = expectationWithDescription("hog")
let expectation = self.expectation(withDescription: "hog")
let timer = NSTimer.scheduledTimer(1.0, repeats: false) { _ in
expectation.fulfill()
}
NSRunLoop.currentRunLoop().addTimer(timer, forMode: NSDefaultRunLoopMode)
waitForExpectationsWithTimeout(0.1, handler: nil)
waitForExpectations(withTimeout: 0.1)
}

// CHECK: Test Case 'ExpectationsTestCase.test_whenTimeoutIsImmediate_andAllExpectationsAreFulfilled_passes' started.
// CHECK: Test Case 'ExpectationsTestCase.test_whenTimeoutIsImmediate_andAllExpectationsAreFulfilled_passes' passed \(\d+\.\d+ seconds\).
func test_whenTimeoutIsImmediate_andAllExpectationsAreFulfilled_passes() {
let expectation = expectationWithDescription("smog")
let expectation = self.expectation(withDescription: "smog")
expectation.fulfill()
waitForExpectationsWithTimeout(0.0, handler: nil)
waitForExpectations(withTimeout: 0.0)
}

// CHECK: Test Case 'ExpectationsTestCase.test_whenTimeoutIsImmediate_butNotAllExpectationsAreFulfilled_fails' started.
// CHECK: .*/Tests/Functional/Asynchronous/Expectations/main.swift:75: error: ExpectationsTestCase.test_whenTimeoutIsImmediate_butNotAllExpectationsAreFulfilled_fails : Asynchronous wait failed - Exceeded timeout of -1.0 seconds, with unfulfilled expectations: dog
// CHECK: Test Case 'ExpectationsTestCase.test_whenTimeoutIsImmediate_butNotAllExpectationsAreFulfilled_fails' failed \(\d+\.\d+ seconds\).
func test_whenTimeoutIsImmediate_butNotAllExpectationsAreFulfilled_fails() {
expectationWithDescription("dog")
waitForExpectationsWithTimeout(-1.0, handler: nil)
expectation(withDescription: "dog")
waitForExpectations(withTimeout: -1.0)
}

static var allTests: [(String, ExpectationsTestCase -> () throws -> Void)] {
Expand Down
8 changes: 4 additions & 4 deletions Tests/Functional/Asynchronous/Handler/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ class HandlerTestCase: XCTestCase {
// CHECK: .*/Tests/Functional/Asynchronous/Handler/main.swift:21: error: HandlerTestCase.test_whenExpectationsAreNotFulfilled_handlerCalled_andFails : Asynchronous wait failed - Exceeded timeout of 0.2 seconds, with unfulfilled expectations: fog
// CHECK: Test Case 'HandlerTestCase.test_whenExpectationsAreNotFulfilled_handlerCalled_andFails' failed \(\d+\.\d+ seconds\).
func test_whenExpectationsAreNotFulfilled_handlerCalled_andFails() {
self.expectationWithDescription("fog")
self.expectation(withDescription: "fog")

var handlerWasCalled = false
self.waitForExpectationsWithTimeout(0.2) { error in
self.waitForExpectations(withTimeout: 0.2) { error in
XCTAssertNotNil(error, "Expectation handlers for unfulfilled expectations should not be nil.")
XCTAssertTrue(error!.domain.hasSuffix("XCTestErrorDomain"), "The last component of the error domain should match Objective-C XCTest.")
XCTAssertEqual(error!.code, 0, "The error code should match Objective-C XCTest.")
Expand All @@ -30,11 +30,11 @@ class HandlerTestCase: XCTestCase {
// CHECK: Test Case 'HandlerTestCase.test_whenExpectationsAreFulfilled_handlerCalled_andPasses' started.
// CHECK: Test Case 'HandlerTestCase.test_whenExpectationsAreFulfilled_handlerCalled_andPasses' passed \(\d+\.\d+ seconds\).
func test_whenExpectationsAreFulfilled_handlerCalled_andPasses() {
let expectation = self.expectationWithDescription("bog")
let expectation = self.expectation(withDescription: "bog")
expectation.fulfill()

var handlerWasCalled = false
self.waitForExpectationsWithTimeout(0.2) { error in
self.waitForExpectations(withTimeout: 0.2) { error in
XCTAssertNil(error, "Expectation handlers for fulfilled expectations should be nil.")
handlerWasCalled = true
}
Expand Down
10 changes: 5 additions & 5 deletions Tests/Functional/Asynchronous/Misuse/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@ class MisuseTestCase: XCTestCase {
// CHECK: .*/Tests/Functional/Asynchronous/Misuse/main.swift:17: unexpected error: MisuseTestCase.test_whenExpectationsAreMade_butNotWaitedFor_fails : - Failed due to unwaited expectations.
// CHECK: Test Case 'MisuseTestCase.test_whenExpectationsAreMade_butNotWaitedFor_fails' failed \(\d+\.\d+ seconds\).
func test_whenExpectationsAreMade_butNotWaitedFor_fails() {
self.expectationWithDescription("the first expectation")
self.expectationWithDescription("the second expectation (the file and line number for this one are included in the failure message")
self.expectation(withDescription: "the first expectation")
self.expectation(withDescription: "the second expectation (the file and line number for this one are included in the failure message")
}

// CHECK: Test Case 'MisuseTestCase.test_whenNoExpectationsAreMade_butTheyAreWaitedFor_fails' started.
// CHECK: .*/Tests/Functional/Asynchronous/Misuse/main.swift:24: unexpected error: MisuseTestCase.test_whenNoExpectationsAreMade_butTheyAreWaitedFor_fails : API violation - call made to wait without any expectations having been set.
// CHECK: Test Case 'MisuseTestCase.test_whenNoExpectationsAreMade_butTheyAreWaitedFor_fails' failed \(\d+\.\d+ seconds\).
func test_whenNoExpectationsAreMade_butTheyAreWaitedFor_fails() {
self.waitForExpectationsWithTimeout(0.1, handler: nil)
self.waitForExpectations(withTimeout: 0.1)
}

// CHECK: Test Case 'MisuseTestCase.test_whenExpectationIsFulfilledMultipleTimes_fails' started.
// CHECK: .*/Tests/Functional/Asynchronous/Misuse/main.swift:34: unexpected error: MisuseTestCase.test_whenExpectationIsFulfilledMultipleTimes_fails : API violation - multiple calls made to XCTestExpectation.fulfill\(\) for rob.
// CHECK: .*/Tests/Functional/Asynchronous/Misuse/main.swift:44: unexpected error: MisuseTestCase.test_whenExpectationIsFulfilledMultipleTimes_fails : API violation - multiple calls made to XCTestExpectation.fulfill\(\) for rob.
// CHECK: Test Case 'MisuseTestCase.test_whenExpectationIsFulfilledMultipleTimes_fails' failed \(\d+\.\d+ seconds\).
func test_whenExpectationIsFulfilledMultipleTimes_fails() {
let expectation = self.expectationWithDescription("rob")
let expectation = self.expectation(withDescription: "rob")
expectation.fulfill()
expectation.fulfill()
// FIXME: The behavior here is subtly different from Objective-C XCTest.
Expand All @@ -42,7 +42,7 @@ class MisuseTestCase: XCTestCase {
// highlights both the lines above and below as failures.
// This should be fixed such that the behavior is identical.
expectation.fulfill()
self.waitForExpectationsWithTimeout(0.1, handler: nil)
self.waitForExpectations(withTimeout: 0.1)
}

static var allTests: [(String, MisuseTestCase -> () throws -> Void)] {
Expand Down
20 changes: 10 additions & 10 deletions Tests/Functional/Asynchronous/Notifications/Expectations/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,38 @@ class NotificationExpectationsTestCase: XCTestCase {
// CHECK: Test Case 'NotificationExpectationsTestCase.test_observeNotificationWithName_passes' passed \(\d+\.\d+ seconds\).
func test_observeNotificationWithName_passes() {
let notificationName = "notificationWithNameTest"
expectationForNotification(notificationName, object:nil, handler:nil)
expectation(forNotification: notificationName, object:nil)
NSNotificationCenter.defaultCenter().postNotificationName(notificationName, object: nil)
waitForExpectationsWithTimeout(0.0, handler: nil)
waitForExpectations(withTimeout: 0.0)
}

// CHECK: Test Case 'NotificationExpectationsTestCase.test_observeNotificationWithNameAndObject_passes' started.
// CHECK: Test Case 'NotificationExpectationsTestCase.test_observeNotificationWithNameAndObject_passes' passed \(\d+\.\d+ seconds\).
func test_observeNotificationWithNameAndObject_passes() {
let notificationName = "notificationWithNameAndObjectTest"
let dummyObject = NSObject()
expectationForNotification(notificationName, object:dummyObject, handler:nil)
expectation(forNotification: notificationName, object:dummyObject)
NSNotificationCenter.defaultCenter().postNotificationName(notificationName, object: dummyObject)
waitForExpectationsWithTimeout(0.0, handler: nil)
waitForExpectations(withTimeout: 0.0)
}

// CHECK: Test Case 'NotificationExpectationsTestCase.test_observeNotificationWithNameAndObject_butExpectingNoObject_passes' started.
// CHECK: Test Case 'NotificationExpectationsTestCase.test_observeNotificationWithNameAndObject_butExpectingNoObject_passes' passed \(\d+\.\d+ seconds\).
func test_observeNotificationWithNameAndObject_butExpectingNoObject_passes() {
let notificationName = "notificationWithNameAndObject_expectNoObjectTest"
expectationForNotification(notificationName, object:nil, handler:nil)
expectation(forNotification: notificationName, object:nil)
let dummyObject = NSObject()
NSNotificationCenter.defaultCenter().postNotificationName(notificationName, object: dummyObject)
waitForExpectationsWithTimeout(0.0, handler: nil)
waitForExpectations(withTimeout: 0.0)
}

// CHECK: Test Case 'NotificationExpectationsTestCase.test_observeNotificationWithIncorrectName_fails' started.
// CHECK: .*/Tests/Functional/Asynchronous/Notifications/Expectations/main.swift:49: error: NotificationExpectationsTestCase.test_observeNotificationWithIncorrectName_fails : Asynchronous wait failed - Exceeded timeout of 0.1 seconds, with unfulfilled expectations: Expect notification 'expectedName' from any object
// CHECK: Test Case 'NotificationExpectationsTestCase.test_observeNotificationWithIncorrectName_fails' failed \(\d+\.\d+ seconds\).
func test_observeNotificationWithIncorrectName_fails() {
expectationForNotification("expectedName", object: nil, handler:nil)
expectation(forNotification: "expectedName", object: nil)
NSNotificationCenter.defaultCenter().postNotificationName("actualName", object: nil)
waitForExpectationsWithTimeout(0.1, handler: nil)
waitForExpectations(withTimeout: 0.1)
}

// CHECK: Test Case 'NotificationExpectationsTestCase.test_observeNotificationWithIncorrectObject_fails' started.
Expand All @@ -56,9 +56,9 @@ class NotificationExpectationsTestCase: XCTestCase {
let notificationName = "notificationWithIncorrectObjectTest"
let dummyObject: NSString = "dummyObject"
let anotherDummyObject = NSObject()
expectationForNotification(notificationName, object: dummyObject, handler: nil)
expectation(forNotification: notificationName, object: dummyObject)
NSNotificationCenter.defaultCenter().postNotificationName(notificationName, object:anotherDummyObject)
waitForExpectationsWithTimeout(0.1, handler: nil)
waitForExpectations(withTimeout: 0.1)
}

static var allTests: [(String, NotificationExpectationsTestCase -> () throws -> Void)] {
Expand Down
Loading