package br.com.ema.maze.components;
import static junit.framework.Assert.*;
import java.util.List;
import org.junit.Test;
import static org.mockito.Mockito.*;
import br.com.ema.maze.agents.MazeCharacter;
import br.com.ema.maze.enums.Direction;
/**
* @author Emanuel Cruz Rodrigues -> emanuelcruzrodrigues@gmail.com
*
*/
public class MazeSpaceTest {
@Test
public void test_AddNearbySpace(){
MazeSpace mazeSpace = new MazeSpace(0,0);
for (Direction direction : Direction.values()) {
assertNull(mazeSpace.getNearbySpace(direction));
MazeSpace anotherSpace = new MazeSpace(0,0);
mazeSpace.addNearbySpace(anotherSpace, direction);
assertEquals(anotherSpace, mazeSpace.getNearbySpace(direction));
}
}
@Test
public void test_Get_Nearby_Space_When_Not_Allows_Passage(){
MazeSpace space = new MazeSpace(0,0);
space.setDecoration(new MazeDecoration() {
@Override
public void setMazeComponent(MazeComponent mazeComponent) {
}
@Override
public boolean allowsPassage() {
return false;
}
});
space.addNearbySpace(space, Direction.NORTH);
assertNull(space.getNearbySpace(Direction.NORTH));
}
@Test
public void test_Get_X_Coordinate(){
MazeSpace mazeSpace = new MazeSpace(1,2);
assertEquals(1, mazeSpace.getX());
}
@Test
public void test_Get_Y_Coordinate(){
MazeSpace mazeSpace = new MazeSpace(1,2);
assertEquals(2, mazeSpace.getY());
}
@Test
public void test_Allows_Passage_With_Space_Without_Decoration(){
MazeSpace space = new MazeSpace(0, 0);
space.setDecoration(null);
assertTrue(space.allowsPassage());
}
@Test
public void test_Allows_Passage_With_Space_With_Decoration_That_Allows_Passage(){
MazeDecoration decorationThatAllowsPassage = mock(MazeDecoration.class);
when(decorationThatAllowsPassage.allowsPassage()).thenReturn(true);
MazeSpace space = new MazeSpace(0, 0);
space.setDecoration(decorationThatAllowsPassage);
assertTrue(space.allowsPassage());
}
@Test
public void test_Allows_Passage_With_Space_With_Decoration_That_Not_Allows_Passage(){
MazeDecoration decorationThatNotAllowsPassage = mock(MazeDecoration.class);
when(decorationThatNotAllowsPassage.allowsPassage()).thenReturn(false);
MazeSpace space = new MazeSpace(0, 0);
space.setDecoration(decorationThatNotAllowsPassage);
assertFalse(space.allowsPassage());
}
@Test
public void test_get_Possible_Routes(){
MazeSpace space = new MazeSpace(0,0);
MazeSpace northSpace = new MazeSpace(0,0);
MazeSpace southSpace = new MazeSpace(0,0).setDecoration(new MazeWall());
MazeSpace eastSpace = new MazeSpace(0,0);
space.addNearbySpace(northSpace, Direction.NORTH);
space.addNearbySpace(southSpace, Direction.SOUTH);
space.addNearbySpace(eastSpace, Direction.EAST);
space.addNearbySpace(null, Direction.WEST);
List<MazeSpace> routes = space.getPossibleRoutes();
assertEquals(2, routes.size());
assertTrue( routes.contains(northSpace) );
assertTrue( routes.contains(eastSpace) );
}
@Test
public void test_Get_Space_Distance(){
MazeSpace spaceA = new MazeSpace(-2,3);
MazeSpace spaceB = new MazeSpace(-5,-9);
assertEquals(12.37, spaceA.getSpaceDistance(spaceB), 0.01);
assertEquals(12.37, spaceB.getSpaceDistance(spaceA), 0.01);
}
@Test
public void test_Get_Space_Distance_When_Destination_Is_Null(){
MazeSpace spaceA = new MazeSpace(-2,3);
assertEquals(0D, spaceA.getSpaceDistance(null), 0D);
}
@Test
public void test_Get_Min_Route(){
/* 13
* 02 12 22
* 11
*/
MazeSpace space13 = new MazeSpace(1,3);
MazeSpace space02 = new MazeSpace(0,2);
MazeSpace space12 = new MazeSpace(1,2);
MazeSpace space22 = new MazeSpace(2,2);
MazeSpace space11 = new MazeSpace(1,1);
space12.addNearbySpace(space13, Direction.NORTH);
space12.addNearbySpace(space02, Direction.WEST);
space12.addNearbySpace(space22, Direction.EAST);
space12.addNearbySpace(space11, Direction.SOUTH);
assertEquals(space11, space12.getMinRoute(new MazeSpace(1,0)));
assertEquals(space13, space12.getMinRoute(new MazeSpace(1,4)));
assertEquals(space02, space12.getMinRoute(new MazeSpace(-1,2)));
assertEquals(space22, space12.getMinRoute(new MazeSpace(3,2)));
}
@Test
public void test_Get_Times_Passed_When_Never_Pass(){
MazeSpace space = new MazeSpace(0,0);
MazeCharacter character = new MazeCharacter(250);
assertEquals(0, space.getTimesPassed(character));
}
@Test
public void test_Get_Times_Passed(){
MazeSpace space = new MazeSpace(0,0);
MazeCharacter character = new MazeCharacter(250);
space.putCharacter(character);
space.putCharacter(character);
space.putCharacter(character);
assertEquals(3, space.getTimesPassed(character));
}
@Test
public void test_Get_Times_Passed_In_Interval(){
MazeSpace space = new MazeSpace(0,0);
MazeCharacter character = new MazeCharacter(250);
space.putCharacter(character);
space.putCharacter(character);
space.putCharacter(character);
try {
Thread.sleep(1100);
} catch (InterruptedException e) {
e.printStackTrace();
}
assertEquals(3, space.getTimesPassed(character));
assertEquals(0, space.getTimesPassed(character, 1000));
}
@Test
public void test_is_Exit(){
MazeSpace space = new MazeSpace(0,0);
space.setDecoration(new MazeExit());
assertTrue(space.isExit());
}
@Test
public void test_is_Not_Exit(){
MazeSpace space = new MazeSpace(0,0);
space.setDecoration(new MazeWall());
assertFalse(space.isExit());
}
@Test
public void test_is_Exit_When_Decoration_Is_Null(){
MazeSpace space = new MazeSpace(0,0);
space.setDecoration(null);
assertFalse(space.isExit());
}
@Test
public void test_Get_Min_Original_Route(){
/* 13<23>
* 12 22
*
*/
MazeSpace space13 = new MazeSpace(1,3);
MazeSpace space12 = new MazeSpace(1,2);
MazeSpace space22 = new MazeSpace(2,2);
MazeSpace space23 = new MazeSpace(2,3);
space12.addNearbySpace(space13, Direction.NORTH);
space13.addNearbySpace(space12, Direction.SOUTH);
space12.addNearbySpace(space22, Direction.EAST);
space22.addNearbySpace(space12, Direction.WEST);
space22.addNearbySpace(space23, Direction.NORTH);
space23.addNearbySpace(space22, Direction.SOUTH);
space13.addNearbySpace(space23, Direction.EAST);
space23.addNearbySpace(space13, Direction.WEST);
MazeCharacter character = new MazeCharacter(250);
space13.putCharacter(character);
assertEquals(space22, space12.getMinOriginalRoute(character, space23));
}
}