|
@ -7,7 +7,6 @@ import java.awt.Component; |
|
|
import java.util.stream.Stream; |
|
|
import java.util.stream.Stream; |
|
|
|
|
|
|
|
|
import javax.swing.JButton; |
|
|
import javax.swing.JButton; |
|
|
import javax.swing.JComponent; |
|
|
|
|
|
import javax.swing.JPanel; |
|
|
import javax.swing.JPanel; |
|
|
|
|
|
|
|
|
import org.junit.jupiter.api.BeforeAll; |
|
|
import org.junit.jupiter.api.BeforeAll; |
|
@ -23,7 +22,7 @@ class GameLogicTest { |
|
|
|
|
|
|
|
|
private final int SIZE = 3; |
|
|
private final int SIZE = 3; |
|
|
private GameLogic game; |
|
|
private GameLogic game; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@BeforeAll |
|
|
@BeforeAll |
|
|
void setUpBeforeClass() throws Exception { |
|
|
void setUpBeforeClass() throws Exception { |
|
|
this.game = new GameLogic(SIZE); |
|
|
this.game = new GameLogic(SIZE); |
|
@ -33,153 +32,160 @@ class GameLogicTest { |
|
|
void createGameLogicTest() { |
|
|
void createGameLogicTest() { |
|
|
GameLogic expectedResult = this.game; |
|
|
GameLogic expectedResult = this.game; |
|
|
GameLogic realResult = new GameLogic(SIZE); |
|
|
GameLogic realResult = new GameLogic(SIZE); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertEquals(expectedResult.getClass(), realResult.getClass()); |
|
|
assertEquals(expectedResult.getClass(), realResult.getClass()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
@Test |
|
|
void getBoardTest() { |
|
|
void getBoardTest() { |
|
|
|
|
|
// @formatter:off |
|
|
char[][] expectedResult = new char[][]{{'-', '-', '-'}, |
|
|
char[][] expectedResult = new char[][]{{'-', '-', '-'}, |
|
|
{'-', '-', '-'}, |
|
|
{'-', '-', '-'}, |
|
|
{'-', '-', '-'}}; |
|
|
{'-', '-', '-'}}; |
|
|
|
|
|
// @formatter:on |
|
|
char[][] realResult = this.game.getBoard(); |
|
|
char[][] realResult = this.game.getBoard(); |
|
|
|
|
|
|
|
|
assertArrayEquals(expectedResult, realResult); |
|
|
assertArrayEquals(expectedResult, realResult); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
@Test |
|
|
void createGameLogicWithGivenBoardTest() { |
|
|
void createGameLogicWithGivenBoardTest() { |
|
|
|
|
|
// @formatter:off |
|
|
char[][] expectedResult = new char[][]{{'x', '-', '-'}, |
|
|
char[][] expectedResult = new char[][]{{'x', '-', '-'}, |
|
|
{'-', 'o', '-'}, |
|
|
{'-', 'o', '-'}, |
|
|
{'x', '-', '-'}}; |
|
|
{'x', '-', '-'}}; |
|
|
|
|
|
// @formatter:on |
|
|
char[][] givenBoard = expectedResult; |
|
|
char[][] givenBoard = expectedResult; |
|
|
char[][] realResult = new GameLogic(givenBoard).getBoard(); |
|
|
char[][] realResult = new GameLogic(givenBoard).getBoard(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertArrayEquals(expectedResult, realResult); |
|
|
assertArrayEquals(expectedResult, realResult); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
@Test |
|
|
void generateGUITest() { |
|
|
void generateGUITest() { |
|
|
JPanel expectedResult = new JPanel(); |
|
|
JPanel expectedResult = new JPanel(); |
|
|
JPanel realResult = game.generateGUI(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JPanel realResult = this.game.generateGUI(); |
|
|
|
|
|
|
|
|
assertEquals(expectedResult.getClass(), realResult.getClass()); |
|
|
assertEquals(expectedResult.getClass(), realResult.getClass()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
@Test |
|
|
void numberOfGUIFieldsTest() { |
|
|
void numberOfGUIFieldsTest() { |
|
|
int realResult = 0; |
|
|
|
|
|
int expectedResult = (int) Math.pow(SIZE, 2); |
|
|
int expectedResult = (int) Math.pow(SIZE, 2); |
|
|
|
|
|
|
|
|
JPanel gui = game.generateGUI(); |
|
|
|
|
|
|
|
|
int realResult = 0; |
|
|
|
|
|
|
|
|
|
|
|
JPanel gui = this.game.generateGUI(); |
|
|
Component[] components = gui.getComponents(); |
|
|
Component[] components = gui.getComponents(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (Component component : components) { |
|
|
for (Component component : components) { |
|
|
if (component instanceof JButton) realResult++; |
|
|
|
|
|
|
|
|
if (component instanceof JButton) |
|
|
|
|
|
realResult++; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertEquals(expectedResult, realResult); |
|
|
assertEquals(expectedResult, realResult); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
@Test |
|
|
void getCurrentPlayerTest() { |
|
|
void getCurrentPlayerTest() { |
|
|
GameLogic game = new GameLogic(SIZE); |
|
|
GameLogic game = new GameLogic(SIZE); |
|
|
char expectedResult = game.getCurrentPlayer(); |
|
|
|
|
|
char realResult = 'x'; |
|
|
|
|
|
|
|
|
char expectedResult = 'x'; |
|
|
|
|
|
char realResult = game.getCurrentPlayer(); |
|
|
|
|
|
|
|
|
assertEquals(expectedResult, realResult); |
|
|
assertEquals(expectedResult, realResult); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
@Test |
|
|
void switchPlayerTest() { |
|
|
void switchPlayerTest() { |
|
|
GameLogic game = new GameLogic(SIZE); |
|
|
GameLogic game = new GameLogic(SIZE); |
|
|
game.switchPlayer(); |
|
|
game.switchPlayer(); |
|
|
|
|
|
|
|
|
char expectedResult = game.getCurrentPlayer(); |
|
|
|
|
|
char realResult = 'o'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
char expectedResult = 'o'; |
|
|
|
|
|
char realResult = game.getCurrentPlayer(); |
|
|
|
|
|
|
|
|
assertEquals(expectedResult, realResult); |
|
|
assertEquals(expectedResult, realResult); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
@Test |
|
|
void resetBoardTest() { |
|
|
void resetBoardTest() { |
|
|
GameLogic game = new GameLogic(SIZE); |
|
|
GameLogic game = new GameLogic(SIZE); |
|
|
game.setField(1, 2, 'x'); |
|
|
game.setField(1, 2, 'x'); |
|
|
|
|
|
|
|
|
|
|
|
// @formatter:off |
|
|
char[][] expectedResult = new char[][]{{'-', '-', '-'}, |
|
|
char[][] expectedResult = new char[][]{{'-', '-', '-'}, |
|
|
{'-', '-', '-'}, |
|
|
{'-', '-', '-'}, |
|
|
{'-', '-', '-'}};; |
|
|
|
|
|
|
|
|
{'-', '-', '-'}}; |
|
|
|
|
|
// @formatter:on |
|
|
game.resetBoard(); |
|
|
game.resetBoard(); |
|
|
char[][] realResult = game.getBoard(); |
|
|
char[][] realResult = game.getBoard(); |
|
|
|
|
|
|
|
|
assertArrayEquals(expectedResult, realResult); |
|
|
assertArrayEquals(expectedResult, realResult); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ParameterizedTest(name = "[{index}] {0} -> {2} fields") |
|
|
|
|
|
@MethodSource("testCasesForCountPlayfields") |
|
|
|
|
|
void fieldCountTest(String testName, int size, int expectedResult) { |
|
|
|
|
|
GameLogic game = new GameLogic(size); |
|
|
|
|
|
int realResult = game.countFields(); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(expectedResult, realResult); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ParameterizedTest(name = "[{index}] {0}") |
|
|
|
|
|
@MethodSource("testCasesForSetField") |
|
|
|
|
|
void setFieldTest(String testName, int column, int row, char player, char[][] expectedResult) { |
|
|
|
|
|
this.game.setField(column, row, player); |
|
|
|
|
|
char[][] realResult = this.game.getBoard(); |
|
|
|
|
|
|
|
|
|
|
|
assertArrayEquals(expectedResult, realResult); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ParameterizedTest(name = "[{index}] {0}") |
|
|
|
|
|
@MethodSource("testCasesForCheckEmptyField") |
|
|
|
|
|
void fieldIsEmptyTest(String testName, int columnToCheck, int rowToCheck, boolean expectedResult, char[][] board) { |
|
|
|
|
|
GameLogic game = new GameLogic(board); |
|
|
|
|
|
boolean realResult = game.fieldIsEmpty(columnToCheck, rowToCheck); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(expectedResult, realResult); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ParameterizedTest(name = "[{index}] {0}: should be {2}") |
|
|
|
|
|
@MethodSource("testCasesForCheckForWin") |
|
|
|
|
|
void checkForWinTest(String testName, char player, boolean expectedResult, char[][] boardToCheck) { |
|
|
|
|
|
boolean realResult = new GameLogic(boardToCheck).checkForWin(player); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(expectedResult, realResult); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ParameterizedTest(name = "[{index}] {0}: should be {1}") |
|
|
|
|
|
@MethodSource("testCasesForCheckEndOfGame") |
|
|
|
|
|
void checkEndOfGameTest(String testName, boolean expectedResult, char[][] boardToCheck) { |
|
|
|
|
|
boolean realResult = new GameLogic(boardToCheck).checkEndOfGame(); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(expectedResult, realResult); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ParameterizedTest(name = "[{index}] {0}: should be {1}") |
|
|
|
|
|
@MethodSource("testCasesForCheckButtonState") |
|
|
|
|
|
void buttonStateTest(String testName, boolean expectedResult, boolean doClick, int column, int row) { |
|
|
|
|
|
game.generateGUI(); |
|
|
|
|
|
JButton currentField = game.getGUIField(column, row); |
|
|
|
|
|
|
|
|
|
|
|
if (doClick) currentField.doClick(); |
|
|
|
|
|
boolean realResult = !currentField.getText().isEmpty(); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(expectedResult, realResult); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static Stream<Arguments> testCasesForCountPlayfields() { |
|
|
|
|
|
return Stream.of( |
|
|
|
|
|
Arguments.of("1x1 board with too few fields", 1, 9), |
|
|
|
|
|
Arguments.of("2x2 board with too few fields", 2, 9), |
|
|
|
|
|
Arguments.of("3x3 board with 9 playfields", 3, 9), |
|
|
|
|
|
Arguments.of("4x4 board with 16 playfields", 4, 16), |
|
|
|
|
|
Arguments.of("5x5 board with 25 playfields", 5,25) |
|
|
|
|
|
); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ParameterizedTest(name = "[{index}] {0} -> {2} fields") |
|
|
|
|
|
@MethodSource("testCasesForCountPlayfields") |
|
|
|
|
|
void fieldCountTest(String testName, int size, int expectedResult) { |
|
|
|
|
|
GameLogic game = new GameLogic(size); |
|
|
|
|
|
int realResult = game.countFields(); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(expectedResult, realResult); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ParameterizedTest(name = "[{index}] {0}") |
|
|
|
|
|
@MethodSource("testCasesForSetField") |
|
|
|
|
|
void setFieldTest(String testName, int column, int row, char player, char[][] expectedResult) { |
|
|
|
|
|
this.game.setField(column, row, player); |
|
|
|
|
|
char[][] realResult = this.game.getBoard(); |
|
|
|
|
|
|
|
|
|
|
|
assertArrayEquals(expectedResult, realResult); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ParameterizedTest(name = "[{index}] {0}") |
|
|
|
|
|
@MethodSource("testCasesForCheckEmptyField") |
|
|
|
|
|
void fieldIsEmptyTest(String testName, int columnToCheck, int rowToCheck, boolean expectedResult, char[][] board) { |
|
|
|
|
|
GameLogic game = new GameLogic(board); |
|
|
|
|
|
boolean realResult = game.fieldIsEmpty(columnToCheck, rowToCheck); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(expectedResult, realResult); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ParameterizedTest(name = "[{index}] {0}: should be {2}") |
|
|
|
|
|
@MethodSource("testCasesForCheckForWin") |
|
|
|
|
|
void checkForWinTest(String testName, char player, boolean expectedResult, char[][] boardToCheck) { |
|
|
|
|
|
boolean realResult = new GameLogic(boardToCheck).checkForWin(player); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(expectedResult, realResult); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ParameterizedTest(name = "[{index}] {0}: should be {1}") |
|
|
|
|
|
@MethodSource("testCasesForCheckEndOfGame") |
|
|
|
|
|
void checkEndOfGameTest(String testName, boolean expectedResult, char[][] boardToCheck) { |
|
|
|
|
|
boolean realResult = new GameLogic(boardToCheck).checkEndOfGame(); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(expectedResult, realResult); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ParameterizedTest(name = "[{index}] {0}: should be {1}") |
|
|
|
|
|
@MethodSource("testCasesForCheckButtonState") |
|
|
|
|
|
void buttonStateTest(String testName, boolean expectedResult, boolean doClick, int column, int row) throws InterruptedException { |
|
|
|
|
|
GameLogic game = new GameLogic(SIZE); |
|
|
|
|
|
game.generateGUI(); |
|
|
|
|
|
JButton currentField = game.getGUIField(0, 0); |
|
|
|
|
|
|
|
|
|
|
|
if (doClick) |
|
|
|
|
|
currentField.doClick(); |
|
|
|
|
|
boolean realResult = !currentField.getText().isEmpty(); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(expectedResult, realResult); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// @formatter:off |
|
|
|
|
|
private static Stream<Arguments> testCasesForCountPlayfields() { |
|
|
|
|
|
return Stream.of(Arguments.of("1x1 board with too few fields", 1, 9), |
|
|
|
|
|
Arguments.of("2x2 board with too few fields", 2, 9), |
|
|
|
|
|
Arguments.of("3x3 board with 9 playfields", 3, 9), |
|
|
|
|
|
Arguments.of("4x4 board with 16 playfields", 4, 16), |
|
|
|
|
|
Arguments.of("5x5 board with 25 playfields", 5, 25)); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
private static Stream<Arguments> testCasesForSetField() { |
|
|
private static Stream<Arguments> testCasesForSetField() { |
|
|
return Stream.of( |
|
|
return Stream.of( |
|
|
Arguments.of("set field [0][0] for player 1", 0, 0, 'x', new char[][] |
|
|
Arguments.of("set field [0][0] for player 1", 0, 0, 'x', new char[][] |
|
@ -326,5 +332,6 @@ class GameLogicTest { |
|
|
Arguments.of("dont't trigger gui field [1][1]", false, false, 1, 1) |
|
|
Arguments.of("dont't trigger gui field [1][1]", false, false, 1, 1) |
|
|
); |
|
|
); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// @formatter:on |
|
|
|
|
|
|
|
|
} |
|
|
} |