You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Connect4/Model/Tests/ModelTests/Rules/FourInARowRulesTests.swift

141 lines
4.7 KiB

import XCTest
@testable import Model
final class FourInARowRulesTests: XCTestCase {
private static let playerA = Player(name: "dummy A", piece_type: .A)
private static let playerB = Player(name: "dummy B", piece_type: .B)
private var rules: FourInARowRules!
private var board: Board!
private var players: [Player]!
override func setUpWithError() throws {
try super.setUpWithError()
self.players = [
Player(name: "A", piece_type: .A),
Player(name: "B", piece_type: .B)
]
guard let rules = FourInARowRules(columns: 3, rows: 3, minAligned: 3, players: players) else {
XCTFail()
return
}
self.rules = rules
self.board = self.rules.createBoard()
}
func testBoardIsValid() {
XCTAssertTrue(self.rules.isValid(board: self.board))
}
func testInitialState() {
XCTAssertEqual(
self.rules.gameState(board: board, last_turn: nil),
GameState.Playing(turn: self.players.first!)
)
}
func testMovesAreValid() {
for move in self.rules.validMoves(board: self.board) {
XCTAssertTrue(self.rules.isValid(board: board, move: move), "\(move)")
}
}
func testMovesOfPlayerAreValid() throws {
for player in self.players {
try self.setUpWithError()
try self._testMovesOfPlayerAreValid(player: player)
try self.tearDownWithError()
}
}
private func _testMovesOfPlayerAreValid(player: Player) throws {
for action in self.rules.validMoves(board: self.board, for_player: player) {
XCTAssertTrue(self.rules.isValid(board: board, move: Move(player: player, action: action)), "\(action)")
}
}
func testOnMoveDone() {
let coord = Coords(1, board.rows - 1)
board[coord] = Piece(owner: Self.playerA)
XCTAssertEqual(
rules.gameState(board: board, last_turn: players.first!),
GameState.Playing(turn: players.last!)
)
}
func testOnMoveDoneDraw() {
board[0, 0] = Piece(owner: Self.playerA)
board[1, 0] = Piece(owner: Self.playerA)
board[2, 0] = Piece(owner: Self.playerB)
board[0, 1] = Piece(owner: Self.playerB)
board[1, 1] = Piece(owner: Self.playerB)
board[2, 1] = Piece(owner: Self.playerA)
board[0, 2] = Piece(owner: Self.playerA)
board[1, 2] = Piece(owner: Self.playerA)
board[2, 2] = Piece(owner: Self.playerB)
XCTAssertTrue(rules.isValid(board: board))
XCTAssertEqual(rules.gameState(board: board, last_turn: players.last!), GameState.Draw)
}
func testOnMoveDoneWin() {
board[0, 0] = Piece(owner: Self.playerA)
board[1, 0] = Piece(owner: Self.playerA)
board[2, 0] = Piece(owner: Self.playerA) //
board[0, 1] = Piece(owner: Self.playerB)
board[1, 1] = Piece(owner: Self.playerB)
board[2, 1] = Piece(owner: Self.playerA)
board[0, 2] = Piece(owner: Self.playerA)
board[1, 2] = Piece(owner: Self.playerA)
board[2, 2] = Piece(owner: Self.playerB)
XCTAssertTrue(rules.isValid(board: board))
XCTAssertEqual(
rules.gameState(board: board, last_turn: players.first!),
GameState.Win(winner: players.first!, board: board, cells: [Coords(0, 0), Coords(1, 0), Coords(2, 0)])
)
}
func testCountMaxRow() throws {
for (coords, expected) in [
(Coords(1, 0), 3),
(Coords(0, 1), 2),
(Coords(0, 2), 2),
(Coords(1, 1), 3),
(Coords(2, 2), 3),
(Coords(2, 1), 2),
] {
try self.setUpWithError()
// AAA
// BAB
// BBA
board[0, 0] = Piece(owner: Self.playerA)
board[1, 0] = Piece(owner: Self.playerA)
board[2, 0] = Piece(owner: Self.playerA)
board[0, 1] = Piece(owner: Self.playerB)
board[1, 1] = Piece(owner: Self.playerA)
board[2, 1] = Piece(owner: Self.playerB)
board[0, 2] = Piece(owner: Self.playerB)
board[1, 2] = Piece(owner: Self.playerB)
board[2, 2] = Piece(owner: Self.playerA)
self._testCountMaxRow(coords: coords, expected: expected)
try self.tearDownWithError()
}
}
private func _testCountMaxRow(coords: Coords, expected: Int) {
XCTAssertEqual(FourInARowRules.countMaxRow(center: coords, board: board), expected, "\(coords)")
}
}