Skip to content

Commit

Permalink
Fix linux build
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffh committed Feb 3, 2016
1 parent ad4b081 commit 498064d
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 23 deletions.
11 changes: 6 additions & 5 deletions Sources/Nimble/Matchers/BeIdenticalTo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Foundation

/// A Nimble matcher that succeeds when the actual value is the same instance
/// as the expected instance.
public func beIdenticalTo<T: AnyObject>(expected: T?) -> NonNilMatcherFunc<T> {
public func beIdenticalTo(expected: AnyObject?) -> NonNilMatcherFunc<AnyObject> {
return NonNilMatcherFunc { actualExpression, failureMessage in
let actual = try actualExpression.evaluate()
failureMessage.actualValue = "\(identityAsString(actual))"
Expand All @@ -12,26 +12,27 @@ public func beIdenticalTo<T: AnyObject>(expected: T?) -> NonNilMatcherFunc<T> {
}
}

public func ===<T: AnyObject>(lhs: Expectation<T>, rhs: T?) {
public func ===(lhs: Expectation<AnyObject>, rhs: AnyObject?) {
lhs.to(beIdenticalTo(rhs))
}
public func !==<T: AnyObject>(lhs: Expectation<T>, rhs: T?) {
public func !==(lhs: Expectation<AnyObject>, rhs: AnyObject?) {
lhs.toNot(beIdenticalTo(rhs))
}

/// A Nimble matcher that succeeds when the actual value is the same instance
/// as the expected instance.
///
/// Alias for "beIdenticalTo".
public func be<T: AnyObject>(expected: T?) -> NonNilMatcherFunc<T> {
public func be(expected: AnyObject?) -> NonNilMatcherFunc<AnyObject> {
return beIdenticalTo(expected)
}

#if _runtime(_ObjC)
extension NMBObjCMatcher {
public class func beIdenticalToMatcher(expected: NSObject?) -> NMBObjCMatcher {
return NMBObjCMatcher(canMatchNil: false) { actualExpression, failureMessage in
return try! beIdenticalTo(expected).matches(actualExpression, failureMessage: failureMessage)
let aExpr = actualExpression.cast { $0 as AnyObject? }
return try! beIdenticalTo(expected).matches(aExpr, failureMessage: failureMessage)
}
}
}
Expand Down
25 changes: 19 additions & 6 deletions Sources/Nimble/Matchers/PostNotification.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,39 @@ import Foundation
internal class NotificationCollector {
private(set) var observedNotifications: [NSNotification]
private let notificationCenter: NSNotificationCenter
#if _runtime(_ObjC)
private var token: AnyObject?
#else
private var token: NSObjectProtocol?
#endif

required init(notificationCenter: NSNotificationCenter) {
self.notificationCenter = notificationCenter
self.observedNotifications = []
}

func startObserving() {
self.token = self.notificationCenter.addObserverForName(nil, object: nil, queue: nil) { [weak self] n -> Void in
self?.observedNotifications.append(n)
self.token = self.notificationCenter.addObserverForName(nil, object: nil, queue: nil) {
// linux-swift gets confused by .append(n)
[weak self] n in self?.observedNotifications += [n]
}
}

deinit {
if let token = self.token {
self.notificationCenter.removeObserver(token)
}
#if _runtime(_ObjC)
if let token = self.token {
self.notificationCenter.removeObserver(token)
}
#else
if let token = self.token as? AnyObject {
self.notificationCenter.removeObserver(token)
}
#endif
}
}

private let mainThread = pthread_self()

public func postNotifications<T where T: Matcher, T.ValueType == [NSNotification]>(
notificationsMatcher: T,
fromNotificationCenter center: NSNotificationCenter = NSNotificationCenter.defaultCenter())
Expand All @@ -35,7 +48,7 @@ public func postNotifications<T where T: Matcher, T.ValueType == [NSNotification
return collector.observedNotifications
}, location: actualExpression.location, withoutCaching: true)

assert(NSThread.isMainThread(), "Only expecting closure to be evaluated on main thread.")
assert(pthread_equal(mainThread, pthread_self()) != 0, "Only expecting closure to be evaluated on main thread.")
if !once {
once = true
try actualExpression.evaluate()
Expand Down
8 changes: 6 additions & 2 deletions Sources/NimbleTests/Matchers/BeIdenticalToTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,12 @@ class BeIdenticalToTest: XCTestCase, XCTestCaseProvider {

func testBeAlias() {
expect(NSNumber(integer:1)).to(be(NSNumber(integer:1)))
expect(NSNumber(integer:1)).toNot(be("turtles"))
expect([1]).toNot(be([1]))
expect(NSNumber(integer:1)).toNot(be(NSString(stringLiteral: "turtles")))
#if _runtime(_ObjC)
expect([1]).toNot(be([1]))
#else
expect(NSArray(array: [NSNumber(integer: 1)])).toNot(beIdenticalTo(NSArray(array: [NSNumber(integer: 1)])))
#endif

let value1 = NSArray(array: [])
let value2 = NSArray(array: [])
Expand Down
37 changes: 27 additions & 10 deletions Sources/NimbleTests/Matchers/PostNotificationTest.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
import XCTest
import Nimble
import Foundation

class PostNotificationTest: XCTestCase, XCTestCaseProvider {
var allTests: [(String, () throws -> Void)] {
return [
("testPassesWhenNoNotificationsArePosted", testPassesWhenNoNotificationsArePosted),
("testPassesWhenExpectedNotificationIsPosted", testPassesWhenExpectedNotificationIsPosted),
("testPassesWhenAllExpectedNotificationsArePosted", testPassesWhenAllExpectedNotificationsArePosted),
("testFailsWhenNoNotificationsArePosted", testFailsWhenNoNotificationsArePosted),
("testFailsWhenNotificationWithWrongNameIsPosted", testFailsWhenNotificationWithWrongNameIsPosted),
("testFailsWhenNotificationWithWrongObjectIsPosted", testFailsWhenNotificationWithWrongObjectIsPosted),
("testPassesWhenExpectedNotificationEventuallyIsPosted", testPassesWhenExpectedNotificationEventuallyIsPosted),
]
}

class PostNotificationTest: XCTestCase {
func testPassesWhenNoNotificationsArePosted() {
expect {
// no notifications here!
Expand All @@ -17,8 +30,8 @@ class PostNotificationTest: XCTestCase {
}

func testPassesWhenAllExpectedNotificationsArePosted() {
let foo = NSObject()
let bar = NSObject()
let foo = NSNumber(int: 1)
let bar = NSNumber(int: 2)
let n1 = NSNotification(name: "Foo", object: foo)
let n2 = NSNotification(name: "Bar", object: bar)
expect {
Expand Down Expand Up @@ -61,12 +74,16 @@ class PostNotificationTest: XCTestCase {
}

func testPassesWhenExpectedNotificationEventuallyIsPosted() {
let testNotification = NSNotification(name: "Foo", object: nil)
expect {
deferToMainQueue {
NSNotificationCenter.defaultCenter().postNotification(testNotification)
}
return nil
}.toEventually(postNotifications(equal([testNotification])))
#if _runtime(_ObjC)
let testNotification = NSNotification(name: "Foo", object: nil)
expect {
deferToMainQueue {
NSNotificationCenter.defaultCenter().postNotification(testNotification)
}
return nil
}.toEventually(postNotifications(equal([testNotification])))
#else
print("\(__FUNCTION__) is missing because toEventually is not implement on this platform")
#endif
}
}

0 comments on commit 498064d

Please sign in to comment.