import static org.assertj.core.api.Assertions.*; 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; import org.junit.jupiter.params.provider.MethodSource; import java.io.ByteArrayInputStream; import java.util.*; import java.util.stream.Stream; public class PlayerTest { private Player p; @BeforeEach void setup() { p = new Player("Rot",0, 40, 39); } @Test void testDice() { int calculatedResult = p.rollDice(); assertThat(calculatedResult).describedAs("Dice result").isBetween(1,6); } @Test void checkStartPositionOfFigures() { int expectedResult = -1; Iterator
it = p.figures.iterator(); int calculatedResult = it.next().position; while(it.hasNext()) { calculatedResult = it.next().position; if(expectedResult != calculatedResult) { calculatedResult = 1; break; } } assertThat(calculatedResult).describedAs("Starting postion of Figures").isEqualTo(expectedResult); } @ParameterizedTest @MethodSource("GameWinTestData") void checkGameWin(String testname, ArrayList
figures, Collection positions, boolean expectedResult) { Iterator
it = figures.iterator(); Iterator it2 = positions.iterator(); while(it.hasNext()) { it.next().setPosition(it2.next()); } boolean calculatedResult = p.checkGameWin(figures); assertThat(calculatedResult).describedAs(testname).isEqualTo(expectedResult); } @ParameterizedTest @MethodSource("BaseTestData") void checkFigureInBase(String testname, ArrayList
figures, Collection positions, int expectedResult) { Iterator
it = figures.iterator(); Iterator it2 = positions.iterator(); while(it.hasNext()) { it.next().setPosition(it2.next()); } int calculatedResult = p.checkFigureInBase(figures); assertThat(calculatedResult).describedAs(testname).isEqualTo(expectedResult); } static Stream GameWinTestData() { return Stream.of( Arguments.of("No Figures in House", new ArrayList<>( Arrays.asList( new Figure(), new Figure(), new Figure(), new Figure()) ), Arrays.asList(-1, -1, -1, -1), false), Arguments.of("One Figure in House", new ArrayList<>( Arrays.asList( new Figure(), new Figure(), new Figure(), new Figure()) ), Arrays.asList(40, -1, -1, -1), false), Arguments.of("Two Figures in House", new ArrayList<>( Arrays.asList( new Figure(), new Figure(), new Figure(), new Figure()) ), Arrays.asList(40, 41, -1, -1), false), Arguments.of("Three Figures in House", new ArrayList<>( Arrays.asList( new Figure(), new Figure(), new Figure(), new Figure()) ), Arrays.asList(40, 41, 42, -1), false), Arguments.of("Four Figures in House", new ArrayList<>( Arrays.asList( new Figure(), new Figure(), new Figure(), new Figure()) ), Arrays.asList(40, 41, 42, 43), true) ); } static Stream BaseTestData() { return Stream.of( Arguments.of("Four Figures in Base", new ArrayList<>( Arrays.asList( new Figure(), new Figure(), new Figure(), new Figure()) ), Arrays.asList(-1, -1, -1, -1), 4), Arguments.of("Three Figures in Base", new ArrayList<>( Arrays.asList( new Figure(), new Figure(), new Figure(), new Figure()) ), Arrays.asList(40, -1, -1, -1), 3), Arguments.of("Two Figures in Base", new ArrayList<>( Arrays.asList( new Figure(), new Figure(), new Figure(), new Figure()) ), Arrays.asList(40, 41, -1, -1), 2), Arguments.of("One Figure in Base", new ArrayList<>( Arrays.asList( new Figure(), new Figure(), new Figure(), new Figure()) ), Arrays.asList(40, 41, 42, -1), 1), Arguments.of("No Figures in Base", new ArrayList<>( Arrays.asList( new Figure(), new Figure(), new Figure(), new Figure()) ), Arrays.asList(40, 41, 42, 43), 0) ); } @ParameterizedTest @MethodSource("checkChooseMethodData") void checkChoose(String testName, String input,ArrayList usableFiugres, int expectedResult) { System.setIn(new ByteArrayInputStream(input.getBytes())); int calculatedResult = p.choose(usableFiugres); assertThat(calculatedResult).describedAs(testName).isEqualTo(expectedResult); } static Stream checkChooseMethodData() { return Stream.of( Arguments.of("Figure 1 choosen", "1\n", new ArrayList<>(Arrays.asList(0,1,2,3)), 0), Arguments.of("Figure 2 choosen", "2\n", new ArrayList<>(Arrays.asList(0,1,2,3)), 1), Arguments.of("Figure 3 choosen", "3\n", new ArrayList<>(Arrays.asList(0,1,2,3)),2), Arguments.of("Figure 4 choosen", "4\n", new ArrayList<>(Arrays.asList(0,1,2,3)),3), Arguments.of("Index out of bounds choosen", "16\n",new ArrayList<>(Arrays.asList(0,1,2,3)), -1), Arguments.of("Index out of bounds choosen", "-2\n",new ArrayList<>(Arrays.asList(0,1,2,3)), -1), Arguments.of("Input a letter or char", "g\n",new ArrayList<>(Arrays.asList(0,1,2,3)), -1), Arguments.of("Input a empty string", "\n",new ArrayList<>(Arrays.asList(0,1,2,3)), -1), Arguments.of("Input a special character", "&\n",new ArrayList<>(Arrays.asList(0,1,2,3)), -1), Arguments.of("Only one Figure available", "1\n", new ArrayList<>(List.of(0)), 0), Arguments.of("Figure not available", "4\n", new ArrayList<>(Arrays.asList(1,2)),-1) ); } }