def setup_commands(self): self.command_48 = Command(48, 0x0, [], [], [""], {}) self.command_49 = Command(49, 0x0, [], [], [""], {}) self.commands = { 48 : self.command_48, 49 : self.command_49, }
def test_resolve_teleport_with_arg(self): command = Command(1, 0x12, [], [], [""], {}) command.teleport_info[6] = self.lighthouse_location command.teleport_info[7] = self.beach_location response = self.resolver.resolve_teleport(command, self.player, "test") self.assertEqual((False, ["request_argless"], ["test"], []), response)
def test_resolve_teleport_with_matching_source(self): self.player.get_location_id.return_value = 6 command = Command(1, 0x12, [], [], [""], {}) command.teleport_info[6] = self.lighthouse_location command.teleport_info[7] = self.beach_location response = self.resolver.resolve_teleport(command, self.player) self.assertEqual((True, [], [], [self.lighthouse_location]), response)
def test_validate_command_non_teleport_with_teleport_info(self): self.commands_by_id.clear() take_command = Command(56, 0x0, [], [], ["take", "t"], {}) self.commands_by_id[56] = take_command take_command.teleport_info[6] = self.lighthouse_location validation = self.validator.validate(self.data_collection) self.assertEqual(1, len(validation)) validation_line = validation[0] self.assertEqual( "Command {0} \"{1}\" is not a teleport command, but teleport info has been given. This teleport info will be ignored.", validation_line.template) self.assertEqual(Severity.WARN, validation_line.severity) self.assertEqual((56, "take"), validation_line.args)
def test_resolve_switchable_with_switch_arg(self): switch_info = {"no" : False, "yes" : True} command = Command(1, 0x100, [ArgInfo(0x1)], [], ["verbose"], switch_info) response = self.resolver.resolve_switchable(command, self.player, "yes") self.assertEqual((True, [], [], [True]), response)
def test_resolve_switchable_without_arg(self): switch_info = {"no" : False, "yes" : True} command = Command(1, 0x100, [ArgInfo(0x0)], [], ["verbose"], switch_info) response = self.resolver.resolve_switchable(command, self.player) self.assertEqual((False, ["request_switch_command"], ["verbose", "no", "yes"], []), response)
def test_init_teleport_info_invalid_matching_location_ids(self): location_inputs = json.loads( "[ \ { \ \"data_id\": 9, \ \"attributes\": \"70F\", \ \"directions\": {}, \ \"labels\": { \ \"shortname\": \"Ward\", \ \"longname\": \"in a medical ward\", \ \"description\": \". The faint electric light is flickering on and off\", \ \"extended_descriptions\": [ \ \". There is no way out of this room\", \ \". There is a hole in the wall that you can escape through\" \ ] \ } \ } \ ]" ) command = Command(150, 0x0, [], [], ["do"], {}) teleport_infos = {command: {9: 9}} collection, validation = LocationParser().parse(location_inputs, teleport_infos) self.assertEqual(1, len(validation)) validation_line = validation[0] self.assertEqual("Source id and destination id {0} are the same for teleport command {1} \"{2}\".", validation_line.template) self.assertEqual(Severity.WARN, validation_line.severity) self.assertEqual((9, 150, "do"), validation_line.args)
def test_resolve_movement_back_without_destination(self): command = Command(5, 0x40, [ArgInfo(0x0)], [], [""], {}) self.player.get_previous_location.return_value = None response = self.resolver.resolve_movement(command, self.player) self.assertEqual((False, ["reject_movement_no_back"], [Direction.BACK], []), response)
def test_resolve_movement_non_back_non_out_without_destination(self): command = Command(34, 0x40, [ArgInfo(0x0)], [], [""], {}) self.player.get_adjacent_location.return_value = None response = self.resolver.resolve_movement(command, self.player) self.assertEqual((False, ["reject_movement_no_direction"], [Direction.NORTH], []), response)
def setup_commands(self): self.commands_by_id = {} self.commands_by_id[56] = Command(56, 0x0, [], [], ["take", "t"], {}) self.smash_command_ids = [] self.command_collection = CommandCollection({}, self.commands_by_id, "command_list", self.smash_command_ids)
def test_init_teleport_info_invalid_unknown_location_ids(self): location_inputs = json.loads( "[ \ { \ \"data_id\": 9, \ \"attributes\": \"70F\", \ \"directions\": {}, \ \"labels\": { \ \"shortname\": \"Ward\", \ \"longname\": \"in a medical ward\", \ \"description\": \". The faint electric light is flickering on and off\", \ \"extended_descriptions\": [ \ \". There is no way out of this room\", \ \". There is a hole in the wall that you can escape through\" \ ] \ } \ } \ ]" ) command = Command(150, 0x0, [], [], ["do"], {}) teleport_infos = {command: {111: 9, 9: 17}} collection, validation = LocationParser().parse(location_inputs, teleport_infos) self.assertEqual(2, len(validation)) validation_line_0 = validation[0] self.assertEqual("Unknown source location id {0} for teleport command {1} \"{2}\". This command will be unreachable.", validation_line_0.template) self.assertEqual(Severity.WARN, validation_line_0.severity) self.assertEqual((111, 150, "do"), validation_line_0.args) validation_line_1 = validation[1] self.assertEqual("Unknown destination location id {0} for teleport command {1} \"{2}\".", validation_line_1.template) self.assertEqual(Severity.ERROR, validation_line_1.severity) self.assertEqual((17, 150, "do"), validation_line_1.args)
def test_resolve_args_switching_command_not_switchable_item(self): command = Command(1, 0x208, [ArgInfo(0xF)], [], ["turn"], {}) self.player.get_carried_item.return_value = self.book self.player.get_nearby_item.return_value = None response = self.resolver.resolve_switching(command, self.player, "book") self.assertEqual((False, ["reject_no_understand_instruction"], ["book"], []), response)
def test_resolve_args_switching_command_switchable_item_valid_next_state(self): command = Command(1, 0x208, [ArgInfo(0xF), ArgInfo(0x0)], [], ["turn"], {}) self.player.get_carried_item.return_value = self.lamp self.player.get_nearby_item.return_value = None response = self.resolver.resolve_switching(command, self.player, "lamp", "off") self.assertEqual((True, [], [], [self.lamp, SwitchTransition.OFF]), response)
def test_resolve_args_switching_command_switchable_item_invalid_next_state(self): command = Command(1, 0x208, [ArgInfo(0xF)], [], ["turn"], {}) self.player.get_carried_item.return_value = self.lamp self.player.get_nearby_item.return_value = None response = self.resolver.resolve_switching(command, self.player, "lamp", "cinnamon") self.assertEqual((False, ["request_switch_item"], ["lamp", "off", "on"], []), response)
def test_resolve_movement_non_back_non_out_with_destination(self): command = Command(34, 0x40, [ArgInfo(0x0)], [], [""], {}) proposed_location = Mock() self.player.get_adjacent_location.return_value = proposed_location response = self.resolver.resolve_movement(command, self.player) self.assertEqual((True, [], [], [Direction.NORTH, proposed_location]), response)
def test_resolve_args_multiple_permissive_with_linker_explicit(self): command = Command(1, 0x9, [ArgInfo(0x7), ArgInfo(0xA, ["in"])], [], ["take"], {}) self.player.get_carried_item.return_value = None self.player.get_nearby_item.return_value = self.book response = self.resolver.resolve_args(command, self.player, "book", "in") self.assertEqual((False, ["request_addinfo"], ["take", " book in"], []), response)
def test_resolve_args_with_item_arg_known_needs_inventory_only_and_player_is_carrying(self): command = Command(1, 0x8, [ArgInfo(0xB)], [], ["drop"], {}) self.player.get_carried_item.return_value = self.book self.player.get_nearby_item.return_value = None response = self.resolver.resolve_args(command, self.player, "book") self.assertEqual((True, [], [], [self.book]), response)
def test_resolve_args_with_item_arg_known_needs_location_only_and_player_not_near(self): command = Command(1, 0x8, [ArgInfo(0x7)], [], ["take"], {}) self.player.get_carried_item.return_value = None self.player.get_nearby_item.return_value = None response = self.resolver.resolve_args(command, self.player, "book") self.assertEqual((False, ["reject_not_here"], ["book"], []), response)
def test_resolve_args_with_item_arg_known_needs_inventory_or_location_and_player_is_near(self): command = Command(1, 0x8, [ArgInfo(0xF)], [], ["describe"], {}) self.player.get_carried_item.return_value = None self.player.get_nearby_item.return_value = self.book response = self.resolver.resolve_args(command, self.player, "book") self.assertEqual((True, [], [], [self.book]), response)
def test_resolve_movement_back_with_destination(self): command = Command(5, 0x40, [ArgInfo(0x0)], [], [""], {}) proposed_location = Mock() self.player.get_previous_location.return_value = proposed_location response = self.resolver.resolve_movement(command, self.player) self.assertEqual((True, [], [], [Direction.BACK, proposed_location]), response)
def test_resolve_args_multiple_items_second_invalid(self): command = Command(1, 0x8, [ArgInfo(0xF), ArgInfo(0xF)], [], ["insert"], {}) self.player.get_carried_item.return_value = self.book self.player.get_nearby_item.return_value = None response = self.resolver.resolve_args(command, self.player, "book", "blah") self.assertEqual((False, ["reject_unknown"], ["blah"], []), response) self.player.reset_current_command.assert_called_once()
def setup_data(self): self.data = Mock() self.command = Command(150, 0x0, [], [], ["do"], {}) self.inventory = Mock() self.player_location = Mock() self.essential_drop_location = Mock() self.non_essential_drop_location = Mock() self.collectible_location = Mock() self.book = Mock()
def test_resolve_args_multiple_missing_arg_without_link_info(self): command = Command(1, 0x8, [ArgInfo(0xF), ArgInfo(0xF)], [], ["insert"], {}) self.player.get_carried_item.return_value = self.book self.player.get_nearby_item.return_value = None response = self.resolver.resolve_args(command, self.player, "book") self.assertEqual((False, ["request_addinfo"], ["insert", " book"], []), response) self.player.reset_current_command.assert_not_called()
def test_resolve_args_multiple_first_resolved(self): command = Command(1, 0x8, [ArgInfo(0xF), ArgInfo(0xF)], [], ["insert"], {}) self.player.get_current_args.return_value = [self.book] self.player.get_carried_item.return_value = self.box self.player.get_nearby_item.return_value = None response = self.resolver.resolve_args(command, self.player, "box") self.assertEqual((True, [], [], [self.book, self.box]), response)
def test_resolve_args_multiple_items_all_valid_with_valid_linker(self): command = Command(1, 0x8, [ArgInfo(0xF), ArgInfo(0xF, ["into", "in"])], [], ["insert"], {}) self.player.get_carried_item.side_effect = [self.book, self.box] self.player.get_nearby_item.return_value = None response = self.resolver.resolve_args(command, self.player, "book", "into", "box") self.assertEqual((True, [], [], [self.book, self.box]), response) self.player.reset_current_command.assert_called_once()
def test_run_movement(self): resolver_functions = [ self.arg_function_movement, self.vision_function, self.handler_function ] command_movement = Command(1, 0x49, 0x0, resolver_functions, [""], {}) result = self.runner.run(command_movement, None, ["test"]) self.assertEqual("Action \"1\" successful.", result)
def test_resolve_args_multiple_missing_arg_with_link_info_linker_explicit(self): arg_infos = [ArgInfo(0xF), ArgInfo(0xF, ["into", "in", "to"])] command = Command(1, 0x8, arg_infos, [], ["insert"], {}) self.player.get_carried_item.return_value = self.book self.player.get_nearby_item.return_value = None response = self.resolver.resolve_args(command, self.player, "book", "to") self.assertEqual((False, ["request_addinfo"], ["insert", " book to"], []), response) self.player.reset_current_command.assert_not_called()
def test_resolve_args_multiple_missing_arg_with_link_info_three_args(self): arg_infos = [ArgInfo(0xF), ArgInfo(0xF, ["into", "in", "to"]), ArgInfo(0xB, ["using"])] command = Command(1, 0x8, arg_infos, [], ["scoop"], {}) self.player.get_carried_item.side_effect = [self.salt, self.box] self.player.get_nearby_item.side_effect = [None, None] response = self.resolver.resolve_args(command, self.player, "salt", "box") self.assertEqual((False, ["request_addinfo"], ["scoop", " salt into box using"], []), response) self.player.reset_current_command.assert_not_called()
def test_run_unsuccessful(self): resolver_functions = [ self.arg_function_unsuccessful, self.vision_function, self.handler_function ] command_unsuccessful = Command(1, 0x9, 0x0, resolver_functions, [""], {}) result = self.runner.run(command_unsuccessful, None, ["test"]) self.assertEqual("Action unsuccessful.", result)
def test_run_item_args(self): resolver_functions = [ self.arg_function_non_movement, self.vision_function, self.handler_function ] command_non_movement = Command(1, 0x9, 0x0, resolver_functions, [""], {}) book = Item(1105, 2, Labels("book", "a book", "a book of fairytales"), 2, "The Pied Piper", {}) result = self.runner.run(command_non_movement, None, [book]) self.assertEqual("Action \"book\" successful.", result)