Skip to content

Rename expect(worker:producingOutput:) to be more clear about what it is asserting #323

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

Merged
merged 1 commit into from
Apr 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 10 additions & 14 deletions Samples/TicTacToe/Tests/AuthenticationWorkflowTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,11 @@ class AuthenticationWorkflowTests: XCTestCase {
onLoginTapped: {}
)
)
.expect(
worker: AuthenticationWorkflow.AuthorizingEmailPasswordWorker(
authenticationService: authenticationService,
email: "[email protected]",
password: "password"
)
)
.expectWorker(AuthenticationWorkflow.AuthorizingEmailPasswordWorker(
authenticationService: authenticationService,
email: "[email protected]",
password: "password"
))
.render { screen in
XCTAssertNil(screen.alert)
}
Expand Down Expand Up @@ -252,13 +250,11 @@ class AuthenticationWorkflowTests: XCTestCase {
onLoginTapped: {}
)
)
.expect(
worker: AuthenticationWorkflow.AuthorizingTwoFactorWorker(
authenticationService: authenticationService,
intermediateToken: "intermediateSession",
twoFactorCode: "twoFactorCode"
)
)
.expectWorker(AuthenticationWorkflow.AuthorizingTwoFactorWorker(
authenticationService: authenticationService,
intermediateToken: "intermediateSession",
twoFactorCode: "twoFactorCode"
))
.render { screen in
XCTAssertNil(screen.alert)
}
Expand Down
5 changes: 1 addition & 4 deletions Samples/Tutorial/Tutorial5.md
Original file line number Diff line number Diff line change
Expand Up @@ -334,10 +334,7 @@ workflow
producingRendering: ChildScreen(),
producingOutput: .closed
)
.expect(
worker: TestWorker(),
producingOutput: .finished
)
.expectWorker(TestWorker(), mockingOutput: .finished)
.render { rendering in
XCTAssertEqual("expected text on rendering", rendering.text)
}
Expand Down
30 changes: 25 additions & 5 deletions WorkflowCombine/Testing/WorkerTesting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,49 @@ import XCTest
@testable import WorkflowCombine

extension RenderTester {
/// Expect the given worker. It will be checked for `isEquivalent(to:)` with the requested worker.
/// Mock the given worker's output, and assert that the workflow's worker `isEquivalent(to:)` the `worker`.
///
/// - Parameters:
/// - worker: The worker to be expected
/// - producingOutput: An output that will be returned when this worker is requested, if any.
/// - worker: The worker that we expect was created by the workflow. Will be compared using
/// `isEquivalent(to:)` to assert that the workflow's worker matches.
/// - producingOutput: The output to be used instead of actually running the worker.
/// If the workflow never tries to run the worker, then this won't be used.
/// - key: Key to expect this `Workflow` to be rendered with.
@available(*, deprecated, renamed: "expectedWorker(_:mockingOutput:key:file:line:)", message: "renamed")
public func expect<ExpectedWorkerType: Worker>(
worker: ExpectedWorkerType,
producingOutput output: ExpectedWorkerType.Output? = nil,
key: String = "",
file: StaticString = #file, line: UInt = #line
) -> RenderTester<WorkflowType> {
expectWorker(worker, mockingOutput: output, key: key, file: file, line: line)
}

/// Mock the given worker's output, and assert that the workflow's worker `isEquivalent(to:)` the `expectedWorker`.
///
/// - Parameters:
/// - expectedWorker: The worker that we expect was created by the workflow. Will be compared using
/// `isEquivalent(to:)` to assert that the workflow's worker matches.
/// - mockingOutput: The output to be used instead of actually running the worker.
/// If the workflow never tries to run the worker, then this won't be used.
/// - key: Key to expect this `Workflow` to be rendered with.
public func expectWorker<ExpectedWorkerType: Worker>(
_ expectedWorker: ExpectedWorkerType,
mockingOutput output: ExpectedWorkerType.Output? = nil,
key: String = "",
file: StaticString = #file, line: UInt = #line
) -> RenderTester<WorkflowType> {
expectWorkflow(
type: WorkerWorkflow<ExpectedWorkerType>.self,
key: key,
producingRendering: (),
producingOutput: output,
assertions: { workflow in
guard !workflow.worker.isEquivalent(to: worker) else {
guard !workflow.worker.isEquivalent(to: expectedWorker) else {
return
}
XCTFail(
"Workers of type \(ExpectedWorkerType.self) not equivalent. Expected: \(worker). Got: \(workflow.worker)",
"Workers of type \(ExpectedWorkerType.self) not equivalent. Expected: \(expectedWorker). Got: \(workflow.worker)",
file: file,
line: line
)
Expand Down
15 changes: 4 additions & 11 deletions WorkflowCombine/TestingTests/TestingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class WorkflowCombineTestingTests: XCTestCase {
.renderTester(initialState: .init(mode: .worker(input: "otherText"), output: ""))

renderTester
.expect(worker: TestWorker(input: "otherText"))
.expectWorker(TestWorker(input: "otherText"))
.render { _ in }
}

Expand All @@ -36,10 +36,7 @@ class WorkflowCombineTestingTests: XCTestCase {
.renderTester(initialState: .init(mode: .worker(input: "otherText"), output: ""))

renderTester
.expect(
worker: TestWorker(input: "otherText"),
producingOutput: "otherText"
)
.expectWorker(TestWorker(input: "otherText"), mockingOutput: "otherText")
.render { _ in }
.verifyState { state in
XCTAssertEqual(state, TestWorkflow.State(mode: .worker(input: "otherText"), output: "otherText"))
Expand All @@ -49,9 +46,7 @@ class WorkflowCombineTestingTests: XCTestCase {
func test_worker_missing() {
let tester = TestWorkflow()
.renderTester()
.expect(
worker: TestWorker(input: "input")
)
.expectWorker(TestWorker(input: "input"))

expectingFailure(#"Expected child workflow of type: WorkerWorkflow<TestWorker>, key: """#) {
tester.render { _ in }
Expand All @@ -61,9 +56,7 @@ class WorkflowCombineTestingTests: XCTestCase {
func test_worker_mismatch() {
let tester = TestWorkflow()
.renderTester(initialState: .init(mode: .worker(input: "test"), output: ""))
.expect(
worker: TestWorker(input: "not-test")
)
.expectWorker(TestWorker(input: "not-test"))

expectingFailures([
#"Workers of type TestWorker not equivalent. Expected: TestWorker(input: "not-test"). Got: TestWorker(input: "test")"#,
Expand Down
26 changes: 23 additions & 3 deletions WorkflowConcurrency/Testing/WorkerTesting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,37 @@ import XCTest
@testable import WorkflowConcurrency

extension RenderTester {
/// Expect the given worker. It will be checked for `isEquivalent(to:)` with the requested worker.
/// Mock the given worker's output, and assert that the workflow's worker `isEquivalent(to:)` the `worker`.
///
/// - Parameters:
/// - worker: The worker to be expected
/// - producingOutput: An output that will be returned when this worker is requested, if any.
/// - worker: The worker that we expect was created by the workflow. Will be compared using
/// `isEquivalent(to:)` to assert that the workflow's worker matches.
/// - producingOutput: The output to be used instead of actually running the worker.
/// If the workflow never tries to run the worker, then this won't be used.
/// - key: Key to expect this `Workflow` to be rendered with.
@available(*, deprecated, renamed: "expectedWorker(_:mockingOutput:key:file:line:)", message: "Renamed")
public func expect<ExpectedWorkerType: Worker>(
worker: ExpectedWorkerType,
producingOutput output: ExpectedWorkerType.Output? = nil,
key: String = "",
file: StaticString = #file, line: UInt = #line
) -> RenderTester<WorkflowType> {
expectWorker(worker, mockingOutput: output, key: key, file: file, line: line)
}

/// Mock the given worker's output, and assert that the workflow's worker `isEquivalent(to:)` the `worker`.
///
/// - Parameters:
/// - worker: The worker that we expect was created by the workflow. Will be compared using
/// `isEquivalent(to:)` to assert that the workflow's worker matches.
/// - mockingOutput: The output to be used instead of actually running the worker.
/// If the workflow never tries to run the worker, then this won't be used.
/// - key: Key to expect this `Workflow` to be rendered with.
public func expectWorker<ExpectedWorkerType: Worker>(
_ worker: ExpectedWorkerType,
mockingOutput output: ExpectedWorkerType.Output? = nil,
key: String = "",
file: StaticString = #file, line: UInt = #line
) -> RenderTester<WorkflowType> {
expectWorkflow(
type: WorkerWorkflow<ExpectedWorkerType>.self,
Expand Down
15 changes: 4 additions & 11 deletions WorkflowConcurrency/TestingTests/TestingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class WorkflowConcurrencyTestingTests: XCTestCase {
.renderTester(initialState: .init(mode: .worker(input: "otherText"), output: ""))

renderTester
.expect(worker: TestWorker(input: "otherText"))
.expectWorker(TestWorker(input: "otherText"))
.render { _ in }
}

Expand All @@ -35,10 +35,7 @@ class WorkflowConcurrencyTestingTests: XCTestCase {
.renderTester(initialState: .init(mode: .worker(input: "otherText"), output: ""))

renderTester
.expect(
worker: TestWorker(input: "otherText"),
producingOutput: "otherText"
)
.expectWorker(TestWorker(input: "otherText"), mockingOutput: "otherText")
.render { _ in }
.verifyState { state in
XCTAssertEqual(state, TestWorkflow.State(mode: .worker(input: "otherText"), output: "otherText"))
Expand All @@ -48,9 +45,7 @@ class WorkflowConcurrencyTestingTests: XCTestCase {
func test_worker_missing() {
let tester = TestWorkflow()
.renderTester()
.expect(
worker: TestWorker(input: "input")
)
.expectWorker(TestWorker(input: "input"))

expectingFailure(#"Expected child workflow of type: WorkerWorkflow<TestWorker>, key: """#) {
tester.render { _ in }
Expand All @@ -60,9 +55,7 @@ class WorkflowConcurrencyTestingTests: XCTestCase {
func test_worker_mismatch() {
let tester = TestWorkflow()
.renderTester(initialState: .init(mode: .worker(input: "test"), output: ""))
.expect(
worker: TestWorker(input: "not-test")
)
.expectWorker(TestWorker(input: "not-test"))

expectingFailures([
#"Workers of type TestWorker not equivalent. Expected: TestWorker(input: "not-test"). Got: TestWorker(input: "test")"#,
Expand Down
30 changes: 25 additions & 5 deletions WorkflowReactiveSwift/Testing/WorkerTesting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,49 @@ import XCTest
@testable import WorkflowReactiveSwift

extension RenderTester {
/// Expect the given worker. It will be checked for `isEquivalent(to:)` with the requested worker.
/// Mock the given worker's output, and assert that the workflow's worker `isEquivalent(to:)` the `worker`.
///
/// - Parameters:
/// - worker: The worker to be expected
/// - producingOutput: An output that will be returned when this worker is requested, if any.
/// - worker: The worker that we expect was created by the workflow. Will be compared using
/// `isEquivalent(to:)` to assert that the workflow's worker matches.
/// - producingOutput: The output to be used instead of actually running the worker.
/// If the workflow never tries to run the worker, then this won't be used.
/// - key: Key to expect this `Workflow` to be rendered with.
@available(*, deprecated, renamed: "expectedWorker(_:mockingOutput:key:file:line:)", message: "Renamed")
public func expect<ExpectedWorkerType: Worker>(
worker: ExpectedWorkerType,
producingOutput output: ExpectedWorkerType.Output? = nil,
key: String = "",
file: StaticString = #file, line: UInt = #line
) -> RenderTester<WorkflowType> {
expectWorker(worker, mockingOutput: output, key: key, file: file, line: line)
}

/// Mock the given worker's output, and assert that the workflow's worker `isEquivalent(to:)` the `expectedWorker`.
///
/// - Parameters:
/// - expectedWorker: The worker that we expect was created by the workflow. Will be compared using
/// `isEquivalent(to:)` to assert that the workflow's worker matches.
/// - mockingOutput: The output to be used instead of actually running the worker.
/// If the workflow never tries to run the worker, then this won't be used.
/// - key: Key to expect this `Workflow` to be rendered with.
public func expectWorker<ExpectedWorkerType: Worker>(
_ expectedWorker: ExpectedWorkerType,
mockingOutput output: ExpectedWorkerType.Output? = nil,
key: String = "",
file: StaticString = #file, line: UInt = #line
) -> RenderTester<WorkflowType> {
expectWorkflow(
type: WorkerWorkflow<ExpectedWorkerType>.self,
key: key,
producingRendering: (),
producingOutput: output,
assertions: { workflow in
guard !workflow.worker.isEquivalent(to: worker) else {
guard !workflow.worker.isEquivalent(to: expectedWorker) else {
return
}
XCTFail(
"Workers of type \(ExpectedWorkerType.self) not equivalent. Expected: \(worker). Got: \(workflow.worker)",
"Workers of type \(ExpectedWorkerType.self) not equivalent. Expected: \(expectedWorker). Got: \(workflow.worker)",
file: file,
line: line
)
Expand Down
15 changes: 4 additions & 11 deletions WorkflowReactiveSwift/TestingTests/TestingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class WorkflowReactiveSwiftTestingTests: XCTestCase {
.renderTester(initialState: .init(mode: .worker(input: "otherText"), output: ""))

renderTester
.expect(worker: TestWorker(input: "otherText"))
.expectWorker(TestWorker(input: "otherText"))
.render { _ in }
}

Expand All @@ -36,10 +36,7 @@ class WorkflowReactiveSwiftTestingTests: XCTestCase {
.renderTester(initialState: .init(mode: .worker(input: "otherText"), output: ""))

renderTester
.expect(
worker: TestWorker(input: "otherText"),
producingOutput: "otherText"
)
.expectWorker(TestWorker(input: "otherText"), mockingOutput: "otherText")
.render { _ in }
.verifyState { state in
XCTAssertEqual(state, TestWorkflow.State(mode: .worker(input: "otherText"), output: "otherText"))
Expand All @@ -49,9 +46,7 @@ class WorkflowReactiveSwiftTestingTests: XCTestCase {
func test_worker_missing() {
let tester = TestWorkflow()
.renderTester()
.expect(
worker: TestWorker(input: "input")
)
.expectWorker(TestWorker(input: "input"))

expectingFailure(#"Expected child workflow of type: WorkerWorkflow<TestWorker>, key: """#) {
tester.render { _ in }
Expand All @@ -61,9 +56,7 @@ class WorkflowReactiveSwiftTestingTests: XCTestCase {
func test_worker_mismatch() {
let tester = TestWorkflow()
.renderTester(initialState: .init(mode: .worker(input: "test"), output: ""))
.expect(
worker: TestWorker(input: "not-test")
)
.expectWorker(TestWorker(input: "not-test"))

expectingFailures([
#"Workers of type TestWorker not equivalent. Expected: TestWorker(input: "not-test"). Got: TestWorker(input: "test")"#,
Expand Down
33 changes: 27 additions & 6 deletions WorkflowRxSwift/Testing/WorkerTesting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,49 @@ import XCTest
@testable import WorkflowRxSwift

extension RenderTester {
/// Expect the given worker. It will be checked for `isEquivalent(to:)` with the requested worker.

/// Mock the given worker's output, and assert that the workflow's worker `isEquivalent(to:)` the `worker`.
///
/// - Parameters:
/// - worker: The worker to be expected
/// - output: An output that will be returned when this worker is requested, if any.
/// - worker: The worker that we expect was created by the workflow. Will be compared using
/// `isEquivalent(to:)` to assert that the workflow's worker matches.
/// - producingOutput: The output to be used instead of actually running the worker.
/// If the workflow never tries to run the worker, then this won't be used.
/// - key: Key to expect this `Workflow` to be rendered with.
@available(*, deprecated, renamed: "expectedWorker(_:mockingOutput:key:file:line:)", message: "Renamed")
public func expect<ExpectedWorkerType: Worker>(
worker: ExpectedWorkerType,
producingOutput output: ExpectedWorkerType.Output? = nil,
key: String = "",
file: StaticString = #file, line: UInt = #line
) -> RenderTester<WorkflowType> {
expectWorker(worker, mockingOutput: output, key: key, file: file, line: line)
}

/// Mock the given worker's output, and assert that the workflow's worker `isEquivalent(to:)` the `expectedWorker`.
///
/// - Parameters:
/// - expectedWorker: The worker that we expect was created by the workflow. Will be compared using
/// `isEquivalent(to:)` to assert that the workflow's worker matches.
/// - mockingOutput: The output to be used instead of actually running the worker.
/// If the workflow never tries to run the worker, then this won't be used.
/// - key: Key to expect this `Workflow` to be rendered with.
public func expectWorker<ExpectedWorkerType: Worker>(
_ expectedWorker: ExpectedWorkerType,
mockingOutput output: ExpectedWorkerType.Output? = nil,
key: String = "",
file: StaticString = #file, line: UInt = #line
) -> RenderTester<WorkflowType> {
expectWorkflow(
type: WorkerWorkflow<ExpectedWorkerType>.self,
key: key,
producingRendering: (),
producingOutput: output,
assertions: { workflow in
guard !workflow.worker.isEquivalent(to: worker) else {
guard !workflow.worker.isEquivalent(to: expectedWorker) else {
return
}
XCTFail(
"Workers of type \(ExpectedWorkerType.self) not equivalent. Expected: \(worker). Got: \(workflow.worker)",
"Workers of type \(ExpectedWorkerType.self) not equivalent. Expected: \(expectedWorker). Got: \(workflow.worker)",
file: file,
line: line
)
Expand Down
Loading
Loading