From 69cd737b71e30eee15ff0e7d2efbbd541400b07e Mon Sep 17 00:00:00 2001 From: William Billingsley <wbilling@une.edu.au> Date: Sun, 28 Jul 2024 05:34:54 +0500 Subject: [PATCH] Added Unit Tests --- .../dotsandboxes/DotsAndBoxesGridTest.java | 68 ++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java b/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java index 1946bed..e639c7d 100644 --- a/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java +++ b/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java @@ -2,7 +2,6 @@ package dotsandboxes; import org.junit.jupiter.api.*; import static org.junit.jupiter.api.Assertions.*; -import static org.junit.jupiter.api.Assumptions.*; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -28,4 +27,71 @@ public class DotsAndBoxesGridTest { } // FIXME: You need to write tests for the two known bugs in the code. + + + @Test + public void testDrawHorizontalLineTwiceThrowsException() { + DotsAndBoxesGrid grid = new DotsAndBoxesGrid(3, 3, 2); + + grid.drawHorizontal(0, 0, 1); + + Exception exception = assertThrows(IllegalStateException.class, () -> { + grid.drawHorizontal(0, 0, 1); + }); + + String expectedMessage = "Line has already been drawn"; + String actualMessage = exception.getMessage(); + + assertTrue(actualMessage.contains(expectedMessage), "Expected an IllegalStateException for drawing a line that was already drawn"); + } + + @Test + public void testDrawVerticalLineTwiceThrowsException() { + DotsAndBoxesGrid grid = new DotsAndBoxesGrid(3, 3, 2); + + grid.drawVertical(0, 0, 1); + + Exception exception = assertThrows(IllegalStateException.class, () -> { + grid.drawVertical(0, 0, 1); + }); + + String expectedMessage = "Line has already been drawn"; + String actualMessage = exception.getMessage(); + + assertTrue(actualMessage.contains(expectedMessage), "Expected an IllegalStateException for drawing a line that was already drawn"); + } + + @Test + public void testBoxIncompleteAtLeftEdge() { + DotsAndBoxesGrid grid = new DotsAndBoxesGrid(3, 3, 2); + + // Draw three lines around the box at (0,1) + grid.drawHorizontal(0, 1, 1); + grid.drawHorizontal(0, 2, 1); + grid.drawVertical(0, 1, 1); + + assertFalse(grid.boxComplete(0, 1), "The box at (0,1) should not be complete with only three lines drawn"); + } + + @Test + public void testBoxIncomplete() { + DotsAndBoxesGrid grid = new DotsAndBoxesGrid(3, 3, 2); + + // Draw only one horizontal line + grid.drawHorizontal(0, 0, 1); + + assertFalse(grid.boxComplete(0, 0), "The box at (0,0) should not be complete with only one line drawn"); + + // Draw two lines (horizontal and vertical) + grid.drawVertical(0, 0, 1); + + assertFalse(grid.boxComplete(0, 0), "The box at (0,0) should not be complete with only two lines drawn"); + + // Draw three lines + grid.drawHorizontal(0, 1, 1); + + assertFalse(grid.boxComplete(0, 0), "The box at (0,0) should not be complete with only three lines drawn"); + } + + } -- GitLab