Skip to content

Commit

Permalink
Merge pull request #819 from Quick/make-postnotifications-generic
Browse files Browse the repository at this point in the history
[BREAKING] Make `postNotifications` generic and usable with non-void closures
  • Loading branch information
ikesyo authored Sep 9, 2020
2 parents 7b8fe7f + acfd2d4 commit a5ed063
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 15 deletions.
16 changes: 8 additions & 8 deletions Sources/Nimble/Matchers/PostNotification.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ internal class NotificationCollector {

private let mainThread = pthread_self()

private func _postNotifications(
private func _postNotifications<Out>(
_ predicate: Predicate<[Notification]>,
from center: NotificationCenter,
names: Set<Notification.Name> = []
) -> Predicate<Any> {
) -> Predicate<Out> {
_ = mainThread // Force lazy-loading of this value
let collector = NotificationCollector(notificationCenter: center, names: names)
collector.startObserving()
Expand Down Expand Up @@ -80,27 +80,27 @@ private func _postNotifications(
}
}

public func postNotifications(
public func postNotifications<Out>(
_ predicate: Predicate<[Notification]>,
from center: NotificationCenter = .default
) -> Predicate<Any> {
) -> Predicate<Out> {
_postNotifications(predicate, from: center)
}

@available(*, deprecated, renamed: "postNotifications(_:from:)")
public func postNotifications(
public func postNotifications<Out>(
_ predicate: Predicate<[Notification]>,
fromNotificationCenter center: NotificationCenter
) -> Predicate<Any> {
) -> Predicate<Out> {
postNotifications(predicate, from: center)
}

#if os(macOS)
public func postDistributedNotifications(
public func postDistributedNotifications<Out>(
_ predicate: Predicate<[Notification]>,
from center: DistributedNotificationCenter = .default(),
names: Set<Notification.Name>
) -> Predicate<Any> {
) -> Predicate<Out> {
_postNotifications(predicate, from: center, names: names)
}
#endif
Expand Down
8 changes: 1 addition & 7 deletions Tests/NimbleTests/Matchers/PostNotificationTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ final class PostNotificationTest: XCTestCase {
func testPassesWhenNoNotificationsArePosted() {
expect {
// no notifications here!
return nil
}.to(postNotifications(beEmpty()))
}

Expand All @@ -24,10 +23,9 @@ final class PostNotificationTest: XCTestCase {
let bar = 2 as NSNumber
let n1 = Notification(name: Notification.Name("Foo"), object: foo)
let n2 = Notification(name: Notification.Name("Bar"), object: bar)
expect {
expect { () -> Void in
self.notificationCenter.post(n1)
self.notificationCenter.post(n2)
return nil
}.to(postNotifications(equal([n1, n2]), from: notificationCenter))
}

Expand All @@ -36,7 +34,6 @@ final class PostNotificationTest: XCTestCase {
failsWithErrorMessage("expected to equal <[\(testNotification)]>, got no notifications") {
expect {
// no notifications here!
return nil
}.to(postNotifications(equal([testNotification]), from: self.notificationCenter))
}
}
Expand All @@ -47,7 +44,6 @@ final class PostNotificationTest: XCTestCase {
failsWithErrorMessage("expected to equal <[\(n1)]>, got <[\(n2)]>") {
expect {
self.notificationCenter.post(n2)
return nil
}.to(postNotifications(equal([n1]), from: self.notificationCenter))
}
}
Expand All @@ -58,7 +54,6 @@ final class PostNotificationTest: XCTestCase {
failsWithErrorMessage("expected to equal <[\(n1)]>, got <[\(n2)]>") {
expect {
self.notificationCenter.post(n2)
return nil
}.to(postNotifications(equal([n1]), from: self.notificationCenter))
}
}
Expand All @@ -69,7 +64,6 @@ final class PostNotificationTest: XCTestCase {
deferToMainQueue {
self.notificationCenter.post(testNotification)
}
return nil
}.toEventually(postNotifications(equal([testNotification]), from: notificationCenter))
}

Expand Down

0 comments on commit a5ed063

Please sign in to comment.