Skip to content

Make the new XCTModify play nicely with non-exhaustive testing. #1939

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 3 commits into from
Feb 24, 2023
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
4 changes: 2 additions & 2 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ let package = Package(
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
.package(url: "https://github.com/google/swift-benchmark", from: "0.1.0"),
.package(url: "https://github.com/pointfreeco/combine-schedulers", from: "0.8.0"),
.package(url: "https://github.com/pointfreeco/swift-case-paths", from: "0.10.0"),
.package(url: "https://github.com/pointfreeco/swift-case-paths", from: "0.13.0"),
.package(url: "https://github.com/apple/swift-collections", from: "1.0.2"),
.package(url: "https://github.com/pointfreeco/swift-custom-dump", from: "0.7.0"),
.package(url: "https://github.com/pointfreeco/swift-dependencies", from: "0.1.2"),
Expand Down
16 changes: 16 additions & 0 deletions Sources/ComposableArchitecture/TestStore.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@_spi(Internals) import CasePaths
import Combine
import CustomDump
import Foundation
Expand Down Expand Up @@ -1168,6 +1169,13 @@ extension TestStore where ScopedState: Equatable {
) throws {
let current = expected
var expected = expected
let updateStateToExpectedResult = updateStateToExpectedResult.map { original in
{ (state: inout ScopedState) in
try XCTModifyLocals.$isExhaustive.withValue(self.exhaustivity == .on) {
try original(&state)
}
}
}

switch self.exhaustivity {
case .on:
Expand Down Expand Up @@ -1745,6 +1753,14 @@ extension TestStore where ScopedState: Equatable {
file: StaticString,
line: UInt
) {
let updateStateToExpectedResult = updateStateToExpectedResult.map { original in
{ (state: inout ScopedState) in
try XCTModifyLocals.$isExhaustive.withValue(self.exhaustivity == .on) {
try original(&state)
}
}
}

guard !self.reducer.receivedActions.isEmpty else {
XCTFail(
failureMessage(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,71 @@
await store.receive(/NonExhaustiveReceive.Action.response2)
}

func testXCTModifyExhaustive() async {
struct State: Equatable {
var child: Int? = 0
var count = 0
}
enum Action: Equatable { case tap, response }
let store = TestStore(
initialState: State(),
reducer: Reduce<State, Action> { state, action in
switch action {
case .tap:
state.count += 1
return .send(.response)
case .response:
state.count += 1
return .none
}
}
)

await store.send(.tap) { state in
state.count = 1
XCTExpectFailure {
XCTModify(&state.child, case: /.some) { _ in }
} issueMatcher: {
$0.compactDescription == """
XCTModify failed: expected "Int" value to be modified but it was unchanged.
"""
}
}
await store.receive(.response) { state in
state.count = 2
XCTExpectFailure {
XCTModify(&state.child, case: /Optional.some) { _ in }
} issueMatcher: {
$0.compactDescription == """
XCTModify failed: expected "Int" value to be modified but it was unchanged.
"""
}
}
}

func testXCTModifyNonExhaustive() async {
enum Action { case tap, response }
let store = TestStore(
initialState: Optional(1),
reducer: Reduce<Int?, Action> { state, action in
switch action {
case .tap:
return .send(.response)
case .response:
return .none
}
}
)
store.exhaustivity = .off

await store.send(.tap) {
XCTModify(&$0, case: /Optional.some) { _ in }
}
await store.receive(.response) {
XCTModify(&$0, case: /Optional.some) { _ in }
}
}

// This example comes from Krzysztof Zabłocki's blog post:
// https://www.merowing.info/exhaustive-testing-in-tca/
func testKrzysztofExample1() {
Expand Down