From 84fa2b14ef3de3df6f8f1bb08cab3d8bf68d8ba7 Mon Sep 17 00:00:00 2001 From: KingPhilip14 Date: Sat, 29 Apr 2023 00:25:10 -0500 Subject: [PATCH 1/2] Created Inventory Controller Tests Inventory Controller is now tested. --- .../tests/test_inventory_controller.py | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 game/test_suite/tests/test_inventory_controller.py diff --git a/game/test_suite/tests/test_inventory_controller.py b/game/test_suite/tests/test_inventory_controller.py new file mode 100644 index 0000000..c0bc160 --- /dev/null +++ b/game/test_suite/tests/test_inventory_controller.py @@ -0,0 +1,95 @@ +import unittest + +from game.common.enums import * +from game.common.avatar import Avatar +from game.common.player import Player +from game.common.items.item import Item +from game.controllers.inventory_controller import InventoryController +from game.common.map.game_board import GameBoard + + +class TestInventoryController(unittest.TestCase): + """ + This class is to test the InventoryController class and its methods. + """ + + def setUp(self) -> None: + self.inventory_controller: InventoryController = InventoryController() + self.item: Item = Item() + self.avatar: Avatar = Avatar() + + self.inventory: [Item] = [Item(1), Item(2), Item(3), Item(4), Item(5), Item(6), Item(7), Item(8), + Item(9), Item(10)] + + self.avatar.inventory = self.inventory + self.player: Player = Player(avatar=self.avatar) + self.game_board: GameBoard = GameBoard() + + # Testing accessing the right Item with the controller + def test_select_slot_0(self): + self.inventory_controller.handle_actions(ActionType.SELECT_SLOT_0, self.player, self.game_board) + self.assertEqual(self.avatar.held_item, self.inventory[0]) + self.check_inventory_item() + + def test_select_slot_1(self): + self.inventory_controller.handle_actions(ActionType.SELECT_SLOT_1, self.player, self.game_board) + self.assertEqual(self.avatar.held_item, self.inventory[1]) + self.check_inventory_item() + + def test_select_slot_2(self): + self.inventory_controller.handle_actions(ActionType.SELECT_SLOT_2, self.player, self.game_board) + self.assertEqual(self.avatar.held_item, self.inventory[2]) + self.check_inventory_item() + + def test_select_slot_3(self): + self.inventory_controller.handle_actions(ActionType.SELECT_SLOT_3, self.player, self.game_board) + self.assertEqual(self.avatar.held_item, self.inventory[3]) + self.check_inventory_item() + + def test_select_slot_4(self): + self.inventory_controller.handle_actions(ActionType.SELECT_SLOT_4, self.player, self.game_board) + self.assertEqual(self.avatar.held_item, self.inventory[4]) + self.check_inventory_item() + + def test_select_slot_5(self): + self.inventory_controller.handle_actions(ActionType.SELECT_SLOT_5, self.player, self.game_board) + self.assertEqual(self.avatar.held_item, self.inventory[5]) + self.check_inventory_item() + + def test_select_slot_6(self): + self.inventory_controller.handle_actions(ActionType.SELECT_SLOT_6, self.player, self.game_board) + self.assertEqual(self.avatar.held_item, self.inventory[6]) + self.check_inventory_item() + + def test_select_slot_7(self): + self.inventory_controller.handle_actions(ActionType.SELECT_SLOT_7, self.player, self.game_board) + self.assertEqual(self.avatar.held_item, self.inventory[7]) + self.check_inventory_item() + + def test_select_slot_8(self): + self.inventory_controller.handle_actions(ActionType.SELECT_SLOT_8, self.player, self.game_board) + self.assertEqual(self.avatar.held_item, self.inventory[8]) + self.check_inventory_item() + + def test_select_slot_9(self): + self.inventory_controller.handle_actions(ActionType.SELECT_SLOT_9, self.player, self.game_board) + self.assertEqual(self.avatar.held_item, self.inventory[9]) + self.check_inventory_item() + + def test_with_wrong_action_type(self): + self.inventory_controller.handle_actions(ActionType.MOVE_LEFT, self.player, self.game_board) + self.assertEqual(self.avatar.held_item, None) + self.check_inventory_item() + + def check_inventory_item(self): + # Test to make sure that the inventory hasn't shift items + self.assertEqual(self.avatar.inventory[0].value, 1) + self.assertEqual(self.avatar.inventory[1].value, 2) + self.assertEqual(self.avatar.inventory[2].value, 3) + self.assertEqual(self.avatar.inventory[3].value, 4) + self.assertEqual(self.avatar.inventory[4].value, 5) + self.assertEqual(self.avatar.inventory[5].value, 6) + self.assertEqual(self.avatar.inventory[6].value, 7) + self.assertEqual(self.avatar.inventory[7].value, 8) + self.assertEqual(self.avatar.inventory[8].value, 9) + self.assertEqual(self.avatar.inventory[9].value, 10) From 3324d4f93b2c8c8c0968d1fb38cbccdfaf674a7d Mon Sep 17 00:00:00 2001 From: KingPhilip14 Date: Fri, 5 May 2023 22:03:53 -0500 Subject: [PATCH 2/2] Added to inventory controller Added a new test to inventory controller tests to throw error when trying to access an index out of bounds. --- game/controllers/inventory_controller.py | 2 +- game/test_suite/tests/test_inventory_controller.py | 13 ++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/game/controllers/inventory_controller.py b/game/controllers/inventory_controller.py index 8529d4f..de02b69 100644 --- a/game/controllers/inventory_controller.py +++ b/game/controllers/inventory_controller.py @@ -30,6 +30,6 @@ def handle_actions(self, action: ActionType, client: Player, world: GameBoard) - try: avatar.held_item = avatar.inventory[index] except IndexError: - raise IndexError(f'The given action type, {action}, is not within bounds of the given inventory of ' + raise IndexError(f'The given action type, {action.name}, is not within bounds of the given inventory of ' f'size {len(avatar.inventory)}. Select an ActionType enum that will be within the ' f'inventory\'s bounds.') diff --git a/game/test_suite/tests/test_inventory_controller.py b/game/test_suite/tests/test_inventory_controller.py index c0bc160..49b8025 100644 --- a/game/test_suite/tests/test_inventory_controller.py +++ b/game/test_suite/tests/test_inventory_controller.py @@ -16,7 +16,7 @@ class TestInventoryController(unittest.TestCase): def setUp(self) -> None: self.inventory_controller: InventoryController = InventoryController() self.item: Item = Item() - self.avatar: Avatar = Avatar() + self.avatar: Avatar = Avatar(max_inventory_size=10) self.inventory: [Item] = [Item(1), Item(2), Item(3), Item(4), Item(5), Item(6), Item(7), Item(8), Item(9), Item(10)] @@ -81,6 +81,17 @@ def test_with_wrong_action_type(self): self.assertEqual(self.avatar.held_item, None) self.check_inventory_item() + def test_with_out_of_bounds(self): + with self.assertRaises(IndexError) as e: + self.inventory: [Item] = [Item(1), Item(2), Item(3), Item(4), Item(5)] + self.avatar.max_inventory_size = 5 + self.avatar.inventory = self.inventory + self.player: Player = Player(avatar=self.avatar) + self.inventory_controller.handle_actions(ActionType.SELECT_SLOT_9, self.player, self.game_board) + self.assertEqual(str(e.exception), 'The given action type, SELECT_SLOT_9, is not within bounds of the given ' + 'inventory of size 5. Select an ActionType enum ' + 'that will be within the inventory\'s bounds.') + def check_inventory_item(self): # Test to make sure that the inventory hasn't shift items self.assertEqual(self.avatar.inventory[0].value, 1)