Rui Peres: Testing FRP Code

1fa9cb8c7997c8c4d3d251fb5e41f749?s=47 Realm
July 08, 2016

Rui Peres: Testing FRP Code

1fa9cb8c7997c8c4d3d251fb5e41f749?s=128

Realm

July 08, 2016
Tweet

Transcript

  1. 1.

    1

  2. 8.

    8

  3. 9.

    9

  4. 10.

    10

  5. 11.

    11

  6. 12.

    12

  7. 13.

    13

  8. 17.
  9. 19.

    let foo = MutableProperty(1) // 1. observe foo foo.signalProducer.startWithNext {

    newFooValue in print(newFooValue) // prints "1" } // 2. modifiy foo's value foo.value = 2 // 3. prints "2" 19
  10. 22.

    let networkCall: SignalProducer<NSData, NSError> = ... let parser: NSData ->

    [Painting] = ... let paintingsProducer = networkCall.map(parser) .startOn(QueueScheduler()) 22
  11. 23.

    paintingsProducer.startWithNext { paintings in print(paintings) // [!, "] } paintingsProducer.on(

    failure: { error in // handle error }, next: { paintings in print(paintings) // [!, "] }) .start() 23
  12. 24.
  13. 25.

    25

  14. 28.

    28

  15. 29.

    let email: MutableProperty<String> = emailTextField.rex_text let password: MutableProperty<String> = passwordTextField.rex_text

    let isValid: (String, String) -> Bool = ... let areCredentialsValid = combineLatest(email.producer, password.producer).map(isValid) loginButton.rex_enabled <~ areCredentialsValid 29
  16. 30.

    30

  17. 32.

    32

  18. 33.

    33

  19. 35.

    35

  20. 36.

    let drinks = SignalProducer<[String], NoError>(value: ["!","!"]) let areSober: [String] ->

    Bool = ... let gamePlan = drinks .concat(drinks.delay(7.5)) .concat(timer(5).flatMapLatest { _ in drinks }.takeWhile(areSober)) 36
  21. 39.

    func testNumberPaintings() { let expectation = self.expectationWithDescription("Expect to have 2

    paintings") defer { self.waitForExpectationsWithTimeout(1.0, handler: nil) } paintingsViewModel.paintingsProducer.startWithNext { paintings in XCTAssertEqual(paintings.count, 2) expectation.fulfill() } } 39
  22. 42.

    final class StubbedNextValues<T: Equatable> { private var nextValues: [T] private

    let expectation: XCTestExpectation init(nextValues: [T] , expectation: XCTestExpectation) { self.nextValues = nextValues self.expectation = expectation } func handleNextValue(nextValue: T) -> Void { guard !nextValues.isEmpty else { fatalError("Can't handle \(nextValue)") } let stubbedValue = nextValues.removeFirst() XCTAssertEqual(stubbedValue, nextValue) if nextValues.isEmpty { expectation.fulfill() } } } 42
  23. 43.

    let producer: SignalProducer<Int, NoError> = ... let stubbedNextValues = StubbedNextValues(nextValues:

    [1, 2, 3, 4], expectation: expectation) producer.startWithNext(stubbed.handleNextValue) 43
  24. 47.

    47