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.

289 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. @Test
  65. void buttonStateTest() {
  66. boolean expectedResult = true;
  67. game.generateGUI();
  68. JButton currentField = game.getGUIField(0);
  69. ((JButton) currentField).doClick();
  70. boolean realResult = currentField.isEnabled();
  71. assertEquals(expectedResult, realResult);
  72. }
  73. @ParameterizedTest(name = "[{index}] {0} -> {2} fields")
  74. @MethodSource("testCasesForCountPlayfields")
  75. void fieldCountTest(String testName, int size, int expectedResult) {
  76. GameLogic game = new GameLogic(size);
  77. int realResult = game.countFields();
  78. assertEquals(expectedResult, realResult);
  79. }
  80. @ParameterizedTest(name = "[{index}] {0}")
  81. @MethodSource("testCasesForSetField")
  82. void setFieldTest(String testName, int column, int row, char player, char[][] expectedResult) {
  83. this.game.setField(column, row, player);
  84. char[][] realResult = this.game.getBoard();
  85. assertArrayEquals(expectedResult, realResult);
  86. }
  87. @ParameterizedTest(name = "[{index}] {0}")
  88. @MethodSource("testCasesForCheckEmptyField")
  89. void fieldIsEmptyTest(String testName, int columnToCheck, int rowToCheck, boolean expectedResult, char[][] board) {
  90. GameLogic game = new GameLogic(board);
  91. boolean realResult = game.fieldIsEmpty(columnToCheck, rowToCheck);
  92. assertEquals(expectedResult, realResult);
  93. }
  94. @ParameterizedTest(name = "[{index}] {0}: should be {2}")
  95. @MethodSource("testCasesForCheckForWin")
  96. void checkForWinTest(String testName, char player, boolean expectedResult, char[][] boardToCheck) {
  97. boolean realResult = new GameLogic(boardToCheck).checkForWin(player);
  98. assertEquals(expectedResult, realResult);
  99. }
  100. @ParameterizedTest(name = "[{index}] {0}: should be {1}")
  101. @MethodSource("testCasesForCheckEndOfGame")
  102. void checkEndOfGameTest(String testName, boolean expectedResult, char[][] boardToCheck) {
  103. boolean realResult = new GameLogic(boardToCheck).checkEndOfGame();
  104. assertEquals(expectedResult, realResult);
  105. }
  106. private static Stream<Arguments> testCasesForCountPlayfields() {
  107. return Stream.of(
  108. Arguments.of("1x1 board with too few fields", 1, 9),
  109. Arguments.of("2x2 board with too few fields", 2, 9),
  110. Arguments.of("3x3 board with 9 playfields", 3, 9),
  111. Arguments.of("4x4 board with 16 playfields", 4, 16),
  112. Arguments.of("5x5 board with 25 playfields", 5,25)
  113. );
  114. }
  115. private static Stream<Arguments> testCasesForSetField() {
  116. return Stream.of(
  117. Arguments.of("set field [0][0] for player 1", 0, 0, 'x', new char[][]
  118. {{'x', '-', '-'},
  119. {'-', '-', '-'},
  120. {'-', '-', '-'}}),
  121. Arguments.of("set field [1][0] for player 2", 1, 0, 'o', new char[][]
  122. {{'x', '-', '-'},
  123. {'o', '-', '-'},
  124. {'-', '-', '-'}}),
  125. Arguments.of("try to set occupied field [1][0] for player 1", 1, 0, 'x', new char[][]
  126. {{'x', '-', '-'},
  127. {'o', '-', '-'},
  128. {'-', '-', '-'}})
  129. );
  130. }
  131. private static Stream<Arguments> testCasesForCheckEmptyField() {
  132. return Stream.of(
  133. Arguments.of("check an empty field", 0, 0, true, new char[][]
  134. {{'-', '-', '-'},
  135. {'-', '-', '-'},
  136. {'-', '-', '-'}}),
  137. Arguments.of("check a field set by player 1", 0, 0, false, new char[][]
  138. {{'x', '-', '-'},
  139. {'-', '-', '-'},
  140. {'-', '-', '-'}}),
  141. Arguments.of("check a field set by player 2", 0, 0, false, new char[][]
  142. {{'o', '-', '-'},
  143. {'-', '-', '-'},
  144. {'-', '-', '-'}})
  145. );
  146. }
  147. private static Stream<Arguments> testCasesForCheckForWin() {
  148. return Stream.of(
  149. Arguments.of("check win in column 0 for player 1", 'x', true, new char[][]
  150. {{'x', '-', '-'},
  151. {'x', '-', '-'},
  152. {'x', '-', '-'}}),
  153. Arguments.of("check win in column 1 for player 1", 'x', true, new char[][]
  154. {{'-', 'x', '-'},
  155. {'-', 'x', '-'},
  156. {'-', 'x', '-'}}),
  157. Arguments.of("check win in column 2 for player 1", 'x', true, new char[][]
  158. {{'-', '-', 'x'},
  159. {'-', '-', 'x'},
  160. {'-', '-', 'x'}}),
  161. Arguments.of("check win in column 0 for player 2", 'o', true, new char[][]
  162. {{'o', '-', '-'},
  163. {'o', '-', '-'},
  164. {'o', '-', '-'}}),
  165. Arguments.of("check win in row 0 for player 1", 'x', true, new char[][]
  166. {{'x', 'x', 'x'},
  167. {'-', '-', '-'},
  168. {'-', '-', '-'}}),
  169. Arguments.of("check win in row 0 for player 2", 'o', true, new char[][]
  170. {{'o', 'o', 'o'},
  171. {'-', '-', '-'},
  172. {'-', '-', '-'}}),
  173. Arguments.of("check win in row 1 for player 2", 'o', true, new char[][]
  174. {{'-', '-', '-'},
  175. {'o', 'o', 'o'},
  176. {'-', '-', '-'}}),
  177. Arguments.of("check win in row 2 for player 2", 'o', true, new char[][]
  178. {{'-', '-', '-'},
  179. {'-', '-', '-'},
  180. {'o', 'o', 'o'}}),
  181. Arguments.of("check win in column 0 for player 1 with full board", 'x', true, new char[][]
  182. {{'x', 'o', 'o'},
  183. {'x', 'o', 'x'},
  184. {'x', 'x', 'o'}}),
  185. Arguments.of("check win in column 1 for player 2 with full board", 'o', true, new char[][]
  186. {{'x', 'o', 'o'},
  187. {'x', 'o', 'x'},
  188. {'o', 'o', 'x'}}),
  189. Arguments.of("check win in column 2 for player 2 with full board", 'o', true, new char[][]
  190. {{'x', 'o', 'o'},
  191. {'x', 'x', 'o'},
  192. {'o', 'o', 'o'}}),
  193. Arguments.of("check win in row 0 for player 1 with full board", 'x', true, new char[][]
  194. {{'x', 'x', 'x'},
  195. {'x', 'o', 'o'},
  196. {'o', 'o', 'x'}}),
  197. Arguments.of("check win in row 1 for player 1 with full board", 'x', true, new char[][]
  198. {{'x', 'x', 'o'},
  199. {'x', 'x', 'x'},
  200. {'o', 'o', 'x'}}),
  201. Arguments.of("check win in row 2 for player 2 with full board", 'o', true, new char[][]
  202. {{'x', 'x', 'o'},
  203. {'o', 'x', 'x'},
  204. {'o', 'o', 'o'}}),
  205. Arguments.of("check win in column 0 for player 2", 'o', false, new char[][]
  206. {{'o', '-', '-'},
  207. {'o', '-', '-'},
  208. {'-', '-', '-'}}),
  209. Arguments.of("check a draw for player 2", 'o', false, new char[][]
  210. {{'o', 'o', 'x'},
  211. {'o', 'x', 'x'},
  212. {'x', 'x', 'o'}}),
  213. Arguments.of("check a draw for player 1", 'x', false, new char[][]
  214. {{'o', 'o', 'x'},
  215. {'o', 'o', 'x'},
  216. {'x', 'x', 'o'}}),
  217. Arguments.of("check diagonal left win for player 1", 'x', true, new char[][]
  218. {{'x', 'o', 'x'},
  219. {'x', 'x', 'o'},
  220. {'o', 'o', 'x'}}),
  221. Arguments.of("check diagonal right win for player 2", 'o', true, new char[][]
  222. {{'x', 'x', 'o'},
  223. {'x', 'o', 'o'},
  224. {'o', 'x', 'x'}})
  225. );
  226. }
  227. private static Stream<Arguments> testCasesForCheckEndOfGame() {
  228. return Stream.of(
  229. Arguments.of("check empty board", false, new char[][]
  230. {{'-', '-', '-'},
  231. {'-', '-', '-'},
  232. {'-', '-', '-'}}),
  233. Arguments.of("end of game with win for player 1", true, new char[][]
  234. {{'x', 'o', 'x'},
  235. {'x', 'x', 'o'},
  236. {'x', 'o', 'o'}}),
  237. Arguments.of("end of game with win for player 2", true, new char[][]
  238. {{'x', 'x', 'o'},
  239. {'o', 'o', 'o'},
  240. {'x', 'o', 'x'}}),
  241. Arguments.of("check tied game", true, new char[][]
  242. {{'x', 'x', 'o'},
  243. {'o', 'x', 'o'},
  244. {'x', 'o', 'x'}}),
  245. Arguments.of("check not yet finished game", false, new char[][]
  246. {{'x', 'x', '-'},
  247. {'o', 'o', '-'},
  248. {'x', 'o', 'x'}})
  249. );
  250. }
  251. }