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.

152 lines
5.3 KiB

  1. package de.tims.tictactoe;
  2. import static org.junit.jupiter.api.Assertions.assertArrayEquals;
  3. import static org.junit.jupiter.api.Assertions.assertEquals;
  4. import java.util.stream.Stream;
  5. import org.junit.jupiter.api.BeforeAll;
  6. import org.junit.jupiter.api.Test;
  7. import org.junit.jupiter.api.TestInstance;
  8. import org.junit.jupiter.api.TestInstance.Lifecycle;
  9. import org.junit.jupiter.params.ParameterizedTest;
  10. import org.junit.jupiter.params.provider.Arguments;
  11. import org.junit.jupiter.params.provider.MethodSource;
  12. @TestInstance(Lifecycle.PER_CLASS)
  13. class GameLogicTest {
  14. private final int SIZE = 3;
  15. private GameLogic game;
  16. @BeforeAll
  17. void setUpBeforeClass() throws Exception {
  18. this.game = new GameLogic(SIZE);
  19. }
  20. @Test
  21. void createGameLogicTest() {
  22. GameLogic expectedResult = this.game;
  23. GameLogic realResult = new GameLogic(SIZE);
  24. assertEquals(expectedResult.getClass(), realResult.getClass());
  25. }
  26. @Test
  27. void getBoardTest() {
  28. char[][] expectedResult = new char[][]{{'-', '-', '-'},
  29. {'-', '-', '-'},
  30. {'-', '-', '-'}};
  31. char[][] realResult = this.game.getBoard();
  32. assertArrayEquals(expectedResult, realResult);
  33. }
  34. @Test
  35. void createGameLogicWithGivenBoardTest() {
  36. char[][] expectedResult = new char[][]{{'x', '-', '-'},
  37. {'-', 'o', '-'},
  38. {'x', '-', '-'}};
  39. char[][] givenBoard = expectedResult;
  40. char[][] realResult = new GameLogic(givenBoard).getBoard();
  41. assertArrayEquals(expectedResult, realResult);
  42. }
  43. @ParameterizedTest(name = "[{index}] {0} -> {2} fields")
  44. @MethodSource("testCasesForCountPlayfields")
  45. void fieldCountTest(String testName, int size, int expectedResult) {
  46. GameLogic game = new GameLogic(size);
  47. int realResult = game.countFields();
  48. assertEquals(expectedResult, realResult);
  49. }
  50. @ParameterizedTest(name = "[{index}] {0}")
  51. @MethodSource("testCasesForSetField")
  52. void setFieldTest(String testName, int column, int row, char player, char[][] expectedResult) {
  53. this.game.setField(column, row, player);
  54. char[][] realResult = this.game.getBoard();
  55. assertArrayEquals(expectedResult, realResult);
  56. }
  57. @ParameterizedTest(name = "[{index}] {0}")
  58. @MethodSource("testCasesForCheckEmptyField")
  59. void fieldIsEmptyTest(String testName, int columnToCheck, int rowToCheck, boolean expectedResult, char[][] board) {
  60. GameLogic game = new GameLogic(board);
  61. boolean realResult = game.fieldIsEmpty(columnToCheck, rowToCheck);
  62. assertEquals(expectedResult, realResult);
  63. }
  64. @ParameterizedTest(name = "[{index}] {0}: should be {2}")
  65. @MethodSource("testCasesForCheckForWin")
  66. void checkForWinTest(String testName, char player, boolean expectedResult, char[][] boardToCheck) {
  67. boolean realResult = new GameLogic(boardToCheck).checkForWin(player);
  68. assertEquals(expectedResult, realResult);
  69. }
  70. private static Stream<Arguments> testCasesForCountPlayfields() {
  71. return Stream.of(
  72. Arguments.of("1x1 board with too few fields", 1, 9),
  73. Arguments.of("2x2 board with too few fields", 2, 9),
  74. Arguments.of("3x3 board with 9 playfields", 3, 9),
  75. Arguments.of("4x4 board with 16 playfields", 4, 16),
  76. Arguments.of("5x5 board with 25 playfields", 5,25)
  77. );
  78. }
  79. private static Stream<Arguments> testCasesForSetField() {
  80. return Stream.of(
  81. Arguments.of("set field [0][0] for player 1", 0, 0, 'x', new char[][]
  82. {{'x', '-', '-'},
  83. {'-', '-', '-'},
  84. {'-', '-', '-'}}),
  85. Arguments.of("set field [1][0] for player 2", 1, 0, 'o', new char[][]
  86. {{'x', '-', '-'},
  87. {'o', '-', '-'},
  88. {'-', '-', '-'}}),
  89. Arguments.of("try to set occupied field [1][0] for player 1", 1, 0, 'x', new char[][]
  90. {{'x', '-', '-'},
  91. {'o', '-', '-'},
  92. {'-', '-', '-'}})
  93. );
  94. }
  95. private static Stream<Arguments> testCasesForCheckEmptyField() {
  96. return Stream.of(
  97. Arguments.of("check an empty field", 0, 0, true, new char[][]
  98. {{'-', '-', '-'},
  99. {'-', '-', '-'},
  100. {'-', '-', '-'}}),
  101. Arguments.of("check a field set by player 1", 0, 0, false, new char[][]
  102. {{'x', '-', '-'},
  103. {'-', '-', '-'},
  104. {'-', '-', '-'}}),
  105. Arguments.of("check a field set by player 2", 0, 0, false, new char[][]
  106. {{'o', '-', '-'},
  107. {'-', '-', '-'},
  108. {'-', '-', '-'}})
  109. );
  110. }
  111. private static Stream<Arguments> testCasesForCheckForWin() {
  112. return Stream.of(
  113. Arguments.of("check win for player 1", 'x', true, new char[][]
  114. {{'x', '-', '-'},
  115. {'x', '-', '-'},
  116. {'x', '-', '-'}}),
  117. Arguments.of("check win for player 2", 'o', true, new char[][]
  118. {{'o', '-', '-'},
  119. {'o', '-', '-'},
  120. {'o', '-', '-'}}),
  121. Arguments.of("check win for player 2", 'o', false, new char[][]
  122. {{'o', '-', '-'},
  123. {'o', '-', '-'},
  124. {'-', '-', '-'}})
  125. );
  126. }
  127. }