-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored
flatMapSuccess(_:)
to utilize flatMap(_:)
, and added `…
…flatMapError(_:)` to the collection. (#7) Also: - Added convenience computed `var error: E?` to StreamState. - Added tests for flatMap methods - Added tests for `Subscriber.combine(_:_:)` - Added additional test helpers for testing Futures and Subscribers.
- Loading branch information
1 parent
c7acd6d
commit 5d0469e
Showing
7 changed files
with
286 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
// FlatMapTests.swift | ||
// Created by Jacob Hawken on 8/13/22. | ||
|
||
import Propagate | ||
import XCTest | ||
|
||
class FlatMapTests: XCTestCase { | ||
|
||
private var promise: Promise<Int,TestError>! | ||
private var future: Future<Int,TestError>! | ||
|
||
override func setUp() { | ||
promise = .init() | ||
future = promise.future | ||
} | ||
|
||
override func tearDown() { | ||
promise = nil | ||
future = nil | ||
} | ||
|
||
func testFlatMapTriggersSecondFuture() { | ||
var flatMappedFuture = future.flatMap { result in | ||
mapIntResultToStringFuture(result) | ||
} | ||
promise.resolve(5) | ||
waitForCompletion(of: flatMappedFuture) | ||
XCTAssertEqual(flatMappedFuture.value, "5") | ||
|
||
promise = .init() | ||
future = promise.future | ||
flatMappedFuture = future.flatMap { result in | ||
mapIntResultToStringFuture(result) | ||
} | ||
promise.reject(TestError.case1) | ||
waitForCompletion(of: flatMappedFuture) | ||
XCTAssertEqual(flatMappedFuture.error, OtherTestError.case1) | ||
} | ||
|
||
func testFlatMapSuccessTriggersSecondFuture() { | ||
let flatMappedFuture = future.flatMapSuccess { int in | ||
triggerFutureForInt(int, shouldSucceed: true) | ||
} | ||
promise.resolve(3) | ||
waitForCompletion(of: flatMappedFuture) | ||
XCTAssertEqual(flatMappedFuture.value, "3") | ||
} | ||
|
||
func testFlatMapSuccessReturnsErrorOfSecondFuture() { | ||
let flatMappedFuture = future.flatMapSuccess { int in | ||
triggerFutureForInt(int, shouldSucceed: false) | ||
} | ||
promise.resolve(3) | ||
waitForCompletion(of: flatMappedFuture) | ||
XCTAssertEqual(flatMappedFuture.error, TestError.case1) | ||
} | ||
|
||
func testFlatMapSuccessPassesThroughError() { | ||
let flatMappedFuture = future.flatMapSuccess { int in | ||
triggerFutureForInt(int, shouldSucceed: true) | ||
} | ||
promise.reject(TestError.case2) | ||
waitForCompletion(of: flatMappedFuture) | ||
XCTAssertEqual(flatMappedFuture.error, TestError.case2) | ||
} | ||
|
||
func testFlatMapErrorTriggersSecondFuture() { | ||
let flatMappedFuture = future.flatMapError { error in | ||
triggerFutureForError(error, shouldSucceed: true) | ||
} | ||
promise.reject(.case1) | ||
waitForCompletion(of: flatMappedFuture) | ||
XCTAssertEqual(flatMappedFuture.value, 69) | ||
} | ||
|
||
func testFlatMapErrorReturnsErrorOfSecondFuture() { | ||
let flatMappedFuture = future.flatMapError { error in | ||
triggerFutureForError(error, shouldSucceed: false) | ||
} | ||
promise.reject(TestError.case2) | ||
waitForCompletion(of: flatMappedFuture) | ||
XCTAssertEqual(flatMappedFuture.error, OtherTestError.case2) | ||
} | ||
|
||
func testFlatMapErrorPassesThroughSuccess() { | ||
let flatMappedFuture = future.flatMapError { error in | ||
triggerFutureForError(error, shouldSucceed: false) | ||
} | ||
promise.resolve(17) | ||
waitForCompletion(of: flatMappedFuture) | ||
XCTAssertEqual(flatMappedFuture.value, 17) | ||
} | ||
|
||
} | ||
|
||
private func mapIntResultToStringFuture(_ result: Result<Int,TestError>) -> Future<String,OtherTestError> { | ||
switch result { | ||
case .success(let int): | ||
return .of("\(int)") | ||
case .failure(let error): | ||
switch error { | ||
case .case1: | ||
return .error(.case1) | ||
case .case2: | ||
return .error(.case2) | ||
} | ||
} | ||
} | ||
|
||
private func triggerFutureForInt(_ int: Int, shouldSucceed: Bool) -> Future<String,TestError> { | ||
if shouldSucceed { | ||
return .of("\(int)") | ||
} | ||
return .error(.case1) | ||
} | ||
|
||
private func triggerFutureForError(_ error: TestError, shouldSucceed: Bool) -> Future<Int,OtherTestError> { | ||
if shouldSucceed { | ||
return .of(69) | ||
} | ||
switch error { | ||
case .case1: | ||
return .error(.case1) | ||
case .case2: | ||
return .error(.case2) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters