From 5c0457cee2772f335bad61444b7f66a64769fc1e Mon Sep 17 00:00:00 2001 From: Chelsea Allen <callen31@myune.edu.au> Date: Thu, 25 Jul 2024 14:18:02 +1000 Subject: [PATCH] Add unit tests for bugs 1. Test boxComplete detects completed boxes and returns false for incomplete boxes. 2. Test horizontal and vertical draw methods throw exceptions for already-drawn lines. --- src/main/java/dotsandboxes/Main.java | 2 +- .../dotsandboxes/DotsAndBoxesGridTest.java | 67 ++++++++++++++++++- 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/src/main/java/dotsandboxes/Main.java b/src/main/java/dotsandboxes/Main.java index 048ad46..be056ba 100644 --- a/src/main/java/dotsandboxes/Main.java +++ b/src/main/java/dotsandboxes/Main.java @@ -11,7 +11,7 @@ public class Main { JFrame mainWindow = new JFrame("Dots and Boxes"); DotsAndBoxesGrid grid = new DotsAndBoxesGrid(15, 8, 2); - // Updated with name and student numer + // Updated with name and student number JLabel label = new JLabel("Name: Chelsea Allen (220155519)"); JPanel borderPane = new JPanel(new BorderLayout()); diff --git a/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java b/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java index 1946bed..326e174 100644 --- a/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java +++ b/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java @@ -27,5 +27,70 @@ public class DotsAndBoxesGridTest { assertTrue(true); } - // FIXME: You need to write tests for the two known bugs in the code. + /* + * Test if boxComplete returns true for complete box + */ + @Test + public void testBoxCompleteDetectsCompletedBoxes() { + DotsAndBoxesGrid grid = new DotsAndBoxesGrid(15, 8, 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)); + } + + /* + * 3 tests to determine if boxComplete returns false for incomplete box (no lines, 1 line, 2 lines) + */ + @Test + public void testBoxCompleteNoLinesDrawn() { + DotsAndBoxesGrid grid = new DotsAndBoxesGrid(20, 10, 2); + assertFalse(grid.boxComplete(0, 0)); + } + + @Test + public void testBoxCompleteOneLineDrawn() { + DotsAndBoxesGrid grid = new DotsAndBoxesGrid(20, 10, 2); + grid.drawHorizontal(0, 0, 1); + assertFalse(grid.boxComplete(0, 0)); + } + + @Test + public void testBoxCompleteTwoLinesDrawn() { + DotsAndBoxesGrid grid = new DotsAndBoxesGrid(20, 10, 2); + grid.drawHorizontal(0, 0, 1); + grid.drawVertical(0, 0, 1); + assertFalse(grid.boxComplete(0, 0)); + } + + /* + * Test if horizontal draw method throws exception for already-drawn lines + */ + @Test + public void testDrawHorizontalDetectsRedrawnLines() { + DotsAndBoxesGrid grid = new DotsAndBoxesGrid(20, 10, 2); + + // draw a horizontal line at (0, 0) and claim a box + grid.drawHorizontal(0, 0, 1); + + // attempt to draw the same line, should throw an IllegalStateException + assertThrows(IllegalStateException.class, () -> grid.drawHorizontal(0, 0, 1)); + + } + + /* + * Test if vertical draw method throws exception for already-drawn lines + */ + @Test + public void testDrawVerticalDetectsRedrawnLines() { + DotsAndBoxesGrid grid = new DotsAndBoxesGrid(20, 10, 2); + + // draw a vertical line at (0, 0) and claim a box + grid.drawVertical(0, 0, 1); + + // attempt to draw the same line, should throw an IllegalStateException + assertThrows(IllegalStateException.class, () -> grid.drawVertical(0, 0, 1)); + + } } -- GitLab