From e0fe5c107242cadeee740dbb6e858fdc963c55ad Mon Sep 17 00:00:00 2001
From: Alex Milliken <amillik4@myune.edu.au>
Date: Thu, 18 Jul 2024 14:27:27 +1000
Subject: [PATCH] Added unit tests

1. Added tests to confirm whether the boxComplete method was functioning correctly. Specifically whether it returns true for a complete box AND false for an incomplete box.

2. Added a test to confirm whether the drawVertical and drawHorizontal methods were detecting and throwing an exception for attempting to draw lines that already existed.

3. Removed FIXME line from previously updated name label
---
 src/main/java/dotsandboxes/Main.java          |  1 -
 .../dotsandboxes/DotsAndBoxesGridTest.java    | 42 ++++++++++++++++++-
 2 files changed, 41 insertions(+), 2 deletions(-)

diff --git a/src/main/java/dotsandboxes/Main.java b/src/main/java/dotsandboxes/Main.java
index 0e7a44b..21d2eaf 100644
--- a/src/main/java/dotsandboxes/Main.java
+++ b/src/main/java/dotsandboxes/Main.java
@@ -11,7 +11,6 @@ public class Main {
         JFrame mainWindow = new JFrame("Dots and Boxes");
         DotsAndBoxesGrid grid = new DotsAndBoxesGrid(15, 8, 2);
 
-        // FIXME: Update this label to show your name and student number
         JLabel label = new JLabel("Name: Alex Milliken | Student Number: 220275842");
 
         JPanel borderPane = new JPanel(new BorderLayout());
diff --git a/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java b/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java
index 1946bed..27f6f46 100644
--- a/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java
+++ b/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java
@@ -27,5 +27,45 @@ public class DotsAndBoxesGridTest {
         assertTrue(true);
     }
 
-    // FIXME: You need to write tests for the two known bugs in the code.
+    /*
+     * This test draws a box and checks that the box is drawn.
+     * Given that the boxComplete method always returns true, this test should always pass.
+     */
+    @Test
+    public void boxCompleteDetectsCompletedBoxes() {
+        DotsAndBoxesGrid grid = new DotsAndBoxesGrid(3, 3, 2);
+        grid.drawHorizontal(0, 0, 1);
+        grid.drawHorizontal(0, 1, 1);
+        grid.drawVertical(0, 0, 1);
+        grid.drawVertical(1, 0, 1);
+        assertTrue(grid.boxComplete(0, 0));
+    }
+
+    /*
+     * This test draws an incomplete box and checks that the box is not drawn.
+     * Given that the boxComplete method always returns true, this test should always fail.
+     */
+    @Test
+    public void boxCompleteDetectsIncompleteBoxes() {
+        DotsAndBoxesGrid grid = new DotsAndBoxesGrid(3, 3, 2);
+        grid.drawHorizontal(0, 0, 1);
+        grid.drawHorizontal(0, 1, 1);
+        grid.drawVertical(0, 0, 1);
+        assertFalse(grid.boxComplete(0, 0));
+    }
+
+
+    /*
+     * This test detects whether the drawHorizontal and drawVertical methods throw an exception if the line already has been drawn.
+     * Given that the drawHorizontal and drawVertical methods do not currently throw an exception, this test should always fail.
+     */
+    @Test
+    public void drawMethodsDetectRedrawnLines() {
+        DotsAndBoxesGrid grid = new DotsAndBoxesGrid(3, 3, 2);
+        grid.drawHorizontal(0, 0, 1);
+        assertThrows(IllegalStateException.class, () -> grid.drawHorizontal(0, 0, 1));
+        grid.drawVertical(0, 0, 1);
+        assertThrows(IllegalStateException.class, () -> grid.drawVertical(0, 0, 1));
+    }
+
 }
-- 
GitLab