From a0a254688574e32a1fadaced1ca6473c33a37eb6 Mon Sep 17 00:00:00 2001 From: Alexis Drai Date: Wed, 8 Feb 2023 16:08:40 +0100 Subject: [PATCH] :white_check_mark: Test Bot --- .../Sources/connect4_lib/players/Human.swift | 6 +-- .../Tests/connect4_libTests/BotTest.swift | 39 ++++++++++++++++++- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/Connect4/connect4_lib/Sources/connect4_lib/players/Human.swift b/Connect4/connect4_lib/Sources/connect4_lib/players/Human.swift index 561edbc..5029ecb 100644 --- a/Connect4/connect4_lib/Sources/connect4_lib/players/Human.swift +++ b/Connect4/connect4_lib/Sources/connect4_lib/players/Human.swift @@ -10,9 +10,5 @@ public class Human: Player { super.init(withId: id, withName: name) } - public override func chooseColumn(inBoard board: Board, - withRules rules: IRules) - -> Int? { - return scanner() - } + public override func chooseColumn(inBoard board: Board, withRules rules: IRules) -> Int? { return scanner() } } diff --git a/Connect4/connect4_lib/Tests/connect4_libTests/BotTest.swift b/Connect4/connect4_lib/Tests/connect4_libTests/BotTest.swift index 20308a7..3ce4b1d 100644 --- a/Connect4/connect4_lib/Tests/connect4_libTests/BotTest.swift +++ b/Connect4/connect4_lib/Tests/connect4_libTests/BotTest.swift @@ -2,5 +2,42 @@ import XCTest import connect4_lib final class BotTest: XCTestCase { - + + func testInit() throws { + func expect(initBotWithId id: Int, + andName name: String, + shouldNotBeNil: Bool) { + let bot = Bot(withId: id, withName: name) + if !shouldNotBeNil { + XCTAssertNil(bot) + return + } + XCTAssertNotNil(bot) + XCTAssertEqual(id, bot?.id) + XCTAssertEqual(name, bot?.name) + } + + expect(initBotWithId: 0, andName: "Bob", shouldNotBeNil: true) + expect(initBotWithId: -1, andName: "Bob", shouldNotBeNil: false) + expect(initBotWithId: 0, andName: "", shouldNotBeNil: false) + expect(initBotWithId: 0, andName: " ", shouldNotBeNil: false) + } + + func testChooseColumn() throws { + guard let board = Board(withGrid: [[1, nil, nil], + [2, 2, nil], + [1, 1, 2]]) else { XCTAssertFalse(true); return } + + guard let rules = BasicDefaultsNoDiag(withMinNbRows: 3, + withMaxNbRows: 5, + withMinNbCols: 3, + withMaxNbCols: 5, + withNbChipsToAlign: 3) else { XCTAssertFalse(true); return } + + for _ in 0..<100 { + let choice = Bot(withId: 1, withName: "Clyde")?.chooseColumn(inBoard: board, withRules: rules) + XCTAssertNotNil(choice) + XCTAssertTrue(0 <= choice! && choice! < board.nbCols) + } + } }