maze-generator/src/test/java/ch/fritteli/maze/generator/model/TileTest.java

192 lines
5.1 KiB
Java

package ch.fritteli.maze.generator.model;
import ch.fritteli.maze.generator.model.Direction;
import ch.fritteli.maze.generator.model.Tile;
import io.vavr.control.Option;
import org.junit.jupiter.api.Test;
import java.util.Random;
import static org.assertj.core.api.Assertions.assertThat;
class TileTest {
@Test
void testConstruct() {
// arrange / act
final Tile sut = new Tile();
//assert
assertThat(sut)
.returns(true, v -> v.hasWallAt(Direction.TOP))
.returns(true, v -> v.hasWallAt(Direction.RIGHT))
.returns(true, v -> v.hasWallAt(Direction.BOTTOM))
.returns(true, v -> v.hasWallAt(Direction.LEFT))
.returns(false, Tile::isSolution);
}
@Test
void testDigFrom() {
// arrange
final Tile sut = new Tile();
// act
boolean result = sut.digFrom(Direction.TOP);
// assert
assertThat(result).isTrue();
assertThat(sut.hasWallAt(Direction.TOP)).isFalse();
// act: can not dig from when already dug
result = sut.digFrom(Direction.BOTTOM);
// assert
assertThat(result).isFalse();
assertThat(sut.hasWallAt(Direction.BOTTOM)).isTrue();
}
@Test
void testDigTo() {
// arrange
final Tile sut = new Tile();
// act / assert
assertThat(sut)
.returns(true, v -> v.digTo(Direction.TOP))
.returns(true, v -> v.digTo(Direction.RIGHT))
.returns(true, v -> v.digTo(Direction.BOTTOM))
.returns(true, v -> v.digTo(Direction.LEFT))
// digging a second time does not succeed
.returns(false, v -> v.digTo(Direction.LEFT));
// assert
assertThat(sut)
.returns(false, v -> v.hasWallAt(Direction.TOP))
.returns(false, v -> v.hasWallAt(Direction.RIGHT))
.returns(false, v -> v.hasWallAt(Direction.BOTTOM))
.returns(false, v -> v.hasWallAt(Direction.LEFT));
}
@Test
void testPreventDiggingToOrFrom() {
// arrange
final Tile sut = new Tile();
// act
sut.preventDiggingToOrFrom(Direction.LEFT);
// assert
assertThat(sut)
.returns(false, v -> v.digTo(Direction.LEFT))
.returns(false, v -> v.digFrom(Direction.LEFT))
.returns(true, v -> v.hasWallAt(Direction.LEFT));
}
@Test
void testEnableDiggingToOrFrom() {
// arrange
final Tile sut = new Tile();
// act: fist, prevent digging
sut.preventDiggingToOrFrom(Direction.LEFT);
// then, re-enable digging
sut.enableDiggingToOrFrom(Direction.LEFT);
// also, enable it from a previously non-disabled direction
sut.enableDiggingToOrFrom(Direction.BOTTOM);
// assert
assertThat(sut)
.returns(true, v -> v.digTo(Direction.LEFT))
.returns(false, v -> v.hasWallAt(Direction.LEFT));
}
@Test
void testUndigTo() {
// arrange
final Tile sut = new Tile();
sut.digTo(Direction.BOTTOM);
// act
sut.undigTo(Direction.BOTTOM);
// assert
assertThat(sut.hasWallAt(Direction.BOTTOM)).isTrue();
}
@Test
void testSetSolution() {
// arrange
final Tile sut = new Tile();
// act
sut.setSolution();
// assert
assertThat(sut.isSolution()).isTrue();
}
@Test
void testGetRandomAvailableDirection() {
// arrange
final Tile sut = new Tile();
// MyDummyRandom always returns 0
final MyDummyRandom dummyRandom = new MyDummyRandom();
// act
Option<Direction> result = sut.getRandomAvailableDirection(dummyRandom);
// assert
assertThat(result)
.singleElement()
.isEqualTo(Direction.TOP);
// re-arrange
sut.preventDiggingToOrFrom(Direction.TOP);
// act
result = sut.getRandomAvailableDirection(dummyRandom);
// assert
assertThat(result)
.singleElement()
.isEqualTo(Direction.RIGHT);
// re-arrange
sut.preventDiggingToOrFrom(Direction.RIGHT);
// act
result = sut.getRandomAvailableDirection(dummyRandom);
// assert
assertThat(result)
.singleElement()
.isEqualTo(Direction.BOTTOM);
// re-arrange
sut.preventDiggingToOrFrom(Direction.BOTTOM);
// act
result = sut.getRandomAvailableDirection(dummyRandom);
// assert
assertThat(result)
.singleElement()
.isEqualTo(Direction.LEFT);
// re-arrange
sut.preventDiggingToOrFrom(Direction.LEFT);
// act
result = sut.getRandomAvailableDirection(dummyRandom);
// assert
assertThat(result).isEmpty();
}
private static class MyDummyRandom extends Random {
@Override
protected int next(final int bits) {
return 0;
}
}
}