Skip to content

Commit

Permalink
Unit Test Utils Added (#152)
Browse files Browse the repository at this point in the history
Added a new utils file for the unit tests to make checking for typos a lot easier.

Co-authored-by: KingPhilip14 <[email protected]>
  • Loading branch information
Shad0wW0lf and KingPhilip14 authored Mar 23, 2024
1 parent 47740f9 commit 631fa31
Show file tree
Hide file tree
Showing 13 changed files with 220 additions and 98 deletions.
16 changes: 9 additions & 7 deletions game/test_suite/tests/test_avatar.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from game.common.avatar import Avatar
from game.common.items.item import Item
from game.utils.vector import Vector
import game.test_suite.utils


class TestAvatar(unittest.TestCase):
Expand All @@ -15,6 +16,7 @@ class TestAvatar(unittest.TestCase):
def setUp(self) -> None:
self.avatar: Avatar = Avatar(None, 1)
self.item: Item = Item(10, 100, 1, 1)
self.utils = game.test_suite.utils

# test set item
def test_avatar_set_item(self):
Expand All @@ -25,9 +27,9 @@ def test_avatar_set_item_fail(self):
value: int = 3
with self.assertRaises(ValueError) as e:
self.avatar.held_item = value
self.assertEqual(str(e.exception), f'Avatar.held_item must be an Item or None. It is a(n) '
f'{value.__class__.__name__} and has the value of '
f'{value}')
self.assertTrue(self.utils.spell_check(str(e.exception), f'Avatar.held_item must be an Item or None. It '
f'is a(n) {value.__class__.__name__} and has the '
f'value of {value}', False))

# test set score
def test_avatar_set_score(self):
Expand All @@ -38,8 +40,8 @@ def test_avatar_set_score_fail(self):
value: str = 'wow'
with self.assertRaises(ValueError) as e:
self.avatar.score = value
self.assertEqual(str(e.exception), f'Avatar.score must be an int. It is a(n) '
f'{value.__class__.__name__} and has the value of {value}')
self.assertTrue(self.utils.spell_check(str(e.exception), f'Avatar.score must be an int. It is a(n) '
f'{value.__class__.__name__} and has the value of {value}', False))

# test set position
def test_avatar_set_position(self):
Expand All @@ -54,8 +56,8 @@ def test_avatar_set_position_fail(self):
value: int = 10
with self.assertRaises(ValueError) as e:
self.avatar.position = value
self.assertEqual(str(e.exception), f'Avatar.position must be a Vector or None. '
f'It is a(n) {value.__class__.__name__} and has the value of {value}')
self.assertTrue(self.utils.spell_check(str(e.exception), f'Avatar.position must be a Vector or None. '
f'It is a(n) {value.__class__.__name__} and has the value of {value}', False))

# test json method
def test_avatar_json_with_none_item(self):
Expand Down
22 changes: 15 additions & 7 deletions game/test_suite/tests/test_avatar_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from game.common.avatar import Avatar
from game.common.items.item import Item
import game.test_suite.utils


class TestAvatarInventory(unittest.TestCase):
Expand All @@ -16,6 +17,7 @@ class TestAvatarInventory(unittest.TestCase):
def setUp(self) -> None:
self.avatar: Avatar = Avatar(None, 1)
self.item: Item = Item(10, 100, 1, 1)
self.utils = game.test_suite.utils

# test set inventory
def test_avatar_set_inventory(self):
Expand All @@ -27,15 +29,18 @@ def test_avatar_set_inventory_fail_1(self):
value: str = 'Fail'
with self.assertRaises(ValueError) as e:
self.avatar.inventory = value
self.assertEqual(str(e.exception), f'Avatar.inventory must be a list of Items. It is a(n) {value.__class__.__name__} and has the value of {value}')
self.assertTrue(
self.utils.spell_check(str(e.exception), f'Avatar.inventory must be a list of Items. It is a(n) '
f'{value.__class__.__name__} and has the value of {value}', False))

# fails if inventory size is greater than the max_inventory_size
def test_avatar_set_inventory_fail_2(self):
value: list = [Item(1,1), Item(4,2)]
value: list = [Item(1, 1), Item(4, 2)]
with self.assertRaises(ValueError) as e:
self.avatar.inventory = value
self.assertEqual(str(e.exception), 'Avatar.inventory size must be less than or equal to '
f'max_inventory_size. It has the value of {len(value)}')
self.assertTrue(self.utils.spell_check(str(e.exception), 'Avatar.inventory size must be less than or equal to '
f'max_inventory_size. It has the value of {len(value)}',
False))

def test_avatar_set_max_inventory_size(self):
self.avatar.max_inventory_size = 10
Expand All @@ -45,7 +50,9 @@ def test_avatar_set_max_inventory_size_fail(self):
value: str = 'Fail'
with self.assertRaises(ValueError) as e:
self.avatar.max_inventory_size = value
self.assertEqual(str(e.exception), f'Avatar.max_inventory_size must be an int. It is a(n) {value.__class__.__name__} and has the value of {value}')
self.assertTrue(self.utils.spell_check(str(e.exception), f'Avatar.max_inventory_size must be an int. '
f'It is a(n) {value.__class__.__name__} and has the '
f'value of {value}', False))

# Tests picking up an item
def test_avatar_pick_up(self):
Expand Down Expand Up @@ -121,8 +128,9 @@ def test_take_fail(self):
value: str = 'wow'
with self.assertRaises(ValueError) as e:
taken = self.avatar.take(value)
self.assertEqual(str(e.exception), f'str.item must be an item.'
f' It is a(n) {value.__class__.__name__} with the value of {value}.')
self.assertTrue(self.utils.spell_check(str(e.exception), f'str.item must be an item.'
f' It is a(n) {value.__class__.__name__} with the '
f'value of {value}.', False))

# Tests picking up an item and failing
def test_avatar_pick_up_full_inventory(self):
Expand Down
20 changes: 12 additions & 8 deletions game/test_suite/tests/test_game_board.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
from game.common.items.item import Item
from game.common.stations.station import Station
from game.common.stations.occupiable_station import OccupiableStation
from game.common.map.tile import Tile
from game.common.map.wall import Wall
from game.utils.vector import Vector
from game.common.game_object import GameObject
from game.common.map.game_board import GameBoard

import game.test_suite.utils

class TestGameBoard(unittest.TestCase):
"""
Expand All @@ -36,36 +35,41 @@ def setUp(self) -> None:
}
self.game_board: GameBoard = GameBoard(1, Vector(10, 10), self.locations, False)
self.game_board.generate_map()

self.utils = game.test_suite.utils
# test that seed cannot be set after generate_map
def test_seed_fail(self):
with self.assertRaises(RuntimeError) as e:
self.game_board.seed = 20
self.assertEqual(str(e.exception), 'GameBoard variables cannot be changed once generate_map is run.')
self.assertTrue(self.utils.spell_check(str(e.exception), 'GameBoard variables cannot be changed once '
'generate_map is run.', False))

# test that map_size cannot be set after generate_map
def test_map_size_fail(self):
with self.assertRaises(RuntimeError) as e:
self.game_board.map_size = Vector(1, 1)
self.assertEqual(str(e.exception), 'GameBoard variables cannot be changed once generate_map is run.')
self.assertTrue(self.utils.spell_check(str(e.exception), 'GameBoard variables cannot be changed once '
'generate_map is run.', False))

# test that locations cannot be set after generate_map
def test_locations_fail(self):
with self.assertRaises(RuntimeError) as e:
self.game_board.locations = self.locations
self.assertEqual(str(e.exception), 'GameBoard variables cannot be changed once generate_map is run.')
self.assertTrue(self.utils.spell_check(str(e.exception), 'GameBoard variables cannot be changed once '
'generate_map is run.', False))

# test that locations raises RuntimeError even with incorrect data type
def test_locations_incorrect_fail(self):
with self.assertRaises(RuntimeError) as e:
self.game_board.locations = Vector(1, 1)
self.assertEqual(str(e.exception), 'GameBoard variables cannot be changed once generate_map is run.')
self.assertTrue(self.utils.spell_check(str(e.exception), 'GameBoard variables cannot be changed once '
'generate_map is run.', False))

# test that walled cannot be set after generate_map
def test_walled_fail(self):
with self.assertRaises(RuntimeError) as e:
self.game_board.walled = False
self.assertEqual(str(e.exception), 'GameBoard variables cannot be changed once generate_map is run.')
self.assertTrue(self.utils.spell_check(str(e.exception), 'GameBoard variables cannot be changed once '
'generate_map is run.', False))

# test that get_objects works correctly with stations
def test_get_objects_station(self):
Expand Down
33 changes: 19 additions & 14 deletions game/test_suite/tests/test_game_board_no_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
from game.common.items.item import Item
from game.common.stations.station import Station
from game.common.stations.occupiable_station import OccupiableStation
from game.common.map.tile import Tile
from game.common.map.wall import Wall
from game.utils.vector import Vector
from game.common.game_object import GameObject
from game.common.map.game_board import GameBoard
import game.test_suite.utils


class TestGameBoard(unittest.TestCase):
Expand All @@ -35,6 +35,7 @@ def setUp(self) -> None:
(Vector(5, 6),): [self.wall]
}
self.game_board: GameBoard = GameBoard(1, Vector(10, 10), self.locations, False)
self.utils = game.test_suite.utils

# test seed
def test_seed(self):
Expand All @@ -45,9 +46,10 @@ def test_seed_fail(self):
value: str = 'False'
with self.assertRaises(ValueError) as e:
self.game_board.seed = value
self.assertEqual(str(e.exception),
f'GameBoard.seed must be an int. '
f'It is a(n) {value.__class__.__name__} with the value of {value}.')
self.assertTrue(self.utils.spell_check(str(e.exception),
f'GameBoard.seed must be an int. '
f'It is a(n) {value.__class__.__name__} with the value of {value}.',
False))

# test map_size
def test_map_size(self):
Expand All @@ -58,9 +60,10 @@ def test_map_size_fail(self):
value: str = 'wow'
with self.assertRaises(ValueError) as e:
self.game_board.map_size = value
self.assertEqual(str(e.exception),
f'GameBoard.map_size must be a Vector.'
f' It is a(n) {value.__class__.__name__} with the value of {value}.')
self.assertTrue(self.utils.spell_check(str(e.exception),
f'GameBoard.map_size must be a Vector. '
f'It is a(n) {value.__class__.__name__} with the value of {value}.',
False))

# test locations
def test_locations(self):
Expand All @@ -77,10 +80,11 @@ def test_locations_fail_type(self):
value: str = 'wow'
with self.assertRaises(ValueError) as e:
self.game_board.locations = value
self.assertEqual(str(e.exception),
f'Locations must be a dict. The key must be a tuple of Vector Objects,'
f' and the value a list of GameObject. '
f'It is a(n) {value.__class__.__name__} with the value of {value}.')
self.assertTrue(self.utils.spell_check(str(e.exception),
f'Locations must be a dict. The key must be a tuple of Vector Objects, '
f'and the value a list of GameObject. '
f'It is a(n) {value.__class__.__name__} with the value of {value}.',
False))

# test walled
def test_walled(self):
Expand All @@ -91,9 +95,10 @@ def test_walled_fail(self):
value: str = 'wow'
with self.assertRaises(ValueError) as e:
self.game_board.walled = value
self.assertEqual(str(e.exception),
f'GameBoard.walled must be a bool.'
f' It is a(n) {value.__class__.__name__} with the value of {value}.')
self.assertTrue(self.utils.spell_check(str(e.exception),
f'GameBoard.walled must be a bool. '
f'It is a(n) {value.__class__.__name__} with the value of {value}.',
False))

# test json method
def test_game_board_json(self):
Expand Down
Loading

0 comments on commit 631fa31

Please sign in to comment.