diff options
| author | mo khan <mo@mokhan.ca> | 2025-06-12 10:23:59 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2025-06-12 10:23:59 -0600 |
| commit | 709ded1030c6797c8bf3df5a97c5afe8d628393a (patch) | |
| tree | 15ddd2f0df11726c833ac0b08b36542593fc5d9f | |
| parent | 2cfab36d8471986ea8c0aeb54244e5e90bd63a48 (diff) | |
test: add tests
| -rw-r--r-- | Makefile | 12 | ||||
| -rw-r--r-- | test_adventure_game.py | 118 | ||||
| -rw-r--r-- | test_game_object.py | 28 |
3 files changed, 158 insertions, 0 deletions
@@ -4,3 +4,15 @@ setup: run: @mise use python@system @python AdventureGame.py + +test: + @mise use python@system + @python -m unittest discover -s . -p "test_*.py" -v + +test-gameobject: + @mise use python@system + @python -m unittest test_game_object -v + +test-adventure: + @mise use python@system + @python -m unittest test_adventure_game -v diff --git a/test_adventure_game.py b/test_adventure_game.py new file mode 100644 index 0000000..1fe9c4e --- /dev/null +++ b/test_adventure_game.py @@ -0,0 +1,118 @@ +import unittest +from unittest.mock import Mock, patch +import sys +import os + +sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) + +import AdventureGame +from GameObject import GameObject + +class TestAdventureGame(unittest.TestCase): + + def setUp(self): + AdventureGame.current_location = AdventureGame.FIRST_LOCATION + AdventureGame.end_of_game = False + AdventureGame.refresh_location = True + AdventureGame.refresh_objects_visible = True + + AdventureGame.sword = GameObject("sword", AdventureGame.FIRST_LOCATION, True, True, False, "Sword") + AdventureGame.shield = GameObject("shield", AdventureGame.FIRST_LOCATION, True, True, False, "Shield") + AdventureGame.bow = GameObject("bow", AdventureGame.FIRST_LOCATION, True, False, False, "The Magic Bow") + AdventureGame.game_objects = [AdventureGame.sword, AdventureGame.shield, AdventureGame.bow] + + def test_get_game_object_found(self): + result = AdventureGame.get_game_object("SWORD") + self.assertEqual(result, AdventureGame.sword) + + def test_get_game_object_not_found(self): + result = AdventureGame.get_game_object("NONEXISTENT") + self.assertIsNone(result) + + def test_location_navigation_north(self): + AdventureGame.current_location = AdventureGame.THIRD_LOCATION + result = AdventureGame.get_location_to_north() + self.assertEqual(result, AdventureGame.FIRST_LOCATION) + + AdventureGame.current_location = AdventureGame.FIRST_LOCATION + result = AdventureGame.get_location_to_north() + self.assertEqual(result, 0) + + def test_location_navigation_south(self): + AdventureGame.current_location = AdventureGame.FIRST_LOCATION + result = AdventureGame.get_location_to_south() + self.assertEqual(result, AdventureGame.THIRD_LOCATION) + + AdventureGame.current_location = AdventureGame.THIRD_LOCATION + result = AdventureGame.get_location_to_south() + self.assertEqual(result, 0) + + def test_location_navigation_east(self): + AdventureGame.current_location = AdventureGame.FIRST_LOCATION + result = AdventureGame.get_location_to_east() + self.assertEqual(result, AdventureGame.SECOND_LOCATION) + + AdventureGame.current_location = AdventureGame.SECOND_LOCATION + result = AdventureGame.get_location_to_east() + self.assertEqual(result, 0) + + def test_location_navigation_west(self): + AdventureGame.current_location = AdventureGame.SECOND_LOCATION + result = AdventureGame.get_location_to_west() + self.assertEqual(result, AdventureGame.FIRST_LOCATION) + + AdventureGame.current_location = AdventureGame.FIRST_LOCATION + result = AdventureGame.get_location_to_west() + self.assertEqual(result, 0) + + @patch('AdventureGame.print_to_description') + def test_perform_go_command_valid_direction(self, mock_print): + AdventureGame.current_location = AdventureGame.FIRST_LOCATION + AdventureGame.perform_go_command("EAST") + self.assertEqual(AdventureGame.current_location, AdventureGame.SECOND_LOCATION) + self.assertTrue(AdventureGame.refresh_location) + + @patch('AdventureGame.print_to_description') + def test_perform_go_command_invalid_direction(self, mock_print): + AdventureGame.current_location = AdventureGame.FIRST_LOCATION + original_location = AdventureGame.current_location + AdventureGame.perform_go_command("NORTH") + self.assertEqual(AdventureGame.current_location, original_location) + mock_print.assert_called_with("You can't go that way!") + + @patch('AdventureGame.print_to_description') + def test_perform_get_command_success(self, mock_print): + AdventureGame.current_location = AdventureGame.FIRST_LOCATION + AdventureGame.sword.location = AdventureGame.FIRST_LOCATION + AdventureGame.sword.visible = True + AdventureGame.sword.carried = False + AdventureGame.sword.movable = True + + AdventureGame.perform_get_command("SWORD") + + self.assertTrue(AdventureGame.sword.carried) + self.assertFalse(AdventureGame.sword.visible) + self.assertTrue(AdventureGame.refresh_objects_visible) + + @patch('AdventureGame.print_to_description') + def test_perform_get_command_not_here(self, mock_print): + AdventureGame.current_location = AdventureGame.FIRST_LOCATION + AdventureGame.sword.location = AdventureGame.SECOND_LOCATION + + AdventureGame.perform_get_command("SWORD") + mock_print.assert_called_with("You don't see one of those here!") + + @patch('AdventureGame.print_to_description') + def test_perform_put_command_success(self, mock_print): + AdventureGame.current_location = AdventureGame.FIRST_LOCATION + AdventureGame.sword.carried = True + + AdventureGame.perform_put_command("SWORD") + + self.assertEqual(AdventureGame.sword.location, AdventureGame.FIRST_LOCATION) + self.assertFalse(AdventureGame.sword.carried) + self.assertTrue(AdventureGame.sword.visible) + self.assertTrue(AdventureGame.refresh_objects_visible) + +if __name__ == '__main__': + unittest.main()
\ No newline at end of file diff --git a/test_game_object.py b/test_game_object.py new file mode 100644 index 0000000..3c4b30c --- /dev/null +++ b/test_game_object.py @@ -0,0 +1,28 @@ +import unittest +from GameObject import GameObject + +class TestGameObject(unittest.TestCase): + + def test_game_object_creation(self): + obj = GameObject("sword", 1, True, True, False, "A sharp sword") + + self.assertEqual(obj.name, "sword") + self.assertEqual(obj.location, 1) + self.assertTrue(obj.movable) + self.assertTrue(obj.visible) + self.assertFalse(obj.carried) + self.assertEqual(obj.description, "A sharp sword") + + def test_game_object_attributes_modification(self): + obj = GameObject("shield", 2, False, False, True, "A sturdy shield") + + obj.carried = False + obj.visible = True + obj.location = 3 + + self.assertFalse(obj.carried) + self.assertTrue(obj.visible) + self.assertEqual(obj.location, 3) + +if __name__ == '__main__': + unittest.main()
\ No newline at end of file |
