From 3d1b468bb21f789c7182b6672c7d8641a77f13f7 Mon Sep 17 00:00:00 2001 From: Monkey-bored Date: Sun, 28 Apr 2024 18:26:12 +0300 Subject: [PATCH 1/7] [#264] Added tests for friendly fire rule --- .../logic/rules/FriendlyFireRule.test.ts | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 core/development/logic/rules/FriendlyFireRule.test.ts diff --git a/core/development/logic/rules/FriendlyFireRule.test.ts b/core/development/logic/rules/FriendlyFireRule.test.ts new file mode 100644 index 00000000..7c2ec3f4 --- /dev/null +++ b/core/development/logic/rules/FriendlyFireRule.test.ts @@ -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"; +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('Valid - attack own color',() => { + // 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); + }); +}); \ No newline at end of file From 35b381ca12f36aba9bb86621e2ca95bcf4a71dcf Mon Sep 17 00:00:00 2001 From: Monkey-bored Date: Sun, 28 Apr 2024 18:36:55 +0300 Subject: [PATCH 2/7] [#264] Added Experience on kill test and minor fix --- .../logic/actions/AttackPieceAction.test.ts | 3 +- .../logic/rules/ExperienceOnKillRule.test.ts | 70 ++++++++++++ .../logic/rules/FriendlyFireRule.test.ts | 100 +++++++++--------- 3 files changed, 123 insertions(+), 50 deletions(-) create mode 100644 core/development/logic/rules/ExperienceOnKillRule.test.ts diff --git a/core/development/logic/actions/AttackPieceAction.test.ts b/core/development/logic/actions/AttackPieceAction.test.ts index ec90b363..71304858 100644 --- a/core/development/logic/actions/AttackPieceAction.test.ts +++ b/core/development/logic/actions/AttackPieceAction.test.ts @@ -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(), @@ -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); diff --git a/core/development/logic/rules/ExperienceOnKillRule.test.ts b/core/development/logic/rules/ExperienceOnKillRule.test.ts new file mode 100644 index 00000000..325cde4e --- /dev/null +++ b/core/development/logic/rules/ExperienceOnKillRule.test.ts @@ -0,0 +1,70 @@ +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()); +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('Friendly fire rule', () => { + afterEach(() => { + jest.clearAllMocks(); + }); + + test('Valid - gain xp 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); + }); +}); diff --git a/core/development/logic/rules/FriendlyFireRule.test.ts b/core/development/logic/rules/FriendlyFireRule.test.ts index 7c2ec3f4..79fdda78 100644 --- a/core/development/logic/rules/FriendlyFireRule.test.ts +++ b/core/development/logic/rules/FriendlyFireRule.test.ts @@ -1,69 +1,71 @@ -import { game } from "../../Game"; -import { PlayerInventory } from "../inventory/PlayerInventory"; -import { Queen } from "../pieces/Queen"; -import { PlayerColor } from "../players/types/PlayerColor"; +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"; +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(), + 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(), + 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(), + 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), + getCurrentPlayer: jest.fn().mockReturnValue(whitePlayer), + getTurnsCount: jest.fn().mockReturnValue(1), }); describe('Friendly fire rule', () => { - afterEach(() => { - jest.clearAllMocks(); - }); + afterEach(() => { + jest.clearAllMocks(); + }); + + test('Valid - attack own color', () => { + // Arrange + const initialKillerPiecePosition: Position = { + coordinates: [0, 0], + boardId: OVERWORLD_BOARD_ID, + }; + const killerPiece = new Queen(whitePlayer, initialKillerPiecePosition); - test('Valid - attack own color',() => { - // 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 initialKilledPiecePosition: Position = { - coordinates: [1, 0], - boardId: OVERWORLD_BOARD_ID, - }; - const killedPiece = new Pawn(whitePlayer, initialKilledPiecePosition); + const attackTeammateAction = new AttackPieceAction( + killerPiece, + killedPiece, + ); - const attackTeammateAction = new AttackPieceAction(killerPiece, killedPiece); - - // Act - const actionResult = attackTeammateAction.execute(); - - // Assert - expect(actionResult).toEqual(ActionResult.SUCCESS); - }); -}); \ No newline at end of file + // Act + const actionResult = attackTeammateAction.execute(); + + // Assert + expect(actionResult).toEqual(ActionResult.SUCCESS); + }); +}); From 5de0641d57183768926ee9bdab57aafc198029b4 Mon Sep 17 00:00:00 2001 From: Monkey-bored Date: Sun, 28 Apr 2024 18:42:50 +0300 Subject: [PATCH 3/7] [#264] minor format fixes --- core/development/logic/items/PiggyBank.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/development/logic/items/PiggyBank.test.ts b/core/development/logic/items/PiggyBank.test.ts index 55a3c625..e0ed9c16 100644 --- a/core/development/logic/items/PiggyBank.test.ts +++ b/core/development/logic/items/PiggyBank.test.ts @@ -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 player\'s gold if position is valid', () => { // Arrange const initialPiecePosition: Position = { coordinates: [0, 0], From 82f427c0e50abe17df6c1ba8eb66fe78ab298ae4 Mon Sep 17 00:00:00 2001 From: Monkey-bored Date: Sun, 28 Apr 2024 18:45:13 +0300 Subject: [PATCH 4/7] [#264] Removed unused import --- core/development/logic/rules/ExperienceOnKillRule.test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/core/development/logic/rules/ExperienceOnKillRule.test.ts b/core/development/logic/rules/ExperienceOnKillRule.test.ts index 325cde4e..ef53351c 100644 --- a/core/development/logic/rules/ExperienceOnKillRule.test.ts +++ b/core/development/logic/rules/ExperienceOnKillRule.test.ts @@ -7,7 +7,6 @@ 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()); const blackPlayer = new Player(PlayerColor.BLACK, new PlayerInventory()); From fce615492a30076d8d352380e79424f93d553508 Mon Sep 17 00:00:00 2001 From: Monkey-bored Date: Sun, 28 Apr 2024 19:02:53 +0300 Subject: [PATCH 5/7] [trivial] minor fixes --- core/development/logic/items/PiggyBank.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/development/logic/items/PiggyBank.test.ts b/core/development/logic/items/PiggyBank.test.ts index e0ed9c16..d0fbffc7 100644 --- a/core/development/logic/items/PiggyBank.test.ts +++ b/core/development/logic/items/PiggyBank.test.ts @@ -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], From f286a37c4cd655f2619cde5af88ee8243147f3fd Mon Sep 17 00:00:00 2001 From: Monkey-bored Date: Sun, 28 Apr 2024 19:09:54 +0300 Subject: [PATCH 6/7] [trivial] refactoring --- core/development/logic/actions/AttackPieceAction.test.ts | 6 +++--- core/development/logic/rules/ExperienceOnKillRule.test.ts | 2 +- core/development/logic/rules/FriendlyFireRule.test.ts | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/core/development/logic/actions/AttackPieceAction.test.ts b/core/development/logic/actions/AttackPieceAction.test.ts index 71304858..806e5f07 100644 --- a/core/development/logic/actions/AttackPieceAction.test.ts +++ b/core/development/logic/actions/AttackPieceAction.test.ts @@ -42,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); @@ -55,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], @@ -77,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], diff --git a/core/development/logic/rules/ExperienceOnKillRule.test.ts b/core/development/logic/rules/ExperienceOnKillRule.test.ts index ef53351c..1f0d103d 100644 --- a/core/development/logic/rules/ExperienceOnKillRule.test.ts +++ b/core/development/logic/rules/ExperienceOnKillRule.test.ts @@ -43,7 +43,7 @@ describe('Friendly fire rule', () => { jest.clearAllMocks(); }); - test('Valid - gain xp from kill', () => { + test('should return SUCCESS if xp gained from kill', () => { // Arrange const initialKillerPiecePosition: Position = { coordinates: [0, 0], diff --git a/core/development/logic/rules/FriendlyFireRule.test.ts b/core/development/logic/rules/FriendlyFireRule.test.ts index 79fdda78..ae1cf6df 100644 --- a/core/development/logic/rules/FriendlyFireRule.test.ts +++ b/core/development/logic/rules/FriendlyFireRule.test.ts @@ -43,7 +43,7 @@ describe('Friendly fire rule', () => { jest.clearAllMocks(); }); - test('Valid - attack own color', () => { + test('should return SUCCESS if killing a piece of the same color is valid', () => { // Arrange const initialKillerPiecePosition: Position = { coordinates: [0, 0], From b58b7adf5f972b8f896f5953a06f3e37ce3a9e28 Mon Sep 17 00:00:00 2001 From: Monkey-bored Date: Sun, 28 Apr 2024 23:02:39 +0300 Subject: [PATCH 7/7] [trivial] fixed tests describe line --- core/development/logic/rules/ExperienceOnKillRule.test.ts | 2 +- core/development/logic/rules/FriendlyFireRule.test.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/core/development/logic/rules/ExperienceOnKillRule.test.ts b/core/development/logic/rules/ExperienceOnKillRule.test.ts index 1f0d103d..e3e04a63 100644 --- a/core/development/logic/rules/ExperienceOnKillRule.test.ts +++ b/core/development/logic/rules/ExperienceOnKillRule.test.ts @@ -38,7 +38,7 @@ game.getPlayersTurnSwitcher = jest.fn().mockReturnValue({ getTurnsCount: jest.fn().mockReturnValue(1), }); -describe('Friendly fire rule', () => { +describe('Experience On Kill Rule', () => { afterEach(() => { jest.clearAllMocks(); }); diff --git a/core/development/logic/rules/FriendlyFireRule.test.ts b/core/development/logic/rules/FriendlyFireRule.test.ts index ae1cf6df..03228542 100644 --- a/core/development/logic/rules/FriendlyFireRule.test.ts +++ b/core/development/logic/rules/FriendlyFireRule.test.ts @@ -38,7 +38,7 @@ game.getPlayersTurnSwitcher = jest.fn().mockReturnValue({ getTurnsCount: jest.fn().mockReturnValue(1), }); -describe('Friendly fire rule', () => { +describe('Friendly Fire Rule', () => { afterEach(() => { jest.clearAllMocks(); });