From 27c49e34a2fbfcae3778e2a747309f01a1bea08b Mon Sep 17 00:00:00 2001 From: Alexis Drai Date: Wed, 8 Feb 2023 15:34:50 +0100 Subject: [PATCH] :white_check_mark: Test Human, add validation to class --- .../Sources/connect4_lib/players/Player.swift | 4 +++ .../Tests/connect4_libTests/BotTest.swift | 6 ++++ .../Tests/connect4_libTests/GameTest.swift | 21 ++++++++++++ .../Tests/connect4_libTests/HumanTest.swift | 32 +++++++++++++++++++ 4 files changed, 63 insertions(+) create mode 100644 Connect4/connect4_lib/Tests/connect4_libTests/BotTest.swift create mode 100644 Connect4/connect4_lib/Tests/connect4_libTests/GameTest.swift create mode 100644 Connect4/connect4_lib/Tests/connect4_libTests/HumanTest.swift diff --git a/Connect4/connect4_lib/Sources/connect4_lib/players/Player.swift b/Connect4/connect4_lib/Sources/connect4_lib/players/Player.swift index 3a1e5f1..00b2f6c 100644 --- a/Connect4/connect4_lib/Sources/connect4_lib/players/Player.swift +++ b/Connect4/connect4_lib/Sources/connect4_lib/players/Player.swift @@ -7,6 +7,10 @@ public class Player { init?(withId id: Int, withName name: String){ + guard(id >= 0 + && !(name.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty)) + else { return nil } + self.id = id self.name = name } diff --git a/Connect4/connect4_lib/Tests/connect4_libTests/BotTest.swift b/Connect4/connect4_lib/Tests/connect4_libTests/BotTest.swift new file mode 100644 index 0000000..20308a7 --- /dev/null +++ b/Connect4/connect4_lib/Tests/connect4_libTests/BotTest.swift @@ -0,0 +1,6 @@ +import XCTest +import connect4_lib + +final class BotTest: XCTestCase { + +} diff --git a/Connect4/connect4_lib/Tests/connect4_libTests/GameTest.swift b/Connect4/connect4_lib/Tests/connect4_libTests/GameTest.swift new file mode 100644 index 0000000..a27f251 --- /dev/null +++ b/Connect4/connect4_lib/Tests/connect4_libTests/GameTest.swift @@ -0,0 +1,21 @@ +import XCTest +import connect4_lib + +final class GameTest: 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. + } +} diff --git a/Connect4/connect4_lib/Tests/connect4_libTests/HumanTest.swift b/Connect4/connect4_lib/Tests/connect4_libTests/HumanTest.swift new file mode 100644 index 0000000..bcf6475 --- /dev/null +++ b/Connect4/connect4_lib/Tests/connect4_libTests/HumanTest.swift @@ -0,0 +1,32 @@ +import XCTest +import connect4_lib + +final class HumanTest: XCTestCase { + + func scan() -> Int { + return 0 + } + + func testInit() throws { + func expect(initHumanWithId id: Int, + andName name: String, + andScanner scanner: @escaping () -> Int, + shouldNotBeNil: Bool) { + let human = Human(withId: id, withName: name, usingScanner: scanner) + if !shouldNotBeNil { + XCTAssertNil(human) + return + } + XCTAssertNotNil(human) + XCTAssertEqual(id, human?.id) + XCTAssertEqual(name, human?.name) + } + + expect(initHumanWithId: 0, andName: "Bob", andScanner: scan, shouldNotBeNil: true) + expect(initHumanWithId: -1, andName: "Bob", andScanner: scan, shouldNotBeNil: false) + expect(initHumanWithId: 0, andName: "", andScanner: scan, shouldNotBeNil: false) + expect(initHumanWithId: 0, andName: " ", andScanner: scan, shouldNotBeNil: false) + } + + //not testing for pebcak +}