From 7c2f4b4b85a40c22a0c4518b9f5dd836a030dec6 Mon Sep 17 00:00:00 2001 From: Stephano Telolahy Date: Tue, 7 Jan 2025 09:06:02 +0100 Subject: [PATCH] wip --- .../Core/App/Sources/SetupGameCore.swift | 16 ++--- .../Core/Game/Tests/CreateGameStore.swift | 16 ----- WildWestOnline/Core/Game/Tests/Dispatch.swift | 43 +++++++++++++ .../Game/Tests/Reducer/ActivateTest.swift | 5 +- .../Core/Game/Tests/Reducer/DamageTest.swift | 10 ++- .../Tests/Reducer/DiscardPlayedTest.swift | 7 +-- .../Core/Game/Tests/Reducer/DiscardTest.swift | 14 ++--- .../Game/Tests/Reducer/DiscoverTest.swift | 61 ++++++------------- .../Game/Tests/Reducer/DrawDeckTest.swift | 35 ++++------- .../Game/Tests/Reducer/DrawDiscardTest.swift | 26 +++----- .../Tests/Reducer/DrawDiscoveredTest.swift | 9 ++- .../Core/Game/Tests/Reducer/DrawTest.swift | 33 +++------- .../Game/Tests/Reducer/EliminateTest.swift | 10 ++- .../Core/Game/Tests/Reducer/EndGameTest.swift | 5 +- .../Core/Game/Tests/Reducer/EndTurnTest.swift | 5 +- .../Core/Game/Tests/Reducer/EquipTest.swift | 28 +++------ .../Game/Tests/Reducer/HandicapTest.swift | 30 +++------ .../Core/Game/Tests/Reducer/HealTest.swift | 34 +++-------- .../Reducer/IncreaseMagnifyingTest.swift | 5 +- .../Reducer/IncreaseRemotenessTest.swift | 5 +- .../Game/Tests/Reducer/PassInPlayTest.swift | 7 +-- .../Core/Game/Tests/Reducer/PlayTest.swift | 41 ++++--------- .../Reducer/SetPlayLimitPerTurnTest.swift | 5 +- .../Game/Tests/Reducer/SetWeaponTest.swift | 5 +- .../Core/Game/Tests/Reducer/ShootTest.swift | 5 +- .../Game/Tests/Reducer/StartTurnTest.swift | 10 ++- .../Core/Game/Tests/Reducer/StealTest.swift | 14 ++--- 27 files changed, 187 insertions(+), 297 deletions(-) delete mode 100644 WildWestOnline/Core/Game/Tests/CreateGameStore.swift create mode 100644 WildWestOnline/Core/Game/Tests/Dispatch.swift diff --git a/WildWestOnline/Core/App/Sources/SetupGameCore.swift b/WildWestOnline/Core/App/Sources/SetupGameCore.swift index 9d3e5d901..aa3d75878 100644 --- a/WildWestOnline/Core/App/Sources/SetupGameCore.swift +++ b/WildWestOnline/Core/App/Sources/SetupGameCore.swift @@ -25,7 +25,7 @@ func setupGameReducer( guard let action = action as? SetupGameAction else { return .none } - + switch action { case .startGame: let settings = state.settings @@ -39,10 +39,10 @@ func setupGameReducer( NavigationStackAction.push(.game) } ]) - + case .setGame(let game): state.game = game - + case .quitGame: return .group([ .run { @@ -52,11 +52,11 @@ func setupGameReducer( NavigationStackAction.pop } ]) - + case .unsetGame: state.game = nil } - + return .none } @@ -66,13 +66,13 @@ private func createGame(settings: SettingsState, inventory: Inventory) -> GameSt inventory: inventory, preferredFigure: settings.preferredFigure ) - + let manualPlayer: String? = settings.simulation ? nil : game.playOrder[0] game.playMode = game.playOrder.reduce(into: [:]) { $0[$1] = $1 == manualPlayer ? .manual : .auto } - + game.actionDelayMilliSeconds = settings.actionDelayMilliSeconds - + return game } diff --git a/WildWestOnline/Core/Game/Tests/CreateGameStore.swift b/WildWestOnline/Core/Game/Tests/CreateGameStore.swift deleted file mode 100644 index 537257fed..000000000 --- a/WildWestOnline/Core/Game/Tests/CreateGameStore.swift +++ /dev/null @@ -1,16 +0,0 @@ -// -// CreateGameStore.swift -// WildWestOnline -// -// Created by Hugues Stéphano TELOLAHY on 03/01/2025. -// -import Redux -import GameCore - -@MainActor func createGameStore(initialState: GameState) -> Store { - .init( - initialState: initialState, - reducer: gameReducer, - dependencies: () - ) -} diff --git a/WildWestOnline/Core/Game/Tests/Dispatch.swift b/WildWestOnline/Core/Game/Tests/Dispatch.swift new file mode 100644 index 000000000..bddc13ab5 --- /dev/null +++ b/WildWestOnline/Core/Game/Tests/Dispatch.swift @@ -0,0 +1,43 @@ +// +// Dispatch.swift +// WildWestOnline +// +// Created by Hugues Stéphano TELOLAHY on 03/01/2025. +// +import Redux +import GameCore +import Combine + +func dispatch( + _ action: GameAction, + state: GameState, + file: StaticString = #file, + line: UInt = #line +) async throws -> GameState { + let sut = await createGameStore(initialState: state) + var receivedErrors: [Error] = [] + var cancellables: Set = [] + await MainActor.run { + sut.errorPublisher + .sink { receivedErrors.append($0) } + .store(in: &cancellables) + } + + // When + let result = try await dispatch(action, state: state) + + // Then + guard receivedErrors.isEmpty else { + throw receivedErrors[0] + } + + return await sut.state +} + +@MainActor private func createGameStore(initialState: GameState) -> Store { + .init( + initialState: initialState, + reducer: gameReducer, + dependencies: () + ) +} diff --git a/WildWestOnline/Core/Game/Tests/Reducer/ActivateTest.swift b/WildWestOnline/Core/Game/Tests/Reducer/ActivateTest.swift index 564cd7946..20768b4d7 100644 --- a/WildWestOnline/Core/Game/Tests/Reducer/ActivateTest.swift +++ b/WildWestOnline/Core/Game/Tests/Reducer/ActivateTest.swift @@ -13,13 +13,12 @@ struct ActivateTest { // Given let state = GameState.makeBuilder() .build() - let sut = await createGameStore(initialState: state) // When let action = GameAction.activate(["c1", "c2"], player: "p1") - await sut.dispatch(action) + let result = try await dispatch(action, state: state) // Then - await #expect(sut.state.active == ["p1": ["c1", "c2"]]) + #expect(result.active == ["p1": ["c1", "c2"]]) } } diff --git a/WildWestOnline/Core/Game/Tests/Reducer/DamageTest.swift b/WildWestOnline/Core/Game/Tests/Reducer/DamageTest.swift index 0e91b0609..24c6de5c6 100644 --- a/WildWestOnline/Core/Game/Tests/Reducer/DamageTest.swift +++ b/WildWestOnline/Core/Game/Tests/Reducer/DamageTest.swift @@ -16,14 +16,13 @@ struct DamageTest { $0.withHealth(2) } .build() - let sut = await createGameStore(initialState: state) // When let action = GameAction.damage(1, player: "p1") - await sut.dispatch(action) + let result = try await dispatch(action, state: state) // Then - await #expect(sut.state.players.get("p1").health == 1) + #expect(result.players.get("p1").health == 1) } @Test func damage_with2LifePoints_shouldReduceHealthBy2() async throws { @@ -33,13 +32,12 @@ struct DamageTest { $0.withHealth(2) } .build() - let sut = await createGameStore(initialState: state) // When let action = GameAction.damage(2, player: "p1") - await sut.dispatch(action) + let result = try await dispatch(action, state: state) // Then - await #expect(sut.state.players.get("p1").health == 0) + #expect(result.players.get("p1").health == 0) } } diff --git a/WildWestOnline/Core/Game/Tests/Reducer/DiscardPlayedTest.swift b/WildWestOnline/Core/Game/Tests/Reducer/DiscardPlayedTest.swift index a0175ee6a..fe33412d7 100644 --- a/WildWestOnline/Core/Game/Tests/Reducer/DiscardPlayedTest.swift +++ b/WildWestOnline/Core/Game/Tests/Reducer/DiscardPlayedTest.swift @@ -16,14 +16,13 @@ struct DiscardPlayedTest { $0.withHand(["c1", "c2"]) } .build() - let sut = await createGameStore(initialState: state) // When let action = GameAction.discardPlayed("c1", player: "p1") - await sut.dispatch(action) + let result = try await dispatch(action, state: state) // Then - await #expect(sut.state.players.get("p1").hand == ["c2"]) - await #expect(sut.state.discard == ["c1"]) + #expect(result.players.get("p1").hand == ["c2"]) + #expect(result.discard == ["c1"]) } } diff --git a/WildWestOnline/Core/Game/Tests/Reducer/DiscardTest.swift b/WildWestOnline/Core/Game/Tests/Reducer/DiscardTest.swift index 369feabb0..f3c8b075f 100644 --- a/WildWestOnline/Core/Game/Tests/Reducer/DiscardTest.swift +++ b/WildWestOnline/Core/Game/Tests/Reducer/DiscardTest.swift @@ -16,15 +16,14 @@ struct DiscardTest { $0.withHand(["c1", "c2"]) } .build() - let sut = await createGameStore(initialState: state) // When let action = GameAction.discardHand("c1", player: "p1") - await sut.dispatch(action) + let result = try await dispatch(action, state: state) // Then - await #expect(sut.state.players.get("p1").hand == ["c2"]) - await #expect(sut.state.discard == ["c1"]) + #expect(result.players.get("p1").hand == ["c2"]) + #expect(result.discard == ["c1"]) } @Test func discard_shouldRemoveCardFromInPlay() async throws { @@ -34,14 +33,13 @@ struct DiscardTest { $0.withInPlay(["c1", "c2"]) } .build() - let sut = await createGameStore(initialState: state) // When let action = GameAction.discardInPlay("c1", player: "p1") - await sut.dispatch(action) + let result = try await dispatch(action, state: state) // Then - await #expect(sut.state.players.get("p1").inPlay == ["c2"]) - await #expect(sut.state.discard == ["c1"]) + #expect(result.players.get("p1").inPlay == ["c2"]) + #expect(result.discard == ["c1"]) } } diff --git a/WildWestOnline/Core/Game/Tests/Reducer/DiscoverTest.swift b/WildWestOnline/Core/Game/Tests/Reducer/DiscoverTest.swift index 1637c6770..5252235dd 100644 --- a/WildWestOnline/Core/Game/Tests/Reducer/DiscoverTest.swift +++ b/WildWestOnline/Core/Game/Tests/Reducer/DiscoverTest.swift @@ -15,15 +15,14 @@ struct DiscoverTest { let state = GameState.makeBuilder() .withDeck(["c1", "c2", "c3"]) .build() - let sut = await createGameStore(initialState: state) // When let action = GameAction.discover(player: "p1") - await sut.dispatch(action) + let result = try await dispatch(action, state: state) // Then - await #expect(sut.state.discovered == ["c1"]) - await #expect(sut.state.deck == ["c1", "c2", "c3"]) + #expect(result.discovered == ["c1"]) + #expect(result.deck == ["c1", "c2", "c3"]) } @Test func discover_withAlreadyDiscoveredCard_shouldAddCardNextToDiscovered() async throws { @@ -32,15 +31,14 @@ struct DiscoverTest { .withDeck(["c1", "c2", "c3"]) .withDiscovered(["c1"]) .build() - let sut = await createGameStore(initialState: state) // When let action = GameAction.discover(player: "p1") - await sut.dispatch(action) + let result = try await dispatch(action, state: state) // Then - await #expect(sut.state.discovered == ["c1", "c2"]) - await #expect(sut.state.deck == ["c1", "c2", "c3"]) + #expect(result.discovered == ["c1", "c2"]) + #expect(result.deck == ["c1", "c2", "c3"]) } @Test func discover_emptyDeck_withEnoughCards_shouldResetDeck() async throws { @@ -49,40 +47,28 @@ struct DiscoverTest { .withDeck([]) .withDiscard(["c1", "c2"]) .build() - let sut = await createGameStore(initialState: state) // When let action = GameAction.discover(player: "p1") - await sut.dispatch(action) + let result = try await dispatch(action, state: state) // Then - await #expect(sut.state.discovered == ["c2"]) - await #expect(sut.state.deck == ["c2"]) - await #expect(sut.state.discard == ["c1"]) + #expect(result.discovered == ["c2"]) + #expect(result.deck == ["c2"]) + #expect(result.discard == ["c1"]) } @Test func discover_emptyDeck_withoutEnoughCards_shouldThrowError() async throws { // Given let state = GameState.makeBuilder() .build() - let sut = await createGameStore(initialState: state) - - var receivedErrors: [Error] = [] - var cancellables: Set = [] - await MainActor.run { - sut.errorPublisher - .sink { receivedErrors.append($0) } - .store(in: &cancellables) - } // When - let action = GameAction.discover(player: "p1") - await sut.dispatch(action) - // Then - #expect(receivedErrors as? [GameError] == [ - .insufficientDeck - ]) + let action = GameAction.discover(player: "p1") + await #expect(throws: GameError.insufficientDeck) { + try await dispatch(action, state: state) + } } @Test func discover_nonEmptyDeck_withoutEnoughCards_shouldThrowError() async throws { @@ -91,23 +77,12 @@ struct DiscoverTest { .withDiscovered(["c1"]) .withDeck(["c1"]) .build() - let sut = await createGameStore(initialState: state) - - var receivedErrors: [Error] = [] - var cancellables: Set = [] - await MainActor.run { - sut.errorPublisher - .sink { receivedErrors.append($0) } - .store(in: &cancellables) - } // When - let action = GameAction.discover(player: "p1") - await sut.dispatch(action) - // Then - #expect(receivedErrors as? [GameError] == [ - .insufficientDeck - ]) + let action = GameAction.discover(player: "p1") + await #expect(throws: GameError.insufficientDeck) { + try await dispatch(action, state: state) + } } } diff --git a/WildWestOnline/Core/Game/Tests/Reducer/DrawDeckTest.swift b/WildWestOnline/Core/Game/Tests/Reducer/DrawDeckTest.swift index 699a78d3f..42cd9cb7b 100644 --- a/WildWestOnline/Core/Game/Tests/Reducer/DrawDeckTest.swift +++ b/WildWestOnline/Core/Game/Tests/Reducer/DrawDeckTest.swift @@ -16,15 +16,14 @@ struct DrawDeckTest { .withPlayer("p1") .withDeck(["c1", "c2"]) .build() - let sut = await createGameStore(initialState: state) // When let action = GameAction.drawDeck(player: "p1") - await sut.dispatch(action) + let result = try await dispatch(action, state: state) // Then - await #expect(sut.state.players.get("p1").hand == ["c1"]) - await #expect(sut.state.deck == ["c2"]) + #expect(result.players.get("p1").hand == ["c1"]) + #expect(result.deck == ["c2"]) } @Test func drawDeck_whitEmptyDeckAndEnoughDiscardPile_shouldResetDeck() async throws { @@ -33,16 +32,15 @@ struct DrawDeckTest { .withPlayer("p1") .withDiscard(["c1", "c2", "c3", "c4"]) .build() - let sut = await createGameStore(initialState: state) // When let action = GameAction.drawDeck(player: "p1") - await sut.dispatch(action) + let result = try await dispatch(action, state: state) // Then - await #expect(sut.state.deck == ["c3", "c4"]) - await #expect(sut.state.discard == ["c1"]) - await #expect(sut.state.players.get("p1").hand == ["c2"]) + #expect(result.deck == ["c3", "c4"]) + #expect(result.discard == ["c1"]) + #expect(result.players.get("p1").hand == ["c2"]) } @Test func drawDeck_whitEmptyDeckAndNotEnoughDiscardPile_shouldThrowError() async throws { @@ -50,23 +48,12 @@ struct DrawDeckTest { let state = GameState.makeBuilder() .withPlayer("p1") .build() - let sut = await createGameStore(initialState: state) - - var receivedErrors: [Error] = [] - var cancellables: Set = [] - await MainActor.run { - sut.errorPublisher - .sink { receivedErrors.append($0) } - .store(in: &cancellables) - } // When - let action = GameAction.drawDeck(player: "p1") - await sut.dispatch(action) - // Then - #expect(receivedErrors as? [GameError] == [ - .insufficientDeck - ]) + let action = GameAction.drawDeck(player: "p1") + await #expect(throws: GameError.insufficientDeck) { + try await dispatch(action, state: state) + } } } diff --git a/WildWestOnline/Core/Game/Tests/Reducer/DrawDiscardTest.swift b/WildWestOnline/Core/Game/Tests/Reducer/DrawDiscardTest.swift index a5a7d9811..1a5b1919a 100644 --- a/WildWestOnline/Core/Game/Tests/Reducer/DrawDiscardTest.swift +++ b/WildWestOnline/Core/Game/Tests/Reducer/DrawDiscardTest.swift @@ -16,15 +16,14 @@ struct DrawDiscardTest { .withPlayer("p1") .withDiscard(["c1", "c2"]) .build() - let sut = await createGameStore(initialState: state) // When let action = GameAction.drawDiscard(player: "p1") - await sut.dispatch(action) + let result = try await dispatch(action, state: state) // Then - await #expect(sut.state.players.get("p1").hand == ["c1"]) - await #expect(sut.state.discard == ["c2"]) + #expect(result.players.get("p1").hand == ["c1"]) + #expect(result.discard == ["c2"]) } @Test func drawDiscard_whitEmptyDiscard_shouldThrowError() async throws { @@ -32,23 +31,12 @@ struct DrawDiscardTest { let state = GameState.makeBuilder() .withPlayer("p1") .build() - let sut = await createGameStore(initialState: state) - - var receivedErrors: [Error] = [] - var cancellables: Set = [] - await MainActor.run { - sut.errorPublisher - .sink { receivedErrors.append($0) } - .store(in: &cancellables) - } // When - let action = GameAction.drawDiscard(player: "p1") - await sut.dispatch(action) - // Then - #expect(receivedErrors as? [GameError] == [ - .insufficientDiscard - ]) + let action = GameAction.drawDiscard(player: "p1") + await #expect(throws: GameError.insufficientDiscard) { + try await dispatch(action, state: state) + } } } diff --git a/WildWestOnline/Core/Game/Tests/Reducer/DrawDiscoveredTest.swift b/WildWestOnline/Core/Game/Tests/Reducer/DrawDiscoveredTest.swift index af90815fa..fc0f94749 100644 --- a/WildWestOnline/Core/Game/Tests/Reducer/DrawDiscoveredTest.swift +++ b/WildWestOnline/Core/Game/Tests/Reducer/DrawDiscoveredTest.swift @@ -16,15 +16,14 @@ struct DrawDiscoveredTest { .withDiscovered(["c1", "c2"]) .withDeck(["c1", "c2"]) .build() - let sut = await createGameStore(initialState: state) // When let action = GameAction.drawDiscovered("c2", player: "p1") - await sut.dispatch(action) + let result = try await dispatch(action, state: state) // Then - await #expect(sut.state.players.get("p1").hand == ["c2"]) - await #expect(sut.state.discovered == ["c1"]) - await #expect(sut.state.deck == ["c1"]) + #expect(result.players.get("p1").hand == ["c2"]) + #expect(result.discovered == ["c1"]) + #expect(result.deck == ["c1"]) } } diff --git a/WildWestOnline/Core/Game/Tests/Reducer/DrawTest.swift b/WildWestOnline/Core/Game/Tests/Reducer/DrawTest.swift index 6e4a8ab88..7a931493e 100644 --- a/WildWestOnline/Core/Game/Tests/Reducer/DrawTest.swift +++ b/WildWestOnline/Core/Game/Tests/Reducer/DrawTest.swift @@ -16,15 +16,14 @@ struct DrawTest { .withDeck(["c2", "c3"]) .withDiscard(["c1"]) .build() - let sut = await createGameStore(initialState: state) // When let action = GameAction.draw(player: "p1") - await sut.dispatch(action) + let result = try await dispatch(action, state: state) // Then - await #expect(sut.state.discard == ["c2", "c1"]) - await #expect(sut.state.deck == ["c3"]) + #expect(result.discard == ["c2", "c1"]) + #expect(result.deck == ["c3"]) } @Test func draw_withEmptyDeck_withEnoughDiscard_shouldResetDeck() async throws { @@ -32,38 +31,26 @@ struct DrawTest { let state = GameState.makeBuilder() .withDiscard(["c1", "c2", "c3"]) .build() - let sut = await createGameStore(initialState: state) // When let action = GameAction.draw(player: "p1") - await sut.dispatch(action) + let result = try await dispatch(action, state: state) // Then - await #expect(sut.state.discard == ["c2", "c1"]) - await #expect(sut.state.deck == ["c3"]) + #expect(result.discard == ["c2", "c1"]) + #expect(result.deck == ["c3"]) } @Test func draw_withEmptyDeck_withoutEnoughDiscard_shouldThrowError() async throws { // Given let state = GameState.makeBuilder() .build() - let sut = await createGameStore(initialState: state) - - var receivedErrors: [Error] = [] - var cancellables: Set = [] - await MainActor.run { - sut.errorPublisher - .sink { receivedErrors.append($0) } - .store(in: &cancellables) - } // When - let action = GameAction.draw(player: "p1") - await sut.dispatch(action) - // Then - #expect(receivedErrors as? [GameError] == [ - .insufficientDeck - ]) + let action = GameAction.draw(player: "p1") + await #expect(throws: GameError.insufficientDeck) { + try await dispatch(action, state: state) + } } } diff --git a/WildWestOnline/Core/Game/Tests/Reducer/EliminateTest.swift b/WildWestOnline/Core/Game/Tests/Reducer/EliminateTest.swift index f23e4268d..1698e693c 100644 --- a/WildWestOnline/Core/Game/Tests/Reducer/EliminateTest.swift +++ b/WildWestOnline/Core/Game/Tests/Reducer/EliminateTest.swift @@ -15,14 +15,13 @@ struct EliminateTest { .withPlayer("p1") .withPlayer("p2") .build() - let sut = await createGameStore(initialState: state) // When let action = GameAction.eliminate(player: "p1") - await sut.dispatch(action) + let result = try await dispatch(action, state: state) // Then - await #expect(sut.state.playOrder == ["p2"]) + #expect(result.playOrder == ["p2"]) } @Test func eliminate_shouldRemovePendingAction() async throws { @@ -38,13 +37,12 @@ struct EliminateTest { ] ) .build() - let sut = await createGameStore(initialState: state) // When let action = GameAction.eliminate(player: "p1") - await sut.dispatch(action) + let result = try await dispatch(action, state: state) // Then - await #expect(sut.state.queue.isEmpty) + #expect(result.queue.isEmpty) } } diff --git a/WildWestOnline/Core/Game/Tests/Reducer/EndGameTest.swift b/WildWestOnline/Core/Game/Tests/Reducer/EndGameTest.swift index 9ae640fa2..001f3abeb 100644 --- a/WildWestOnline/Core/Game/Tests/Reducer/EndGameTest.swift +++ b/WildWestOnline/Core/Game/Tests/Reducer/EndGameTest.swift @@ -13,13 +13,12 @@ struct EndGameTest { // Given let state = GameState.makeBuilder() .build() - let sut = await createGameStore(initialState: state) // When let action = GameAction.endGame(player: "p1") - await sut.dispatch(action) + let result = try await dispatch(action, state: state) // Then - await #expect(sut.state.isOver == true) + #expect(result.isOver == true) } } diff --git a/WildWestOnline/Core/Game/Tests/Reducer/EndTurnTest.swift b/WildWestOnline/Core/Game/Tests/Reducer/EndTurnTest.swift index 4569853c8..5bd7657d8 100644 --- a/WildWestOnline/Core/Game/Tests/Reducer/EndTurnTest.swift +++ b/WildWestOnline/Core/Game/Tests/Reducer/EndTurnTest.swift @@ -14,13 +14,12 @@ struct EndTurnTest { let state = GameState.makeBuilder() .withTurn("p1") .build() - let sut = await createGameStore(initialState: state) // When let action = GameAction.endTurn(player: "p1") - await sut.dispatch(action) + let result = try await dispatch(action, state: state) // Then - await #expect(sut.state.turn == nil) + #expect(result.turn == nil) } } diff --git a/WildWestOnline/Core/Game/Tests/Reducer/EquipTest.swift b/WildWestOnline/Core/Game/Tests/Reducer/EquipTest.swift index ff42ebf8c..e2bda5e10 100644 --- a/WildWestOnline/Core/Game/Tests/Reducer/EquipTest.swift +++ b/WildWestOnline/Core/Game/Tests/Reducer/EquipTest.swift @@ -17,16 +17,15 @@ struct EquipTest { $0.withHand(["c1", "c2"]) } .build() - let sut = await createGameStore(initialState: state) // When let action = GameAction.equip("c1", player: "p1") - await sut.dispatch(action) + let result = try await dispatch(action, state: state) // Then - await #expect(sut.state.players.get("p1").hand == ["c2"]) - await #expect(sut.state.players.get("p1").inPlay == ["c1"]) - await #expect(sut.state.discard.isEmpty) + #expect(result.players.get("p1").hand == ["c2"]) + #expect(result.players.get("p1").inPlay == ["c1"]) + #expect(result.discard.isEmpty) } @Test func equip_withCardAlreadyInPlay_shouldThrowError() async throws { @@ -37,23 +36,12 @@ struct EquipTest { .withInPlay(["c-2"]) } .build() - let sut = await createGameStore(initialState: state) - - var receivedErrors: [Error] = [] - var cancellables: Set = [] - await MainActor.run { - sut.errorPublisher - .sink { receivedErrors.append($0) } - .store(in: &cancellables) - } // When - let action = GameAction.equip("c-1", player: "p1") - await sut.dispatch(action) - // Then - #expect(receivedErrors as? [GameError] == [ - .cardAlreadyInPlay("c") - ]) + let action = GameAction.equip("c-1", player: "p1") + await #expect(throws: GameError.cardAlreadyInPlay("c")) { + try await dispatch(action, state: state) + } } } diff --git a/WildWestOnline/Core/Game/Tests/Reducer/HandicapTest.swift b/WildWestOnline/Core/Game/Tests/Reducer/HandicapTest.swift index 40d62e688..9f86959d5 100644 --- a/WildWestOnline/Core/Game/Tests/Reducer/HandicapTest.swift +++ b/WildWestOnline/Core/Game/Tests/Reducer/HandicapTest.swift @@ -18,17 +18,16 @@ struct HandicapTest { } .withPlayer("p2") .build() - let sut = await createGameStore(initialState: state) // When let action = GameAction.handicap("c1", target: "p2", player: "p1") - await sut.dispatch(action) + let result = try await dispatch(action, state: state) // Then - await #expect(sut.state.players.get("p1").hand == ["c2"]) - await #expect(sut.state.players.get("p2").inPlay == ["c1"]) - await #expect(sut.state.players.get("p1").inPlay.isEmpty) - await #expect(sut.state.discard.isEmpty) + #expect(result.players.get("p1").hand == ["c2"]) + #expect(result.players.get("p2").inPlay == ["c1"]) + #expect(result.players.get("p1").inPlay.isEmpty) + #expect(result.discard.isEmpty) } @Test func handicap_withCardAlreadyInPlay_shouldThrowError() async throws { @@ -41,23 +40,12 @@ struct HandicapTest { $0.withInPlay(["c-2"]) } .build() - let sut = await createGameStore(initialState: state) - - var receivedErrors: [Error] = [] - var cancellables: Set = [] - await MainActor.run { - sut.errorPublisher - .sink { receivedErrors.append($0) } - .store(in: &cancellables) - } // When - let action = GameAction.handicap("c-1", target: "p2", player: "p1") - await sut.dispatch(action) - // Then - #expect(receivedErrors as? [GameError] == [ - .cardAlreadyInPlay("c") - ]) + let action = GameAction.handicap("c-1", target: "p2", player: "p1") + await #expect(throws: GameError.cardAlreadyInPlay("c")) { + try await dispatch(action, state: state) + } } } diff --git a/WildWestOnline/Core/Game/Tests/Reducer/HealTest.swift b/WildWestOnline/Core/Game/Tests/Reducer/HealTest.swift index ed064a3c1..8dbdd5136 100644 --- a/WildWestOnline/Core/Game/Tests/Reducer/HealTest.swift +++ b/WildWestOnline/Core/Game/Tests/Reducer/HealTest.swift @@ -18,14 +18,13 @@ struct HealTest { .withMaxHealth(4) } .build() - let sut = await createGameStore(initialState: state) // When let action = GameAction.heal(1, player: "p1") - await sut.dispatch(action) + let result = try await dispatch(action, state: state) // Then - await #expect(sut.state.players.get("p1").health == 3) + #expect(result.players.get("p1").health == 3) } @Test func heal_beingDamaged_amountEqualDamage_shouldGainLifePoints() async throws { @@ -36,14 +35,13 @@ struct HealTest { .withMaxHealth(4) } .build() - let sut = await createGameStore(initialState: state) // When let action = GameAction.heal(2, player: "p1") - await sut.dispatch(action) + let result = try await dispatch(action, state: state) // Then - await #expect(sut.state.players.get("p1").health == 4) + #expect(result.players.get("p1").health == 4) } @Test func heal_beingDamaged_amountGreaterThanDamage_shouldGainLifePointsLimitedToMaxHealth() async throws { @@ -54,14 +52,13 @@ struct HealTest { .withMaxHealth(4) } .build() - let sut = await createGameStore(initialState: state) // When let action = GameAction.heal(3, player: "p1") - await sut.dispatch(action) + let result = try await dispatch(action, state: state) // Then - await #expect(sut.state.players.get("p1").health == 4) + #expect(result.players.get("p1").health == 4) } @Test func heal_alreadyMaxHealth_shouldThrowError() async throws { @@ -72,23 +69,12 @@ struct HealTest { .withMaxHealth(4) } .build() - let sut = await createGameStore(initialState: state) - - var receivedErrors: [Error] = [] - var cancellables: Set = [] - await MainActor.run { - sut.errorPublisher - .sink { receivedErrors.append($0) } - .store(in: &cancellables) - } // When - let action = GameAction.heal(1, player: "p1") - await sut.dispatch(action) - // Then - #expect(receivedErrors as? [GameError] == [ - .playerAlreadyMaxHealth("p1") - ]) + let action = GameAction.heal(1, player: "p1") + await #expect(throws: GameError.playerAlreadyMaxHealth("p1")) { + try await dispatch(action, state: state) + } } } diff --git a/WildWestOnline/Core/Game/Tests/Reducer/IncreaseMagnifyingTest.swift b/WildWestOnline/Core/Game/Tests/Reducer/IncreaseMagnifyingTest.swift index a3f07095b..42734f331 100644 --- a/WildWestOnline/Core/Game/Tests/Reducer/IncreaseMagnifyingTest.swift +++ b/WildWestOnline/Core/Game/Tests/Reducer/IncreaseMagnifyingTest.swift @@ -16,13 +16,12 @@ struct IncreaseMagnifyingTest { $0.withMagnifying(0) } .build() - let sut = await createGameStore(initialState: state) // When let action = GameAction.increaseMagnifying(1, player: "p1") - await sut.dispatch(action) + let result = try await dispatch(action, state: state) // Then - await #expect(sut.state.players.get("p1").magnifying == 1) + #expect(result.players.get("p1").magnifying == 1) } } diff --git a/WildWestOnline/Core/Game/Tests/Reducer/IncreaseRemotenessTest.swift b/WildWestOnline/Core/Game/Tests/Reducer/IncreaseRemotenessTest.swift index 57ef7595d..bafb9d9a0 100644 --- a/WildWestOnline/Core/Game/Tests/Reducer/IncreaseRemotenessTest.swift +++ b/WildWestOnline/Core/Game/Tests/Reducer/IncreaseRemotenessTest.swift @@ -16,13 +16,12 @@ struct IncreaseRemotenessTest { $0.withRemoteness(0) } .build() - let sut = await createGameStore(initialState: state) // When let action = GameAction.increaseRemoteness(1, player: "p1") - await sut.dispatch(action) + let result = try await dispatch(action, state: state) // Then - await #expect(sut.state.players.get("p1").remoteness == 1) + #expect(result.players.get("p1").remoteness == 1) } } diff --git a/WildWestOnline/Core/Game/Tests/Reducer/PassInPlayTest.swift b/WildWestOnline/Core/Game/Tests/Reducer/PassInPlayTest.swift index 0baf1ded5..41eb5b6c3 100644 --- a/WildWestOnline/Core/Game/Tests/Reducer/PassInPlayTest.swift +++ b/WildWestOnline/Core/Game/Tests/Reducer/PassInPlayTest.swift @@ -17,14 +17,13 @@ struct PassInPlayTest { } .withPlayer("p2") .build() - let sut = await createGameStore(initialState: state) // When let action = GameAction.passInPlay("c1", target: "p2", player: "p1") - await sut.dispatch(action) + let result = try await dispatch(action, state: state) // Then - await #expect(sut.state.players.get("p1").inPlay == ["c2"]) - await #expect(sut.state.players.get("p2").inPlay == ["c1"]) + #expect(result.players.get("p1").inPlay == ["c2"]) + #expect(result.players.get("p2").inPlay == ["c1"]) } } diff --git a/WildWestOnline/Core/Game/Tests/Reducer/PlayTest.swift b/WildWestOnline/Core/Game/Tests/Reducer/PlayTest.swift index ade026acc..26b02ec81 100644 --- a/WildWestOnline/Core/Game/Tests/Reducer/PlayTest.swift +++ b/WildWestOnline/Core/Game/Tests/Reducer/PlayTest.swift @@ -18,15 +18,14 @@ struct PlayTest { } .withCards(["c1": Card(name: "c1", onPlay: [.init(action: .drawDeck)])]) .build() - let sut = await createGameStore(initialState: state) // When let action = GameAction.play("c1", player: "p1") - await sut.dispatch(action) + let result = try await dispatch(action, state: state) // Then - await #expect(sut.state.players.get("p1").hand == ["c1"]) - await #expect(sut.state.discard.isEmpty) + #expect(result.players.get("p1").hand == ["c1"]) + #expect(result.discard.isEmpty) } @Test func play_firstTime_shouldSetPlayedThisTurn() async throws { @@ -37,14 +36,13 @@ struct PlayTest { } .withCards(["c1": Card(name: "c1", onPlay: [.init(action: .drawDeck)])]) .build() - let sut = await createGameStore(initialState: state) // When let action = GameAction.play("c1", player: "p1") - await sut.dispatch(action) + let result = try await dispatch(action, state: state) // Then - await #expect(sut.state.playedThisTurn["c1"] == 1) + #expect(result.playedThisTurn["c1"] == 1) } @Test func play_secondTime_shouldIncrementPlayedThisTurn() async throws { @@ -56,14 +54,13 @@ struct PlayTest { .withCards(["c1": Card(name: "c1", onPlay: [.init(action: .drawDeck)])]) .withPlayedThisTurn(["c1": 1]) .build() - let sut = await createGameStore(initialState: state) // When let action = GameAction.play("c1", player: "p1") - await sut.dispatch(action) + let result = try await dispatch(action, state: state) // Then - await #expect(sut.state.playedThisTurn["c1"] == 2) + #expect(result.playedThisTurn["c1"] == 2) } @Test func play_shouldQueueEffectsOfMatchingCardName() async throws { @@ -74,14 +71,13 @@ struct PlayTest { } .withCards(["c": Card(name: "c", onPlay: [.init(action: .drawDeck)])]) .build() - let sut = await createGameStore(initialState: state) // When let action = GameAction.play("c-2❤️", player: "p1") - await sut.dispatch(action) + let result = try await dispatch(action, state: state) // Then - await #expect(sut.state.queue.count == 1) + #expect(result.queue.count == 1) } @Test func play_withoutEffects_shouldThrowError() async throws { @@ -92,23 +88,12 @@ struct PlayTest { } .withCards(["c1": Card(name: "c1")]) .build() - let sut = await createGameStore(initialState: state) - - var receivedErrors: [Error] = [] - var cancellables: Set = [] - await MainActor.run { - sut.errorPublisher - .sink { receivedErrors.append($0) } - .store(in: &cancellables) - } // When - let action = GameAction.play("c1", player: "p1") - await sut.dispatch(action) - // Assert - #expect(receivedErrors as? [GameError] == [ - .cardNotPlayable("c1") - ]) + let action = GameAction.play("c1", player: "p1") + await #expect(throws: GameError.cardNotPlayable("c1")) { + try await dispatch(action, state: state) + } } } diff --git a/WildWestOnline/Core/Game/Tests/Reducer/SetPlayLimitPerTurnTest.swift b/WildWestOnline/Core/Game/Tests/Reducer/SetPlayLimitPerTurnTest.swift index cc709e2c7..70ae30ce4 100644 --- a/WildWestOnline/Core/Game/Tests/Reducer/SetPlayLimitPerTurnTest.swift +++ b/WildWestOnline/Core/Game/Tests/Reducer/SetPlayLimitPerTurnTest.swift @@ -14,13 +14,12 @@ struct SetPlayLimitPerTurnTest { let state = GameState.makeBuilder() .withPlayer("p1") .build() - let sut = await createGameStore(initialState: state) // When let action = GameAction.setPlayLimitPerTurn(["c1": 2], player: "p1") - await sut.dispatch(action) + let result = try await dispatch(action, state: state) // Then - await #expect(sut.state.players.get("p1").playLimitPerTurn["c1"] == 2) + #expect(result.players.get("p1").playLimitPerTurn["c1"] == 2) } } diff --git a/WildWestOnline/Core/Game/Tests/Reducer/SetWeaponTest.swift b/WildWestOnline/Core/Game/Tests/Reducer/SetWeaponTest.swift index bf57647d7..881952378 100644 --- a/WildWestOnline/Core/Game/Tests/Reducer/SetWeaponTest.swift +++ b/WildWestOnline/Core/Game/Tests/Reducer/SetWeaponTest.swift @@ -14,13 +14,12 @@ struct SetWeaponTest { let state = GameState.makeBuilder() .withPlayer("p1") .build() - let sut = await createGameStore(initialState: state) // When let action = GameAction.setWeapon(3, player: "p1") - await sut.dispatch(action) + let result = try await dispatch(action, state: state) // Then - await #expect(sut.state.players.get("p1").weapon == 3) + #expect(result.players.get("p1").weapon == 3) } } diff --git a/WildWestOnline/Core/Game/Tests/Reducer/ShootTest.swift b/WildWestOnline/Core/Game/Tests/Reducer/ShootTest.swift index 93b6027f0..a6e874a34 100644 --- a/WildWestOnline/Core/Game/Tests/Reducer/ShootTest.swift +++ b/WildWestOnline/Core/Game/Tests/Reducer/ShootTest.swift @@ -15,14 +15,13 @@ struct ShootTest { .withPlayer("p1") .withPlayer("p2") .build() - let sut = await createGameStore(initialState: state) // When let action = GameAction.shoot("p2", player: "p1") - await sut.dispatch(action) + let result = try await dispatch(action, state: state) // Then - let pending = try await #require(sut.state.queue.first) + let pending = try #require(result.queue.first) #expect(pending.kind == .damage) #expect(pending.payload.target == "p2") #expect(pending.payload.amount == 1) diff --git a/WildWestOnline/Core/Game/Tests/Reducer/StartTurnTest.swift b/WildWestOnline/Core/Game/Tests/Reducer/StartTurnTest.swift index 74c24562c..415fa8e54 100644 --- a/WildWestOnline/Core/Game/Tests/Reducer/StartTurnTest.swift +++ b/WildWestOnline/Core/Game/Tests/Reducer/StartTurnTest.swift @@ -13,14 +13,13 @@ struct StartTurnTest { // Given let state = GameState.makeBuilder() .build() - let sut = await createGameStore(initialState: state) // When let action = GameAction.startTurn(player: "p1") - await sut.dispatch(action) + let result = try await dispatch(action, state: state) // Then - await #expect(sut.state.turn == "p1") + #expect(result.turn == "p1") } @Test func startTurn_shouldResetPlayCounters() async throws { @@ -28,13 +27,12 @@ struct StartTurnTest { let state = GameState.makeBuilder() .withPlayedThisTurn(["c1": 1, "c2": 1]) .build() - let sut = await createGameStore(initialState: state) // When let action = GameAction.startTurn(player: "p1") - await sut.dispatch(action) + let result = try await dispatch(action, state: state) // Then - await #expect(sut.state.playedThisTurn.isEmpty) + #expect(result.playedThisTurn.isEmpty) } } diff --git a/WildWestOnline/Core/Game/Tests/Reducer/StealTest.swift b/WildWestOnline/Core/Game/Tests/Reducer/StealTest.swift index ff1959496..18f790ab5 100644 --- a/WildWestOnline/Core/Game/Tests/Reducer/StealTest.swift +++ b/WildWestOnline/Core/Game/Tests/Reducer/StealTest.swift @@ -17,15 +17,14 @@ struct StealTest { $0.withHand(["c21", "c22"]) } .build() - let sut = await createGameStore(initialState: state) // When let action = GameAction.stealHand("c21", target: "p2", player: "p1") - await sut.dispatch(action) + let result = try await dispatch(action, state: state) // Then - await #expect(sut.state.players.get("p1").hand == ["c21"]) - await #expect(sut.state.players.get("p2").hand == ["c22"]) + #expect(result.players.get("p1").hand == ["c21"]) + #expect(result.players.get("p2").hand == ["c22"]) } @Test func steal_shouldRemoveCardFromTargetInPlay() async throws { @@ -36,14 +35,13 @@ struct StealTest { $0.withInPlay(["c21", "c22"]) } .build() - let sut = await createGameStore(initialState: state) // When let action = GameAction.stealInPlay("c21", target: "p2", player: "p1") - await sut.dispatch(action) + let result = try await dispatch(action, state: state) // Then - await #expect(sut.state.players.get("p1").hand == ["c21"]) - await #expect(sut.state.players.get("p2").inPlay == ["c22"]) + #expect(result.players.get("p1").hand == ["c21"]) + #expect(result.players.get("p2").inPlay == ["c22"]) } }