-
-
Save byaruhaf/559533563bf81a5c985a80d18de88859 to your computer and use it in GitHub Desktop.
assertState
This file contains hidden or 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
import Combine | |
import XCTest | |
/// Runs the assertions provided for the publisher variable. | |
public class AssertState { | |
private var subscribers: Set<AnyCancellable> = [] | |
/// Initializes AssertState object. | |
public init() {} | |
} | |
public extension AssertState { | |
/// Runs the assertions provided for the publisher variable. | |
/// Useful for testing ValueState objects. | |
/// - Parameters: | |
/// - when: The method triggering the publisher values. | |
/// - type: The type of the values emitted by the publisher. | |
/// - testCase: The object running the test case. | |
/// - publisher: The publisher emitting the state changes. | |
/// - valuesLimit: The maximum amount of values emitted by the publisher. Default is `3`. | |
/// - initialAssertions: The initial assertions, before executing the `when` closure. | |
/// - valueAssertions: The assertions considering the values emitted by the publisher. | |
func assert<T>( | |
when: () -> Void, | |
type _: T.Type, | |
testCase: XCTestCase, | |
publisher: any Publisher<T, Never>, | |
valuesLimit: Int = 3, | |
initialAssertions: () -> Void, | |
valuesAssertions: @escaping ([T]) -> Void | |
) { | |
var stateValues = [T]() | |
let expectation = XCTestExpectation(description: "Awaits published values, then finishes") | |
initialAssertions() | |
publisher.sink { value in | |
stateValues.append(value) | |
if stateValues.count == valuesLimit { | |
valuesAssertions(stateValues) | |
expectation.fulfill() | |
} else if stateValues.count > valuesLimit { | |
XCTFail("Expecting only \(valuesLimit) values") | |
} | |
}.store(in: &subscribers) | |
when() | |
testCase.wait(for: [expectation], timeout: 0.5) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment