Skip to content

[Asynchronous] Move into separate directory #172

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
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
Original file line number Diff line number Diff line change
Expand Up @@ -151,94 +151,6 @@ public extension XCTestCase {
completionHandler(error)
}
}

/// Creates and returns an expectation for a notification.
///
/// - Parameter notificationName: The name of the notification the
/// expectation observes.
/// - Parameter object: The object whose notifications the expectation will
/// receive; that is, only notifications with this object are observed by
/// the test case. If you pass nil, the expectation doesn't use
/// a notification's object to decide whether it is fulfilled.
/// - Parameter handler: If provided, the handler will be invoked when the
/// notification is observed. It will not be invoked on timeout. Use the
/// handler to further investigate if the notification fulfills the
/// expectation.
@discardableResult func expectation(forNotification notificationName: String, object objectToObserve: AnyObject?, handler: XCNotificationExpectationHandler? = nil) -> XCTestExpectation {
let objectDescription = objectToObserve == nil ? "any object" : "\(objectToObserve!)"
let expectation = self.expectation(description: "Expect notification '\(notificationName)' from " + objectDescription)
// Start observing the notification with specified name and object.
var observer: NSObjectProtocol? = nil
func removeObserver() {
if let observer = observer {
NotificationCenter.default.removeObserver(observer)
}
}

weak var weakExpectation = expectation
observer = NotificationCenter
.default
.addObserver(forName: Notification.Name(rawValue: notificationName),
object: objectToObserve,
queue: nil,
usingBlock: {
notification in
guard let expectation = weakExpectation else {
removeObserver()
return
}

// If the handler is invoked, the test will
// only pass if true is returned.
if let handler = handler {
if handler(notification) {
expectation.fulfill()
removeObserver()
}
} else {
expectation.fulfill()
removeObserver()
}
})

return expectation
}

/// Creates and returns an expectation that is fulfilled if the predicate
/// returns true when evaluated with the given object. The expectation
/// periodically evaluates the predicate and also may use notifications or
/// other events to optimistically re-evaluate.
///
/// - Parameter predicate: The predicate that will be used to evaluate the
/// object.
/// - Parameter object: The object that is evaluated against the conditions
/// specified by the predicate.
/// - Parameter file: The file name to use in the error message if
/// this expectation is not waited for. Default is the file
/// containing the call to this method. It is rare to provide this
/// parameter when calling this method.
/// - Parameter line: The line number to use in the error message if the
/// this expectation is not waited for. Default is the line
/// number of the call to this method in the calling file. It is rare to
/// provide this parameter when calling this method.
/// - Parameter handler: A block to be invoked when evaluating the predicate
/// against the object returns true. If the block is not provided the
/// first successful evaluation will fulfill the expectation. If provided,
/// the handler can override that behavior which leaves the caller
/// responsible for fulfilling the expectation.
@discardableResult func expectation(for predicate: NSPredicate, evaluatedWith object: AnyObject, file: StaticString = #file, line: UInt = #line, handler: XCPredicateExpectationHandler? = nil) -> XCTestExpectation {
let expectation = XCPredicateExpectation(
predicate: predicate,
object: object,
description: "Expect `\(predicate)` for object \(object)",
file: file,
line: line,
testCase: self,
handler: handler)
_allExpectations.append(expectation)
expectation.considerFulfilling()
return expectation
}
}

internal extension XCTestCase {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//
// XCTestCase+NotificationExpectation.swift
//

#if os(Linux) || os(FreeBSD)
import Foundation
#else
import SwiftFoundation
#endif

public extension XCTestCase {
/// Creates and returns an expectation for a notification.
///
/// - Parameter notificationName: The name of the notification the
/// expectation observes.
/// - Parameter object: The object whose notifications the expectation will
/// receive; that is, only notifications with this object are observed by
/// the test case. If you pass nil, the expectation doesn't use
/// a notification's object to decide whether it is fulfilled.
/// - Parameter handler: If provided, the handler will be invoked when the
/// notification is observed. It will not be invoked on timeout. Use the
/// handler to further investigate if the notification fulfills the
/// expectation.
@discardableResult func expectation(forNotification notificationName: String, object objectToObserve: AnyObject?, handler: XCNotificationExpectationHandler? = nil) -> XCTestExpectation {
let objectDescription = objectToObserve == nil ? "any object" : "\(objectToObserve!)"
let expectation = self.expectation(description: "Expect notification '\(notificationName)' from " + objectDescription)
// Start observing the notification with specified name and object.
var observer: NSObjectProtocol? = nil
func removeObserver() {
if let observer = observer {
NotificationCenter.default.removeObserver(observer)
}
}

weak var weakExpectation = expectation
observer = NotificationCenter
.default
.addObserver(forName: Notification.Name(rawValue: notificationName),
object: objectToObserve,
queue: nil,
usingBlock: {
notification in
guard let expectation = weakExpectation else {
removeObserver()
return
}

// If the handler is invoked, the test will
// only pass if true is returned.
if let handler = handler {
if handler(notification) {
expectation.fulfill()
removeObserver()
}
} else {
expectation.fulfill()
removeObserver()
}
})

return expectation
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//
// XCTestCase+PredicateExpectation.swift
//

#if os(Linux) || os(FreeBSD)
import Foundation
#else
import SwiftFoundation
#endif

public extension XCTestCase {
/// Creates and returns an expectation that is fulfilled if the predicate
/// returns true when evaluated with the given object. The expectation
/// periodically evaluates the predicate and also may use notifications or
/// other events to optimistically re-evaluate.
///
/// - Parameter predicate: The predicate that will be used to evaluate the
/// object.
/// - Parameter object: The object that is evaluated against the conditions
/// specified by the predicate.
/// - Parameter file: The file name to use in the error message if
/// this expectation is not waited for. Default is the file
/// containing the call to this method. It is rare to provide this
/// parameter when calling this method.
/// - Parameter line: The line number to use in the error message if the
/// this expectation is not waited for. Default is the line
/// number of the call to this method in the calling file. It is rare to
/// provide this parameter when calling this method.
/// - Parameter handler: A block to be invoked when evaluating the predicate
/// against the object returns true. If the block is not provided the
/// first successful evaluation will fulfill the expectation. If provided,
/// the handler can override that behavior which leaves the caller
/// responsible for fulfilling the expectation.
@discardableResult func expectation(for predicate: NSPredicate, evaluatedWith object: AnyObject, file: StaticString = #file, line: UInt = #line, handler: XCPredicateExpectationHandler? = nil) -> XCTestExpectation {
let expectation = XCPredicateExpectation(
predicate: predicate,
object: object,
description: "Expect `\(predicate)` for object \(object)",
file: file,
line: line,
testCase: self,
handler: handler)
_allExpectations.append(expectation)
expectation.considerFulfilling()
return expectation
}
}
Loading