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

[#264] Added tests for friendly fire rule #296

Merged
merged 7 commits into from
Apr 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
9 changes: 5 additions & 4 deletions core/development/logic/actions/AttackPieceAction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { AttackPieceAction } from './AttackPieceAction';
import { ActionResult } from './types/ActionResult';

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

jest.mock('../../ui/BoardManager.ts', () => ({
destroyElementOnBoard: jest.fn(),
Expand Down Expand Up @@ -41,7 +42,7 @@ describe('AttackPieceAction', () => {
jest.clearAllMocks();
});

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

Expand All @@ -54,7 +55,7 @@ describe('AttackPieceAction', () => {
expect(actionResult).toEqual(ActionResult.FAILURE);
});

test('Valid - Normal Kill', () => {
test('should return SUCCESS if valid kill action', () => {
// Arrange
const initialKillerPiecePosition: Position = {
coordinates: [0, 0],
Expand All @@ -65,7 +66,7 @@ describe('AttackPieceAction', () => {
coordinates: [1, 0],
boardId: OVERWORLD_BOARD_ID,
};
const killedPiece = new Queen(whitePlayer, initialKilledPiecePosition);
const killedPiece = new Queen(blackPlayer, initialKilledPiecePosition);

const attackPieceAction = new AttackPieceAction(killerPiece, killedPiece);

Expand All @@ -76,7 +77,7 @@ describe('AttackPieceAction', () => {
expect(actionResult).toEqual(ActionResult.SUCCESS);
});

test('Valid - Attack Self', () => {
test('should return SUCCESS if valid self attack', () => {
// Arrange
const initialPiecePosition: Position = {
coordinates: [3, 4],
Expand Down
2 changes: 1 addition & 1 deletion core/development/logic/items/PiggyBank.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('PiggyBank', () => {
jest.clearAllMocks();
});

test("should return SUCCESS and increase player's gold if position is valid", () => {
test('should return SUCCESS and increase the amount of gold the player has if position is valid', () => {
// Arrange
const initialPiecePosition: Position = {
coordinates: [0, 0],
Expand Down
69 changes: 69 additions & 0 deletions core/development/logic/rules/ExperienceOnKillRule.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { game } from '../../Game';
import { PlayerInventory } from '../inventory/PlayerInventory';
import { Queen } from '../pieces/Queen';
import { PlayerColor } from '../players/types/PlayerColor';
import { Player } from '../players/Player';
import { Pawn } from '../pieces/Pawn';
import { Position } from '../pieces/types/Position';
import { OVERWORLD_BOARD_ID } from '../../Constants';
import { AttackPieceAction } from '../actions/AttackPieceAction';

const whitePlayer = new Player(PlayerColor.WHITE, new PlayerInventory());
const blackPlayer = new Player(PlayerColor.BLACK, 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(),
spawnItemOnChildElement: 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('Experience On Kill Rule', () => {
afterEach(() => {
jest.clearAllMocks();
});

test('should return SUCCESS if xp gained from kill', () => {
// Arrange
const initialKillerPiecePosition: Position = {
coordinates: [0, 0],
boardId: OVERWORLD_BOARD_ID,
};
const killerPiece = new Queen(whitePlayer, initialKillerPiecePosition);
const initialKillerPlayerXp = killerPiece.player.xp;
const initialKilledPiecePosition: Position = {
coordinates: [1, 0],
boardId: OVERWORLD_BOARD_ID,
};
const killedPiece = new Pawn(blackPlayer, initialKilledPiecePosition);

const attackPieceAction = new AttackPieceAction(killerPiece, killedPiece);

// Act
attackPieceAction.execute();
const newKillerPlayerXp = killerPiece.player.xp;

// Assert
expect(newKillerPlayerXp).toBeGreaterThan(initialKillerPlayerXp);
});
});
71 changes: 71 additions & 0 deletions core/development/logic/rules/FriendlyFireRule.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { game } from '../../Game';
import { PlayerInventory } from '../inventory/PlayerInventory';
import { Queen } from '../pieces/Queen';
import { PlayerColor } from '../players/types/PlayerColor';
import { Player } from '../players/Player';
import { Pawn } from '../pieces/Pawn';
import { Position } from '../pieces/types/Position';
import { OVERWORLD_BOARD_ID } from '../../Constants';
import { AttackPieceAction } from '../actions/AttackPieceAction';
import { ActionResult } from '../actions/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(),
spawnItemOnChildElement: 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('Friendly Fire Rule', () => {
afterEach(() => {
jest.clearAllMocks();
});

test('should return SUCCESS if killing a piece of the same color is valid', () => {
// 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 Pawn(whitePlayer, initialKilledPiecePosition);

const attackTeammateAction = new AttackPieceAction(
killerPiece,
killedPiece,
);

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

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