Add tests.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Manuel Friedli 2020-10-05 20:58:05 +02:00
parent 79dccbcc0f
commit 15f93310e4
4 changed files with 181 additions and 2 deletions

View file

@ -13,8 +13,8 @@ public class Walls {
private final SortedSet<Direction> directions = new TreeSet<>(); private final SortedSet<Direction> directions = new TreeSet<>();
private final Set<Direction> hardened = new HashSet<>(); private final Set<Direction> hardened = new HashSet<>();
public boolean set(@NonNull final Direction direction) { public void set(@NonNull final Direction direction) {
return this.directions.add(direction); this.directions.add(direction);
} }
public void setAll() { public void setAll() {

View file

@ -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());
}
}

View file

@ -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);
}
}

View file

@ -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<Direction> 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);
}
}