Repository: marcusbuffett/command-line-chess (language: python)

Issue:
Adding unittests
There are not really tests in this repository which makes it a bit hard to ensure there are no bugs and/or unintentional behavior.

To fix this, unittests should be written.
When this is done, integration tests can be done too.

~~Right now there is only one (unnecessary) test.~~
All new tests should be created in a new directory called `tests/` and because it is already in use, `pytest` should be used as test-framework.


Discussion / hints:
Hi, I am want to solve this, can you assign me?

Fix patch:
diff --git a/src/Board.py b/src/Board.py
index 4b4709d..a064c99 100644
--- a/src/Board.py
+++ b/src/Board.py
@@ -2,8 +2,6 @@
 
 from typing import Optional, no_type_check
 
-from termcolor import colored
-
 from src.Bishop import Bishop
 from src.Coordinate import Coordinate as C
 from src.King import King
@@ -13,7 +11,6 @@
 from src.Piece import Piece
 from src.Queen import Queen
 from src.Rook import Rook
-import colored
 from colored import fg, bg, attr
 
 WHITE = True


Test patch:
diff --git a/pytest.ini b/pytest.ini
new file mode 100644
index 0000000..cf74f90
--- /dev/null
+++ b/pytest.ini
@@ -0,0 +1,3 @@
+[pytest]
+addopts = -rP
+python_files = Test*.py
\ No newline at end of file
diff --git a/tests/TestBoard.py b/tests/TestBoard.py
new file mode 100644
index 0000000..c448079
--- /dev/null
+++ b/tests/TestBoard.py
@@ -0,0 +1,53 @@
+import pytest
+from src.Board import Board
+
+
+@pytest.mark.parametrize(
+    "moves",
+    [
+        # Scholar’s mate
+        ["e4", "e5", "Bc4", "Nc6", "Qh5", "Nf6", "Qxf7"],
+        # Fool’s mate
+        ["e4", "g5", "Nc3", "f5", "Qh5"],
+        ["g4", "e5", "f3", "Qh4"],
+        # Smothered Checkmate
+        ["e4", "e5", "Ne2", "Nc6", "Nbc3", "Nd4", "g3", "Nf3"],
+        # Hippopotamus mate
+        [
+            "e4",
+            "e5",
+            "Ne2",
+            "Qh4",
+            "Nbc3",
+            "Nc6",
+            "g3",
+            "Qg5",
+            "d4",
+            "Nxd4",
+            "Bxg5",
+            "Nf3",
+        ],
+        # Legal’s mate
+        [
+            "e4",
+            "e5",
+            "Bc4",
+            "d6",
+            "Nf3",
+            "Bg4",
+            "Nc3",
+            "g6",
+            "Nxe5",
+            "Bxd1",
+            "Bxf7",
+            "Ke7",
+            "Nd5",
+        ],
+    ],
+)
+def testMate(moves, makeBoardMoves):
+    board = Board()
+
+    makeBoardMoves(board, moves)
+
+    assert board.isCheckmate()
diff --git a/tests/TestCoordinate.py b/tests/TestCoordinate.py
new file mode 100644
index 0000000..8aec0b4
--- /dev/null
+++ b/tests/TestCoordinate.py
@@ -0,0 +1,35 @@
+import pytest
+
+from src.Coordinate import Coordinate
+
+
+@pytest.mark.parametrize(
+    ["coords", "other", "expected"],
+    [
+        [(1, 1), (1, 1), (2, 2)],
+        [(0, 1), (2, 3), (2, 4)],
+    ],
+)
+def testAddition(coords, other, expected):
+    assert Coordinate(*coords) + Coordinate(*other) == Coordinate(*expected)
+
+@pytest.mark.parametrize(
+    ["coords", "other", "expected"],
+    [
+        [(1, 1), (1, 1), (0, 0)],
+        [(0, 1), (2, 3), (-2, -2)],
+    ],
+)
+def testSubtraction(coords, other, expected):
+    assert Coordinate(*coords) - Coordinate(*other) == Coordinate(*expected)
+
+@pytest.mark.parametrize(
+    ["coords", "other"],
+    [
+        [(1, 1), (1, 1)],
+        [(0, 1), 42],
+    ],
+)
+def testInvalidInput(coords, other):
+    with pytest.raises(Exception):
+        _ = Coordinate(*coords) - other
\ No newline at end of file
diff --git a/tests/TestPieces.py b/tests/TestPieces.py
new file mode 100644
index 0000000..c297386
--- /dev/null
+++ b/tests/TestPieces.py
@@ -0,0 +1,25 @@
+import pytest
+
+from src.Board import Board
+from src.Coordinate import Coordinate
+from src.Knight import Knight
+
+
+@pytest.mark.parametrize(
+    ["coord", "expected"],
+    [
+        [(4, 4), [(6, 5), (6, 3), (2, 5), (2, 3), (5, 6), (5, 2), (3, 2), (3, 6)]],
+        [(6, 4), [(8, 5), (8, 3), (4, 5), (4, 3), (7, 6), (7, 2), (5, 2), (5, 6)]],
+    ],
+)
+def testKnight(coord, expected):
+    board = Board()
+
+    knight = Knight(board, False, coord)
+
+    poss = []
+    for i in knight.getPossibleMoves():
+        assert i.oldPos == coord
+        poss.append(Coordinate(*i.newPos[:2]) + Coordinate(*i.newPos[2:]))
+
+    assert poss == expected
diff --git a/tests/__init__.py b/tests/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/tests/conftest.py b/tests/conftest.py
new file mode 100644
index 0000000..ee87964
--- /dev/null
+++ b/tests/conftest.py
@@ -0,0 +1,17 @@
+import pytest
+
+from src.InputParser import InputParser
+from src.Board import Board
+
+
+@pytest.fixture
+def makeBoardMoves():
+    def makeBoardMovesFactory(board, moves):
+        parser = InputParser(board, True)
+
+        for moveStr in moves:
+            move = parser.parse(moveStr)
+            parser.side = not parser.side
+            board.makeMove(move)
+
+    return makeBoardMovesFactory