Legger til en WallType enum og tester

This commit is contained in:
Kristian Knarvik 2020-02-17 22:18:23 +01:00
parent b31c52edd1
commit 162713128c
2 changed files with 104 additions and 0 deletions

View File

@ -0,0 +1,43 @@
package inf112.fiasko.roborally.abstractions;
/**
* This enum represents all possible wall types
*/
public enum WallType {
WALL_NORMAL (1),
WALL_CORNER (2),
WALL_LASER_SINGLE (3),
WALL_LASER_DOUBLE (4);
private final int wallTypeID;
/**
* Constructor to let a wall type be represented by a numerical identifier
* @param wallTypeID <p>The numerical identifier assigned to the wall type</p>
*/
WallType(int wallTypeID) {
this.wallTypeID = wallTypeID;
}
/**
* Gets the numerical id used for alternate identification of a wall type
* @return <p>The numerical id of the wall type</p>
*/
public int getWallTypeID() {
return this.wallTypeID;
}
/**
* Gets a wall type value from its numerical representation
* @param wallTypeID <p>The numerical representation of a wall type</p>
* @return <p>The enum value representing the wall type, or null if the id is invalid</p>
*/
public static WallType getWallTypeFromID(int wallTypeID) {
for (WallType type : WallType.values()) {
if (type.wallTypeID == wallTypeID) {
return type;
}
}
return null;
}
}

View File

@ -0,0 +1,61 @@
package inf112.fiasko.roborally;
import inf112.fiasko.roborally.abstractions.TileType;
import inf112.fiasko.roborally.abstractions.WallType;
import org.junit.Test;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
public class WallTypeTest {
@Test
public void getWallTypeIDForWallNormal() {
assertEquals(1, WallType.WALL_NORMAL.getWallTypeID());
}
@Test
public void getWallTypeFromWallNormalID() {
assertEquals(WallType.WALL_NORMAL, WallType.getWallTypeFromID(1));
}
@Test
public void getWallTypeIDForWallLaserSingle() {
assertEquals(3, WallType.WALL_LASER_SINGLE.getWallTypeID());
}
@Test
public void getWallTypeFromWallLaserSingleID() {
assertEquals(WallType.WALL_LASER_SINGLE, WallType.getWallTypeFromID(3));
}
@Test
public void allWallTypesIDConversionToIDAndBack() {
for (WallType type : WallType.values()) {
assertEquals(type, WallType.getWallTypeFromID(type.getWallTypeID()));
}
}
@Test
public void invalidWallTypeIDReturnsNull() {
assertNull(TileType.getTileTypeFromID(-1));
}
@Test
public void allWallsHaveUniqueId() {
/* This test is also done implicitly by the allTileTypesIDConversionToIDAndBack test, but that test may fail
even if this test passes, so this test is needed for clarity. */
Set<Integer> set = new HashSet<>();
List<Integer> list = new ArrayList<>();
for (WallType type : WallType.values()) {
set.add(type.getWallTypeID());
list.add(type.getWallTypeID());
}
assertEquals(list.size(), set.size());
}
}