|
| 1 | +import XCTest |
| 2 | +import Nimble |
| 3 | +import ReactiveSwift |
| 4 | +@testable import Loop |
| 5 | + |
| 6 | +class FeedbackVariantTests: XCTestCase { |
| 7 | + func test_whenBecomesTrue_positive_edge() { |
| 8 | + var receivedValues: [String] = [] |
| 9 | + |
| 10 | + let loop = Loop<String, String>( |
| 11 | + initial: "", |
| 12 | + reducer: { content, string in |
| 13 | + content.append(contentsOf: string) |
| 14 | + }, |
| 15 | + feedbacks: [ |
| 16 | + .whenBecomesTrue( |
| 17 | + { $0.hasSuffix("_") }, |
| 18 | + effects: { state -> SignalProducer<String, Never> in |
| 19 | + receivedValues.append(state) |
| 20 | + |
| 21 | + return SignalProducer(value: "feedback_") |
| 22 | + } |
| 23 | + ) |
| 24 | + ] |
| 25 | + ) |
| 26 | + |
| 27 | + expect(loop.box._current) == "" |
| 28 | + |
| 29 | + loop.send("hello") |
| 30 | + expect(loop.box._current) == "hello" |
| 31 | + expect(receivedValues.last).to(beNil()) |
| 32 | + |
| 33 | + // This should trigger a positive edge in `whenBecomesTrue`. |
| 34 | + loop.send("_") |
| 35 | + expect(loop.box._current) == "hello_feedback_" |
| 36 | + expect(receivedValues.last) == "hello_" |
| 37 | + |
| 38 | + // The predicate stays `true`, so no transition should occur. |
| 39 | + loop.send("_") |
| 40 | + expect(loop.box._current) == "hello_feedback__" |
| 41 | + expect(receivedValues.last) == "hello_" |
| 42 | + |
| 43 | + // This should lead to a negative edge. |
| 44 | + loop.send("world") |
| 45 | + expect(loop.box._current) == "hello_feedback__world" |
| 46 | + expect(receivedValues.last) == "hello_" |
| 47 | + |
| 48 | + // This should trigger a positive edge again in `whenBecomesTrue`. |
| 49 | + loop.send("_") |
| 50 | + expect(loop.box._current) == "hello_feedback__world_feedback_" |
| 51 | + expect(receivedValues.last) == "hello_feedback__world_" |
| 52 | + } |
| 53 | + |
| 54 | + func test_whenBecomesTrue_negative_edge() { |
| 55 | + var hasStarted = false |
| 56 | + var hasCancelled = false |
| 57 | + |
| 58 | + let loop = Loop<String, String>( |
| 59 | + initial: "", |
| 60 | + reducer: { content, string in |
| 61 | + content.append(contentsOf: string) |
| 62 | + }, |
| 63 | + feedbacks: [ |
| 64 | + .whenBecomesTrue( |
| 65 | + { $0.hasSuffix("_") }, |
| 66 | + effects: { _ in |
| 67 | + SignalProducer.never |
| 68 | + .on(started: { hasStarted = true }) |
| 69 | + .on(disposed: { hasCancelled = true }) |
| 70 | + } |
| 71 | + ) |
| 72 | + ] |
| 73 | + ) |
| 74 | + |
| 75 | + expect(loop.box._current) == "" |
| 76 | + expect(hasStarted) == false |
| 77 | + |
| 78 | + // This should trigger a positive edge in `whenBecomesTrue`. |
| 79 | + loop.send("_") |
| 80 | + expect(hasStarted) == true |
| 81 | + expect(hasCancelled) == false |
| 82 | + |
| 83 | + loop.send("_") |
| 84 | + expect(hasCancelled) == false |
| 85 | + |
| 86 | + loop.send("_") |
| 87 | + expect(hasCancelled) == false |
| 88 | + |
| 89 | + // This should lead to a negative edge, which in turn should cancel the effect. |
| 90 | + loop.send("word") |
| 91 | + expect(hasCancelled) == true |
| 92 | + } |
| 93 | + |
| 94 | + func test_firstValueAfterNil_positive_edge() { |
| 95 | + var receivedValues: [String] = [] |
| 96 | + |
| 97 | + let loop = Loop<String, String>( |
| 98 | + initial: "", |
| 99 | + reducer: { content, string in |
| 100 | + content = string |
| 101 | + }, |
| 102 | + feedbacks: [ |
| 103 | + .firstValueAfterNil( |
| 104 | + { $0.hasPrefix("hello") ? $0 : nil }, |
| 105 | + effects: { state -> SignalProducer<String, Never> in |
| 106 | + receivedValues.append(state) |
| 107 | + |
| 108 | + return SignalProducer(value: "\(String(repeating: state, count: 2))") |
| 109 | + } |
| 110 | + ) |
| 111 | + ] |
| 112 | + ) |
| 113 | + |
| 114 | + expect(loop.box._current) == "" |
| 115 | + expect(receivedValues.last).to(beNil()) |
| 116 | + |
| 117 | + // This should trigger a positive edge in `firstValueAfterNil`. |
| 118 | + loop.send("hello#") |
| 119 | + expect(loop.box._current) == "hello#hello#" |
| 120 | + expect(receivedValues.last) == "hello#" |
| 121 | + |
| 122 | + // The transform output stays non-nil, so no transition should occur. |
| 123 | + loop.send("hello_world") |
| 124 | + expect(loop.box._current) == "hello_world" |
| 125 | + expect(receivedValues.last) == "hello#" |
| 126 | + |
| 127 | + // This should lead to a negative edge. |
| 128 | + loop.send("goodbye") |
| 129 | + expect(loop.box._current) == "goodbye" |
| 130 | + expect(receivedValues.last) == "hello#" |
| 131 | + |
| 132 | + // This should trigger a positive edge again in `firstValueAfterNil`. |
| 133 | + loop.send("hello_it_is_me#") |
| 134 | + expect(loop.box._current) == "hello_it_is_me#hello_it_is_me#" |
| 135 | + expect(receivedValues.last) == "hello_it_is_me#" |
| 136 | + } |
| 137 | + |
| 138 | + func test_firstValueAfterNil_negative_edge() { |
| 139 | + var hasStarted = false |
| 140 | + var hasCancelled = false |
| 141 | + |
| 142 | + let loop = Loop<String, String>( |
| 143 | + initial: "", |
| 144 | + reducer: { content, string in |
| 145 | + content = string |
| 146 | + }, |
| 147 | + feedbacks: [ |
| 148 | + .firstValueAfterNil( |
| 149 | + { $0.hasPrefix("hello") ? $0 : nil }, |
| 150 | + effects: { _ in |
| 151 | + SignalProducer.never |
| 152 | + .on(started: { hasStarted = true }) |
| 153 | + .on(disposed: { hasCancelled = true }) |
| 154 | + } |
| 155 | + ) |
| 156 | + ] |
| 157 | + ) |
| 158 | + |
| 159 | + expect(loop.box._current) == "" |
| 160 | + expect(hasStarted) == false |
| 161 | + |
| 162 | + // This should trigger a positive edge in `whenBecomesTrue`. |
| 163 | + loop.send("hello1") |
| 164 | + expect(hasStarted) == true |
| 165 | + expect(hasCancelled) == false |
| 166 | + |
| 167 | + loop.send("hello2") |
| 168 | + expect(hasCancelled) == false |
| 169 | + |
| 170 | + loop.send("hello3") |
| 171 | + expect(hasCancelled) == false |
| 172 | + |
| 173 | + // This should lead to a negative edge, which in turn should cancel the effect. |
| 174 | + loop.send("world") |
| 175 | + expect(hasCancelled) == true |
| 176 | + } |
| 177 | +} |
0 commit comments