Browse Source

Refactoring: MatchfieldShipTest class

fleetstorm
Lorenz Hohmann 3 years ago
parent
commit
0419b89aaa
  1. 1
      src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java
  2. 71
      src/test/java/de/tims/fleetstorm/matchfield/MatchfieldShipTest.java

1
src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java

@ -118,7 +118,6 @@ public class Matchfield {
return false;
if (direction == 0) {
next = new Coordinate(next.getX() + i, next.getY());
} else if (direction == 1) {
next = new Coordinate(next.getX(), next.getY() + i);

71
src/test/java/de/tims/fleetstorm/matchfield/MatchfieldShipTest.java

@ -6,6 +6,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import java.util.stream.Stream;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
@ -13,19 +14,27 @@ import org.junit.jupiter.params.provider.MethodSource;
class MatchfieldShipTest {
private Matchfield matchfield;
private int matchfieldSize = 10;
@BeforeEach
void setup() {
matchfield = new Matchfield(matchfieldSize);
matchfield.createMatchfield();
}
@ParameterizedTest(name = "ship was set on the correct positions")
@MethodSource("testShipPositioning")
void testMatchfieldShipSetHasCorrectPositions(String testName, int matchfieldSize, Coordinate coordinate,
int direction, int length, Coordinate[] coordinatesWithShip) {
Matchfield matchfield = new Matchfield(matchfieldSize);
matchfield.createMatchfield();
void testMatchfieldShipSetHasCorrectPositions(String testName, Coordinate coordinate, int direction, int length,
Coordinate[] coordinatesWithShip) {
matchfield.setShip(coordinate, length, direction);
int shipSetCounter = 0;
for (Coordinate toCheckCoordinate : coordinatesWithShip) {
if (matchfield.getField(toCheckCoordinate).getState() == Coordinate.SHIP)
if (matchfield.getField(toCheckCoordinate).getState() == Coordinate.SHIP) {
shipSetCounter++;
}
}
assertThat(shipSetCounter).describedAs(testName).isEqualTo(length);
@ -39,20 +48,18 @@ class MatchfieldShipTest {
new Coordinate(0, 4), new Coordinate(0, 5) };
return Stream.of(
Arguments.of("set ship from 0:0, length 2, direction 0", 10, new Coordinate(0, 0), 0, 2,
Arguments.of("set ship from 0:0, length 2, direction 0", new Coordinate(0, 0), 0, 2,
coordinatesWithShip00_2_0),
Arguments.of("set ship from 0:1, length 3, direction 0", 10, new Coordinate(0, 1), 0, 3,
Arguments.of("set ship from 0:1, length 3, direction 0", new Coordinate(0, 1), 0, 3,
coordinatesWithShip01_3_0),
Arguments.of("set ship from 0:2, length 4, direction 1", 10, new Coordinate(0, 2), 1, 4,
Arguments.of("set ship from 0:2, length 4, direction 1", new Coordinate(0, 2), 1, 4,
coordinatesWithShip02_4_1));
}
@ParameterizedTest(name = "ship positioning on invalid or valid coordinates")
@MethodSource("testShipPositioningFailed")
void testMatchfieldShipSetWithInvalidOrValidCoordinates(String testName, int matchfieldSize, Coordinate coordinate,
int direction, int length, boolean expectedResult) {
Matchfield matchfield = new Matchfield(matchfieldSize);
matchfield.createMatchfield();
void testMatchfieldShipSetWithInvalidOrValidCoordinates(String testName, Coordinate coordinate, int direction,
int length, boolean expectedResult) {
boolean calculatedResult = matchfield.setShip(coordinate, length, direction);
@ -65,16 +72,13 @@ class MatchfieldShipTest {
static Stream<Arguments> testShipPositioningFailed() {
return Stream.of(
Arguments.of("invalid ship position from 9:0, length 2, direction 0", 10, new Coordinate(9, 0), 0, 2,
Arguments.of("invalid ship position from 9:0, length 2, direction 0", new Coordinate(9, 0), 0, 2,
false),
Arguments.of("valid ship position from 8:0, length 5, direction 1", 10, new Coordinate(9, 0), 1, 5,
true));
Arguments.of("valid ship position from 8:0, length 5, direction 1", new Coordinate(9, 0), 1, 5, true));
}
@Test
void testShipPositionAlreadySetWithShip() {
Matchfield matchfield = new Matchfield(10);
matchfield.createMatchfield();
matchfield.setShip(new Coordinate(0, 0), 5, 1);
assertThat(matchfield.setShip(new Coordinate(0, 1), 5, 1)).describedAs("set ship on coordinate with ship")
@ -88,9 +92,6 @@ class MatchfieldShipTest {
@Test
void testIfAllShipsHitReturnsFalseWhenNoShipIsHit() {
Matchfield matchfield = new Matchfield(10);
matchfield.createMatchfield();
matchfield.setShip(new Coordinate(0, 0), 5, 1);
boolean calulatedResult = matchfield.areAllShipsHit();
assertFalse(calulatedResult);
@ -98,9 +99,6 @@ class MatchfieldShipTest {
@Test
void testIfAllShipsHitReturnsTrueWhenAllShipsAreHit() {
Matchfield matchfield = new Matchfield(10);
matchfield.createMatchfield();
matchfield.setShip(new Coordinate(0, 0), 5, 1);
matchfield.getField(0, 0).setState(Coordinate.HIT);
matchfield.getField(0, 1).setState(Coordinate.HIT);
@ -114,9 +112,6 @@ class MatchfieldShipTest {
@Test
void testIfAllShipsHitReturnsFalseWhenTwoShipsAreNotFullyHit() {
Matchfield matchfield = new Matchfield(10);
matchfield.createMatchfield();
matchfield.setShip(new Coordinate(0, 0), 5, 1);
matchfield.getField(0, 0).setState(Coordinate.HIT);
matchfield.getField(0, 1).setState(Coordinate.HIT);
@ -131,13 +126,25 @@ class MatchfieldShipTest {
assertFalse(calulatedResult);
}
@Test
void testMatchfieldIsFreePositionWithNonFreeField() {
Matchfield matchfield = new Matchfield(10);
@ParameterizedTest(name = "matchfield returns correct state if a position is free or not")
@MethodSource("testIsFreeMethod")
void testMatchfieldReturnsCorrectValueAboutFreeCoorinateSlots(String testName, Coordinate originCoordinate,
int direction, int length, Coordinate[] coordinatesWithShip, boolean expectedResult) {
Matchfield matchfield = new Matchfield(matchfieldSize);
matchfield.createMatchfield();
matchfield.setState(new Coordinate(0, 0), Coordinate.SHIP);
boolean calculatedResult = matchfield.isFreePosition(new Coordinate(0, 0), 2, 1);
assertFalse(calculatedResult);
for (Coordinate coordinate : coordinatesWithShip) {
matchfield.getField(coordinate).setState(Coordinate.SHIP);
}
boolean calculatedResult = matchfield.isFreePosition(originCoordinate, length, direction);
assertThat(calculatedResult).describedAs(testName).isEqualTo(expectedResult);
}
static Stream<Arguments> testIsFreeMethod() {
return Stream.of(Arguments.of("one ship is within the coordinates", new Coordinate(0, 0), 1, 2,
new Coordinate[] { new Coordinate(0, 0) }, false));
}
}
Loading…
Cancel
Save