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.

277 lines
11 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.awt.Component;
  5. import java.util.stream.Stream;
  6. import javax.swing.JButton;
  7. import javax.swing.JComponent;
  8. import javax.swing.JPanel;
  9. import org.junit.jupiter.api.BeforeAll;
  10. import org.junit.jupiter.api.Test;
  11. import org.junit.jupiter.api.TestInstance;
  12. import org.junit.jupiter.api.TestInstance.Lifecycle;
  13. import org.junit.jupiter.params.ParameterizedTest;
  14. import org.junit.jupiter.params.provider.Arguments;
  15. import org.junit.jupiter.params.provider.MethodSource;
  16. @TestInstance(Lifecycle.PER_CLASS)
  17. class GameLogicTest {
  18. private final int SIZE = 3;
  19. private GameLogic game;
  20. @BeforeAll
  21. void setUpBeforeClass() throws Exception {
  22. this.game = new GameLogic(SIZE);
  23. }
  24. @Test
  25. void createGameLogicTest() {
  26. GameLogic expectedResult = this.game;
  27. GameLogic realResult = new GameLogic(SIZE);
  28. assertEquals(expectedResult.getClass(), realResult.getClass());
  29. }
  30. @Test
  31. void getBoardTest() {
  32. char[][] expectedResult = new char[][]{{'-', '-', '-'},
  33. {'-', '-', '-'},
  34. {'-', '-', '-'}};
  35. char[][] realResult = this.game.getBoard();
  36. assertArrayEquals(expectedResult, realResult);
  37. }
  38. @Test
  39. void createGameLogicWithGivenBoardTest() {
  40. char[][] expectedResult = new char[][]{{'x', '-', '-'},
  41. {'-', 'o', '-'},
  42. {'x', '-', '-'}};
  43. char[][] givenBoard = expectedResult;
  44. char[][] realResult = new GameLogic(givenBoard).getBoard();
  45. assertArrayEquals(expectedResult, realResult);
  46. }
  47. @Test
  48. void generateGUITest() {
  49. JPanel expectedResult = new JPanel();
  50. JPanel realResult = game.generateGUI();
  51. assertEquals(expectedResult.getClass(), realResult.getClass());
  52. }
  53. @Test
  54. void numberOfGUIFieldsTest() {
  55. int realResult = 0;
  56. int expectedResult = (int) Math.pow(SIZE, 2);
  57. JPanel gui = game.generateGUI();
  58. Component[] components = gui.getComponents();
  59. for (Component component : components) {
  60. if (component instanceof JButton) realResult++;
  61. }
  62. assertEquals(expectedResult, realResult);
  63. }
  64. @ParameterizedTest(name = "[{index}] {0} -> {2} fields")
  65. @MethodSource("testCasesForCountPlayfields")
  66. void fieldCountTest(String testName, int size, int expectedResult) {
  67. GameLogic game = new GameLogic(size);
  68. int realResult = game.countFields();
  69. assertEquals(expectedResult, realResult);
  70. }
  71. @ParameterizedTest(name = "[{index}] {0}")
  72. @MethodSource("testCasesForSetField")
  73. void setFieldTest(String testName, int column, int row, char player, char[][] expectedResult) {
  74. this.game.setField(column, row, player);
  75. char[][] realResult = this.game.getBoard();
  76. assertArrayEquals(expectedResult, realResult);
  77. }
  78. @ParameterizedTest(name = "[{index}] {0}")
  79. @MethodSource("testCasesForCheckEmptyField")
  80. void fieldIsEmptyTest(String testName, int columnToCheck, int rowToCheck, boolean expectedResult, char[][] board) {
  81. GameLogic game = new GameLogic(board);
  82. boolean realResult = game.fieldIsEmpty(columnToCheck, rowToCheck);
  83. assertEquals(expectedResult, realResult);
  84. }
  85. @ParameterizedTest(name = "[{index}] {0}: should be {2}")
  86. @MethodSource("testCasesForCheckForWin")
  87. void checkForWinTest(String testName, char player, boolean expectedResult, char[][] boardToCheck) {
  88. boolean realResult = new GameLogic(boardToCheck).checkForWin(player);
  89. assertEquals(expectedResult, realResult);
  90. }
  91. @ParameterizedTest(name = "[{index}] {0}: should be {1}")
  92. @MethodSource("testCasesForCheckEndOfGame")
  93. void checkEndOfGameTest(String testName, boolean expectedResult, char[][] boardToCheck) {
  94. boolean realResult = new GameLogic(boardToCheck).checkEndOfGame();
  95. assertEquals(expectedResult, realResult);
  96. }
  97. private static Stream<Arguments> testCasesForCountPlayfields() {
  98. return Stream.of(
  99. Arguments.of("1x1 board with too few fields", 1, 9),
  100. Arguments.of("2x2 board with too few fields", 2, 9),
  101. Arguments.of("3x3 board with 9 playfields", 3, 9),
  102. Arguments.of("4x4 board with 16 playfields", 4, 16),
  103. Arguments.of("5x5 board with 25 playfields", 5,25)
  104. );
  105. }
  106. private static Stream<Arguments> testCasesForSetField() {
  107. return Stream.of(
  108. Arguments.of("set field [0][0] for player 1", 0, 0, 'x', new char[][]
  109. {{'x', '-', '-'},
  110. {'-', '-', '-'},
  111. {'-', '-', '-'}}),
  112. Arguments.of("set field [1][0] for player 2", 1, 0, 'o', new char[][]
  113. {{'x', '-', '-'},
  114. {'o', '-', '-'},
  115. {'-', '-', '-'}}),
  116. Arguments.of("try to set occupied field [1][0] for player 1", 1, 0, 'x', new char[][]
  117. {{'x', '-', '-'},
  118. {'o', '-', '-'},
  119. {'-', '-', '-'}})
  120. );
  121. }
  122. private static Stream<Arguments> testCasesForCheckEmptyField() {
  123. return Stream.of(
  124. Arguments.of("check an empty field", 0, 0, true, new char[][]
  125. {{'-', '-', '-'},
  126. {'-', '-', '-'},
  127. {'-', '-', '-'}}),
  128. Arguments.of("check a field set by player 1", 0, 0, false, new char[][]
  129. {{'x', '-', '-'},
  130. {'-', '-', '-'},
  131. {'-', '-', '-'}}),
  132. Arguments.of("check a field set by player 2", 0, 0, false, new char[][]
  133. {{'o', '-', '-'},
  134. {'-', '-', '-'},
  135. {'-', '-', '-'}})
  136. );
  137. }
  138. private static Stream<Arguments> testCasesForCheckForWin() {
  139. return Stream.of(
  140. Arguments.of("check win in column 0 for player 1", 'x', true, new char[][]
  141. {{'x', '-', '-'},
  142. {'x', '-', '-'},
  143. {'x', '-', '-'}}),
  144. Arguments.of("check win in column 1 for player 1", 'x', true, new char[][]
  145. {{'-', 'x', '-'},
  146. {'-', 'x', '-'},
  147. {'-', 'x', '-'}}),
  148. Arguments.of("check win in column 2 for player 1", 'x', true, new char[][]
  149. {{'-', '-', 'x'},
  150. {'-', '-', 'x'},
  151. {'-', '-', 'x'}}),
  152. Arguments.of("check win in column 0 for player 2", 'o', true, new char[][]
  153. {{'o', '-', '-'},
  154. {'o', '-', '-'},
  155. {'o', '-', '-'}}),
  156. Arguments.of("check win in row 0 for player 1", 'x', true, new char[][]
  157. {{'x', 'x', 'x'},
  158. {'-', '-', '-'},
  159. {'-', '-', '-'}}),
  160. Arguments.of("check win in row 0 for player 2", 'o', true, new char[][]
  161. {{'o', 'o', 'o'},
  162. {'-', '-', '-'},
  163. {'-', '-', '-'}}),
  164. Arguments.of("check win in row 1 for player 2", 'o', true, new char[][]
  165. {{'-', '-', '-'},
  166. {'o', 'o', 'o'},
  167. {'-', '-', '-'}}),
  168. Arguments.of("check win in row 2 for player 2", 'o', true, new char[][]
  169. {{'-', '-', '-'},
  170. {'-', '-', '-'},
  171. {'o', 'o', 'o'}}),
  172. Arguments.of("check win in column 0 for player 1 with full board", 'x', true, new char[][]
  173. {{'x', 'o', 'o'},
  174. {'x', 'o', 'x'},
  175. {'x', 'x', 'o'}}),
  176. Arguments.of("check win in column 1 for player 2 with full board", 'o', true, new char[][]
  177. {{'x', 'o', 'o'},
  178. {'x', 'o', 'x'},
  179. {'o', 'o', 'x'}}),
  180. Arguments.of("check win in column 2 for player 2 with full board", 'o', true, new char[][]
  181. {{'x', 'o', 'o'},
  182. {'x', 'x', 'o'},
  183. {'o', 'o', 'o'}}),
  184. Arguments.of("check win in row 0 for player 1 with full board", 'x', true, new char[][]
  185. {{'x', 'x', 'x'},
  186. {'x', 'o', 'o'},
  187. {'o', 'o', 'x'}}),
  188. Arguments.of("check win in row 1 for player 1 with full board", 'x', true, new char[][]
  189. {{'x', 'x', 'o'},
  190. {'x', 'x', 'x'},
  191. {'o', 'o', 'x'}}),
  192. Arguments.of("check win in row 2 for player 2 with full board", 'o', true, new char[][]
  193. {{'x', 'x', 'o'},
  194. {'o', 'x', 'x'},
  195. {'o', 'o', 'o'}}),
  196. Arguments.of("check win in column 0 for player 2", 'o', false, new char[][]
  197. {{'o', '-', '-'},
  198. {'o', '-', '-'},
  199. {'-', '-', '-'}}),
  200. Arguments.of("check a draw for player 2", 'o', false, new char[][]
  201. {{'o', 'o', 'x'},
  202. {'o', 'x', 'x'},
  203. {'x', 'x', 'o'}}),
  204. Arguments.of("check a draw for player 1", 'x', false, new char[][]
  205. {{'o', 'o', 'x'},
  206. {'o', 'o', 'x'},
  207. {'x', 'x', 'o'}}),
  208. Arguments.of("check diagonal left win for player 1", 'x', true, new char[][]
  209. {{'x', 'o', 'x'},
  210. {'x', 'x', 'o'},
  211. {'o', 'o', 'x'}}),
  212. Arguments.of("check diagonal right win for player 2", 'o', true, new char[][]
  213. {{'x', 'x', 'o'},
  214. {'x', 'o', 'o'},
  215. {'o', 'x', 'x'}})
  216. );
  217. }
  218. private static Stream<Arguments> testCasesForCheckEndOfGame() {
  219. return Stream.of(
  220. Arguments.of("check empty board", false, new char[][]
  221. {{'-', '-', '-'},
  222. {'-', '-', '-'},
  223. {'-', '-', '-'}}),
  224. Arguments.of("end of game with win for player 1", true, new char[][]
  225. {{'x', 'o', 'x'},
  226. {'x', 'x', 'o'},
  227. {'x', 'o', 'o'}}),
  228. Arguments.of("end of game with win for player 2", true, new char[][]
  229. {{'x', 'x', 'o'},
  230. {'o', 'o', 'o'},
  231. {'x', 'o', 'x'}}),
  232. Arguments.of("check tied game", true, new char[][]
  233. {{'x', 'x', 'o'},
  234. {'o', 'x', 'o'},
  235. {'x', 'o', 'x'}}),
  236. Arguments.of("check not yet finished game", false, new char[][]
  237. {{'x', 'x', '-'},
  238. {'o', 'o', '-'},
  239. {'x', 'o', 'x'}})
  240. );
  241. }
  242. }