-
Notifications
You must be signed in to change notification settings - Fork 54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add a test displaying a system used as a feedback of another system #15
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -128,4 +128,58 @@ class CombineFeedbackTests: XCTestCase { | |
|
||
XCTAssertEqual("initial_a", value) | ||
} | ||
|
||
func test_system_in_a_system() { | ||
let scheduler = TestScheduler() | ||
|
||
let feedback1 = Feedback<String, String>(effects: { state -> AnyPublisher<String, Never> in | ||
if state.count % 3 == 1 { | ||
return Just("a").eraseToAnyPublisher() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would suggest to add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the best example I could write at the time that uses simple string manipulation to display a system within a system. The outer system sends the inner system a string, the inner system accepts the string and returns another string back to the outer system as an event. The Outer system then appends the string to it's state and then because we've reached a point in the inner systems where it will no longer send a new event. Looking at it 20 days later I've got to ask myself if it is a good choice for an example, not really? But it certainly is okay for testing that things should work. However if I tried to keep your system of using Any other recommendations? I can go farther, not use a string as the state but and make the internal state model something else, but I'd like your approval on that stylistically first since it will no longer be a file of test that always uses a simple string as it's state |
||
} else { | ||
return Empty<String,Never>().eraseToAnyPublisher() | ||
} | ||
}) | ||
|
||
let feedback2 = Feedback<String, String>(effects: { state -> AnyPublisher<String, Never> in | ||
if state.count % 3 == 0 { | ||
return Just("d").eraseToAnyPublisher() | ||
} else { | ||
return Empty<String,Never>().eraseToAnyPublisher() | ||
} | ||
}) | ||
|
||
let innerSystem = Feedback<String, String>{ (stringPublisher: AnyPublisher<String, Never>) -> AnyPublisher<String, Never> in | ||
return stringPublisher.flatMap { (string) -> AnyPublisher<String, Never> in | ||
return Publishers.system(initial: string, | ||
feedbacks: [feedback1, feedback2], | ||
scheduler: scheduler) { (state: String, event: String) -> String in | ||
return state + event | ||
}.filter({ (state) -> Bool in | ||
state.count % 2 == 0 | ||
}).map({ (state) -> String in | ||
return state + "c" | ||
}).eraseToAnyPublisher() | ||
}.eraseToAnyPublisher() | ||
} | ||
|
||
let outerSystem = Publishers.system( | ||
initial: "initial", | ||
feedbacks: [innerSystem], | ||
scheduler: scheduler, | ||
reduce: { (state: String, event: String) -> String in | ||
return event | ||
} | ||
) | ||
|
||
var value: String? | ||
|
||
_ = outerSystem.sink( | ||
receiveValue: { | ||
value = $0 | ||
} | ||
) | ||
|
||
scheduler.advance() | ||
XCTAssertEqual("initialacdc", value) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.