def test_parse_unknown(self): input_1 = "Nothing to see here!" output_1 = parser.parse_command(input_1) expected_output_1 = {"type": "other", "processed": False} self.assertEqual(output_1, expected_output_1) input_2 = "This isn't something that we should match on..." output_2 = parser.parse_command(input_2) expected_output_2 = {"type": "other", "processed": False} self.assertEqual(output_2, expected_output_2)
def test_parse_feature_action(self): input_1 = "look at the leanto" output_1 = parser.parse_command(input_1) expected_output_1 = { "type": "feature_action", "command": { "action": "look at", "feature": "leanto" }, "processed": True } self.assertEqual(output_1, expected_output_1) input_2 = "search safe" output_2 = parser.parse_command(input_2) expected_output_2 = { "type": "feature_action", "command": { "action": "search", "feature": "locked safe" }, "processed": True } self.assertEqual(output_2, expected_output_2) input_3 = "search campfire" output_3 = parser.parse_command(input_3) expected_output_3 = { "type": "feature_action", "command": { "action": "search", "feature": "campfire pit" }, "processed": True } self.assertEqual(output_3, expected_output_3) input_4 = "look at snow-capped island" output_4 = parser.parse_command(input_4) expected_output_4 = { "type": "feature_action", "command": { "action": "look at", "feature": "snow capped island" }, "processed": True } self.assertEqual(output_4, expected_output_4)
def input_cycle(self): """The input cycle continues to request input until we have a valid processed command """ processed_command = None while True: self.validate_curses() if (processed_command is not None and processed_command['processed'] == False ): text = "Sorry I did not understand that.\n " + DO_WHAT else: text = DO_WHAT if RANDOM_TESTER: userInput = random_input_tester() else: userInput = self.input_handler(text) userInput = self.check_save_load_quit(userInput) #if we have no userInput skip to asking and check_save_load_quit again if userInput == None: continue processed_command = parse.parse_command(userInput) #line below for testing if DEBUG_PARSE: print json.dumps(processed_command, indent=4) if processed_command['processed'] == True: return processed_command
def test_process_feature_only_runs(self): """ Tests that the process_parsed_command method does not crash if the input command is any of the features that the game recognizes. NOTE: This test does not check for the proper functioning of the process_parsed_command method beyond its ability to take the input without crashing. """ # Get all the features that the game should recognize. data_dir = os.path.abspath('data') features_full_path = os.path.join(data_dir, FEATURES_FILENAME) with open(features_full_path, "r") as features_file: features_dict_str = features_file.read() features_dict = json.loads(features_dict_str) # Parse each feature command, put the parsed results into the format # the game engine should expect, and run process_parsed_command(). for feature in features_dict: print "TESTING COMMAND: " + feature processed_command = parser.parse_command(feature) output_type = processed_command["type"] title = None action = None top_level = ["item", "room", "feature"] for word in top_level: if word in processed_command['command']: title = processed_command['command'][word] if "action" in processed_command['command']: action = processed_command['command']['action'] res = self.game.process_parsed_command(output_type, title, action) if res: self.game.post_process(res)
def test_process_room_only_runs(self): """ Tests that the pprocess_parsed_command method does not crash if the input command is any of the rooms that the game recognizes (or a cardinal direction). NOTE: This test does not check for the proper functioning of the process_parsed_command method beyond its ability to take the input without crashing. """ # Get all the rooms that the game should recognize. data_dir = os.path.abspath('data') rooms_full_path = os.path.join(data_dir, ROOMS_FILENAME) with open(rooms_full_path, "r") as rooms_file: rooms_dict_str = rooms_file.read() rooms_dict = json.loads(rooms_dict_str) # Add the cardinal directions to the rooms dict rooms_dict["north"] = "north" rooms_dict["east"] = "east" rooms_dict["south"] = "south" rooms_dict["west"] = "west" for room in rooms_dict: print "TESTING COMMAND: " + room processed_command = parser.parse_command(room) output_type = processed_command["type"] title = None action = None top_level = ["item", "room", "feature"] for word in top_level: if word in processed_command['command']: title = processed_command['command'][word] if "action" in processed_command['command']: action = processed_command['command']['action'] res = self.game.process_parsed_command(output_type, title, action) if res: self.game.post_process(res)
def test_room_only(self): input_1 = "South" output_1 = parser.parse_command(input_1) expected_output_1 = { "type": "room_only", "command": { "room": "south" }, "processed": True } self.assertEqual(output_1, expected_output_1) input_2 = "Game trail!" output_2 = parser.parse_command(input_2) expected_output_2 = { "type": "room_only", "command": { "room": "game trail" }, "processed": True }
def test_parse_item_only(self): input_1 = "lantern" output_1 = parser.parse_command(input_1) expected_output_1 = { "type": "item_only", "command": { "item": "lantern" }, "processed": True } self.assertEqual(output_1, expected_output_1) input_2 = "map" output_2 = parser.parse_command(input_2) expected_output_2 = { "type": "item_only", "command": { "item": "old map" }, "processed": True } self.assertEqual(output_2, expected_output_2)
def test_parse_feature_only(self): input_1 = "deer" output_1 = parser.parse_command(input_1) expected_output_1 = { "type": "feature_only", "command": { "feature": "deer carcass" }, "processed": True } self.assertEqual(output_1, expected_output_1) input_2 = "island" output_2 = parser.parse_command(input_2) expected_output_2 = { "type": "feature_only", "command": { "feature": "snow capped island" }, "processed": True } self.assertEqual(output_2, expected_output_2)
def test_parse_action_only(self): input_1 = "move" output_1 = parser.parse_command(input_1) expected_output_1 = { "type": "action_only", "command": { "action": "go" }, "processed": True } self.assertEqual(output_1, expected_output_1) input_2 = "eat" output_2 = parser.parse_command(input_2) expected_output_2 = { "type": "action_only", "command": { "action": "eat" }, "processed": True } self.assertEqual(output_2, expected_output_2)
def test_room_action(self): input_1 = "search the mountain summit" output_1 = parser.parse_command(input_1) expected_output_1 = { "type": "room_action", "command": { "room": "mountain summit", "action": "search" }, "processed": True } self.assertEqual(output_1, expected_output_1) input_2 = "look at the stream" output_2 = parser.parse_command(input_2) expected_output_2 = { "type": "room_action", "command": { "room": "river", "action": "look at" }, "processed": True } self.assertEqual(output_2, expected_output_2)
def test_parse_item_action(self): input_1 = "search the medical kit" output_1 = parser.parse_command(input_1) expected_output_1 = { "type": "item_action", "command": { "item": "medical kit", "action": "search" }, "processed": True } self.assertEqual(output_1, expected_output_1) input_2 = "eat a candy bar" output_2 = parser.parse_command(input_2) expected_output_2 = { "type": "item_action", "command": { "item": "candy bar", "action": "eat" }, "processed": True } self.assertEqual(output_2, expected_output_2)
def processInput(test_user_command_1): processed_user_command = parse.parse_command(test_user_command_1) processed = processed_user_command["other"]["processed"] if processed: output_type = processed_user_command["type"] if output_type == "item_action": item = processed_user_command["item"]["name"] action = processed_user_command["item"]["action"] #process_item_action(item, action) elif output_type == "action_only": action = processed_user_command["general"]["action"] #process_action_only(action) elif output_type == "room_action": room = processed_user_command["room"]["name"] action = processed_user_command["room"]["action"] #process_room_action(room, action) else: "Error command type not supported yet." else: print "I'm sorry. I didn't understand that command. Please enter a different command."
def test_process_item_action_runs(self): """ Tests that the process_parsed_command method does not crash if the input command is any combination of an action and an item that the game recognizes, separated by a space. NOTE: This test does not check for the proper functioning of the process_parsed_command method beyond its ability to take the input without crashing. """ # Get all the actions that the game should recognize. data_dir = os.path.abspath('data') verbs_full_path = os.path.join(data_dir, VERBS_FILENAME) with open(verbs_full_path, "r") as verbs_file: verbs_dict_str = verbs_file.read() verbs_dict = json.loads(verbs_dict_str) # Get all the features that the game should recognize. data_dir = os.path.abspath('data') items_full_path = os.path.join(data_dir, ITEMS_FILENAME) with open(items_full_path, "r") as items_file: items_dict_str = items_file.read() items_dict = json.loads(items_dict_str) for action in verbs_dict: for item in items_dict: combined_command = action + ' ' + item print "TESTING COMMAND: " + combined_command processed_command = parser.parse_command(combined_command) output_type = processed_command["type"] title = None action = None top_level = ["item", "room", "feature"] for word in top_level: if word in processed_command['command']: title = processed_command['command'][word] if "action" in processed_command['command']: action = processed_command['command']['action'] res = self.game.process_parsed_command(output_type, title, action) if res: self.game.post_process(res)
""" A program for testing methods in the command_parser module """ import json import language_parser.command_parser as parse # # test_input = "This is A TEST string!!!!" # # print "The test input is: " + test_input # # print "The test output after calling _preprocess is: " + parse._preprocess(test_input) test_input = "paddle" print "The test input is: " + test_input print "The parsed command output is:" print parse.parse_command(test_input) print "" test_input = "edge of lake" print "The test input is: " + test_input print "The parsed command output is:" print parse.parse_command(test_input) print "" # test_input = "the paddle a dog these geese thesis statement these" # print "The test input is: " + test_input # print "The output without noise words is: " + parse._remove_noise(test_input) test_input = "eat oar" print "The test input is: " + test_input print "Calling parse command on it..."