diff --git a/src/main/java/ch/fritteli/labyrinth/model/Walls.java b/src/main/java/ch/fritteli/labyrinth/model/Walls.java index 332be6e..a48b82b 100644 --- a/src/main/java/ch/fritteli/labyrinth/model/Walls.java +++ b/src/main/java/ch/fritteli/labyrinth/model/Walls.java @@ -13,8 +13,8 @@ public class Walls { private final SortedSet directions = new TreeSet<>(); private final Set hardened = new HashSet<>(); - public boolean set(@NonNull final Direction direction) { - return this.directions.add(direction); + public void set(@NonNull final Direction direction) { + this.directions.add(direction); } public void setAll() { diff --git a/src/test/java/ch/fritteli/labyrinth/model/DirectionTest.java b/src/test/java/ch/fritteli/labyrinth/model/DirectionTest.java new file mode 100644 index 0000000..74b0d36 --- /dev/null +++ b/src/test/java/ch/fritteli/labyrinth/model/DirectionTest.java @@ -0,0 +1,15 @@ +package ch.fritteli.labyrinth.model; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class DirectionTest { + @Test + void invert() { + assertEquals(Direction.BOTTOM, Direction.TOP.invert()); + assertEquals(Direction.LEFT, Direction.RIGHT.invert()); + assertEquals(Direction.TOP, Direction.BOTTOM.invert()); + assertEquals(Direction.RIGHT, Direction.LEFT.invert()); + } +} diff --git a/src/test/java/ch/fritteli/labyrinth/model/PositionTest.java b/src/test/java/ch/fritteli/labyrinth/model/PositionTest.java new file mode 100644 index 0000000..c23d6d8 --- /dev/null +++ b/src/test/java/ch/fritteli/labyrinth/model/PositionTest.java @@ -0,0 +1,27 @@ +package ch.fritteli.labyrinth.model; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class PositionTest { + @Test + void move() { + // arrange + final Position sut = new Position(0, 0); + + // act + final Position resultTOP = sut.move(Direction.TOP); + final Position resultRIGHT = sut.move(Direction.RIGHT); + final Position resultBOTTOM = sut.move(Direction.BOTTOM); + final Position resultLEFT = sut.move(Direction.LEFT); + + // assert + // Original is unchanged + assertEquals(new Position(0, 0), sut); + assertEquals(new Position(0, -1), resultTOP); + assertEquals(new Position(1, 0), resultRIGHT); + assertEquals(new Position(0, 1), resultBOTTOM); + assertEquals(new Position(-1, 0), resultLEFT); + } +} diff --git a/src/test/java/ch/fritteli/labyrinth/model/WallsTest.java b/src/test/java/ch/fritteli/labyrinth/model/WallsTest.java new file mode 100644 index 0000000..69e19ba --- /dev/null +++ b/src/test/java/ch/fritteli/labyrinth/model/WallsTest.java @@ -0,0 +1,137 @@ +package ch.fritteli.labyrinth.model; + +import io.vavr.collection.Stream; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class WallsTest { + @Test + void testConstruct() { + // arrange / act + final Walls sut = new Walls(); + + // assert + assertFalse(sut.isSet(Direction.TOP)); + assertFalse(sut.isSet(Direction.RIGHT)); + assertFalse(sut.isSet(Direction.BOTTOM)); + assertFalse(sut.isSet(Direction.LEFT)); + } + + @Test + void testSet() { + // arrange + final Walls sut = new Walls(); + + //act + sut.set(Direction.RIGHT); + + // assert + assertFalse(sut.isSet(Direction.TOP)); + assertTrue(sut.isSet(Direction.RIGHT)); + assertFalse(sut.isSet(Direction.BOTTOM)); + assertFalse(sut.isSet(Direction.LEFT)); + + //act: Setting twice has no effect + sut.set(Direction.RIGHT); + + // assert + assertFalse(sut.isSet(Direction.TOP)); + assertTrue(sut.isSet(Direction.RIGHT)); + assertFalse(sut.isSet(Direction.BOTTOM)); + assertFalse(sut.isSet(Direction.LEFT)); + } + + @Test + void testSetAll() { + // arrange + final Walls sut = new Walls(); + + //act + sut.setAll(); + + // assert + assertTrue(sut.isSet(Direction.TOP)); + assertTrue(sut.isSet(Direction.RIGHT)); + assertTrue(sut.isSet(Direction.BOTTOM)); + assertTrue(sut.isSet(Direction.LEFT)); + } + + @Test + void testHarden() { + // arrange + final Walls sut = new Walls(); + sut.set(Direction.TOP); + sut.set(Direction.RIGHT); + + // act + sut.harden(Direction.TOP); + + // assert + assertTrue(sut.isSet(Direction.TOP)); + assertTrue(sut.isSet(Direction.RIGHT)); + assertFalse(sut.isSet(Direction.BOTTOM)); + assertFalse(sut.isSet(Direction.LEFT)); + + // act: try to clear hardened wall + final boolean result = sut.clear(Direction.TOP); + + // assert: TOP wall is still set + assertFalse(result); + assertTrue(sut.isSet(Direction.TOP)); + + // act / assert: try to harden un-set wall + assertThrows(IllegalStateException.class, () -> sut.harden(Direction.LEFT)); + } + + @Test + void testUnharden() { + // arrange + final Walls sut = new Walls(); + sut.setAll(); + sut.harden(Direction.TOP); + + // pre-assert: TOP can't be cleared while hardened + assertFalse(sut.clear(Direction.TOP)); + + // act + sut.unharden(Direction.TOP); + + // assert: TOP can be cleared + assertTrue(sut.clear(Direction.TOP)); + } + + @Test + void testGetUnhardenedSet() { + // arrange + final Walls sut = new Walls(); + + // act + Stream result = sut.getUnhardenedSet(); + + // assert + assertTrue(result.isEmpty()); + + // arrange: set some directions + sut.set(Direction.TOP); + sut.set(Direction.LEFT); + + // act + result = sut.getUnhardenedSet(); + + // assert + assertEquals(Stream.of(Direction.TOP, Direction.LEFT), result); + + // arrange: harden a direction + sut.harden(Direction.TOP); + + // act + result = sut.getUnhardenedSet(); + + // assert + assertEquals(Stream.of(Direction.LEFT), result); + } +}