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.

196 lines
7.7 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 in column 0 for player 1", 'x', true, new char[][]
  114. {{'x', '-', '-'},
  115. {'x', '-', '-'},
  116. {'x', '-', '-'}}),
  117. Arguments.of("check win in column 1 for player 1", 'x', true, new char[][]
  118. {{'-', 'x', '-'},
  119. {'-', 'x', '-'},
  120. {'-', 'x', '-'}}),
  121. Arguments.of("check win in column 2 for player 1", 'x', true, new char[][]
  122. {{'-', '-', 'x'},
  123. {'-', '-', 'x'},
  124. {'-', '-', 'x'}}),
  125. Arguments.of("check win in column 0 for player 2", 'o', true, new char[][]
  126. {{'o', '-', '-'},
  127. {'o', '-', '-'},
  128. {'o', '-', '-'}}),
  129. Arguments.of("check win in column 0 for player 2", 'o', false, new char[][]
  130. {{'o', '-', '-'},
  131. {'o', '-', '-'},
  132. {'-', '-', '-'}}),
  133. Arguments.of("check win in row 0 for player 1", 'x', true, new char[][]
  134. {{'x', 'x', 'x'},
  135. {'-', '-', '-'},
  136. {'-', '-', '-'}}),
  137. Arguments.of("check win in row 0 for player 2", 'o', true, new char[][]
  138. {{'o', 'o', 'o'},
  139. {'-', '-', '-'},
  140. {'-', '-', '-'}}),
  141. Arguments.of("check win in row 1 for player 2", 'o', true, new char[][]
  142. {{'-', '-', '-'},
  143. {'o', 'o', 'o'},
  144. {'-', '-', '-'}}),
  145. Arguments.of("check win in row 2 for player 2", 'o', true, new char[][]
  146. {{'-', '-', '-'},
  147. {'-', '-', '-'},
  148. {'o', 'o', 'o'}}),
  149. Arguments.of("check win in column 0 for player 1 with full board", 'x', true, new char[][]
  150. {{'x', 'o', 'o'},
  151. {'x', 'o', 'x'},
  152. {'x', 'x', 'o'}}),
  153. Arguments.of("check win in column 1 for player 2 with full board", 'o', true, new char[][]
  154. {{'x', 'o', 'o'},
  155. {'x', 'o', 'x'},
  156. {'o', 'o', 'x'}}),
  157. Arguments.of("check win in column 2 for player 2 with full board", 'o', true, new char[][]
  158. {{'x', 'o', 'o'},
  159. {'x', 'x', 'o'},
  160. {'o', 'o', 'o'}}),
  161. Arguments.of("check win in row 0 for player 1 with full board", 'x', true, new char[][]
  162. {{'x', 'x', 'x'},
  163. {'x', 'o', 'o'},
  164. {'o', 'o', 'x'}}),
  165. Arguments.of("check win in row 1 for player 1 with full board", 'x', true, new char[][]
  166. {{'x', 'x', 'o'},
  167. {'x', 'x', 'x'},
  168. {'o', 'o', 'x'}})
  169. );
  170. }
  171. }