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/EmptyBoardTests.swift

154 lines
5.0 KiB

import XCTest
@testable import Model
final class EmptyBoardTests: XCTestCase {
private var board: Board!
override func setUpWithError() throws {
try super.setUpWithError()
guard let board = Board(columns: 5, rows: 6) else {
XCTFail()
return
}
self.board = board
}
func testWidth() throws {
XCTAssertEqual(board.columns, 5)
}
func testHeight() throws {
XCTAssertEqual(board.rows, 6)
}
func testEmptyByDefault() throws {
XCTAssertNil(board[1, 1])
let result = board.countPieces()
XCTAssertEqual(result, 0)
}
func testCustomSubscript() throws {
for r in 0..<board.rows {
for c in 0..<board.columns {
for p: PieceType in [.A, .B] {
try self.setUpWithError()
try self._testCustomSubscript(at: Coords(c, r), player: p)
try self.tearDownWithError()
}
}
}
}
private func _testCustomSubscript(at: Coords, player: PieceType) throws {
board[at] = Piece(type: player)
XCTAssertEqual(board[at]?.type, player)
}
func testCounts() throws {
board[1, 2] = Piece(type: .B)
board[0, 2] = Piece(type: .A)
board[1, 1] = Piece(type: .A)
XCTAssertEqual(board.countPieces { piece in piece.type == .A }, 2)
XCTAssertEqual(board.countPieces { piece in piece.type == .B }, 1)
}
func testInsertsSides() throws {
for (side, offset, expected): (Direction, Int, (Int, Int)) in [
(.Top, 0, (0, 0)),
(.Left, 0, (0, 0)),
(.Bottom, 0, (0, 5)),
(.Right, 0, (4, 0)),
/* ... */
(.Top, 4, (4, 0)),
(.Left, 5, (0, 5)),
(.Bottom, 4, (4, 5)),
(.Right, 5, (4, 5)),
] {
try self.setUpWithError()
try self._testInsertSide(side: side, offset: offset, expectedCoords: Coords(pair: expected))
try self.tearDownWithError()
}
}
private func _testInsertSide(side: Direction, offset: Int, expectedCoords: Coords) throws {
let result = board.getInsertionCoordinates(from: side, offset: offset)
// Note: Not sure I want Coordinates to be a simple tuple or a struct, as both have advantages that the other dont:
// Tuples can be initialized like so (col, row) which I find quite handy
// Structs supports == operators
// ...
XCTAssertEqual(result.col, expectedCoords.col)
XCTAssertEqual(result.row, expectedCoords.row)
}
func testInsertsFallNoPiece() throws {
for (from, direction, expected): ((Int, Int), Direction, (Int, Int)) in [
((0, 0), .Bottom, (0, 5)),
((0, 0), .Left, (0, 0)),
((0, 0), .Right, (4, 0)),
((0, 0), .Top, (0, 0)),
/* ... */
((4, 5), .Bottom, (4, 5)),
((4, 5), .Left, (0, 5)),
((4, 5), .Right, (4, 5)),
((4, 5), .Top, (4, 0)),
] {
try self.setUpWithError()
try self._testInsertFall(from: Coords(pair: from), dir: direction, expectedCoords: .Border(at: Coords(pair: expected)))
try self.tearDownWithError()
}
}
func testInsertsFallOnPiece() throws {
for (from, direction, (expected, touched)): ((Int, Int), Direction, ((Int, Int), (Int, Int))) in [
((0, 0), .Bottom, ((0, 1), (0, 2))),
((0, 0), .Right, ((1, 0), (2, 0))),
((4, 5), .Left, ((3, 5), (2, 5))),
((4, 5), .Top, ((4, 3), (4, 2))),
] {
try self.setUpWithError()
// Place some pieces
for pos in [
(2, 0),
(2, 1),
(0, 2), (1, 2), (2, 2), (3, 2), (4, 2),
(2, 3),
(2, 4),
(2, 5),
] {
// ensure it works with any player
board[Coords(pair: pos)] = if (pos.0 + pos.1) & 1 == 1 {
Piece(type: .A)
} else {
Piece(type: .B)
}
}
try self._testInsertFall(
from: Coords(pair: from),
dir: direction,
expectedCoords: .Piece(at: Coords(pair: expected), touched: Coords(pair: touched))
)
try self.tearDownWithError()
}
}
private func _testInsertFall(from: Coords, dir: Direction, expectedCoords: Board.FallResult) throws {
let result = board.fallCoordinates(initialCoords: from, direction: dir)
XCTAssertEqual(result, expectedCoords)
}
}