feat - add tests
continuous-integration/drone/push Build is passing Details

main
DJYohann 2 years ago
parent 78c4818516
commit 700d5e804e

@ -35,7 +35,3 @@ if var board = Board(withGrid: [[1,2,1], [2,1,nil], [nil,nil,nil]]){
insertPiece(id: 1, column: 0, &board)
insertPiece(id: 1, column: 0, &board)
}
var c = ClassicRules()
var b = c.createBoard()
print(b)

@ -79,7 +79,7 @@ public struct Board : CustomStringConvertible {
/// Verify if the board is full
///
/// - Returns: True if the board is full else false
private func isFull() -> Bool{
public func isFull() -> Bool{
for column in 0..<nbColumns{
if !isColumnFull(column: column){
return false
@ -111,8 +111,10 @@ public struct Board : CustomStringConvertible {
/// - Parameter column : The column to add the piece
/// - Returns: The result of the insertion in a `BoardResult`
public mutating func insertPiece(id : Int, column:Int) -> BoardResult {
for row in 0..<nbRows{
return insertPiece(id: id, row: row, column: column)
for row in 0..<nbRows {
if (row == nil) {
return insertPiece(id: id, row: row, column: column)
}
}
return .ok
}

@ -1,18 +1,21 @@
/// Cclassic rules of Connect 4 game
public class ClassicRules {
public func isGameOver() -> (Bool, Int, GameResult) {
(true, 8, .lose)
}
public static var nbRows: Int = 6
public static var nbColumns: Int = 7
public static var nbAlignedPieces: Int = 4
public static var nbTrials: Int = 3
public init() {}
///
public func createBoard() -> Board {
return Board(withNbRows: Self.nbRows, withNbColumns: Self.nbColumns)!
}
// public func createBoard() -> Board {
//
// }
//
// public func isGameOver() -> (Bool, Int, GameResult) {
// <#code#>
// }
//
// public func getNextPlayer() -> Int {
// <#code#>
// }
}

@ -1,30 +0,0 @@
//
// BoardTest.swift
//
//
// Created by BREUIL Yohann on 19/01/2023.
//
import XCTest
import Model
final class BoardTest: XCTestCase {
func testInit() throws {
func expect(nbRows : Int, nbColumns : Int, notNil : Bool) {
let board = Board(withNbRows: nbRows, withNbColumns: nbColumns)
if !notNil {
XCTAssertNil(board)
return
}
XCTAssertNotNil(board)
XCTAssertEqual(board?.nbRows, nbRows)
XCTAssertEqual(board?.nbColumns, nbColumns)
}
expect(nbRows: 6, nbColumns: 7, notNil: true)
expect(nbRows: 0, nbColumns: 7, notNil: false)
expect(nbRows: 6, nbColumns: 0, notNil: false)
expect(nbRows: 0, nbColumns: 0, notNil: false)
}
}

@ -0,0 +1,79 @@
import XCTest
import Model
final class BoardTest: XCTestCase {
func testInitWithVaalues() throws {
func expect(nbRows : Int, nbColumns : Int, notNil : Bool) {
let board = Board(withNbRows: nbRows, withNbColumns: nbColumns)
if !notNil {
XCTAssertNil(board)
return
}
XCTAssertNotNil(board)
XCTAssertEqual(board?.nbRows, nbRows)
XCTAssertEqual(board?.nbColumns, nbColumns)
}
expect(nbRows: 6, nbColumns: 7, notNil: true)
expect(nbRows: 0, nbColumns: 7, notNil: false)
expect(nbRows: 6, nbColumns: 0, notNil: false)
expect(nbRows: 0, nbColumns: 0, notNil: false)
expect(nbRows: -2, nbColumns: 7, notNil: false)
expect(nbRows: 6, nbColumns: -2, notNil: false)
}
func testInitWithGrid() {
func expect(grid : [[Int?]], notNil: Bool) {
let board = Board(withGrid: grid)
if !notNil {
XCTAssertNil(board)
return
}
XCTAssertNotNil(board)
XCTAssertEqual(board?.gridBoard, grid)
}
expect(grid: [[1,2], [2,1], [2,1]], notNil: true)
expect(grid: [[1,2], [2,1], [nil,nil]], notNil: true)
expect(grid: [[nil,nil], [nil,nil], [nil,nil]], notNil: true)
expect(grid: [[1], [2,1], [nil,nil]], notNil: false)
}
func testIsFull() {
func expect(grid : [[Int?]], isFull: Bool, notNil: Bool) {
let board = Board(withGrid: grid)
if !notNil {
XCTAssertNil(board)
return
}
XCTAssertNotNil(board)
XCTAssertEqual(board?.isFull(), isFull)
}
expect(grid: [[1,2], [2,1]], isFull: true, notNil: true)
expect(grid: [[1,nil], [2,nil]], isFull: false, notNil: true)
expect(grid: [[1,2], [2,nil]], isFull: false, notNil: true)
expect(grid: [[1,nil], [2,1]], isFull: false, notNil: true)
}
func testInsertPiece() {
func expect(grid : [[Int?]], idPlayer: Int, column: Int, boardResult : BoardResult, notNil: Bool) {
var board = Board(withGrid: grid)
if !notNil {
XCTAssertNil(board)
return
}
XCTAssertNotNil(board)
//XCTAssertEqual(board?.insertPiece(id: idPlayer, column: column), boardResult)
}
expect(grid: [[1,nil], [2,nil]], idPlayer: 2, column: 0, boardResult: .ok, notNil: true)
}
}

@ -0,0 +1,35 @@
//
// HumanTests.swift
//
//
// Created by BREUIL Yohann on 11/02/2023.
//
import XCTest
final class HumanTests: XCTestCase {
override func setUpWithError() throws {
// Put setup code here. This method is called before the invocation of each test method in the class.
}
override func tearDownWithError() throws {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}
func testExample() throws {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.
// Any test you write for XCTest can be annotated as throws and async.
// Mark your test throws to produce an unexpected failure when your test encounters an uncaught error.
// Mark your test async to allow awaiting for asynchronous code to complete. Check the results with assertions afterwards.
}
func testPerformanceExample() throws {
// This is an example of a performance test case.
self.measure {
// Put the code you want to measure the time of here.
}
}
}
Loading…
Cancel
Save