Browse Source

Refactoring MatchfieldShipTest class

fleetstorm
Lorenz Hohmann 2 years ago
parent
commit
f753e07ac1
  1. 36
      src/test/java/de/tims/fleetstorm/matchfield/MatchfieldShipTest.java

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

@ -3,9 +3,7 @@ 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.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
@ -62,19 +60,15 @@ class MatchfieldShipTest {
int length, boolean expectedResult) {
boolean calculatedResult = matchfield.setShip(coordinate, length, direction);
if (expectedResult)
assertThat(calculatedResult).describedAs(testName).isEqualTo(true);
if (!expectedResult)
assertThat(calculatedResult).describedAs(testName).isEqualTo(false);
assertThat(calculatedResult).describedAs(testName).isEqualTo(expectedResult);
}
static Stream<Arguments> testShipPositioningFailed() {
return Stream.of(
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", new Coordinate(9, 0), 1, 5, true));
Arguments.of("invalid ship position from 9:0, length 2, direction 0 (x-axis)", new Coordinate(9, 0), 0,
2, false),
Arguments.of("valid ship position from 8:0, length 5, direction 1 (y-axis)", new Coordinate(9, 0), 1, 5,
true));
}
@Test
@ -100,11 +94,8 @@ class MatchfieldShipTest {
@Test
void testIfAllShipsHitReturnsTrueWhenAllShipsAreHit() {
matchfield.setShip(new Coordinate(0, 0), 5, 1);
matchfield.getField(0, 0).setState(Coordinate.HIT);
matchfield.getField(0, 1).setState(Coordinate.HIT);
matchfield.getField(0, 2).setState(Coordinate.HIT);
matchfield.getField(0, 3).setState(Coordinate.HIT);
matchfield.getField(0, 4).setState(Coordinate.HIT);
setFieldRangeHit(matchfield);
boolean calulatedResult = matchfield.areAllShipsHit();
assertTrue(calulatedResult);
@ -113,11 +104,8 @@ class MatchfieldShipTest {
@Test
void testIfAllShipsHitReturnsFalseWhenTwoShipsAreNotFullyHit() {
matchfield.setShip(new Coordinate(0, 0), 5, 1);
matchfield.getField(0, 0).setState(Coordinate.HIT);
matchfield.getField(0, 1).setState(Coordinate.HIT);
matchfield.getField(0, 2).setState(Coordinate.HIT);
matchfield.getField(0, 3).setState(Coordinate.HIT);
matchfield.getField(0, 4).setState(Coordinate.HIT);
setFieldRangeHit(matchfield);
matchfield.setShip(new Coordinate(3, 4), 2, 0);
matchfield.getField(4, 4).setState(Coordinate.HIT);
@ -126,6 +114,12 @@ class MatchfieldShipTest {
assertFalse(calulatedResult);
}
void setFieldRangeHit(Matchfield matchfield) {
for (int i = 0; i <= 4; i++) {
matchfield.getField(0, i).setState(Coordinate.HIT);
}
}
@ParameterizedTest(name = "matchfield returns correct state if a position is free or not")
@MethodSource("testIsFreeMethod")
void testMatchfieldReturnsCorrectValueAboutFreeCoordinateSlots(String testName, Coordinate originCoordinate,

Loading…
Cancel
Save