Browse Source

Added test case for invalid coordinates on setShip (2x1, direction 0)

fleetstorm
Lorenz Hohmann 2 years ago
parent
commit
246ecfa6bd
  1. 7
      src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java
  2. 18
      src/test/java/de/tims/fleetstorm/matchfield/MatchfieldPositioningTest.java

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

@ -76,7 +76,7 @@ public class Matchfield {
/**
* direction 0 => x axis (to the right) direction 1 => y axis (to the bottom)
*/
public void setShip(Coordinate coordinate, int length, int direction) {
public boolean setShip(Coordinate coordinate, int length, int direction) {
for (int i = 0; i < length; i++) {
this.getField(coordinate).setState(Coordinate.SHIP);
@ -84,7 +84,12 @@ public class Matchfield {
coordinate = this.getRight(coordinate);
if (direction == 1)
coordinate = this.getAbove(coordinate);
if (coordinate == null)
return false;
}
return true;
}
}

18
src/test/java/de/tims/fleetstorm/matchfield/MatchfieldPositioningTest.java

@ -1,6 +1,7 @@
package de.tims.fleetstorm.matchfield;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.util.stream.Stream;
@ -44,4 +45,21 @@ class MatchfieldPositioningTest {
Arguments.of("set ship from 0:2, length 4, direction 1", 10, 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) {
Matchfield matchfield = new Matchfield(matchfieldSize);
matchfield.createMatchfield();
boolean calculatedResult = matchfield.setShip(coordinate, length, direction);
assertFalse(calculatedResult);
}
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));
}
}
Loading…
Cancel
Save