Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

16 create more tests #62

Merged
merged 3 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions src/test/java/helper/TestHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package helper;

import dx.schema.types.PieceType;
import jchess.common.IChessGame;
import jchess.common.moveset.MoveIntention;
import jchess.common.moveset.NormalMove;
import jchess.ecs.Entity;

import java.awt.Point;

public class TestHelper {
public static Entity getTileAtPosition(IChessGame game, int x, int y) {
return game.getEntityManager().getEntities().stream()
.filter(entity -> entity.tile != null
&& entity.tile.position.x == x
&& entity.tile.position.y == y)
.findFirst().orElse(null);
}

public static void movePiece(IChessGame game, Entity from, Entity to) {
NormalMove.getMove(game, from, to).onClick().run();
}

public static boolean hasPiece(Entity entity, PieceType pieceType, int owner) {
return entity.piece != null
&& entity.piece.identifier.pieceType() == pieceType
&& entity.piece.identifier.ownerId() == owner;
}

public static boolean hasNoPiece(Entity entity) {
return entity.piece == null;
}

public static MoveIntention findMoveToTile(IChessGame game, Entity piece, Point displayTile) {
return piece.findValidMoves(game, false)
.filter(move -> {
Entity displayEntity = move.displayTile();
if (displayEntity == null || displayEntity.tile == null) return false;

return displayEntity.tile.position.equals(displayTile);
})
.findFirst().orElse(null);
}

public static MoveIntention findMoveToTile(IChessGame game, Entity piece, Entity displayTile) {
return piece.findValidMoves(game, false)
.filter(move -> {
Entity displayEntity = move.displayTile();
if (displayEntity == null || displayEntity.tile == null) return false;

return displayEntity == displayTile;
})
.findFirst().orElse(null);
}
}
16 changes: 3 additions & 13 deletions src/test/java/jchess/RegressionTests.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package jchess;

import dx.schema.types.PieceType;
import jchess.common.IChessGame;
import jchess.common.events.BoardClickedEvent;
import jchess.common.moveset.NormalMove;
import jchess.ecs.Entity;
Expand All @@ -15,19 +14,10 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class RegressionTests {
private static Entity getTileAtPosition(IChessGame game, int x, int y) {
return game.getEntityManager().getEntities().stream()
.filter(entity -> entity.tile != null
&& entity.tile.position.x == x
&& entity.tile.position.y == y)
.findFirst().orElse(null);
}

private static void movePiece(IChessGame game, Entity from, Entity to) {
NormalMove.getMove(game, from, to).onClick().run();
}
import static helper.TestHelper.getTileAtPosition;
import static helper.TestHelper.movePiece;

public class RegressionTests {
/**
* Simulates an error-condition caused by incorrect MoveSimulator#revert logic, as described in Issue #27
*/
Expand Down
155 changes: 155 additions & 0 deletions src/test/java/jchess/common/moveset/special/CastlingTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package jchess.common.moveset.special;

import dx.schema.types.PieceType;
import helper.TestHelper;
import jchess.common.components.TileComponent;
import jchess.common.moveset.MoveIntention;
import jchess.ecs.Entity;
import jchess.gamemode.PieceStore;
import jchess.gamemode.square2p.Square2PlayerGame;
import jchess.gamemode.square2p.Square2pPieceLayouts;
import jchess.gamemode.square2p.Square2pPieces;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.awt.Point;

import static helper.TestHelper.getTileAtPosition;
import static helper.TestHelper.hasPiece;

@SuppressWarnings("FieldCanBeLocal")
public class CastlingTest {
private Square2PlayerGame game;
private Entity x0y0;
private Entity x1y0;
private Entity x2y0;
private Entity x3y0;
private Entity x4y0;
private Entity x7y7;
private Entity x6y7;
private Entity x5y7;
private Entity x4y7;

@BeforeEach
public void init() {
game = new Square2PlayerGame(new PieceStore(Square2pPieces.values()), Square2pPieceLayouts.Standard);
game.start();

x0y0 = getTileAtPosition(game, 0, 0);
x1y0 = getTileAtPosition(game, 1, 0);
x2y0 = getTileAtPosition(game, 2, 0);
x3y0 = getTileAtPosition(game, 3, 0);
x4y0 = getTileAtPosition(game, 4, 0);

x7y7 = getTileAtPosition(game, 7, 7);
x6y7 = getTileAtPosition(game, 6, 7);
x5y7 = getTileAtPosition(game, 5, 7);
x4y7 = getTileAtPosition(game, 4, 7);

Assertions.assertTrue(hasPiece(x4y0, PieceType.KING, 1));
Assertions.assertTrue(hasPiece(x4y7, PieceType.KING, 0));
Assertions.assertTrue(hasPiece(x0y0, PieceType.ROOK, 1));
Assertions.assertTrue(hasPiece(x7y7, PieceType.ROOK, 0));
}

@Test
public void test_castlingAllowed() {
x1y0.piece = null;
x2y0.piece = null;
x3y0.piece = null;
x6y7.piece = null;
x5y7.piece = null;
TileComponent.updateAttackInfo(game);

MoveIntention blackCastleMove = TestHelper.findMoveToTile(game, x4y0, new Point(2, 0));
MoveIntention whiteCastleMove = TestHelper.findMoveToTile(game, x4y7, new Point(6, 7));
Assertions.assertNotNull(blackCastleMove);
Assertions.assertNotNull(whiteCastleMove);

blackCastleMove.onClick().run();
whiteCastleMove.onClick().run();
Assertions.assertTrue(hasPiece(x2y0, PieceType.KING, 1));
Assertions.assertTrue(hasPiece(x6y7, PieceType.KING, 0));
Assertions.assertTrue(hasPiece(x3y0, PieceType.ROOK, 1));
Assertions.assertTrue(hasPiece(x5y7, PieceType.ROOK, 0));
}

@Test
public void test_castlingBlocked() {
// x1y0.piece = null;
x2y0.piece = null;
x3y0.piece = null;
x6y7.piece = null;
// x5y7.piece = null;
TileComponent.updateAttackInfo(game);

MoveIntention blackCastleMove = TestHelper.findMoveToTile(game, x4y0, new Point(2, 0));
MoveIntention whiteCastleMove = TestHelper.findMoveToTile(game, x4y7, new Point(6, 7));
Assertions.assertNull(blackCastleMove);
Assertions.assertNull(whiteCastleMove);
}

@Test
public void test_castlingPieceMoved() {
x1y0.piece = null;
x2y0.piece = null;
x3y0.piece = null;
x6y7.piece = null;
x5y7.piece = null;
TileComponent.updateAttackInfo(game);

// move black king and back
TestHelper.movePiece(game, x4y0, x3y0);
TestHelper.movePiece(game, x3y0, x4y0);

// move white rook and back
TestHelper.movePiece(game, x7y7, x6y7);
TestHelper.movePiece(game, x6y7, x7y7);

MoveIntention blackCastleMove = TestHelper.findMoveToTile(game, x4y0, new Point(2, 0));
MoveIntention whiteCastleMove = TestHelper.findMoveToTile(game, x4y7, new Point(6, 7));
Assertions.assertNull(blackCastleMove);
Assertions.assertNull(whiteCastleMove);
}

@Test
public void test_castlingKingChecked() {
x1y0.piece = null;
x2y0.piece = null;
x3y0.piece = null;
x6y7.piece = null;
x5y7.piece = null;
game.createPiece(getTileAtPosition(game, 4, 6), PieceType.ROOK, 1); // create black rook checking white king
game.createPiece(getTileAtPosition(game, 4, 1), PieceType.ROOK, 0); // create white rook checking black king
TileComponent.updateAttackInfo(game);

Assertions.assertTrue(x4y0.isAttacked()); // black king is attacked
Assertions.assertTrue(x4y7.isAttacked()); // white king is attacked

MoveIntention blackCastleMove = TestHelper.findMoveToTile(game, x4y0, new Point(2, 0));
MoveIntention whiteCastleMove = TestHelper.findMoveToTile(game, x4y7, new Point(6, 7));
Assertions.assertNull(blackCastleMove);
Assertions.assertNull(whiteCastleMove);
}

@Test
public void test_castlingKingPathChecked() {
x1y0.piece = null;
x2y0.piece = null;
x3y0.piece = null;
x6y7.piece = null;
x5y7.piece = null;
game.createPiece(getTileAtPosition(game, 5, 6), PieceType.ROOK, 1); // create black rook checking white kings path
game.createPiece(getTileAtPosition(game, 2, 1), PieceType.ROOK, 0); // create white rook checking black kings destination
TileComponent.updateAttackInfo(game);

Assertions.assertTrue(x2y0.isAttacked(1)); // black king destination is attacked
Assertions.assertTrue(x5y7.isAttacked(0)); // white king path is attacked

MoveIntention blackCastleMove = TestHelper.findMoveToTile(game, x4y0, new Point(2, 0));
MoveIntention whiteCastleMove = TestHelper.findMoveToTile(game, x4y7, new Point(6, 7));
Assertions.assertNull(blackCastleMove);
Assertions.assertNull(whiteCastleMove);
}
}
129 changes: 129 additions & 0 deletions src/test/java/jchess/common/moveset/special/EnPassantTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package jchess.common.moveset.special;

import dx.schema.types.PieceType;
import helper.TestHelper;
import jchess.common.moveset.MoveIntention;
import jchess.ecs.Entity;
import jchess.gamemode.PieceStore;
import jchess.gamemode.square2p.Square2PlayerGame;
import jchess.gamemode.square2p.Square2pPieceLayouts;
import jchess.gamemode.square2p.Square2pPieces;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static helper.TestHelper.getTileAtPosition;
import static helper.TestHelper.hasPiece;

@SuppressWarnings("FieldCanBeLocal")
public class EnPassantTest {
private Square2PlayerGame game;

// enPassant pawn
private Entity x1y4;

// unrelated pawn / movement
private Entity x0y1;
private Entity x0y2;

// white pawn movement
private Entity x2y6;
private Entity x2y5;
private Entity x2y4;


@BeforeEach
public void init() {
game = new Square2PlayerGame(new PieceStore(Square2pPieces.values()), Square2pPieceLayouts.Standard);
game.start();

x1y4 = getTileAtPosition(game, 1, 4);

x0y1 = getTileAtPosition(game, 0, 1);
x0y2 = getTileAtPosition(game, 0, 2);

x2y6 = getTileAtPosition(game, 2, 6);
x2y5 = getTileAtPosition(game, 2, 5);
x2y4 = getTileAtPosition(game, 2, 4);

game.createPiece(x1y4, PieceType.PAWN, 1);

Assertions.assertTrue(hasPiece(x1y4, PieceType.PAWN, 1));
Assertions.assertTrue(hasPiece(x0y1, PieceType.PAWN, 1));
Assertions.assertTrue(hasPiece(x2y6, PieceType.PAWN, 0));
}

@Test
public void test_enPassantAllowed() {
MoveIntention whiteDoubleMove = TestHelper.findMoveToTile(game, x2y6, x2y4);
Assertions.assertNotNull(whiteDoubleMove);
whiteDoubleMove.onClick().run();

MoveIntention enPassantMove = TestHelper.findMoveToTile(game, x1y4, x2y5);
Assertions.assertNull(x2y5.piece);
Assertions.assertNotNull(enPassantMove);

enPassantMove.onClick().run();
Assertions.assertNull(x2y4.piece);
Assertions.assertTrue(hasPiece(x2y5, PieceType.PAWN, 1));
}

@Test
public void test_enPassantTooLate() {
MoveIntention whiteDoubleMove = TestHelper.findMoveToTile(game, x2y6, x2y4);
Assertions.assertNotNull(whiteDoubleMove);
whiteDoubleMove.onClick().run();

// black misses the enPassant opportunity by moving an unrelated piece
TestHelper.movePiece(game, x0y1, x0y2);

MoveIntention enPassantMove = TestHelper.findMoveToTile(game, x1y4, x2y5);
Assertions.assertNull(enPassantMove);
}

@Test
public void test_enPassantAlreadyCaptured() {
MoveIntention whiteDoubleMove = TestHelper.findMoveToTile(game, x2y6, x2y4);
Assertions.assertNotNull(whiteDoubleMove);
whiteDoubleMove.onClick().run();

// pretend another player (relevant for 3-Player chess) has already captured the pawn
x2y4.piece = null;

MoveIntention enPassantMove = TestHelper.findMoveToTile(game, x1y4, x2y5);
Assertions.assertNull(enPassantMove);
}

@Test
public void test_enPassantBlocked() {
MoveIntention whiteDoubleMove = TestHelper.findMoveToTile(game, x2y6, x2y4);
Assertions.assertNotNull(whiteDoubleMove);
whiteDoubleMove.onClick().run();

// suppose the active player is occupying the tile that was skipped over (relevant for SpecialFirstMove that isn't the default pawn behaviour)
game.createPiece(x2y5, PieceType.BISHOP, 1);

// then enPassant is blocked by a friendly piece
MoveIntention enPassantMove = TestHelper.findMoveToTile(game, x1y4, x2y5);
Assertions.assertNull(enPassantMove);
}

@Test
public void test_enPassantBlockedCapture() {
MoveIntention whiteDoubleMove = TestHelper.findMoveToTile(game, x2y6, x2y4);
Assertions.assertNotNull(whiteDoubleMove);
whiteDoubleMove.onClick().run();

// suppose another opponent moved onto the tile skipped by the white pawn
game.createPiece(x2y5, PieceType.BISHOP, 0); // 0 in this case, because 2 player game

// then the move should be a normal capture move
MoveIntention enPassantMove = TestHelper.findMoveToTile(game, x1y4, x2y5);
Assertions.assertTrue(hasPiece(x2y5, PieceType.BISHOP, 0));
Assertions.assertNotNull(enPassantMove);

enPassantMove.onClick().run();
Assertions.assertTrue(hasPiece(x2y5, PieceType.PAWN, 1));
Assertions.assertTrue(hasPiece(x2y4, PieceType.PAWN, 0)); // white pawn should not be captured in this edge-case
}
}
Loading
Loading