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()