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

Wrote more unit tests #288

Merged
merged 8 commits into from
Apr 22, 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
95 changes: 95 additions & 0 deletions core/development/logic/actions/AttackPieceAction.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { OVERWORLD_BOARD_ID } from '../../Constants';
import { game } from '../../Game';
import { PlayerInventory } from '../inventory/PlayerInventory';
import { Queen } from '../pieces/Queen';
import { Position } from '../pieces/types/Position';
import { Player } from '../players/Player';
import { PlayerColor } from '../players/types/PlayerColor';
import { AttackPieceAction } from './AttackPieceAction';
import { ActionResult } from './types/ActionResult';

const whitePlayer = new Player(PlayerColor.WHITE, new PlayerInventory());

jest.mock('../../ui/BoardManager.ts', () => ({
destroyElementOnBoard: jest.fn(),
moveElementOnBoard: jest.fn(),
getSquareElementById: jest.fn(),
spawnPieceElementOnBoard: jest.fn(),
getAllSquareElements: jest.fn(),
highlightLastMove: jest.fn(),
}));
jest.mock('../../ui/Screen.ts', () => ({
renderGameInformation: jest.fn(),
hideUnicornAttackButton: jest.fn(),
}));
jest.mock('../../ui/logs/Logger.ts');
jest.mock('../../ui/Events.ts', () => ({}));
jest.mock('../../ui/InventoriesUI.ts', () => ({
initializeInventoryUI: jest.fn(),
switchShownInventory: jest.fn(),
showItemOnInventory: jest.fn(),
}));
jest.mock('../../ui/ShopUI.ts');

game.getPlayersTurnSwitcher = jest.fn().mockReturnValue({
getCurrentPlayer: jest.fn().mockReturnValue(whitePlayer),
getTurnsCount: jest.fn().mockReturnValue(1),
});

describe('AttackPieceAction', () => {
afterEach(() => {
jest.clearAllMocks();
});

test('Invalid - Piece Undefined Position', () => {
// Arrange
const piece = new Queen(whitePlayer, undefined);

const attackPieceAction = new AttackPieceAction(piece, piece);

// Act
const actionResult = attackPieceAction.execute();

// Assert
expect(actionResult).toEqual(ActionResult.FAILURE);
});

test('Valid - Normal Kill', () => {
// Arrange
const initialKillerPiecePosition: Position = {
coordinates: [0, 0],
boardId: OVERWORLD_BOARD_ID,
};
const killerPiece = new Queen(whitePlayer, initialKillerPiecePosition);
const initialKilledPiecePosition: Position = {
coordinates: [1, 0],
boardId: OVERWORLD_BOARD_ID,
};
const killedPiece = new Queen(whitePlayer, initialKilledPiecePosition);

const attackPieceAction = new AttackPieceAction(killerPiece, killedPiece);

// Act
const actionResult = attackPieceAction.execute();

// Assert
expect(actionResult).toEqual(ActionResult.SUCCESS);
});

test('Valid - Attack Self', () => {
// Arrange
const initialPiecePosition: Position = {
coordinates: [3, 4],
boardId: OVERWORLD_BOARD_ID,
};
const piece = new Queen(whitePlayer, initialPiecePosition);

const attackPieceAction = new AttackPieceAction(piece, piece);

// Act
const actionResult = attackPieceAction.execute();

// Assert
expect(actionResult).toEqual(ActionResult.SUCCESS);
});
});
4 changes: 3 additions & 1 deletion core/development/logic/actions/AttackPieceAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ export class AttackPieceAction implements GameAction {
this.target,
this.piece,
).execute();
if (killPieceByPieceResult === ActionResult.FAILURE)

if (killPieceByPieceResult === ActionResult.FAILURE) {
return ActionResult.FAILURE;
}

const targetSquare: Square = { position: targetPosition };
move(this.piece, targetSquare.position);
Expand Down
123 changes: 123 additions & 0 deletions core/development/logic/actions/CastleAction.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import { OVERWORLD_BOARD_ID } from '../../Constants';
import { game } from '../../Game';
import { PlayerInventory } from '../inventory/PlayerInventory';
import { King } from '../pieces/King';
import { Position } from '../pieces/types/Position';
import { Player } from '../players/Player';
import { PlayerColor } from '../players/types/PlayerColor';
import { CastleAction } from './CastleAction';
import { ActionResult } from './types/ActionResult';

const whitePlayer = new Player(PlayerColor.WHITE, new PlayerInventory());

jest.mock('../../ui/BoardManager.ts', () => ({
destroyElementOnBoard: jest.fn(),
moveElementOnBoard: jest.fn(),
getSquareElementById: jest.fn(),
spawnPieceElementOnBoard: jest.fn(),
getAllSquareElements: jest.fn(),
highlightLastMove: jest.fn(),
}));
jest.mock('../../ui/Screen.ts', () => ({
renderGameInformation: jest.fn(),
hideUnicornAttackButton: jest.fn(),
}));
jest.mock('../../ui/logs/Logger.ts');
jest.mock('../../ui/Events.ts', () => ({}));
jest.mock('../../ui/InventoriesUI.ts', () => ({
initializeInventoryUI: jest.fn(),
switchShownInventory: jest.fn(),
showItemOnInventory: jest.fn(),
}));
jest.mock('../../ui/ShopUI.ts');

game.getPlayersTurnSwitcher = jest.fn().mockReturnValue({
getCurrentPlayer: jest.fn().mockReturnValue(whitePlayer),
getTurnsCount: jest.fn().mockReturnValue(1),
});

describe('CastleAction', () => {
afterEach(() => {
jest.clearAllMocks();
});

test('Invalid - Piece Undefined Position', () => {
// Arrange
const kingPiece = new King(whitePlayer, undefined);
const targetPosition: Position = {
coordinates: [6, 0],
boardId: OVERWORLD_BOARD_ID,
};

const castleAction = new CastleAction(kingPiece, targetPosition);

// Act
const actionResult = castleAction.execute();

// Assert
expect(actionResult).toEqual(ActionResult.FAILURE);
});

test('Invalid - Piece Invalid Position', () => {
// Arrange
const initialPosition: Position = {
coordinates: [4, 0],
boardId: OVERWORLD_BOARD_ID,
};
const kingPiece = new King(whitePlayer, initialPosition);
const targetPosition: Position = {
coordinates: [5, 0],
boardId: OVERWORLD_BOARD_ID,
};

const castleAction = new CastleAction(kingPiece, targetPosition);

// Act
const actionResult = castleAction.execute();

// Assert
expect(actionResult).toEqual(ActionResult.FAILURE);
});

test('Valid - Kingside Castling', () => {
// Arrange
const initialPosition: Position = {
coordinates: [4, 0],
boardId: OVERWORLD_BOARD_ID,
};
const kingPiece = new King(whitePlayer, initialPosition);
const targetPosition: Position = {
coordinates: [6, 0],
boardId: OVERWORLD_BOARD_ID,
};

const castleAction = new CastleAction(kingPiece, targetPosition);

// Act
const actionResult = castleAction.execute();

// Assert
expect(actionResult).toEqual(ActionResult.SUCCESS);
});

test('Valid - Queenside Castling', () => {
// Arrange
const initialPosition: Position = {
coordinates: [4, 0],
boardId: OVERWORLD_BOARD_ID,
};
const kingPiece = new King(whitePlayer, initialPosition);
const targetPosition: Position = {
coordinates: [2, 0],
boardId: OVERWORLD_BOARD_ID,
};

const castleAction = new CastleAction(kingPiece, targetPosition);

// Act
const actionResult = castleAction.execute();

// Assert
expect(actionResult).toEqual(ActionResult.SUCCESS);
});
});
6 changes: 5 additions & 1 deletion core/development/logic/actions/CastleAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ export class CastleAction implements GameAction {

execute(): ActionResult {
if (!this.piece.position) return ActionResult.FAILURE;

const targetXPosition = this.targetPosition.coordinates[0];
const kingXPosition = this.piece.position.coordinates[0];
const deltaX = targetXPosition - kingXPosition;
const isKingsideCastling = deltaX > 0;

const rookPiece = (this.piece as King).getRookForCastling(
const isValidCastling = Math.abs(deltaX) == 2;
if (!isValidCastling) return ActionResult.FAILURE;

const rookPiece = this.piece.getRookForCastling(
this.piece.player,
isKingsideCastling,
);
Expand Down
Loading