Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
stephtelolahy committed Jan 7, 2025
1 parent 91b77a1 commit 7c2f4b4
Show file tree
Hide file tree
Showing 27 changed files with 187 additions and 297 deletions.
16 changes: 8 additions & 8 deletions WildWestOnline/Core/App/Sources/SetupGameCore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func setupGameReducer(
guard let action = action as? SetupGameAction else {
return .none
}

switch action {
case .startGame:
let settings = state.settings
Expand All @@ -39,10 +39,10 @@ func setupGameReducer(
NavigationStackAction<MainDestination>.push(.game)
}
])

case .setGame(let game):
state.game = game

case .quitGame:
return .group([
.run {
Expand All @@ -52,11 +52,11 @@ func setupGameReducer(
NavigationStackAction<MainDestination>.pop
}
])

case .unsetGame:
state.game = nil
}

return .none
}

Expand All @@ -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
}
16 changes: 0 additions & 16 deletions WildWestOnline/Core/Game/Tests/CreateGameStore.swift

This file was deleted.

43 changes: 43 additions & 0 deletions WildWestOnline/Core/Game/Tests/Dispatch.swift
Original file line number Diff line number Diff line change
@@ -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<AnyCancellable> = []
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<GameState, Void> {
.init(
initialState: initialState,
reducer: gameReducer,
dependencies: ()
)
}
5 changes: 2 additions & 3 deletions WildWestOnline/Core/Game/Tests/Reducer/ActivateTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"]])
}
}
10 changes: 4 additions & 6 deletions WildWestOnline/Core/Game/Tests/Reducer/DamageTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
}
}
14 changes: 6 additions & 8 deletions WildWestOnline/Core/Game/Tests/Reducer/DiscardTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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"])
}
}
61 changes: 18 additions & 43 deletions WildWestOnline/Core/Game/Tests/Reducer/DiscoverTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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<AnyCancellable> = []
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 {
Expand All @@ -91,23 +77,12 @@ struct DiscoverTest {
.withDiscovered(["c1"])
.withDeck(["c1"])
.build()
let sut = await createGameStore(initialState: state)

var receivedErrors: [Error] = []
var cancellables: Set<AnyCancellable> = []
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)
}
}
}
Loading

0 comments on commit 7c2f4b4

Please sign in to comment.