Permalink
Browse files

Tests: Switch Relay to BehaviorSubject / BehaviorRelay

  • Loading branch information...
freak4pc authored and kzaher committed Aug 15, 2018
1 parent a7e9fd5 commit 1caef4080e0152f87d23984faf8a00c5f25cc37a
@@ -222,10 +222,10 @@ final class DriverTest_ : DriverTest, RxTestCase {
("testDriveObserver", DriverTest.testDriveObserver),
("testDriveOptionalObserver", DriverTest.testDriveOptionalObserver),
("testDriveNoAmbiguity", DriverTest.testDriveNoAmbiguity),
("testDriveVariable", DriverTest.testDriveVariable),
("testDriveOptionalVariable1", DriverTest.testDriveOptionalVariable1),
("testDriveOptionalVariable2", DriverTest.testDriveOptionalVariable2),
("testDriveVariableNoAmbiguity", DriverTest.testDriveVariableNoAmbiguity),
("testDriveRelay", DriverTest.testDriveRelay),
("testDriveOptionalRelay1", DriverTest.testDriveOptionalRelay1),
("testDriveOptionalRelay2", DriverTest.testDriveOptionalRelay2),
("testDriveRelayNoAmbiguity", DriverTest.testDriveRelayNoAmbiguity),
("testDriveBehaviorRelay", DriverTest.testDriveBehaviorRelay),
("testDriveBehaviorRelay1", DriverTest.testDriveBehaviorRelay1),
("testDriveBehaviorRelay2", DriverTest.testDriveBehaviorRelay2),
@@ -1862,7 +1862,7 @@ final class SignalTests_ : SignalTests, RxTestCase {
("testSignalRelay", SignalTests.testSignalRelay),
("testSignalOptionalRelay1", SignalTests.testSignalOptionalRelay1),
("testSignalOptionalRelay2", SignalTests.testSignalOptionalRelay2),
("testDriveVariableNoAmbiguity", SignalTests.testDriveVariableNoAmbiguity),
("testDriveRelayNoAmbiguity", SignalTests.testDriveRelayNoAmbiguity),
] }
}
@@ -262,22 +262,22 @@ extension DriverTest {
var disposeBag = DisposeBag()
let scheduler = TestScheduler(initialClock: 0)
let observer = scheduler.createObserver(String.self)
let variable = Variable("initial")
let relay = BehaviorRelay(value: "initial")
variable.asDriver()
relay.asDriver()
.drive(observer)
.disposed(by: disposeBag)
prepareSampleDriver(with: "first")
.drive(variable)
.drive(relay)
.disposed(by: disposeBag)
prepareSampleDriver(with: "second")
.drive(variable)
.drive(relay)
.disposed(by: disposeBag)
Observable.just("third")
.bind(to: variable)
.bind(to: relay)
.disposed(by: disposeBag)
disposeBag = DisposeBag()
@@ -293,8 +293,8 @@ extension DriverTest {
func testDrivingOrderOfSynchronousSubscriptions2() {
var latestValue: Int?
let state = Variable(1)
_ = state.asDriver()
let state = BehaviorSubject(value: 1)
let subscription = state.asDriver(onErrorJustReturn: 0)
.flatMapLatest { x in
return Driver.just(x * 2)
}
@@ -311,6 +311,8 @@ extension DriverTest {
latestValue = element
})
subscription.dispose()
XCTAssertEqual(latestValue, 2)
}
}
@@ -356,40 +358,40 @@ extension DriverTest {
}
}
// MARK: drive variable
// MARK: drive relay
extension DriverTest {
func testDriveVariable() {
let variable = Variable<Int>(0)
func testDriveRelay() {
let relay = BehaviorRelay<Int>(value: 0)
_ = (Driver.just(1) as Driver<Int>).drive(variable)
_ = (Driver.just(1) as Driver<Int>).drive(relay)
XCTAssertEqual(variable.value, 1)
XCTAssertEqual(relay.value, 1)
}
func testDriveOptionalVariable1() {
let variable = Variable<Int?>(0)
func testDriveOptionalRelay1() {
let relay = BehaviorRelay<Int?>(value: 0)
_ = (Driver.just(1) as Driver<Int>).drive(variable)
_ = (Driver.just(1) as Driver<Int>).drive(relay)
XCTAssertEqual(variable.value, 1)
XCTAssertEqual(relay.value, 1)
}
func testDriveOptionalVariable2() {
let variable = Variable<Int?>(0)
func testDriveOptionalRelay2() {
let relay = BehaviorRelay<Int?>(value: 0)
_ = (Driver.just(1) as Driver<Int?>).drive(variable)
_ = (Driver.just(1) as Driver<Int?>).drive(relay)
XCTAssertEqual(variable.value, 1)
XCTAssertEqual(relay.value, 1)
}
func testDriveVariableNoAmbiguity() {
let variable = Variable<Int?>(0)
func testDriveRelayNoAmbiguity() {
let relay = BehaviorRelay<Int?>(value: 0)
// shouldn't cause compile time error
_ = Driver.just(1).drive(variable)
_ = Driver.just(1).drive(relay)
XCTAssertEqual(variable.value, 1)
XCTAssertEqual(relay.value, 1)
}
}
@@ -18,7 +18,7 @@ extension RxTest {
func ensurePropertyDeallocated<C, T>(_ createControl: () -> C, _ initialValue: T, comparer: (T, T) -> Bool, file: StaticString = #file, line: UInt = #line, _ propertySelector: (C) -> ControlProperty<T>) where C: NSObject {
let variable = Variable(initialValue)
let relay = BehaviorRelay(value: initialValue)
var completed = false
var deallocated = false
@@ -29,7 +29,7 @@ extension RxTest {
let property = propertySelector(control)
let disposable = variable.asObservable().bind(to: property)
let disposable = relay.bind(to: property)
_ = property.subscribe(onNext: { n in
lastReturnedPropertyValue = n
@@ -324,7 +324,7 @@ extension SignalTests {
}
}
// MARK: emit variable
// MARK: Emit to relay
extension SignalTests {
func testSignalRelay() {
@@ -366,7 +366,7 @@ extension SignalTests {
XCTAssertEqual(latest, 1)
}
func testDriveVariableNoAmbiguity() {
func testDriveRelayNoAmbiguity() {
let relay = PublishRelay<Int?>()
var latest: Int? = nil
@@ -17,20 +17,20 @@ final class UIActivityIndicatorViewTests: RxTest {
extension UIActivityIndicatorViewTests {
func testActivityIndicator_HasWeakReference() {
ensureControlObserverHasWeakReference(UIActivityIndicatorView(), { (view: UIActivityIndicatorView) -> AnyObserver<Bool> in view.rx.isAnimating.asObserver() }, { Variable<Bool>(true).asObservable() })
ensureControlObserverHasWeakReference(UIActivityIndicatorView(), { (view: UIActivityIndicatorView) -> AnyObserver<Bool> in view.rx.isAnimating.asObserver() }, { BehaviorRelay<Bool>(value: true).asObservable() })
}
func testActivityIndicator_NextElementsSetsValue() {
let subject = UIActivityIndicatorView()
let boolSequence = Variable<Bool>(false)
let boolSequence = BehaviorRelay<Bool>(value: false)
let disposable = boolSequence.asObservable().bind(to: subject.rx.isAnimating)
defer { disposable.dispose() }
boolSequence.value = true
boolSequence.accept(true)
XCTAssertTrue(subject.isAnimating, "Expected animation to be started")
boolSequence.value = false
boolSequence.accept(false)
XCTAssertFalse(subject.isAnimating, "Expected animation to be stopped")
}
}
@@ -17,16 +17,16 @@ final class UIProgressViewTests: RxTest {
extension UIProgressViewTests {
func testProgressView_HasWeakReference() {
ensureControlObserverHasWeakReference(UIProgressView(), { (progressView: UIProgressView) -> AnyObserver<Float> in progressView.rx.progress.asObserver() }, { Variable<Float>(0.0).asObservable() })
ensureControlObserverHasWeakReference(UIProgressView(), { (progressView: UIProgressView) -> AnyObserver<Float> in progressView.rx.progress.asObserver() }, { BehaviorRelay<Float>(value: 0.0).asObservable() })
}
func testProgressView_NextElementsSetsValue() {
let subject = UIProgressView()
let progressSequence = Variable<Float>(0.0)
let progressSequence = BehaviorRelay<Float>(value: 0.0)
let disposable = progressSequence.asObservable().bind(to: subject.rx.progress)
defer { disposable.dispose() }
progressSequence.value = 1.0
progressSequence.accept(1.0)
XCTAssert(subject.progress == progressSequence.value, "Expected progress to have been set")
}
}

0 comments on commit 1caef40

Please sign in to comment.