Skip to content
Snippets Groups Projects
Select Git revision
  • 53fceabcf5c878971e27791666293bce7fd1453d
  • main default protected
  • 1-fix-assignment-errors
  • testsfail
4 results

Main.java

Blame
  • DotsAndBoxesGridTest.java 2.49 KiB
    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;
    
    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 testBoxCompletionDetection() {
            DotsAndBoxesGrid grid = new DotsAndBoxesGrid(4, 3, 2);
            
            / Draw only three sides of a box (not completing it)
            grid.drawHorizontal(0, 0, 1); // Top
            grid.drawVertical(0, 0, 1);   // Left
            grid.drawHorizontal(0, 1, 1); // Bottom
            
            // Check if the box at (0, 0) is incorrectly identified as completed
            assertTrue(grid.boxComplete(0, 0), "The box should not be completed.");
        }
    
        @Test
        public void testBoxComplete() {
            // Draw lines around a box and check if it's completed
            game.drawHorizontal(0, 0, 1); // Top horizontal line
            game.drawVertical(0, 0, 1); // Left vertical line
            game.drawHorizontal(0, 1, 1); // Bottom horizontal line
            game.drawVertical(1, 0, 1); // Right vertical line
    
            assertTrue("Box should be completed", game.boxComplete(0, 0));
        }
    
        @Test(expected = IllegalStateException.class)
        public void testDrawHorizontalLineAlreadyDrawn() {
            game.drawHorizontal(0, 0, 1);
            game.drawHorizontal(0, 0, 1); // Drawing the same line should throw an exception
        }
    
        @Test(expected = IllegalStateException.class)
        public void testDrawVerticalLineAlreadyDrawn() {
            game.drawVertical(0, 0, 1);
            game.drawVertical(0, 0, 1); // Drawing the same line should throw an exception
        }
    }