|
|
@ -7,7 +7,6 @@ import java.awt.Component; |
|
|
|
import java.util.stream.Stream; |
|
|
|
|
|
|
|
import javax.swing.JButton; |
|
|
|
import javax.swing.JComponent; |
|
|
|
import javax.swing.JPanel; |
|
|
|
|
|
|
|
import org.junit.jupiter.api.BeforeAll; |
|
|
@ -39,9 +38,11 @@ class GameLogicTest { |
|
|
|
|
|
|
|
@Test |
|
|
|
void getBoardTest() { |
|
|
|
// @formatter:off |
|
|
|
char[][] expectedResult = new char[][]{{'-', '-', '-'}, |
|
|
|
{'-', '-', '-'}, |
|
|
|
{'-', '-', '-'}}; |
|
|
|
// @formatter:on |
|
|
|
char[][] realResult = this.game.getBoard(); |
|
|
|
|
|
|
|
assertArrayEquals(expectedResult, realResult); |
|
|
@ -49,9 +50,11 @@ class GameLogicTest { |
|
|
|
|
|
|
|
@Test |
|
|
|
void createGameLogicWithGivenBoardTest() { |
|
|
|
// @formatter:off |
|
|
|
char[][] expectedResult = new char[][]{{'x', '-', '-'}, |
|
|
|
{'-', 'o', '-'}, |
|
|
|
{'x', '-', '-'}}; |
|
|
|
// @formatter:on |
|
|
|
char[][] givenBoard = expectedResult; |
|
|
|
char[][] realResult = new GameLogic(givenBoard).getBoard(); |
|
|
|
|
|
|
@ -61,21 +64,22 @@ class GameLogicTest { |
|
|
|
@Test |
|
|
|
void generateGUITest() { |
|
|
|
JPanel expectedResult = new JPanel(); |
|
|
|
JPanel realResult = game.generateGUI(); |
|
|
|
JPanel realResult = this.game.generateGUI(); |
|
|
|
|
|
|
|
assertEquals(expectedResult.getClass(), realResult.getClass()); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
void numberOfGUIFieldsTest() { |
|
|
|
int realResult = 0; |
|
|
|
int expectedResult = (int) Math.pow(SIZE, 2); |
|
|
|
int realResult = 0; |
|
|
|
|
|
|
|
JPanel gui = game.generateGUI(); |
|
|
|
JPanel gui = this.game.generateGUI(); |
|
|
|
Component[] components = gui.getComponents(); |
|
|
|
|
|
|
|
for (Component component : components) { |
|
|
|
if (component instanceof JButton) realResult++; |
|
|
|
if (component instanceof JButton) |
|
|
|
realResult++; |
|
|
|
} |
|
|
|
|
|
|
|
assertEquals(expectedResult, realResult); |
|
|
@ -84,8 +88,8 @@ class GameLogicTest { |
|
|
|
@Test |
|
|
|
void getCurrentPlayerTest() { |
|
|
|
GameLogic game = new GameLogic(SIZE); |
|
|
|
char expectedResult = game.getCurrentPlayer(); |
|
|
|
char realResult = 'x'; |
|
|
|
char expectedResult = 'x'; |
|
|
|
char realResult = game.getCurrentPlayer(); |
|
|
|
|
|
|
|
assertEquals(expectedResult, realResult); |
|
|
|
} |
|
|
@ -95,8 +99,8 @@ class GameLogicTest { |
|
|
|
GameLogic game = new GameLogic(SIZE); |
|
|
|
game.switchPlayer(); |
|
|
|
|
|
|
|
char expectedResult = game.getCurrentPlayer(); |
|
|
|
char realResult = 'o'; |
|
|
|
char expectedResult = 'o'; |
|
|
|
char realResult = game.getCurrentPlayer(); |
|
|
|
|
|
|
|
assertEquals(expectedResult, realResult); |
|
|
|
} |
|
|
@ -105,80 +109,82 @@ class GameLogicTest { |
|
|
|
void resetBoardTest() { |
|
|
|
GameLogic game = new GameLogic(SIZE); |
|
|
|
game.setField(1, 2, 'x'); |
|
|
|
|
|
|
|
// @formatter:off |
|
|
|
char[][] expectedResult = new char[][]{{'-', '-', '-'}, |
|
|
|
{'-', '-', '-'}, |
|
|
|
{'-', '-', '-'}};; |
|
|
|
{'-', '-', '-'}}; |
|
|
|
// @formatter:on |
|
|
|
game.resetBoard(); |
|
|
|
char[][] realResult = game.getBoard(); |
|
|
|
|
|
|
|
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(); |
|
|
|
@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); |
|
|
|
} |
|
|
|
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(); |
|
|
|
@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); |
|
|
|
} |
|
|
|
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); |
|
|
|
@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); |
|
|
|
} |
|
|
|
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); |
|
|
|
@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); |
|
|
|
} |
|
|
|
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(); |
|
|
|
@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); |
|
|
|
} |
|
|
|
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); |
|
|
|
@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(); |
|
|
|
if (doClick) |
|
|
|
currentField.doClick(); |
|
|
|
boolean realResult = !currentField.getText().isEmpty(); |
|
|
|
|
|
|
|
assertEquals(expectedResult, realResult); |
|
|
|
} |
|
|
|
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) |
|
|
|
); |
|
|
|
} |
|
|
|
// @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() { |
|
|
|
return Stream.of( |
|
|
@ -326,5 +332,6 @@ class GameLogicTest { |
|
|
|
Arguments.of("dont't trigger gui field [1][1]", false, false, 1, 1) |
|
|
|
); |
|
|
|
} |
|
|
|
// @formatter:on |
|
|
|
|
|
|
|
} |