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.
82 lines
2.6 KiB
82 lines
2.6 KiB
package de.tims.fleetstorm.matchfield;
|
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
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 CoordinateTest {
|
|
|
|
int x = 1;
|
|
int y = 3;
|
|
|
|
Coordinate coordinate = new Coordinate(x, y);
|
|
|
|
@Test
|
|
void testGetCorrectValuesForXY() {
|
|
assertEquals(coordinate.getX(), x);
|
|
assertEquals(coordinate.getY(), y);
|
|
}
|
|
|
|
@Test
|
|
void testCoordinatehasDefaultStates() {
|
|
assertEquals(coordinate.getState(), Coordinate.EMPTY);
|
|
assertEquals(coordinate.getState(), 0);
|
|
}
|
|
|
|
@ParameterizedTest(name = "All Getters/Setters are working")
|
|
@MethodSource("AllGettersAreWorking")
|
|
void testGetAndSetForAllStates(String testName, int state, int stateCode, int expectedResult) {
|
|
coordinate.setState(state);
|
|
int result = coordinate.getState();
|
|
assertThat(result).describedAs(testName).isEqualTo(expectedResult);
|
|
}
|
|
|
|
static Stream<Arguments> AllGettersAreWorking() {
|
|
return Stream.of(Arguments.of("State = SHIP", 1, Coordinate.SHIP, Coordinate.SHIP),
|
|
Arguments.of("State = SHOT", 2, Coordinate.SHOT, Coordinate.SHOT),
|
|
Arguments.of("State = HIT", 3, Coordinate.HIT, Coordinate.HIT),
|
|
Arguments.of("State = EMPTY", 0, Coordinate.EMPTY, Coordinate.EMPTY));
|
|
}
|
|
|
|
@ParameterizedTest(name = "test if shoot function sets correct field states")
|
|
@MethodSource("ShootFunctionFieldStates")
|
|
void testShootFunctionSetsCorrectFieldStates(String testName, Coordinate coordinateToTest, int expectedState) {
|
|
Matchfield matchfield = new Matchfield(10);
|
|
matchfield.createMatchfield();
|
|
|
|
// set dummy ship
|
|
matchfield.setShip(new Coordinate(0, 0), 5, 0);
|
|
|
|
Coordinate realCoordinate = matchfield.getField(coordinateToTest);
|
|
realCoordinate.shoot();
|
|
int calculatedResult = realCoordinate.getState();
|
|
assertThat(calculatedResult).describedAs(testName).isEqualTo(expectedState);
|
|
}
|
|
|
|
static Stream<Arguments> ShootFunctionFieldStates() {
|
|
return Stream.of(Arguments.of("Field State is HIT", new Coordinate(0, 0), Coordinate.HIT),
|
|
Arguments.of("Field State is SHOT", new Coordinate(0, 1), Coordinate.SHOT));
|
|
}
|
|
|
|
@Test
|
|
void testIfShootCheckWhenFieldIsAlreadyShot() {
|
|
Matchfield matchfield = new Matchfield(10);
|
|
matchfield.createMatchfield();
|
|
|
|
// set dummy ship
|
|
matchfield.setShip(new Coordinate(3, 3), 4, 1);
|
|
|
|
assertTrue(matchfield.getField(0, 0).shoot());
|
|
assertFalse(matchfield.getField(0, 0).shoot());
|
|
|
|
matchfield.getField(3, 4).shoot();
|
|
assertFalse(matchfield.getField(3, 4).shoot());
|
|
}
|
|
|
|
}
|