This repository has been archived by the owner on Jun 1, 2023. It is now read-only.
forked from refactoring-challenge/reversi-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameWithPlayerAutomatorModelTests.swift
172 lines (140 loc) · 6.42 KB
/
GameWithPlayerAutomatorModelTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import XCTest
import Hydra
import ReactiveSwift
import MirrorDiffKit
import ReversiCore
class GameWithPlayerAutomatorModelTests: XCTestCase {
private let (lifetime, token) = ReactiveSwift.Lifetime.make()
func testPvP() throws {
let model = GameWithPlayerAutomatorModel(
strategy: PlayerAutomator.pendingSelector,
gameModel: GameModel(startsWith: .initial),
playersAutomationAvailabilityModel: PlayersAutomationAvailabilityModel(
startsWith: PlayersAutomationAvailability(
first: .disabled,
second: .disabled
)
)
)
XCTAssertEqual(model.gameModel.place(at: Coordinate(x: .f, y: .five)), .accepted)
try self.waitUntilTurnReady(gameModel: model.gameModel, turn: .second)
XCTAssertEqual(model.gameModel.place(at: Coordinate(x: .f, y: .six)), .accepted)
try self.waitUntilTurnReady(gameModel: model.gameModel, turn: .first)
XCTAssertEqual(model.gameModel.place(at: Coordinate(x: .c, y: .four)), .accepted)
try self.waitUntilTurnReady(gameModel: model.gameModel, turn: .second)
let actual = model.gameModel.stateDidChange.value.gameState
let expected = GameState(
board: Board(unsafeArray: [
[nil, nil, nil, nil, nil, nil, nil, nil],
[nil, nil, nil, nil, nil, nil, nil, nil],
[nil, nil, nil, nil, nil, nil, nil, nil],
[nil, nil, .dark, .dark, .dark, nil, nil, nil],
[nil, nil, nil, .dark, .light, .dark, nil, nil],
[nil, nil, nil, nil, nil, .light, nil, nil],
[nil, nil, nil, nil, nil, nil, nil, nil],
[nil, nil, nil, nil, nil, nil, nil, nil],
]),
turn: .second
)
XCTAssertEqual(actual, expected, diff(between: expected, and: actual))
}
func testPvC() throws {
let model = GameWithPlayerAutomatorModel(
strategy: PlayerAutomator.topLeftSelector,
gameModel: GameModel(startsWith: .initial),
playersAutomationAvailabilityModel: PlayersAutomationAvailabilityModel(
startsWith: PlayersAutomationAvailability(
first: .disabled,
second: .enabled
)
)
)
XCTAssertEqual(model.gameModel.place(at: Coordinate(x: .f, y: .five)), .accepted)
// NOTE: topLeftSelector will place at f4.
try self.waitUntilTurnReady(gameModel: model.gameModel, turn: .first)
XCTAssertEqual(model.gameModel.place(at: Coordinate(x: .e, y: .three)), .accepted)
// NOTE: topLeftSelector will place at d2.
try self.waitUntilTurnReady(gameModel: model.gameModel, turn: .first)
let actual = model.gameModel.stateDidChange.value.gameState
let expected = GameState(
board: Board(unsafeArray: [
[nil, nil, nil, nil, nil, nil, nil, nil],
[nil, nil, nil, .light, nil, nil, nil, nil],
[nil, nil, nil, nil, .light, nil, nil, nil],
[nil, nil, nil, .light, .dark, .light, nil, nil],
[nil, nil, nil, .dark, .dark, .dark, nil, nil],
[nil, nil, nil, nil, nil, nil, nil, nil],
[nil, nil, nil, nil, nil, nil, nil, nil],
[nil, nil, nil, nil, nil, nil, nil, nil],
]),
turn: .first
)
XCTAssertEqual(actual, expected, diff(between: expected, and: actual))
}
func testCvC() throws {
let model = GameWithPlayerAutomatorModel(
// NOTE: Use topLeftSelector to make the last board consistent.
strategy: PlayerAutomator.topLeftSelector,
gameModel: GameModel(startsWith: .initial),
playersAutomationAvailabilityModel: PlayersAutomationAvailabilityModel(
startsWith: PlayersAutomationAvailability(
first: .enabled,
second: .enabled
)
)
)
try self.waitUntilGameSet(gameModel: model.gameModel)
let actual = model.gameModel.stateDidChange.value.gameState
let expected = GameState(
board: Board(unsafeArray: [
[.light, .light, .light, .light, .light, .light, .light, .dark ],
[.light, .light, .light, .light, .light, .light, .dark, .dark ],
[.light, .light, .light, .light, .light, .dark, .light, .dark ],
[.light, .light, .light, .light, .dark, .light, .light, .dark ],
[.light, .light, .light, .light, .light, .light, .light, .dark ],
[.light, .light, .light, .dark, .light, .light, .light, .dark ],
[.light, .light, .light, .light, .dark, .dark, .light, .dark ],
[.dark, .dark, .dark, .dark, .dark, .dark, .light, .light ],
]),
turn: .first
)
XCTAssertEqual(actual, expected, diff(between: expected, and: actual))
}
private func waitUntilTurnReady(gameModel: GameModelProtocol, turn: Turn, line: UInt = #line) throws {
try self.waitUntilGameState(gameModel: gameModel, line: line) { gameModelState -> Bool in
switch gameModelState {
case .completed, .processing:
return false
case .ready(let gameState, _):
return gameState.turn == turn
}
}
}
private func waitUntilGameSet(gameModel: GameModelProtocol, line: UInt = #line) throws {
try self.waitUntilGameState(gameModel: gameModel, line: line) { gameModelState -> Bool in
switch gameModelState {
case .processing, .ready:
return false
case .completed:
return true
}
}
}
private func waitUntilGameState(gameModel: GameModelProtocol, line: UInt = #line, _ condition: @escaping (GameModelState) -> Bool) throws {
let result = gameModel.stateDidChange
.producer
.take(during: self.lifetime)
.filter(condition)
.take(first: 1)
.timeout(after: 10, raising: TimeoutExceeded(), on: ReactiveSwift.QueueScheduler())
.single()!
switch result {
case .success:
return
case .failure(let error):
XCTFail("\(error)", line: line)
throw error
}
}
struct TimeoutExceeded: Error {}
}