Skip to content

Commit

Permalink
Merge pull request #33 from 19mdavenport/main
Browse files Browse the repository at this point in the history
Test Updates and Code Style test addition
  • Loading branch information
leesjensen authored Dec 11, 2024
2 parents a92c8ce + 402f46d commit 4ddf543
Show file tree
Hide file tree
Showing 19 changed files with 314 additions and 326 deletions.
10 changes: 10 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified server/lib/passoff-dependencies.jar
Binary file not shown.
4 changes: 1 addition & 3 deletions shared/src/test/java/passoff/chess/ChessBoardTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import static passoff.chess.TestUtilities.*;

public class ChessBoardTests {

@Test
Expand All @@ -33,7 +31,7 @@ public void getAddPiece() {
@Test
@DisplayName("Reset Board")
public void defaultGameBoard() {
var expectedBoard = defaultBoard();
var expectedBoard = TestUtilities.defaultBoard();

var actualBoard = new ChessBoard();
actualBoard.resetBoard();
Expand Down
40 changes: 27 additions & 13 deletions shared/src/test/java/passoff/chess/TestUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,35 @@
import chess.*;
import org.junit.jupiter.api.Assertions;

import java.util.Collection;
import java.util.HashSet;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class TestUtilities {
static public void validateMoves(String boardText, ChessPosition startPosition, int[][] endPositions) {
public static void validateMoves(String boardText, ChessPosition startPosition, int[][] endPositions) {
var board = loadBoard(boardText);
var testPiece = board.getPiece(startPosition);
var validMoves = loadMoves(startPosition, endPositions);
validateMoves(board, testPiece, startPosition, validMoves);
}

static public void validateMoves(ChessBoard board, ChessPiece testPiece, ChessPosition startPosition, Set<ChessMove> validMoves) {
var pieceMoves = new HashSet<>(testPiece.pieceMoves(board, startPosition));
assertCollectionsEquals(validMoves, pieceMoves, "Wrong moves");
public static void validateMoves(ChessBoard board, ChessPiece testPiece, ChessPosition startPosition,
List<ChessMove> validMoves) {
var pieceMoves = new ArrayList<>(testPiece.pieceMoves(board, startPosition));
validateMoves(validMoves, pieceMoves);
}

static public <T> void assertCollectionsEquals(Collection<T> first, Collection<T> second, String message) {
Assertions.assertEquals(new HashSet<>(first), new HashSet<>(second), message);
Assertions.assertEquals(first.size(), second.size(), "Collections not the same size");
public static void validateMoves(List<ChessMove> expected, List<ChessMove> actual) {
Comparator<ChessMove> comparator = Comparator.comparingInt(TestUtilities::moveToInt);
expected.sort(comparator);
actual.sort(comparator);

Assertions.assertEquals(expected, actual, "Wrong moves");
}

final static Map<Character, ChessPiece.PieceType> CHAR_TO_TYPE_MAP = Map.of(

private static final Map<Character, ChessPiece.PieceType> CHAR_TO_TYPE_MAP = Map.of(
'p', ChessPiece.PieceType.PAWN,
'n', ChessPiece.PieceType.KNIGHT,
'r', ChessPiece.PieceType.ROOK,
Expand Down Expand Up @@ -74,12 +79,21 @@ public static ChessBoard defaultBoard() {
""");
}

public static Set<ChessMove> loadMoves(ChessPosition startPosition, int[][] endPositions) {
var validMoves = new HashSet<ChessMove>();
public static List<ChessMove> loadMoves(ChessPosition startPosition, int[][] endPositions) {
var validMoves = new ArrayList<ChessMove>();
for (var endPosition : endPositions) {
validMoves.add(new ChessMove(startPosition,
new ChessPosition(endPosition[0], endPosition[1]), null));
}
return validMoves;
}

private static int positionToInt(ChessPosition position) {
return 10 * position.getRow() + position.getColumn();
}

private static int moveToInt(ChessMove move) {
return 1000 * positionToInt(move.getStartPosition()) + 10 * positionToInt(move.getEndPosition()) +
((move.getPromotionPiece() != null) ? move.getPromotionPiece().ordinal() + 1 : 0);
}
}
9 changes: 4 additions & 5 deletions shared/src/test/java/passoff/chess/piece/BishopMoveTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

import chess.ChessPosition;
import org.junit.jupiter.api.Test;

import static passoff.chess.TestUtilities.validateMoves;
import passoff.chess.TestUtilities;

public class BishopMoveTests {

@Test
public void bishopMoveUntilEdge() {
validateMoves("""
TestUtilities.validateMoves("""
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
Expand All @@ -32,7 +31,7 @@ public void bishopMoveUntilEdge() {

@Test
public void bishopCaptureEnemy() {
validateMoves("""
TestUtilities.validateMoves("""
| | | | | | | | |
| | | |Q| | | | |
| | | | | | | | |
Expand All @@ -55,7 +54,7 @@ public void bishopCaptureEnemy() {

@Test
public void bishopBlocked() {
validateMoves("""
TestUtilities.validateMoves("""
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
Expand Down
9 changes: 4 additions & 5 deletions shared/src/test/java/passoff/chess/piece/KingMoveTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

import chess.ChessPosition;
import org.junit.jupiter.api.Test;

import static passoff.chess.TestUtilities.validateMoves;
import passoff.chess.TestUtilities;

public class KingMoveTests {

@Test
public void kingMiddleOfBoard() {
validateMoves("""
TestUtilities.validateMoves("""
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
Expand All @@ -27,7 +26,7 @@ public void kingMiddleOfBoard() {

@Test
public void kingCaptureEnemy() {
validateMoves("""
TestUtilities.validateMoves("""
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
Expand All @@ -45,7 +44,7 @@ public void kingCaptureEnemy() {

@Test
public void kingBlocked() {
validateMoves("""
TestUtilities.validateMoves("""
| | | | | | |r|k|
| | | | | | |p|p|
| | | | | | | | |
Expand Down
29 changes: 14 additions & 15 deletions shared/src/test/java/passoff/chess/piece/KnightMoveTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

import chess.ChessPosition;
import org.junit.jupiter.api.Test;

import static passoff.chess.TestUtilities.validateMoves;
import passoff.chess.TestUtilities;

public class KnightMoveTests {

@Test
public void knightMiddleOfBoardWhite() {
validateMoves("""
TestUtilities.validateMoves("""
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
Expand All @@ -28,7 +27,7 @@ public void knightMiddleOfBoardWhite() {

@Test
public void knightMiddleOfBoardBlack() {
validateMoves("""
TestUtilities.validateMoves("""
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
Expand All @@ -48,7 +47,7 @@ public void knightMiddleOfBoardBlack() {

@Test
public void knightEdgeOfBoardLeft() {
validateMoves("""
TestUtilities.validateMoves("""
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
Expand All @@ -65,7 +64,7 @@ public void knightEdgeOfBoardLeft() {

@Test
public void knightEdgeOfBoardRight() {
validateMoves("""
TestUtilities.validateMoves("""
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
Expand All @@ -82,7 +81,7 @@ public void knightEdgeOfBoardRight() {

@Test
public void knightEdgeOfBoardBottom() {
validateMoves("""
TestUtilities.validateMoves("""
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
Expand All @@ -99,7 +98,7 @@ public void knightEdgeOfBoardBottom() {

@Test
public void knightEdgeOfBoardTop() {
validateMoves("""
TestUtilities.validateMoves("""
| | |N| | | | | |
| | | | | | | | |
| | | | | | | | |
Expand All @@ -117,7 +116,7 @@ public void knightEdgeOfBoardTop() {

@Test
public void knightCornerOfBoardBottomRight() {
validateMoves("""
TestUtilities.validateMoves("""
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
Expand All @@ -134,7 +133,7 @@ public void knightCornerOfBoardBottomRight() {

@Test
public void knightCornerOfBoardTopRight() {
validateMoves("""
TestUtilities.validateMoves("""
| | | | | | | |N|
| | | | | | | | |
| | | | | | | | |
Expand All @@ -151,7 +150,7 @@ public void knightCornerOfBoardTopRight() {

@Test
public void knightCornerOfBoardTopLeft() {
validateMoves("""
TestUtilities.validateMoves("""
|n| | | | | | | |
| | | | | | | | |
| | | | | | | | |
Expand All @@ -168,7 +167,7 @@ public void knightCornerOfBoardTopLeft() {

@Test
public void knightCornerOfBoardBottomLeft() {
validateMoves("""
TestUtilities.validateMoves("""
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
Expand All @@ -185,7 +184,7 @@ public void knightCornerOfBoardBottomLeft() {

@Test
public void knightSurroundedButNotBlocked() {
validateMoves("""
TestUtilities.validateMoves("""
| | | | | | | | |
| | | | | | | | |
| | | |R|R|R| | |
Expand All @@ -204,7 +203,7 @@ public void knightSurroundedButNotBlocked() {

@Test
public void knightBlocked() {
validateMoves("""
TestUtilities.validateMoves("""
| | | | | | | | |
| | | |R| | | | |
| | | | | | |P| |
Expand All @@ -222,7 +221,7 @@ public void knightBlocked() {

@Test
public void knightCaptureEnemy() {
validateMoves("""
TestUtilities.validateMoves("""
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
Expand Down
Loading

0 comments on commit 4ddf543

Please sign in to comment.