From 57d533194a19b898602b9900bdb406bef70f8e5e Mon Sep 17 00:00:00 2001 From: Ido-Barnea Date: Mon, 22 Apr 2024 09:55:05 +0300 Subject: [PATCH 1/8] Wrote test for AttackPieceAction --- .../logic/actions/AttackPieceAction.test.ts | 93 +++++++++++++++++++ .../logic/actions/AttackPieceAction.ts | 4 +- 2 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 core/development/logic/actions/AttackPieceAction.test.ts diff --git a/core/development/logic/actions/AttackPieceAction.test.ts b/core/development/logic/actions/AttackPieceAction.test.ts new file mode 100644 index 0000000..bea954e --- /dev/null +++ b/core/development/logic/actions/AttackPieceAction.test.ts @@ -0,0 +1,93 @@ +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'); + +const getCurrentPlayerMock = jest.fn().mockReturnValue(whitePlayer); +const getTurnsCount = jest.fn().mockReturnValue(1); +game.getPlayersTurnSwitcher = jest.fn().mockReturnValue({ + getCurrentPlayer: getCurrentPlayerMock, + getTurnsCount: getTurnsCount, +}); + +describe('AttackPieceAction', () => { + 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); + }); +}); diff --git a/core/development/logic/actions/AttackPieceAction.ts b/core/development/logic/actions/AttackPieceAction.ts index 02c5cd1..5254a1f 100644 --- a/core/development/logic/actions/AttackPieceAction.ts +++ b/core/development/logic/actions/AttackPieceAction.ts @@ -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); From 55180beb934590547f71ba2a4251e00fd6a0ea34 Mon Sep 17 00:00:00 2001 From: Ido-Barnea Date: Mon, 22 Apr 2024 10:02:22 +0300 Subject: [PATCH 2/8] Wrote CastleAction tests --- .../logic/actions/CastleAction.test.ts | 121 ++++++++++++++++++ .../development/logic/actions/CastleAction.ts | 6 +- core/development/logic/pieces/King.ts | 8 +- 3 files changed, 131 insertions(+), 4 deletions(-) create mode 100644 core/development/logic/actions/CastleAction.test.ts diff --git a/core/development/logic/actions/CastleAction.test.ts b/core/development/logic/actions/CastleAction.test.ts new file mode 100644 index 0000000..18a635e --- /dev/null +++ b/core/development/logic/actions/CastleAction.test.ts @@ -0,0 +1,121 @@ +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'); + +const getCurrentPlayerMock = jest.fn().mockReturnValue(whitePlayer); +const getTurnsCount = jest.fn().mockReturnValue(1); +game.getPlayersTurnSwitcher = jest.fn().mockReturnValue({ + getCurrentPlayer: getCurrentPlayerMock, + getTurnsCount: getTurnsCount, +}); + +describe('CastleAction', () => { + 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); + }); +}); diff --git a/core/development/logic/actions/CastleAction.ts b/core/development/logic/actions/CastleAction.ts index 1f32d94..7eb7546 100644 --- a/core/development/logic/actions/CastleAction.ts +++ b/core/development/logic/actions/CastleAction.ts @@ -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, ); diff --git a/core/development/logic/pieces/King.ts b/core/development/logic/pieces/King.ts index 21786eb..ebfb712 100644 --- a/core/development/logic/pieces/King.ts +++ b/core/development/logic/pieces/King.ts @@ -15,21 +15,23 @@ export class King extends BasePiece { } getRookForCastling(player: Player, kingside: boolean): Rook | undefined { - const rank = player.color === PlayerColor.WHITE ? 0 : 7; + const rank = player.color === PlayerColor.WHITE ? 7 : 0; if (!this.position) return; if (kingside) { // Kingside castling + const kingsideCastlingRookXCoordinate = 7; const kingsideRookPosition: Position = { - coordinates: [7, rank], + coordinates: [kingsideCastlingRookXCoordinate, rank], boardId: this.position.boardId, }; return getPieceByPosition(kingsideRookPosition) as Rook | undefined; } else { // Queenside castling + const queensideCastlingRookXCoordinate = 0; const queensideRookPosition: Position = { - coordinates: [0, rank], + coordinates: [queensideCastlingRookXCoordinate, rank], boardId: this.position.boardId, }; From bb28446591047074c02e5dc06994af28a87d7b61 Mon Sep 17 00:00:00 2001 From: Ido-Barnea Date: Mon, 22 Apr 2024 10:02:42 +0300 Subject: [PATCH 3/8] Formatting --- core/development/logic/actions/CastleAction.test.ts | 6 +++--- core/development/logic/actions/CastleAction.ts | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/core/development/logic/actions/CastleAction.test.ts b/core/development/logic/actions/CastleAction.test.ts index 18a635e..673216b 100644 --- a/core/development/logic/actions/CastleAction.test.ts +++ b/core/development/logic/actions/CastleAction.test.ts @@ -61,7 +61,7 @@ describe('CastleAction', () => { const initialPosition: Position = { coordinates: [4, 0], boardId: OVERWORLD_BOARD_ID, - } + }; const kingPiece = new King(whitePlayer, initialPosition); const targetPosition: Position = { coordinates: [5, 0], @@ -82,7 +82,7 @@ describe('CastleAction', () => { const initialPosition: Position = { coordinates: [4, 0], boardId: OVERWORLD_BOARD_ID, - } + }; const kingPiece = new King(whitePlayer, initialPosition); const targetPosition: Position = { coordinates: [6, 0], @@ -103,7 +103,7 @@ describe('CastleAction', () => { const initialPosition: Position = { coordinates: [4, 0], boardId: OVERWORLD_BOARD_ID, - } + }; const kingPiece = new King(whitePlayer, initialPosition); const targetPosition: Position = { coordinates: [2, 0], diff --git a/core/development/logic/actions/CastleAction.ts b/core/development/logic/actions/CastleAction.ts index 7eb7546..9a22e00 100644 --- a/core/development/logic/actions/CastleAction.ts +++ b/core/development/logic/actions/CastleAction.ts @@ -17,7 +17,7 @@ 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; From 68622fc9e45cb272a5014e219672f5af4b8929c4 Mon Sep 17 00:00:00 2001 From: Ido-Barnea Date: Mon, 22 Apr 2024 10:25:34 +0300 Subject: [PATCH 4/8] Wrote KillPieceAction tests --- .../logic/actions/KillPieceAction.test.ts | 159 ++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 core/development/logic/actions/KillPieceAction.test.ts diff --git a/core/development/logic/actions/KillPieceAction.test.ts b/core/development/logic/actions/KillPieceAction.test.ts new file mode 100644 index 0000000..12984e9 --- /dev/null +++ b/core/development/logic/actions/KillPieceAction.test.ts @@ -0,0 +1,159 @@ +import { HEAVEN_BOARD_ID, HELL_BOARD_ID, OVERWORLD_BOARD_ID } from '../../Constants'; +import { game } from '../../Game'; +import { PlayerInventory } from '../inventory/PlayerInventory'; +import { King } from '../pieces/King'; +import { Pawn } from '../pieces/Pawn'; +import { Position } from '../pieces/types/Position'; +import { Player } from '../players/Player'; +import { PlayerColor } from '../players/types/PlayerColor'; +import { KillPieceAction } from './KillPieceAction'; +import { PermanentlyKillPieceAction } from './PermanentlyKillPieceAction'; +import { SpawnPieceInHeavenAction } from './SpawnPieceInHeavenAction'; +import { SpawnPieceInHellAction } from './SpawnPieceInHellAction'; +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'); + +const getCurrentPlayerMock = jest.fn().mockReturnValue(whitePlayer); +const getTurnsCount = jest.fn().mockReturnValue(1); +game.getPlayersTurnSwitcher = jest.fn().mockReturnValue({ + getCurrentPlayer: getCurrentPlayerMock, + getTurnsCount: getTurnsCount, +}); + +jest.mock('./SpawnPieceInHellAction', () => ({ + __esModule: true, + SpawnPieceInHellAction: jest.fn().mockImplementation(() => ({ + execute: jest.fn().mockReturnValue(1), + })), +})); +jest.mock('./SpawnPieceInHeavenAction', () => ({ + __esModule: true, + SpawnPieceInHeavenAction: jest.fn().mockImplementation(() => ({ + execute: jest.fn().mockReturnValue(1), + })), +})); +jest.mock('./PermanentlyKillPieceAction', () => ({ + __esModule: true, + PermanentlyKillPieceAction: jest.fn().mockImplementation(() => ({ + execute: jest.fn().mockReturnValue(1), + })), +})); + +describe('KillPieceAction', () => { + afterEach(() => { + jest.clearAllMocks(); + }); + + test('should spawn piece in hell if killedPiece is on overworld board and is king', () => { + // Arrange + const initialPosition: Position = { + coordinates: [4, 0], + boardId: OVERWORLD_BOARD_ID, + }; + const killedPiece = new King(whitePlayer, initialPosition); + const killPieceAction = new KillPieceAction(killedPiece, OVERWORLD_BOARD_ID); + + // Act + const actionResult = killPieceAction.execute(); + + // Assert + expect(actionResult).toEqual(ActionResult.SUCCESS); + expect(SpawnPieceInHellAction).toHaveBeenCalledWith(killedPiece); + expect(SpawnPieceInHellAction).toHaveBeenCalledTimes(1); + }); + + test('should spawn piece in hell if killedPiece is on overworld board and has killed another piece', () => { + // Arrange + const initialPosition: Position = { + coordinates: [4, 0], + boardId: OVERWORLD_BOARD_ID, + }; + const killedPiece = new Pawn(whitePlayer, initialPosition); + killedPiece.killCount = 1; + const killPieceAction = new KillPieceAction(killedPiece, OVERWORLD_BOARD_ID); + + // Act + const actionResult = killPieceAction.execute(); + + // Assert + expect(actionResult).toEqual(ActionResult.SUCCESS); + expect(SpawnPieceInHellAction).toHaveBeenCalledWith(killedPiece); + expect(SpawnPieceInHellAction).toHaveBeenCalledTimes(1); + }); + + test('should spawn piece in heaven if killedPiece is on overworld board and has not killed another piece', () => { + // Arrange + const initialPosition: Position = { + coordinates: [4, 0], + boardId: OVERWORLD_BOARD_ID, + }; + const killedPiece = new Pawn(whitePlayer, initialPosition); + const killPieceAction = new KillPieceAction(killedPiece, OVERWORLD_BOARD_ID); + + // Act + const actionResult = killPieceAction.execute(); + + // Assert + expect(actionResult).toEqual(ActionResult.SUCCESS); + expect(SpawnPieceInHeavenAction).toHaveBeenCalledWith(killedPiece); + expect(SpawnPieceInHeavenAction).toHaveBeenCalledTimes(1); + }); + + test('should permanently kill piece if killedPiece is on heaven board', () => { + // Arrange + const initialPosition: Position = { + coordinates: [4, 0], + boardId: HEAVEN_BOARD_ID, + }; + const killedPiece = new Pawn(whitePlayer, initialPosition); + const killPieceAction = new KillPieceAction(killedPiece, OVERWORLD_BOARD_ID); + + // Act + const actionResult = killPieceAction.execute(); + + // Assert + expect(actionResult).toEqual(ActionResult.SUCCESS); + expect(PermanentlyKillPieceAction).toHaveBeenCalledWith(killedPiece); + expect(PermanentlyKillPieceAction).toHaveBeenCalledTimes(1); + }); + + test('should permanently kill piece if killedPiece is on hell board', () => { + // Arrange + const initialPosition: Position = { + coordinates: [4, 0], + boardId: HELL_BOARD_ID, + }; + const killedPiece = new Pawn(whitePlayer, initialPosition); + const killPieceAction = new KillPieceAction(killedPiece, OVERWORLD_BOARD_ID); + + // Act + const actionResult = killPieceAction.execute(); + + // Assert + expect(actionResult).toEqual(ActionResult.SUCCESS); + expect(PermanentlyKillPieceAction).toHaveBeenCalledWith(killedPiece); + expect(PermanentlyKillPieceAction).toHaveBeenCalledTimes(1); + }); +}); From 742066765292e73858a4b874294608c768cc040e Mon Sep 17 00:00:00 2001 From: Ido-Barnea Date: Mon, 22 Apr 2024 10:26:27 +0300 Subject: [PATCH 5/8] Formatting --- .../logic/actions/AttackPieceAction.test.ts | 4 +++ .../logic/actions/CastleAction.test.ts | 4 +++ .../logic/actions/KillPieceAction.test.ts | 31 +++++++++++++++---- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/core/development/logic/actions/AttackPieceAction.test.ts b/core/development/logic/actions/AttackPieceAction.test.ts index bea954e..5ac3380 100644 --- a/core/development/logic/actions/AttackPieceAction.test.ts +++ b/core/development/logic/actions/AttackPieceAction.test.ts @@ -39,6 +39,10 @@ game.getPlayersTurnSwitcher = jest.fn().mockReturnValue({ }); describe('AttackPieceAction', () => { + afterEach(() => { + jest.clearAllMocks(); + }); + test('Invalid - Piece Undefined Position', () => { // Arrange const piece = new Queen(whitePlayer, undefined); diff --git a/core/development/logic/actions/CastleAction.test.ts b/core/development/logic/actions/CastleAction.test.ts index 673216b..8c2829f 100644 --- a/core/development/logic/actions/CastleAction.test.ts +++ b/core/development/logic/actions/CastleAction.test.ts @@ -39,6 +39,10 @@ game.getPlayersTurnSwitcher = jest.fn().mockReturnValue({ }); describe('CastleAction', () => { + afterEach(() => { + jest.clearAllMocks(); + }); + test('Invalid - Piece Undefined Position', () => { // Arrange const kingPiece = new King(whitePlayer, undefined); diff --git a/core/development/logic/actions/KillPieceAction.test.ts b/core/development/logic/actions/KillPieceAction.test.ts index 12984e9..a1cc5c1 100644 --- a/core/development/logic/actions/KillPieceAction.test.ts +++ b/core/development/logic/actions/KillPieceAction.test.ts @@ -1,4 +1,8 @@ -import { HEAVEN_BOARD_ID, HELL_BOARD_ID, OVERWORLD_BOARD_ID } from '../../Constants'; +import { + HEAVEN_BOARD_ID, + HELL_BOARD_ID, + OVERWORLD_BOARD_ID, +} from '../../Constants'; import { game } from '../../Game'; import { PlayerInventory } from '../inventory/PlayerInventory'; import { King } from '../pieces/King'; @@ -73,7 +77,10 @@ describe('KillPieceAction', () => { boardId: OVERWORLD_BOARD_ID, }; const killedPiece = new King(whitePlayer, initialPosition); - const killPieceAction = new KillPieceAction(killedPiece, OVERWORLD_BOARD_ID); + const killPieceAction = new KillPieceAction( + killedPiece, + OVERWORLD_BOARD_ID, + ); // Act const actionResult = killPieceAction.execute(); @@ -92,7 +99,10 @@ describe('KillPieceAction', () => { }; const killedPiece = new Pawn(whitePlayer, initialPosition); killedPiece.killCount = 1; - const killPieceAction = new KillPieceAction(killedPiece, OVERWORLD_BOARD_ID); + const killPieceAction = new KillPieceAction( + killedPiece, + OVERWORLD_BOARD_ID, + ); // Act const actionResult = killPieceAction.execute(); @@ -110,7 +120,10 @@ describe('KillPieceAction', () => { boardId: OVERWORLD_BOARD_ID, }; const killedPiece = new Pawn(whitePlayer, initialPosition); - const killPieceAction = new KillPieceAction(killedPiece, OVERWORLD_BOARD_ID); + const killPieceAction = new KillPieceAction( + killedPiece, + OVERWORLD_BOARD_ID, + ); // Act const actionResult = killPieceAction.execute(); @@ -128,7 +141,10 @@ describe('KillPieceAction', () => { boardId: HEAVEN_BOARD_ID, }; const killedPiece = new Pawn(whitePlayer, initialPosition); - const killPieceAction = new KillPieceAction(killedPiece, OVERWORLD_BOARD_ID); + const killPieceAction = new KillPieceAction( + killedPiece, + OVERWORLD_BOARD_ID, + ); // Act const actionResult = killPieceAction.execute(); @@ -146,7 +162,10 @@ describe('KillPieceAction', () => { boardId: HELL_BOARD_ID, }; const killedPiece = new Pawn(whitePlayer, initialPosition); - const killPieceAction = new KillPieceAction(killedPiece, OVERWORLD_BOARD_ID); + const killPieceAction = new KillPieceAction( + killedPiece, + OVERWORLD_BOARD_ID, + ); // Act const actionResult = killPieceAction.execute(); From 241937d82e4b3a9c48afa79c588ed3a44b28407f Mon Sep 17 00:00:00 2001 From: Ido-Barnea Date: Mon, 22 Apr 2024 10:45:33 +0300 Subject: [PATCH 6/8] Wrote KillPieceByFallingOffTheBoardAction tests --- .../actions/KillPieceByEnvironmentAction.ts | 2 +- ...illPieceByFallingOffTheBoardAction.test.ts | 83 +++++++++++++++++++ .../KillPieceByFallingOffTheBoardAction.ts | 4 +- .../logic/actions/TriggerPieceOnTrapAction.ts | 2 +- jest.config.ts | 1 - 5 files changed, 87 insertions(+), 5 deletions(-) create mode 100644 core/development/logic/actions/KillPieceByFallingOffTheBoardAction.test.ts diff --git a/core/development/logic/actions/KillPieceByEnvironmentAction.ts b/core/development/logic/actions/KillPieceByEnvironmentAction.ts index 1a8d180..f4e192a 100644 --- a/core/development/logic/actions/KillPieceByEnvironmentAction.ts +++ b/core/development/logic/actions/KillPieceByEnvironmentAction.ts @@ -4,7 +4,7 @@ import { KillPieceAction } from './KillPieceAction'; import { KillLog } from '../../ui/logs/Log'; import { game } from '../../Game'; -export class KillPieceByEnvironment extends KillPieceAction { +export class KillPieceByEnvironmentAction extends KillPieceAction { private killingSource: string; constructor( diff --git a/core/development/logic/actions/KillPieceByFallingOffTheBoardAction.test.ts b/core/development/logic/actions/KillPieceByFallingOffTheBoardAction.test.ts new file mode 100644 index 0000000..7f80046 --- /dev/null +++ b/core/development/logic/actions/KillPieceByFallingOffTheBoardAction.test.ts @@ -0,0 +1,83 @@ +import { OVERWORLD_BOARD_ID, VOID_BOARD_ID } from '../../Constants'; +import { game } from '../../Game'; +import { PlayerInventory } from '../inventory/PlayerInventory'; +import { Pawn } from '../pieces/Pawn'; +import { BasePiece } from '../pieces/abstract/BasePiece'; +import { Position } from '../pieces/types/Position'; +import { Player } from '../players/Player'; +import { PlayerColor } from '../players/types/PlayerColor'; +import { KillPieceByEnvironmentAction } from './KillPieceByEnvironmentAction'; +import { KillPieceByFallingOffTheBoardAction } from './KillPieceByFallingOffTheBoardAction'; +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'); + +const getCurrentPlayerMock = jest.fn().mockReturnValue(whitePlayer); +const getTurnsCount = jest.fn().mockReturnValue(1); +game.getPlayersTurnSwitcher = jest.fn().mockReturnValue({ + getCurrentPlayer: getCurrentPlayerMock, + getTurnsCount: getTurnsCount, +}); + +jest.mock('./KillPieceByEnvironmentAction', () => ({ + __esModule: true, + KillPieceByEnvironmentAction: jest.fn().mockImplementation((killedPiece: BasePiece) => ({ + execute: jest.fn().mockReturnValueOnce(killedPiece.position ? ActionResult.SUCCESS : ActionResult.FAILURE), + })), +})); + +describe('KillPieceByFallingOffTheBoardAction', () => { + afterEach(() => { + jest.clearAllMocks(); + }); + + test('should return FAILURE if killedPiece.position is undefined', () => { + // Arrange + const killedPiece = new Pawn(whitePlayer, undefined); + const killPieceByFallingOffTheBoardAction = new KillPieceByFallingOffTheBoardAction(killedPiece); + + // Act + const actionResult = killPieceByFallingOffTheBoardAction.execute(); + + // Assert + expect(actionResult).toEqual(ActionResult.FAILURE); + }); + + test('should call super.execute() if killedPiece.position exists', () => { + // Arrange + const initialPosition: Position = { + coordinates: [4, 0], + boardId: OVERWORLD_BOARD_ID, + }; + const killedPiece = new Pawn(whitePlayer, initialPosition); + const killPieceByFallingOffTheBoardAction = new KillPieceByFallingOffTheBoardAction(killedPiece); + + // Act + const actionResult = killPieceByFallingOffTheBoardAction.execute(); + + // Assert + expect(actionResult).toEqual(ActionResult.SUCCESS); + expect(KillPieceByEnvironmentAction).toHaveBeenCalledTimes(1); + }); +}); diff --git a/core/development/logic/actions/KillPieceByFallingOffTheBoardAction.ts b/core/development/logic/actions/KillPieceByFallingOffTheBoardAction.ts index 9dc3ea0..0fb6565 100644 --- a/core/development/logic/actions/KillPieceByFallingOffTheBoardAction.ts +++ b/core/development/logic/actions/KillPieceByFallingOffTheBoardAction.ts @@ -1,8 +1,8 @@ import { VOID_BOARD_ID } from '../../Constants'; import { BasePiece } from '../pieces/abstract/BasePiece'; -import { KillPieceByEnvironment } from './KillPieceByEnvironmentAction'; +import { KillPieceByEnvironmentAction } from './KillPieceByEnvironmentAction'; import { ActionResult } from './types/ActionResult'; -export class KillPieceByFallingOffTheBoardAction extends KillPieceByEnvironment { +export class KillPieceByFallingOffTheBoardAction extends KillPieceByEnvironmentAction { constructor(killedPiece: BasePiece) { super(killedPiece, 'the void', killedPiece.position?.boardId); } diff --git a/core/development/logic/actions/TriggerPieceOnTrapAction.ts b/core/development/logic/actions/TriggerPieceOnTrapAction.ts index 95b1c8e..ce5e2b0 100644 --- a/core/development/logic/actions/TriggerPieceOnTrapAction.ts +++ b/core/development/logic/actions/TriggerPieceOnTrapAction.ts @@ -3,7 +3,7 @@ import { destroyItemOnBoard } from '../../LogicAdapter'; import { move } from '../PieceLogic'; import { BaseItem } from '../items/abstract/Item'; import { BasePiece } from '../pieces/abstract/BasePiece'; -import { KillPieceByEnvironment as KillPieceByEnvironmentAction } from './KillPieceByEnvironmentAction'; +import { KillPieceByEnvironmentAction as KillPieceByEnvironmentAction } from './KillPieceByEnvironmentAction'; import { GameAction } from './abstract/GameAction'; import { ActionResult } from './types/ActionResult'; diff --git a/jest.config.ts b/jest.config.ts index 86e6596..b2853e5 100644 --- a/jest.config.ts +++ b/jest.config.ts @@ -2,7 +2,6 @@ export default { displayName: 'chess-but-better', coverageDirectory: 'test-coverage', - coverageReporters: ['lcov'], preset: 'ts-jest', testEnvironment: 'node', testMatch: ['/core/**/*.test.ts'], From 2e247859304a4644b132448fdfaadaa4f2e32442 Mon Sep 17 00:00:00 2001 From: Ido-Barnea Date: Mon, 22 Apr 2024 10:45:49 +0300 Subject: [PATCH 7/8] Formatting --- ...KillPieceByFallingOffTheBoardAction.test.ts | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/core/development/logic/actions/KillPieceByFallingOffTheBoardAction.test.ts b/core/development/logic/actions/KillPieceByFallingOffTheBoardAction.test.ts index 7f80046..9f12734 100644 --- a/core/development/logic/actions/KillPieceByFallingOffTheBoardAction.test.ts +++ b/core/development/logic/actions/KillPieceByFallingOffTheBoardAction.test.ts @@ -42,9 +42,15 @@ game.getPlayersTurnSwitcher = jest.fn().mockReturnValue({ jest.mock('./KillPieceByEnvironmentAction', () => ({ __esModule: true, - KillPieceByEnvironmentAction: jest.fn().mockImplementation((killedPiece: BasePiece) => ({ - execute: jest.fn().mockReturnValueOnce(killedPiece.position ? ActionResult.SUCCESS : ActionResult.FAILURE), - })), + KillPieceByEnvironmentAction: jest + .fn() + .mockImplementation((killedPiece: BasePiece) => ({ + execute: jest + .fn() + .mockReturnValueOnce( + killedPiece.position ? ActionResult.SUCCESS : ActionResult.FAILURE, + ), + })), })); describe('KillPieceByFallingOffTheBoardAction', () => { @@ -55,7 +61,8 @@ describe('KillPieceByFallingOffTheBoardAction', () => { test('should return FAILURE if killedPiece.position is undefined', () => { // Arrange const killedPiece = new Pawn(whitePlayer, undefined); - const killPieceByFallingOffTheBoardAction = new KillPieceByFallingOffTheBoardAction(killedPiece); + const killPieceByFallingOffTheBoardAction = + new KillPieceByFallingOffTheBoardAction(killedPiece); // Act const actionResult = killPieceByFallingOffTheBoardAction.execute(); @@ -71,7 +78,8 @@ describe('KillPieceByFallingOffTheBoardAction', () => { boardId: OVERWORLD_BOARD_ID, }; const killedPiece = new Pawn(whitePlayer, initialPosition); - const killPieceByFallingOffTheBoardAction = new KillPieceByFallingOffTheBoardAction(killedPiece); + const killPieceByFallingOffTheBoardAction = + new KillPieceByFallingOffTheBoardAction(killedPiece); // Act const actionResult = killPieceByFallingOffTheBoardAction.execute(); From 51e75f1c99ee75b7e7f71ba46d6ceff4d9f15f17 Mon Sep 17 00:00:00 2001 From: Ido-Barnea Date: Mon, 22 Apr 2024 10:59:23 +0300 Subject: [PATCH 8/8] Wrote KillPieceByEnvironmentAction tests --- .../logic/actions/AttackPieceAction.test.ts | 6 +- .../logic/actions/CastleAction.test.ts | 6 +- .../logic/actions/KillPieceAction.test.ts | 6 +- .../KillPieceByEnvironmentAction.test.ts | 65 +++++++++++++++++++ ...illPieceByFallingOffTheBoardAction.test.ts | 6 +- 5 files changed, 73 insertions(+), 16 deletions(-) create mode 100644 core/development/logic/actions/KillPieceByEnvironmentAction.test.ts diff --git a/core/development/logic/actions/AttackPieceAction.test.ts b/core/development/logic/actions/AttackPieceAction.test.ts index 5ac3380..ec90b36 100644 --- a/core/development/logic/actions/AttackPieceAction.test.ts +++ b/core/development/logic/actions/AttackPieceAction.test.ts @@ -31,11 +31,9 @@ jest.mock('../../ui/InventoriesUI.ts', () => ({ })); jest.mock('../../ui/ShopUI.ts'); -const getCurrentPlayerMock = jest.fn().mockReturnValue(whitePlayer); -const getTurnsCount = jest.fn().mockReturnValue(1); game.getPlayersTurnSwitcher = jest.fn().mockReturnValue({ - getCurrentPlayer: getCurrentPlayerMock, - getTurnsCount: getTurnsCount, + getCurrentPlayer: jest.fn().mockReturnValue(whitePlayer), + getTurnsCount: jest.fn().mockReturnValue(1), }); describe('AttackPieceAction', () => { diff --git a/core/development/logic/actions/CastleAction.test.ts b/core/development/logic/actions/CastleAction.test.ts index 8c2829f..0e326f8 100644 --- a/core/development/logic/actions/CastleAction.test.ts +++ b/core/development/logic/actions/CastleAction.test.ts @@ -31,11 +31,9 @@ jest.mock('../../ui/InventoriesUI.ts', () => ({ })); jest.mock('../../ui/ShopUI.ts'); -const getCurrentPlayerMock = jest.fn().mockReturnValue(whitePlayer); -const getTurnsCount = jest.fn().mockReturnValue(1); game.getPlayersTurnSwitcher = jest.fn().mockReturnValue({ - getCurrentPlayer: getCurrentPlayerMock, - getTurnsCount: getTurnsCount, + getCurrentPlayer: jest.fn().mockReturnValue(whitePlayer), + getTurnsCount: jest.fn().mockReturnValue(1), }); describe('CastleAction', () => { diff --git a/core/development/logic/actions/KillPieceAction.test.ts b/core/development/logic/actions/KillPieceAction.test.ts index a1cc5c1..e79ef36 100644 --- a/core/development/logic/actions/KillPieceAction.test.ts +++ b/core/development/logic/actions/KillPieceAction.test.ts @@ -39,11 +39,9 @@ jest.mock('../../ui/InventoriesUI.ts', () => ({ })); jest.mock('../../ui/ShopUI.ts'); -const getCurrentPlayerMock = jest.fn().mockReturnValue(whitePlayer); -const getTurnsCount = jest.fn().mockReturnValue(1); game.getPlayersTurnSwitcher = jest.fn().mockReturnValue({ - getCurrentPlayer: getCurrentPlayerMock, - getTurnsCount: getTurnsCount, + getCurrentPlayer: jest.fn().mockReturnValue(whitePlayer), + getTurnsCount: jest.fn().mockReturnValue(1), }); jest.mock('./SpawnPieceInHellAction', () => ({ diff --git a/core/development/logic/actions/KillPieceByEnvironmentAction.test.ts b/core/development/logic/actions/KillPieceByEnvironmentAction.test.ts new file mode 100644 index 0000000..3bae870 --- /dev/null +++ b/core/development/logic/actions/KillPieceByEnvironmentAction.test.ts @@ -0,0 +1,65 @@ +import { game } from '../../Game'; +import { PlayerInventory } from '../inventory/PlayerInventory'; +import { Pawn } from '../pieces/Pawn'; +import { Player } from '../players/Player'; +import { PlayerColor } from '../players/types/PlayerColor'; +import { KillPieceByEnvironmentAction } from './KillPieceByEnvironmentAction'; +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), +}); +game.endMove = jest.fn(); + +jest.mock('./KillPieceAction', () => ({ + __esModule: true, + KillPieceAction: jest.fn().mockImplementation(() => ({ + execute: jest.fn().mockReturnValue(1), + })), +})); + +describe('KillPieceByEnvironmentAction', () => { + afterEach(() => { + jest.clearAllMocks(); + }); + + test('should return SUCCESS', () => { + // Arrange + const killedPiece = new Pawn(whitePlayer, undefined); + const killPieceByEnvironmentAction = new KillPieceByEnvironmentAction( + killedPiece, + 'cause', + killedPiece.position?.boardId, + ); + + // Act + const actionResult = killPieceByEnvironmentAction.execute(); + + // Assert + expect(actionResult).toEqual(ActionResult.SUCCESS); + }); +}); diff --git a/core/development/logic/actions/KillPieceByFallingOffTheBoardAction.test.ts b/core/development/logic/actions/KillPieceByFallingOffTheBoardAction.test.ts index 9f12734..ac0c8bf 100644 --- a/core/development/logic/actions/KillPieceByFallingOffTheBoardAction.test.ts +++ b/core/development/logic/actions/KillPieceByFallingOffTheBoardAction.test.ts @@ -33,11 +33,9 @@ jest.mock('../../ui/InventoriesUI.ts', () => ({ })); jest.mock('../../ui/ShopUI.ts'); -const getCurrentPlayerMock = jest.fn().mockReturnValue(whitePlayer); -const getTurnsCount = jest.fn().mockReturnValue(1); game.getPlayersTurnSwitcher = jest.fn().mockReturnValue({ - getCurrentPlayer: getCurrentPlayerMock, - getTurnsCount: getTurnsCount, + getCurrentPlayer: jest.fn().mockReturnValue(whitePlayer), + getTurnsCount: jest.fn().mockReturnValue(1), }); jest.mock('./KillPieceByEnvironmentAction', () => ({