TileTestHelper.java
Download filepackage de.joshuagleitze.tilinggame.test;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import de.joshuagleitze.tilinggame.LineType;
import de.joshuagleitze.tilinggame.Tile;
/**
* Helper class to test {@link Tile}.
*
* @author Joshua Gleitze
*/
public class TileTestHelper {
/**
* String representation of an empty tile.
*/
static final String EMPTY_TILE_DEFINITION = "------";
static final String[] TEST_TILES_DEFINITIONS = {EMPTY_TILE_DEFINITION, "YY----", "R-R---", "G--G--",
"Y----Y", "RYGRYG"};
/**
* Helper class, not meant to be instantiated.
*/
private TileTestHelper() {
}
/**
* Creates a tile out of the provided definition string.
*
* @param definitionString
* A string representation of a tile.
* @return The tile {@code t} such that (for correct implementations)
* {@code t.toString() == definitionString}.
*/
public static Tile tile(final String definitionString) {
return new Tile(definitionString.chars()
.mapToObj(c -> TileTestHelper.getByAbbreviation((char) c))
.toArray(LineType[]::new));
}
private static LineType getByAbbreviation(final char abbreviation) {
for (final LineType type : LineType.values()) {
if (type.getAbbreviation() == abbreviation) {
return type;
}
}
throw new IllegalArgumentException("No abbreviation: " + abbreviation);
}
/**
* Creates a matcher asserting that a tile is
* {@linkplain Tile#isExactlyEqualTo(Tile) exactly equal} to the tile
* defined by the provided definition string.
*
* @param reference
* A string representation of a tile.
* @return A matcher asserting that a tile is
* {@linkplain Tile#isExactlyEqualTo(Tile) exactly equal} to the
* tile defined by {@code reference}.
*/
public static Matcher<Tile> isTile(final String reference) {
return isTile(tile(reference));
}
/**
* Creates a matcher asserting that a tile is
* {@linkplain Tile#isExactlyEqualTo(Tile) exactly equal} to the provided
* tile.
*
* @param reference
* A tile to compare with.
* @return A matcher asserting that a tile es
* {@linkplain Tile#isExactlyEqualTo(Tile) exactly equal} to
* {@code reference}.
*/
public static Matcher<Tile> isTile(final Tile reference) {
return new TypeSafeMatcher<Tile>() {
@Override
public void describeTo(final Description description) {
description.appendValue(reference);
}
@Override
protected boolean matchesSafely(final Tile item) {
return item != null && reference.isExactlyEqualTo(item);
}
};
}
}