From a647841c796674c20a5677fc6a9b5fbeb026d207 Mon Sep 17 00:00:00 2001 From: William Billingsley <wbilling@une.edu.au> Date: Wed, 31 Jul 2024 10:51:35 +1000 Subject: [PATCH] Add unit tests to detect assignment errors --- .../dotsandboxes/DotsAndBoxesGridTest.java | 46 ++++++++++++++----- 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java b/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java index 1946bed..21ae10e 100644 --- a/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java +++ b/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java @@ -8,24 +8,46 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; public class DotsAndBoxesGridTest { - /* - * Because Test classes are classes, they can have fields, and can have static fields. - * This field is a logger. Loggers are like a more advanced println, for writing messages out to the console or a log file. - */ private static final Logger logger = LogManager.getLogger(DotsAndBoxesGridTest.class); - /* - * Tests are functions that have an @Test annotation before them. - * The typical format of a test is that it contains some code that does something, and then one - * or more assertions to check that a condition holds. - * - * This is a dummy test just to show that the test suite itself runs - */ @Test public void testTestSuiteRuns() { logger.info("Dummy test to show the test suite runs"); assertTrue(true); } - // FIXME: You need to write tests for the two known bugs in the code. + @Test + public void testBoxComplete() { + DotsAndBoxesGrid grid = new DotsAndBoxesGrid(4, 4, 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), "Box at (0, 0) should be complete"); + } + + @Test + public void testDrawHorizontalAlreadyDrawn() { + DotsAndBoxesGrid grid = new DotsAndBoxesGrid(4, 4, 2); + grid.drawHorizontal(0, 0, 1); + IllegalStateException exception = assertThrows(IllegalStateException.class, () -> { + grid.drawHorizontal(0, 0, 1); + }); + String expectedMessage = "Line already drawn"; + String actualMessage = exception.getMessage(); + assertTrue(actualMessage.contains(expectedMessage)); + } + + @Test + public void testDrawVerticalAlreadyDrawn() { + DotsAndBoxesGrid grid = new DotsAndBoxesGrid(4, 4, 2); + grid.drawVertical(0, 0, 1); + IllegalStateException exception = assertThrows(IllegalStateException.class, () -> { + grid.drawVertical(0, 0, 1); + }); + String expectedMessage = "Line already drawn"; + String actualMessage = exception.getMessage(); + assertTrue(actualMessage.contains(expectedMessage)); + } } -- GitLab