Skip to content

Commit 7d61248

Browse files
authored
Merge pull request #172 from modocache/async-fixme-runloop-source-project-reorg
[Asynchronous] Move into separate directory
2 parents 01676a2 + ac97606 commit 7d61248

9 files changed

+179
-111
lines changed

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

Lines changed: 0 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -151,94 +151,6 @@ public extension XCTestCase {
151151
completionHandler(error)
152152
}
153153
}
154-
155-
/// Creates and returns an expectation for a notification.
156-
///
157-
/// - Parameter notificationName: The name of the notification the
158-
/// expectation observes.
159-
/// - Parameter object: The object whose notifications the expectation will
160-
/// receive; that is, only notifications with this object are observed by
161-
/// the test case. If you pass nil, the expectation doesn't use
162-
/// a notification's object to decide whether it is fulfilled.
163-
/// - Parameter handler: If provided, the handler will be invoked when the
164-
/// notification is observed. It will not be invoked on timeout. Use the
165-
/// handler to further investigate if the notification fulfills the
166-
/// expectation.
167-
@discardableResult func expectation(forNotification notificationName: String, object objectToObserve: AnyObject?, handler: XCNotificationExpectationHandler? = nil) -> XCTestExpectation {
168-
let objectDescription = objectToObserve == nil ? "any object" : "\(objectToObserve!)"
169-
let expectation = self.expectation(description: "Expect notification '\(notificationName)' from " + objectDescription)
170-
// Start observing the notification with specified name and object.
171-
var observer: NSObjectProtocol? = nil
172-
func removeObserver() {
173-
if let observer = observer {
174-
NotificationCenter.default.removeObserver(observer)
175-
}
176-
}
177-
178-
weak var weakExpectation = expectation
179-
observer = NotificationCenter
180-
.default
181-
.addObserver(forName: Notification.Name(rawValue: notificationName),
182-
object: objectToObserve,
183-
queue: nil,
184-
usingBlock: {
185-
notification in
186-
guard let expectation = weakExpectation else {
187-
removeObserver()
188-
return
189-
}
190-
191-
// If the handler is invoked, the test will
192-
// only pass if true is returned.
193-
if let handler = handler {
194-
if handler(notification) {
195-
expectation.fulfill()
196-
removeObserver()
197-
}
198-
} else {
199-
expectation.fulfill()
200-
removeObserver()
201-
}
202-
})
203-
204-
return expectation
205-
}
206-
207-
/// Creates and returns an expectation that is fulfilled if the predicate
208-
/// returns true when evaluated with the given object. The expectation
209-
/// periodically evaluates the predicate and also may use notifications or
210-
/// other events to optimistically re-evaluate.
211-
///
212-
/// - Parameter predicate: The predicate that will be used to evaluate the
213-
/// object.
214-
/// - Parameter object: The object that is evaluated against the conditions
215-
/// specified by the predicate.
216-
/// - Parameter file: The file name to use in the error message if
217-
/// this expectation is not waited for. Default is the file
218-
/// containing the call to this method. It is rare to provide this
219-
/// parameter when calling this method.
220-
/// - Parameter line: The line number to use in the error message if the
221-
/// this expectation is not waited for. Default is the line
222-
/// number of the call to this method in the calling file. It is rare to
223-
/// provide this parameter when calling this method.
224-
/// - Parameter handler: A block to be invoked when evaluating the predicate
225-
/// against the object returns true. If the block is not provided the
226-
/// first successful evaluation will fulfill the expectation. If provided,
227-
/// the handler can override that behavior which leaves the caller
228-
/// responsible for fulfilling the expectation.
229-
@discardableResult func expectation(for predicate: NSPredicate, evaluatedWith object: AnyObject, file: StaticString = #file, line: UInt = #line, handler: XCPredicateExpectationHandler? = nil) -> XCTestExpectation {
230-
let expectation = XCPredicateExpectation(
231-
predicate: predicate,
232-
object: object,
233-
description: "Expect `\(predicate)` for object \(object)",
234-
file: file,
235-
line: line,
236-
testCase: self,
237-
handler: handler)
238-
_allExpectations.append(expectation)
239-
expectation.considerFulfilling()
240-
return expectation
241-
}
242154
}
243155

244156
internal extension XCTestCase {
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// This source file is part of the Swift.org open source project
2+
//
3+
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
4+
// Licensed under Apache License v2.0 with Runtime Library Exception
5+
//
6+
// See http://swift.org/LICENSE.txt for license information
7+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
8+
//
9+
//
10+
// XCTestCase+NotificationExpectation.swift
11+
//
12+
13+
#if os(Linux) || os(FreeBSD)
14+
import Foundation
15+
#else
16+
import SwiftFoundation
17+
#endif
18+
19+
public extension XCTestCase {
20+
/// Creates and returns an expectation for a notification.
21+
///
22+
/// - Parameter notificationName: The name of the notification the
23+
/// expectation observes.
24+
/// - Parameter object: The object whose notifications the expectation will
25+
/// receive; that is, only notifications with this object are observed by
26+
/// the test case. If you pass nil, the expectation doesn't use
27+
/// a notification's object to decide whether it is fulfilled.
28+
/// - Parameter handler: If provided, the handler will be invoked when the
29+
/// notification is observed. It will not be invoked on timeout. Use the
30+
/// handler to further investigate if the notification fulfills the
31+
/// expectation.
32+
@discardableResult func expectation(forNotification notificationName: String, object objectToObserve: AnyObject?, handler: XCNotificationExpectationHandler? = nil) -> XCTestExpectation {
33+
let objectDescription = objectToObserve == nil ? "any object" : "\(objectToObserve!)"
34+
let expectation = self.expectation(description: "Expect notification '\(notificationName)' from " + objectDescription)
35+
// Start observing the notification with specified name and object.
36+
var observer: NSObjectProtocol? = nil
37+
func removeObserver() {
38+
if let observer = observer {
39+
NotificationCenter.default.removeObserver(observer)
40+
}
41+
}
42+
43+
weak var weakExpectation = expectation
44+
observer = NotificationCenter
45+
.default
46+
.addObserver(forName: Notification.Name(rawValue: notificationName),
47+
object: objectToObserve,
48+
queue: nil,
49+
usingBlock: {
50+
notification in
51+
guard let expectation = weakExpectation else {
52+
removeObserver()
53+
return
54+
}
55+
56+
// If the handler is invoked, the test will
57+
// only pass if true is returned.
58+
if let handler = handler {
59+
if handler(notification) {
60+
expectation.fulfill()
61+
removeObserver()
62+
}
63+
} else {
64+
expectation.fulfill()
65+
removeObserver()
66+
}
67+
})
68+
69+
return expectation
70+
}
71+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// This source file is part of the Swift.org open source project
2+
//
3+
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
4+
// Licensed under Apache License v2.0 with Runtime Library Exception
5+
//
6+
// See http://swift.org/LICENSE.txt for license information
7+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
8+
//
9+
//
10+
// XCTestCase+PredicateExpectation.swift
11+
//
12+
13+
#if os(Linux) || os(FreeBSD)
14+
import Foundation
15+
#else
16+
import SwiftFoundation
17+
#endif
18+
19+
public extension XCTestCase {
20+
/// Creates and returns an expectation that is fulfilled if the predicate
21+
/// returns true when evaluated with the given object. The expectation
22+
/// periodically evaluates the predicate and also may use notifications or
23+
/// other events to optimistically re-evaluate.
24+
///
25+
/// - Parameter predicate: The predicate that will be used to evaluate the
26+
/// object.
27+
/// - Parameter object: The object that is evaluated against the conditions
28+
/// specified by the predicate.
29+
/// - Parameter file: The file name to use in the error message if
30+
/// this expectation is not waited for. Default is the file
31+
/// containing the call to this method. It is rare to provide this
32+
/// parameter when calling this method.
33+
/// - Parameter line: The line number to use in the error message if the
34+
/// this expectation is not waited for. Default is the line
35+
/// number of the call to this method in the calling file. It is rare to
36+
/// provide this parameter when calling this method.
37+
/// - Parameter handler: A block to be invoked when evaluating the predicate
38+
/// against the object returns true. If the block is not provided the
39+
/// first successful evaluation will fulfill the expectation. If provided,
40+
/// the handler can override that behavior which leaves the caller
41+
/// responsible for fulfilling the expectation.
42+
@discardableResult func expectation(for predicate: NSPredicate, evaluatedWith object: AnyObject, file: StaticString = #file, line: UInt = #line, handler: XCPredicateExpectationHandler? = nil) -> XCTestExpectation {
43+
let expectation = XCPredicateExpectation(
44+
predicate: predicate,
45+
object: object,
46+
description: "Expect `\(predicate)` for object \(object)",
47+
file: file,
48+
line: line,
49+
testCase: self,
50+
handler: handler)
51+
_allExpectations.append(expectation)
52+
expectation.considerFulfilling()
53+
return expectation
54+
}
55+
}

0 commit comments

Comments
 (0)