Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rui Peres: Testing FRP Code

Realm
July 08, 2016

Rui Peres: Testing FRP Code

Realm

July 08, 2016
Tweet

More Decks by Realm

Other Decks in Technology

Transcript

  1. 1

  2. 8

  3. 9

  4. 10

  5. 11

  6. 12

  7. 13

  8. 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
  9. let networkCall: SignalProducer<NSData, NSError> = ... let parser: NSData ->

    [Painting] = ... let paintingsProducer = networkCall.map(parser) .startOn(QueueScheduler()) 22
  10. paintingsProducer.startWithNext { paintings in print(paintings) // [!, "] } paintingsProducer.on(

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

  12. 28

  13. 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
  14. 30

  15. 32

  16. 33

  17. 35

  18. 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
  19. 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
  20. 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
  21. let producer: SignalProducer<Int, NoError> = ... let stubbedNextValues = StubbedNextValues(nextValues:

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