Replaces spin lock with expectation mechanism, removes C style for loop, error comparison reuses logic from event comparison.

This commit is contained in:
Krunoslav Zaher 2015-12-25 21:43:05 +01:00
parent e1a2daa0a5
commit 75a528ea6d
6 changed files with 34 additions and 20 deletions

View File

@ -15,12 +15,17 @@ Compares two events. They are equal if they are both the same member of `Event`
In case `Error` events are being compared, they are equal in case their `NSError` representations are equal (domain and code)
and their string representations are equal.
*/
func == <Element: Equatable>(lhs: Event<Element>, rhs: Event<Element>) -> Bool {
public func == <Element: Equatable>(lhs: Event<Element>, rhs: Event<Element>) -> Bool {
switch (lhs, rhs) {
case (.Completed, .Completed): return true
case (.Error(let e1), .Error(let e2)):
let error1 = e1 as NSError
let error2 = e2 as NSError
// if the references are equal, then it's the same object
if let lhsObject = lhs as? AnyObject, rhsObject = rhs as? AnyObject where lhsObject === rhsObject {
return true
}
return error1.domain == error2.domain
&& error1.code == error2.code

View File

@ -18,11 +18,11 @@ func clearRealTest() {
}
func returnSomething() -> Observable<AnyObject?> {
return Observable.just("a")
return Observable.just(NSNull())
}
func returnSomething() -> Observable<CGRect?> {
return Observable.just(CGRectMake(0, 0, 100, 100))
func returnSomething() -> Observable<Int?> {
return Observable.just(3)
}
class AssumptionsTest : RxTest {
@ -66,12 +66,12 @@ class AssumptionsTest : RxTest {
func testFunctionReturnValueOverload() {
_ = returnSomething()
.subscribeNext { (n: AnyObject?) in
XCTAssertEqual("\(n ?? NSNull())", "a")
XCTAssertEqual(n as? NSNull, NSNull())
}
_ = returnSomething()
.subscribeNext { (n: CGRect?) in
XCTAssertEqual(n!, CGRectMake(0, 0, 100, 100))
.subscribeNext { (n: Int?) in
XCTAssertEqual(n!, 3)
}
}

View File

@ -27,7 +27,11 @@ func isPrime(i: Int) -> Bool {
}
let max = Int(sqrt(Float(i)))
for (var j = 2; j <= max; ++j) {
if max <= 1 {
return true
}
for j in 2 ... max {
if i % j == 0 {
return false
}

View File

@ -759,30 +759,32 @@ extension ObservableTimeTest {
let observer = PrimitiveMockObserver<Int64>()
var lock = OS_SPINLOCK_INIT
OSSpinLockLock(&lock)
let expectCompleted = expectationWithDescription("It will complete")
let d = Observable<Int64>.interval(0, scheduler: scheduler).takeWhile { $0 < 10 } .subscribe(onNext: { t in
observer.on(.Next(t))
}, onCompleted: {
OSSpinLockUnlock(&lock)
expectCompleted.fulfill()
})
defer {
d.dispose()
}
OSSpinLockLock(&lock)
OSSpinLockUnlock(&lock)
waitForExpectationsWithTimeout(1.0) { e in
XCTAssert(e == nil, "Did not complete")
}
let cleanResources = expectationWithDescription("Clean resources")
scheduler.schedule(()) { _ in
OSSpinLockUnlock(&lock)
cleanResources.fulfill()
return NopDisposable.instance
}
// wait until dispatch queue cleans it's resources
OSSpinLockLock(&lock)
waitForExpectationsWithTimeout(1.0) { e in
XCTAssert(e == nil, "Did not clean up")
}
XCTAssertTrue(observer.events.count == 10)
}

View File

@ -63,11 +63,11 @@ extension SubjectConcurrencyTest {
}
}
else if state == 1 {
XCTAssertTrue(!NSThread.isMainThread())
XCTAssertTrue(!NSThread.currentThread().isMainThread)
state = 2
}
else if state == 2 {
XCTAssertTrue(NSThread.isMainThread())
XCTAssertTrue(NSThread.currentThread().isMainThread)
allDone = true
}
}

View File

@ -26,7 +26,10 @@ import RxTests
typealias Time = Int
func XCTAssertErrorEqual(lhs: ErrorType, _ rhs: ErrorType) {
XCTAssertTrue(lhs as NSError === rhs as NSError)
let event1: Event<Int> = .Error(lhs)
let event2: Event<Int> = .Error(rhs)
XCTAssertTrue(event1 == event2)
}
func XCTAssertEqualNSValues(lhs: AnyObject, rhs: AnyObject) {