|
| 1 | +from test.bases import WorldTestBase |
| 2 | +from test.general import gen_steps, setup_multiworld |
| 3 | +from test.multiworld.test_multiworlds import MultiworldTestBase |
| 4 | +from typing import Any, ClassVar, Dict, Iterable, List, Mapping, Union, cast |
| 5 | + |
| 6 | +from BaseClasses import CollectionState, Entrance, Item, Location, Region |
| 7 | + |
| 8 | +from .. import WitnessWorld |
| 9 | + |
| 10 | + |
| 11 | +class WitnessTestBase(WorldTestBase): |
| 12 | + game = "The Witness" |
| 13 | + player: ClassVar[int] = 1 |
| 14 | + |
| 15 | + world: WitnessWorld |
| 16 | + |
| 17 | + def can_beat_game_with_items(self, items: Iterable[Item]) -> bool: |
| 18 | + """ |
| 19 | + Check that the items listed are enough to beat the game. |
| 20 | + """ |
| 21 | + |
| 22 | + state = CollectionState(self.multiworld) |
| 23 | + for item in items: |
| 24 | + state.collect(item) |
| 25 | + return state.multiworld.can_beat_game(state) |
| 26 | + |
| 27 | + def assert_dependency_on_event_item(self, spot: Union[Location, Region, Entrance], item_name: str) -> None: |
| 28 | + """ |
| 29 | + WorldTestBase.assertAccessDependency, but modified & simplified to work with event items |
| 30 | + """ |
| 31 | + event_items = [item for item in self.multiworld.get_items() if item.name == item_name] |
| 32 | + self.assertTrue(event_items, f"Event item {item_name} does not exist.") |
| 33 | + |
| 34 | + event_locations = [cast(Location, event_item.location) for event_item in event_items] |
| 35 | + |
| 36 | + # Checking for an access dependency on an event item requires a bit of extra work, |
| 37 | + # as state.remove forces a sweep, which will pick up the event item again right after we tried to remove it. |
| 38 | + # So, we temporarily set the access rules of the event locations to be impossible. |
| 39 | + original_rules = {event_location.name: event_location.access_rule for event_location in event_locations} |
| 40 | + for event_location in event_locations: |
| 41 | + event_location.access_rule = lambda _: False |
| 42 | + |
| 43 | + # We can't use self.assertAccessDependency here, it doesn't work for event items. (As of 2024-06-30) |
| 44 | + test_state = self.multiworld.get_all_state(False) |
| 45 | + |
| 46 | + self.assertFalse(spot.can_reach(test_state), f"{spot.name} is reachable without {item_name}") |
| 47 | + |
| 48 | + test_state.collect(event_items[0]) |
| 49 | + |
| 50 | + self.assertTrue(spot.can_reach(test_state), f"{spot.name} is not reachable despite having {item_name}") |
| 51 | + |
| 52 | + # Restore original access rules. |
| 53 | + for event_location in event_locations: |
| 54 | + event_location.access_rule = original_rules[event_location.name] |
| 55 | + |
| 56 | + def assert_location_exists(self, location_name: str, strict_check: bool = True) -> None: |
| 57 | + """ |
| 58 | + Assert that a location exists in this world. |
| 59 | + If strict_check, also make sure that this (non-event) location COULD exist. |
| 60 | + """ |
| 61 | + |
| 62 | + if strict_check: |
| 63 | + self.assertIn(location_name, self.world.location_name_to_id, f"Location {location_name} can never exist") |
| 64 | + |
| 65 | + try: |
| 66 | + self.world.get_location(location_name) |
| 67 | + except KeyError: |
| 68 | + self.fail(f"Location {location_name} does not exist.") |
| 69 | + |
| 70 | + def assert_location_does_not_exist(self, location_name: str, strict_check: bool = True) -> None: |
| 71 | + """ |
| 72 | + Assert that a location exists in this world. |
| 73 | + If strict_check, be explicit about whether the location could exist in the first place. |
| 74 | + """ |
| 75 | + |
| 76 | + if strict_check: |
| 77 | + self.assertIn(location_name, self.world.location_name_to_id, f"Location {location_name} can never exist") |
| 78 | + |
| 79 | + self.assertRaises( |
| 80 | + KeyError, |
| 81 | + lambda _: self.world.get_location(location_name), |
| 82 | + f"Location {location_name} exists, but is not supposed to.", |
| 83 | + ) |
| 84 | + |
| 85 | + def assert_can_beat_with_minimally(self, required_item_counts: Mapping[str, int]) -> None: |
| 86 | + """ |
| 87 | + Assert that the specified mapping of items is enough to beat the game, |
| 88 | + and that having one less of any item would result in the game being unbeatable. |
| 89 | + """ |
| 90 | + # Find the actual items |
| 91 | + found_items = [item for item in self.multiworld.get_items() if item.name in required_item_counts] |
| 92 | + actual_items: Dict[str, List[Item]] = {item_name: [] for item_name in required_item_counts} |
| 93 | + for item in found_items: |
| 94 | + if len(actual_items[item.name]) < required_item_counts[item.name]: |
| 95 | + actual_items[item.name].append(item) |
| 96 | + |
| 97 | + # Assert that enough items exist in the item pool to satisfy the specified required counts |
| 98 | + for item_name, item_objects in actual_items.items(): |
| 99 | + self.assertEqual( |
| 100 | + len(item_objects), |
| 101 | + required_item_counts[item_name], |
| 102 | + f"Couldn't find {required_item_counts[item_name]} copies of item {item_name} available in the pool, " |
| 103 | + f"only found {len(item_objects)}", |
| 104 | + ) |
| 105 | + |
| 106 | + # assert that multiworld is beatable with the items specified |
| 107 | + self.assertTrue( |
| 108 | + self.can_beat_game_with_items(item for items in actual_items.values() for item in items), |
| 109 | + f"Could not beat game with items: {required_item_counts}", |
| 110 | + ) |
| 111 | + |
| 112 | + # assert that one less copy of any item would result in the multiworld being unbeatable |
| 113 | + for item_name, item_objects in actual_items.items(): |
| 114 | + with self.subTest(f"Verify cannot beat game with one less copy of {item_name}"): |
| 115 | + removed_item = item_objects.pop() |
| 116 | + self.assertFalse( |
| 117 | + self.can_beat_game_with_items(item for items in actual_items.values() for item in items), |
| 118 | + f"Game was beatable despite having {len(item_objects)} copies of {item_name} " |
| 119 | + f"instead of the specified {required_item_counts[item_name]}", |
| 120 | + ) |
| 121 | + item_objects.append(removed_item) |
| 122 | + |
| 123 | + |
| 124 | +class WitnessMultiworldTestBase(MultiworldTestBase): |
| 125 | + options_per_world: List[Dict[str, Any]] |
| 126 | + common_options: Dict[str, Any] = {} |
| 127 | + |
| 128 | + def setUp(self) -> None: |
| 129 | + """ |
| 130 | + Set up a multiworld with multiple players, each using different options. |
| 131 | + """ |
| 132 | + |
| 133 | + self.multiworld = setup_multiworld([WitnessWorld] * len(self.options_per_world), ()) |
| 134 | + |
| 135 | + for world, options in zip(self.multiworld.worlds.values(), self.options_per_world): |
| 136 | + for option_name, option_value in {**self.common_options, **options}.items(): |
| 137 | + option = getattr(world.options, option_name) |
| 138 | + self.assertIsNotNone(option) |
| 139 | + |
| 140 | + option.value = option.from_any(option_value).value |
| 141 | + |
| 142 | + self.assertSteps(gen_steps) |
| 143 | + |
| 144 | + def collect_by_name(self, item_names: Union[str, Iterable[str]], player: int) -> List[Item]: |
| 145 | + """ |
| 146 | + Collect all copies of a specified item name (or list of item names) for a player in the multiworld item pool. |
| 147 | + """ |
| 148 | + |
| 149 | + items = self.get_items_by_name(item_names, player) |
| 150 | + for item in items: |
| 151 | + self.multiworld.state.collect(item) |
| 152 | + return items |
| 153 | + |
| 154 | + def get_items_by_name(self, item_names: Union[str, Iterable[str]], player: int) -> List[Item]: |
| 155 | + """ |
| 156 | + Return all copies of a specified item name (or list of item names) for a player in the multiworld item pool. |
| 157 | + """ |
| 158 | + |
| 159 | + if isinstance(item_names, str): |
| 160 | + item_names = (item_names,) |
| 161 | + return [item for item in self.multiworld.itempool if item.name in item_names and item.player == player] |
0 commit comments