|
| 1 | +// Copyright (c) 2020 Spotify AB. |
| 2 | +// |
| 3 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 4 | +// or more contributor license agreements. See the NOTICE file |
| 5 | +// distributed with this work for additional information |
| 6 | +// regarding copyright ownership. The ASF licenses this file |
| 7 | +// to you under the Apache License, Version 2.0 (the |
| 8 | +// "License"); you may not use this file except in compliance |
| 9 | +// with the License. You may obtain a copy of the License at |
| 10 | +// |
| 11 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +// |
| 13 | +// Unless required by applicable law or agreed to in writing, |
| 14 | +// software distributed under the License is distributed on an |
| 15 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | +// KIND, either express or implied. See the License for the |
| 17 | +// specific language governing permissions and limitations |
| 18 | +// under the License. |
| 19 | + |
| 20 | +import MobiusCore |
| 21 | +import MobiusExtras |
| 22 | +import XCTest |
| 23 | + |
| 24 | +/// Test cases that reproduce the Getting Started section of the GitHub wiki |
| 25 | +class WikiTutorialTest: XCTestCase { |
| 26 | + // swiftlint:disable function_body_length |
| 27 | + |
| 28 | + func testWikiCreatingALoop() { |
| 29 | + // Dummy implementation of print() |
| 30 | + var printedValues: [String] = [] |
| 31 | + func print(_ value: Any) { |
| 32 | + printedValues.append(String(describing: value)) |
| 33 | + } |
| 34 | + |
| 35 | + // ---8<--- Wiki content start |
| 36 | + enum MyEvent { |
| 37 | + case up |
| 38 | + case down |
| 39 | + } |
| 40 | + |
| 41 | + func update(counter: Int, event: MyEvent) -> Int { |
| 42 | + switch event { |
| 43 | + case .up: |
| 44 | + return counter + 1 |
| 45 | + case .down: |
| 46 | + return counter > 0 |
| 47 | + ? counter - 1 |
| 48 | + : counter |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + let loop = Mobius.beginnerLoop(update: update) |
| 53 | + .start(from: 2) |
| 54 | + |
| 55 | + loop.addObserver { counter in print(counter) } |
| 56 | + |
| 57 | + loop.dispatchEvent(.down) // prints "1" |
| 58 | + loop.dispatchEvent(.down) // prints "0" |
| 59 | + loop.dispatchEvent(.down) // prints "0" |
| 60 | + loop.dispatchEvent(.up) // prints "1" |
| 61 | + loop.dispatchEvent(.up) // prints "2" |
| 62 | + loop.dispatchEvent(.down) // prints "1" |
| 63 | + |
| 64 | + loop.dispose() |
| 65 | + // ---8<--- Wiki content end |
| 66 | + |
| 67 | + XCTAssertEqual(printedValues, ["2", "1", "0", "0", "1", "2", "1"]) |
| 68 | + } |
| 69 | + |
| 70 | + func testWikiCreatingALoop_addingEffects() { |
| 71 | + // Dummy implementation of print() |
| 72 | + var printedValues: [String] = [] |
| 73 | + func print(_ value: Any) { |
| 74 | + printedValues.append(String(describing: value)) |
| 75 | + } |
| 76 | + |
| 77 | + // Carried over from previous example |
| 78 | + enum MyEvent { |
| 79 | + case up |
| 80 | + case down |
| 81 | + } |
| 82 | + |
| 83 | + // ---8<--- Wiki content start |
| 84 | + enum MyEffect { |
| 85 | + case reportErrorNegative |
| 86 | + } |
| 87 | + |
| 88 | + func update1(model: Int, event: MyEvent) -> Next<Int, MyEffect> { |
| 89 | + switch event { |
| 90 | + case .up: |
| 91 | + return .next(model + 1) |
| 92 | + case .down: |
| 93 | + return model > 0 |
| 94 | + ? .next(model - 1) |
| 95 | + : .next(model) |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + func update2(model: Int, event: MyEvent) -> Next<Int, MyEffect> { |
| 100 | + switch event { |
| 101 | + case .up: |
| 102 | + return .next(model + 1) |
| 103 | + case .down: |
| 104 | + return model > 0 |
| 105 | + ? .next(model - 1) |
| 106 | + : .next(model, effects: [.reportErrorNegative]) |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + func update(model: Int, event: MyEvent) -> Next<Int, MyEffect> { |
| 111 | + switch event { |
| 112 | + case .up: |
| 113 | + return .next(model + 1) |
| 114 | + case .down: |
| 115 | + return model > 0 |
| 116 | + ? .next(model - 1) |
| 117 | + : .dispatchEffects([.reportErrorNegative]) |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + func handleReportErrorNegative() { |
| 122 | + print("error!") |
| 123 | + } |
| 124 | + |
| 125 | + let effectHandler = EffectRouter<MyEffect, MyEvent>() |
| 126 | + .routeCase(MyEffect.reportErrorNegative).to(handleReportErrorNegative) |
| 127 | + .asConnectable |
| 128 | + |
| 129 | + let loop = Mobius.loop(update: update, effectHandler: effectHandler) |
| 130 | + .start(from: 2) |
| 131 | + |
| 132 | + loop.addObserver { counter in print(counter) } |
| 133 | + |
| 134 | + loop.dispatchEvent(.down) // prints "1" |
| 135 | + loop.dispatchEvent(.down) // prints "0" |
| 136 | + loop.dispatchEvent(.down) // followed by "error!" |
| 137 | + loop.dispatchEvent(.up) // prints "1" |
| 138 | + loop.dispatchEvent(.up) // prints "2" |
| 139 | + loop.dispatchEvent(.down) // prints "1" |
| 140 | + |
| 141 | + loop.dispose() |
| 142 | + // ---8<--- Wiki content end |
| 143 | + |
| 144 | + XCTAssertEqual(printedValues, ["2", "1", "0", "error!", "1", "2", "1"]) |
| 145 | + } |
| 146 | +} |
0 commit comments