You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
5.2 KiB

package de.tims.fleetstorm.matchfield;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
class MatchfieldCreationTest {
@Test
void testMatchfieldCreateNotEmpty() {
Matchfield matchfield = new Matchfield(0);
Coordinate[][] calcResult = matchfield.createMatchfield();
assertNotNull(calcResult);
}
@ParameterizedTest(name = "matchfield creation has correct size")
@MethodSource("testMatchfieldSize")
void testMatchfieldCreationHasCorrectSize(String testName, int size, int expectedResult) {
Matchfield matchfield = new Matchfield(size);
matchfield.createMatchfield();
int calcResult = matchfield.getSize();
assertThat(calcResult).describedAs(testName).isEqualTo(expectedResult);
}
static Stream<Arguments> testMatchfieldSize() {
return Stream.of(Arguments.of("field size 10x10", 10, 100), Arguments.of("field size 15x15", 15, 225));
}
@ParameterizedTest(name = "matchfield get field is correct")
@MethodSource("testMatchfieldGetFieldState")
void testMatchfieldGetCorrectState(String testName, int x, int y, int expectedResult) {
Matchfield matchfield = new Matchfield(10);
matchfield.createMatchfield();
int calcResult = matchfield.getState(x, y);
assertThat(calcResult).describedAs(testName).isEqualTo(expectedResult);
}
static Stream<Arguments> testMatchfieldGetFieldState() {
return Stream.of(Arguments.of("field x:0 y:0 has empty state", 0, 0, Coordinate.EMPTY));
}
@ParameterizedTest(name = "matchfield change field is correct")
@MethodSource("testMatchfieldSetStateIsCorrect")
void testMatchfieldGetCorrectState(String testName, int x, int y, int state, int expectedResult) {
Matchfield matchfield = new Matchfield(10);
matchfield.createMatchfield();
matchfield.setState(x, y, state);
int calcResult = matchfield.getState(x, y);
assertThat(calcResult).describedAs(testName).isEqualTo(expectedResult);
}
static Stream<Arguments> testMatchfieldSetStateIsCorrect() {
return Stream.of(
Arguments.of("field x:0 y:0 has state SHIP after setState()", 0, 0, Coordinate.SHIP, Coordinate.SHIP));
}
@ParameterizedTest(name = "Get the Coordinate right")
@MethodSource("getCoordinateRight")
void testGetRight(String testName, Matchfield matchfield, Coordinate center, Coordinate expectedResult) {
Coordinate result = matchfield.getRight(center);
assertThat(result).describedAs(testName).isEqualTo(expectedResult);
}
static Stream<Arguments> getCoordinateRight() {
Matchfield matchfield = new Matchfield(10);
return Stream.of(
Arguments.of("right from (5/5) - should be (6,5)", matchfield, new Coordinate(5, 5),
matchfield.getField(6, 5)),
Arguments.of("right from (9/5) - should be null", matchfield, new Coordinate(9, 5), null));
}
@ParameterizedTest(name = "Get the Coordinate left")
@MethodSource("getCoordinateLeft")
void testGetLeft(String testName, Matchfield matchfield, Coordinate center, Coordinate expectedResult) {
Coordinate result = matchfield.getLeft(center);
assertThat(result).describedAs(testName).isEqualTo(expectedResult);
}
static Stream<Arguments> getCoordinateLeft() {
Matchfield matchfield = new Matchfield(10);
return Stream.of(
Arguments.of("left from (5/5) - should be (4,5)", matchfield, new Coordinate(5, 5),
matchfield.getField(4, 5)),
Arguments.of("left from (0/5) - should be null", matchfield, new Coordinate(0, 5), null));
}
@ParameterizedTest(name = "Get the Coordinate above")
@MethodSource("getCoordinateAbove")
void testGetAbove(String testName, Matchfield matchfield, Coordinate center, Coordinate expectedResult) {
Coordinate result = matchfield.getAbove(center);
assertThat(result).describedAs(testName).isEqualTo(expectedResult);
}
static Stream<Arguments> getCoordinateAbove() {
Matchfield matchfield = new Matchfield(10);
return Stream.of(
Arguments.of("above from (5/5) - should be (5,6)", matchfield, new Coordinate(5, 5),
matchfield.getField(5, 6)),
Arguments.of("above from (5/9) - should be null", matchfield, new Coordinate(5, 5), null));
}
@ParameterizedTest(name = "Get the Coordinate below")
@MethodSource("getCoordinateBelow")
void testGetBelow(String testName, Matchfield matchfield, Coordinate center, Coordinate expectedResult) {
Coordinate result = matchfield.getBelow(center);
assertThat(result).describedAs(testName).isEqualTo(expectedResult);
}
static Stream<Arguments> getCoordinateBelow() {
Matchfield matchfield = new Matchfield(10);
return Stream.of(
Arguments.of("below from (5/5) - should be (5,4)", matchfield, new Coordinate(5, 5),
matchfield.getField(5, 4)),
Arguments.of("below from (5/0) - should be null", matchfield, new Coordinate(5, 0), null));
}
@Test
void testsetStateOverloaded() {
Matchfield matchfield = new Matchfield(10);
matchfield.createMatchfield();
int x = 3;
int y = 3;
matchfield.setState(new Coordinate(x, y), Coordinate.SHIP);
int result = matchfield.getState(x, y);
assertEquals(result, Coordinate.SHIP);
}
}