forked from ReactiveCocoa/ReactiveSwift
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ReactiveCocoa#30 from ReactiveCocoa/any-binding-ta…
…rget Introduce `BindingTarget` (class).
- Loading branch information
Showing
4 changed files
with
162 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import Result | ||
import Nimble | ||
import Quick | ||
@testable import ReactiveSwift | ||
|
||
class UnidirectionalBindingSpec: QuickSpec { | ||
override func spec() { | ||
describe("BindingTarget") { | ||
var token: Lifetime.Token! | ||
var lifetime: Lifetime! | ||
var target: BindingTarget<Int>! | ||
var value: Int! | ||
|
||
beforeEach { | ||
token = Lifetime.Token() | ||
lifetime = Lifetime(token) | ||
target = BindingTarget(lifetime: lifetime, setter: { value = $0 }) | ||
value = nil | ||
} | ||
|
||
it("should pass through the lifetime") { | ||
expect(target.lifetime).to(beIdenticalTo(lifetime)) | ||
} | ||
|
||
it("should trigger the supplied setter") { | ||
expect(value).to(beNil()) | ||
|
||
target.consume(1) | ||
expect(value) == 1 | ||
} | ||
|
||
it("should accept bindings from properties") { | ||
expect(value).to(beNil()) | ||
|
||
let property = MutableProperty(1) | ||
target <~ property | ||
expect(value) == 1 | ||
|
||
property.value = 2 | ||
expect(value) == 2 | ||
} | ||
|
||
it("should not deadlock on the same queue") { | ||
target = BindingTarget(on: .main, | ||
lifetime: lifetime, | ||
setter: { value = $0 }) | ||
|
||
let property = MutableProperty(1) | ||
target <~ property | ||
expect(value) == 1 | ||
} | ||
|
||
it("should not deadlock even if the value is originated from the same queue indirectly") { | ||
let key = DispatchSpecificKey<Void>() | ||
DispatchQueue.main.setSpecific(key: key, value: ()) | ||
|
||
let mainQueueCounter = Atomic(0) | ||
|
||
let setter: (Int) -> Void = { | ||
value = $0 | ||
mainQueueCounter.modify { $0 += DispatchQueue.getSpecific(key: key) != nil ? 1 : 0 } | ||
} | ||
|
||
target = BindingTarget(on: .main, | ||
lifetime: lifetime, | ||
setter: setter) | ||
|
||
let scheduler: QueueScheduler | ||
if #available(OSX 10.10, *) { | ||
scheduler = QueueScheduler() | ||
} else { | ||
scheduler = QueueScheduler(queue: DispatchQueue(label: "com.reactivecocoa.ReactiveSwift.UnidirectionalBindingSpec")) | ||
} | ||
|
||
let property = MutableProperty(1) | ||
target <~ property.producer | ||
.start(on: scheduler) | ||
.observe(on: scheduler) | ||
|
||
expect(value).toEventually(equal(1)) | ||
expect(mainQueueCounter.value).toEventually(equal(1)) | ||
|
||
property.value = 2 | ||
expect(value).toEventually(equal(2)) | ||
expect(mainQueueCounter.value).toEventually(equal(2)) | ||
} | ||
} | ||
} | ||
} |