| 
									
										
										
										
											2023-02-26 19:19:15 -05:00
										 |  |  | import pathlib | 
					
						
							| 
									
										
										
										
											2022-12-11 13:15:23 +01:00
										 |  |  | import typing | 
					
						
							| 
									
										
										
										
											2020-04-29 01:34:30 +10:00
										 |  |  | import unittest | 
					
						
							| 
									
										
										
										
											2022-12-11 13:15:23 +01:00
										 |  |  | from argparse import Namespace | 
					
						
							| 
									
										
										
										
											2021-04-15 04:01:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | import Utils | 
					
						
							| 
									
										
										
										
											2022-12-11 13:15:23 +01:00
										 |  |  | from test.general import gen_steps | 
					
						
							|  |  |  | from worlds import AutoWorld | 
					
						
							|  |  |  | from worlds.AutoWorld import call_all | 
					
						
							| 
									
										
										
										
											2021-04-15 04:01:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | file_path = pathlib.Path(__file__).parent.parent | 
					
						
							|  |  |  | Utils.local_path.cached_path = file_path | 
					
						
							| 
									
										
										
										
											2020-04-29 01:34:30 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-11 13:15:23 +01:00
										 |  |  | from BaseClasses import MultiWorld, CollectionState, ItemClassification, Item | 
					
						
							| 
									
										
										
										
											2020-10-24 05:38:56 +02:00
										 |  |  | from worlds.alttp.Items import ItemFactory | 
					
						
							| 
									
										
										
										
											2020-04-29 01:34:30 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-06 06:09:15 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-29 01:34:30 +10:00
										 |  |  | class TestBase(unittest.TestCase): | 
					
						
							| 
									
										
										
										
											2022-10-31 21:41:21 -05:00
										 |  |  |     multiworld: MultiWorld | 
					
						
							| 
									
										
										
										
											2020-04-29 01:34:30 +10:00
										 |  |  |     _state_cache = {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def get_state(self, items): | 
					
						
							| 
									
										
										
										
											2022-10-31 21:41:21 -05:00
										 |  |  |         if (self.multiworld, tuple(items)) in self._state_cache: | 
					
						
							|  |  |  |             return self._state_cache[self.multiworld, tuple(items)] | 
					
						
							|  |  |  |         state = CollectionState(self.multiworld) | 
					
						
							| 
									
										
										
										
											2020-04-29 01:34:30 +10:00
										 |  |  |         for item in items: | 
					
						
							| 
									
										
										
										
											2022-06-17 03:23:27 +02:00
										 |  |  |             item.classification = ItemClassification.progression | 
					
						
							| 
									
										
										
										
											2020-04-29 01:34:30 +10:00
										 |  |  |             state.collect(item) | 
					
						
							|  |  |  |         state.sweep_for_events() | 
					
						
							| 
									
										
										
										
											2022-10-31 21:41:21 -05:00
										 |  |  |         self._state_cache[self.multiworld, tuple(items)] = state | 
					
						
							| 
									
										
										
										
											2020-04-29 01:34:30 +10:00
										 |  |  |         return state | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-02 11:24:17 +01:00
										 |  |  |     def get_path(self, state, region): | 
					
						
							|  |  |  |         def flist_to_iter(node): | 
					
						
							|  |  |  |             while node: | 
					
						
							|  |  |  |                 value, node = node | 
					
						
							|  |  |  |                 yield value | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         from itertools import zip_longest | 
					
						
							|  |  |  |         reversed_path_as_flist = state.path.get(region, (region, None)) | 
					
						
							|  |  |  |         string_path_flat = reversed(list(map(str, flist_to_iter(reversed_path_as_flist)))) | 
					
						
							|  |  |  |         # Now we combine the flat string list into (region, exit) pairs | 
					
						
							|  |  |  |         pathsiter = iter(string_path_flat) | 
					
						
							|  |  |  |         pathpairs = zip_longest(pathsiter, pathsiter) | 
					
						
							|  |  |  |         return list(pathpairs) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-29 01:34:30 +10:00
										 |  |  |     def run_location_tests(self, access_pool): | 
					
						
							| 
									
										
										
										
											2021-02-02 11:24:17 +01:00
										 |  |  |         for i, (location, access, *item_pool) in enumerate(access_pool): | 
					
						
							| 
									
										
										
										
											2020-04-29 01:34:30 +10:00
										 |  |  |             items = item_pool[0] | 
					
						
							|  |  |  |             all_except = item_pool[1] if len(item_pool) > 1 else None | 
					
						
							| 
									
										
										
										
											2021-02-02 11:24:17 +01:00
										 |  |  |             state = self._get_items(item_pool, all_except) | 
					
						
							| 
									
										
										
										
											2022-10-31 21:41:21 -05:00
										 |  |  |             path = self.get_path(state, self.multiworld.get_location(location, 1).parent_region) | 
					
						
							| 
									
										
										
										
											2021-02-02 11:24:17 +01:00
										 |  |  |             with self.subTest(msg="Reach Location", location=location, access=access, items=items, | 
					
						
							|  |  |  |                               all_except=all_except, path=path, entry=i): | 
					
						
							| 
									
										
										
										
											2020-04-29 01:34:30 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-31 21:41:21 -05:00
										 |  |  |                 self.assertEqual(self.multiworld.get_location(location, 1).can_reach(state), access) | 
					
						
							| 
									
										
										
										
											2020-04-29 01:34:30 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-02 11:24:17 +01:00
										 |  |  |             # check for partial solution | 
					
						
							|  |  |  |             if not all_except and access:  # we are not supposed to be able to reach location with partial inventory | 
					
						
							| 
									
										
										
										
											2020-12-19 21:13:35 +01:00
										 |  |  |                 for missing_item in item_pool[0]: | 
					
						
							|  |  |  |                     with self.subTest(msg="Location reachable without required item", location=location, | 
					
						
							| 
									
										
										
										
											2021-02-02 11:24:17 +01:00
										 |  |  |                                       items=item_pool[0], missing_item=missing_item, entry=i): | 
					
						
							|  |  |  |                         state = self._get_items_partial(item_pool, missing_item) | 
					
						
							| 
									
										
										
										
											2022-10-31 21:41:21 -05:00
										 |  |  |                         self.assertEqual(self.multiworld.get_location(location, 1).can_reach(state), False) | 
					
						
							| 
									
										
										
										
											2020-12-05 14:59:48 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-29 01:34:30 +10:00
										 |  |  |     def run_entrance_tests(self, access_pool): | 
					
						
							| 
									
										
										
										
											2021-02-02 11:24:17 +01:00
										 |  |  |         for i, (entrance, access, *item_pool) in enumerate(access_pool): | 
					
						
							| 
									
										
										
										
											2020-04-29 01:34:30 +10:00
										 |  |  |             items = item_pool[0] | 
					
						
							|  |  |  |             all_except = item_pool[1] if len(item_pool) > 1 else None | 
					
						
							| 
									
										
										
										
											2021-02-02 11:24:17 +01:00
										 |  |  |             state = self._get_items(item_pool, all_except) | 
					
						
							| 
									
										
										
										
											2022-10-31 21:41:21 -05:00
										 |  |  |             path = self.get_path(state, self.multiworld.get_entrance(entrance, 1).parent_region) | 
					
						
							| 
									
										
										
										
											2021-02-02 11:24:17 +01:00
										 |  |  |             with self.subTest(msg="Reach Entrance", entrance=entrance, access=access, items=items, | 
					
						
							|  |  |  |                               all_except=all_except, path=path, entry=i): | 
					
						
							| 
									
										
										
										
											2020-04-29 01:34:30 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-31 21:41:21 -05:00
										 |  |  |                 self.assertEqual(self.multiworld.get_entrance(entrance, 1).can_reach(state), access) | 
					
						
							| 
									
										
										
										
											2020-12-05 14:59:48 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-02 11:24:17 +01:00
										 |  |  |             # check for partial solution | 
					
						
							|  |  |  |             if not all_except and access:  # we are not supposed to be able to reach location with partial inventory | 
					
						
							| 
									
										
										
										
											2020-12-19 21:13:35 +01:00
										 |  |  |                 for missing_item in item_pool[0]: | 
					
						
							|  |  |  |                     with self.subTest(msg="Entrance reachable without required item", entrance=entrance, | 
					
						
							| 
									
										
										
										
											2021-02-02 11:24:17 +01:00
										 |  |  |                                       items=item_pool[0], missing_item=missing_item, entry=i): | 
					
						
							|  |  |  |                         state = self._get_items_partial(item_pool, missing_item) | 
					
						
							| 
									
										
										
										
											2022-10-31 21:41:21 -05:00
										 |  |  |                         self.assertEqual(self.multiworld.get_entrance(entrance, 1).can_reach(state), False) | 
					
						
							| 
									
										
										
										
											2021-02-02 11:24:17 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def _get_items(self, item_pool, all_except): | 
					
						
							|  |  |  |         if all_except and len(all_except) > 0: | 
					
						
							| 
									
										
										
										
											2022-10-31 21:41:21 -05:00
										 |  |  |             items = self.multiworld.itempool[:] | 
					
						
							| 
									
										
										
										
											2021-02-02 11:24:17 +01:00
										 |  |  |             items = [item for item in items if | 
					
						
							|  |  |  |                      item.name not in all_except and not ("Bottle" in item.name and "AnyBottle" in all_except)] | 
					
						
							|  |  |  |             items.extend(ItemFactory(item_pool[0], 1)) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             items = ItemFactory(item_pool[0], 1) | 
					
						
							|  |  |  |         return self.get_state(items) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def _get_items_partial(self, item_pool, missing_item): | 
					
						
							|  |  |  |         new_items = item_pool[0].copy() | 
					
						
							|  |  |  |         new_items.remove(missing_item) | 
					
						
							|  |  |  |         items = ItemFactory(new_items, 1) | 
					
						
							|  |  |  |         return self.get_state(items) | 
					
						
							| 
									
										
										
										
											2022-12-11 13:15:23 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class WorldTestBase(unittest.TestCase): | 
					
						
							|  |  |  |     options: typing.Dict[str, typing.Any] = {} | 
					
						
							|  |  |  |     multiworld: MultiWorld | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     game: typing.ClassVar[str]  # define game name in subclass, example "Secret of Evermore" | 
					
						
							|  |  |  |     auto_construct: typing.ClassVar[bool] = True | 
					
						
							|  |  |  |     """ automatically set up a world for each test in this class """ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def setUp(self) -> None: | 
					
						
							|  |  |  |         if self.auto_construct: | 
					
						
							|  |  |  |             self.world_setup() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def world_setup(self, seed: typing.Optional[int] = None) -> None: | 
					
						
							| 
									
										
										
										
											2023-02-26 18:24:54 -06:00
										 |  |  |         if type(self) is WorldTestBase: | 
					
						
							|  |  |  |             return  # setUp gets called for tests defined in the base class. We skip world_setup here. | 
					
						
							| 
									
										
										
										
											2022-12-11 13:15:23 +01:00
										 |  |  |         if not hasattr(self, "game"): | 
					
						
							|  |  |  |             raise NotImplementedError("didn't define game name") | 
					
						
							|  |  |  |         self.multiworld = MultiWorld(1) | 
					
						
							|  |  |  |         self.multiworld.game[1] = self.game | 
					
						
							|  |  |  |         self.multiworld.player_name = {1: "Tester"} | 
					
						
							|  |  |  |         self.multiworld.set_seed(seed) | 
					
						
							|  |  |  |         args = Namespace() | 
					
						
							|  |  |  |         for name, option in AutoWorld.AutoWorldRegister.world_types[self.game].option_definitions.items(): | 
					
						
							|  |  |  |             setattr(args, name, { | 
					
						
							|  |  |  |                 1: option.from_any(self.options.get(name, getattr(option, "default"))) | 
					
						
							|  |  |  |             }) | 
					
						
							|  |  |  |         self.multiworld.set_options(args) | 
					
						
							|  |  |  |         self.multiworld.set_default_common_options() | 
					
						
							|  |  |  |         for step in gen_steps: | 
					
						
							|  |  |  |             call_all(self.multiworld, step) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-26 18:24:54 -06:00
										 |  |  |     # methods that can be called within tests | 
					
						
							| 
									
										
										
										
											2022-12-11 13:15:23 +01:00
										 |  |  |     def collect_all_but(self, item_names: typing.Union[str, typing.Iterable[str]]) -> None: | 
					
						
							| 
									
										
										
										
											2023-02-26 18:24:54 -06:00
										 |  |  |         """Collects all pre-placed items and items in the multiworld itempool except those provided""" | 
					
						
							| 
									
										
										
										
											2022-12-11 13:15:23 +01:00
										 |  |  |         if isinstance(item_names, str): | 
					
						
							|  |  |  |             item_names = (item_names,) | 
					
						
							|  |  |  |         for item in self.multiworld.get_items(): | 
					
						
							|  |  |  |             if item.name not in item_names: | 
					
						
							|  |  |  |                 self.multiworld.state.collect(item) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def get_item_by_name(self, item_name: str) -> Item: | 
					
						
							| 
									
										
										
										
											2023-02-26 18:24:54 -06:00
										 |  |  |         """Returns the first item found in placed items, or in the itempool with the matching name""" | 
					
						
							| 
									
										
										
										
											2022-12-11 13:15:23 +01:00
										 |  |  |         for item in self.multiworld.get_items(): | 
					
						
							|  |  |  |             if item.name == item_name: | 
					
						
							|  |  |  |                 return item | 
					
						
							|  |  |  |         raise ValueError("No such item") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def get_items_by_name(self, item_names: typing.Union[str, typing.Iterable[str]]) -> typing.List[Item]: | 
					
						
							| 
									
										
										
										
											2023-02-26 18:24:54 -06:00
										 |  |  |         """Returns actual items from the itempool that match the provided name(s)""" | 
					
						
							| 
									
										
										
										
											2022-12-11 13:15:23 +01:00
										 |  |  |         if isinstance(item_names, str): | 
					
						
							|  |  |  |             item_names = (item_names,) | 
					
						
							|  |  |  |         return [item for item in self.multiworld.itempool if item.name in item_names] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def collect_by_name(self, item_names: typing.Union[str, typing.Iterable[str]]) -> typing.List[Item]: | 
					
						
							|  |  |  |         """ collect all of the items in the item pool that have the given names """ | 
					
						
							|  |  |  |         items = self.get_items_by_name(item_names) | 
					
						
							|  |  |  |         self.collect(items) | 
					
						
							|  |  |  |         return items | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def collect(self, items: typing.Union[Item, typing.Iterable[Item]]) -> None: | 
					
						
							| 
									
										
										
										
											2023-02-26 18:24:54 -06:00
										 |  |  |         """Collects the provided item(s) into state""" | 
					
						
							| 
									
										
										
										
											2022-12-11 13:15:23 +01:00
										 |  |  |         if isinstance(items, Item): | 
					
						
							|  |  |  |             items = (items,) | 
					
						
							|  |  |  |         for item in items: | 
					
						
							|  |  |  |             self.multiworld.state.collect(item) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def remove(self, items: typing.Union[Item, typing.Iterable[Item]]) -> None: | 
					
						
							| 
									
										
										
										
											2023-02-26 18:24:54 -06:00
										 |  |  |         """Removes the provided item(s) from state""" | 
					
						
							| 
									
										
										
										
											2022-12-11 13:15:23 +01:00
										 |  |  |         if isinstance(items, Item): | 
					
						
							|  |  |  |             items = (items,) | 
					
						
							|  |  |  |         for item in items: | 
					
						
							|  |  |  |             if item.location and item.location.event and item.location in self.multiworld.state.events: | 
					
						
							|  |  |  |                 self.multiworld.state.events.remove(item.location) | 
					
						
							|  |  |  |             self.multiworld.state.remove(item) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def can_reach_location(self, location: str) -> bool: | 
					
						
							| 
									
										
										
										
											2023-02-26 18:24:54 -06:00
										 |  |  |         """Determines if the current state can reach the provide location name""" | 
					
						
							| 
									
										
										
										
											2022-12-11 13:15:23 +01:00
										 |  |  |         return self.multiworld.state.can_reach(location, "Location", 1) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-19 09:17:16 -06:00
										 |  |  |     def can_reach_entrance(self, entrance: str) -> bool: | 
					
						
							| 
									
										
										
										
											2023-02-26 18:24:54 -06:00
										 |  |  |         """Determines if the current state can reach the provided entrance name""" | 
					
						
							| 
									
										
										
										
											2023-01-19 09:17:16 -06:00
										 |  |  |         return self.multiworld.state.can_reach(entrance, "Entrance", 1) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-11 13:15:23 +01:00
										 |  |  |     def count(self, item_name: str) -> int: | 
					
						
							| 
									
										
										
										
											2023-02-26 18:24:54 -06:00
										 |  |  |         """Returns the amount of an item currently in state""" | 
					
						
							| 
									
										
										
										
											2022-12-11 13:15:23 +01:00
										 |  |  |         return self.multiworld.state.count(item_name, 1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def assertAccessDependency(self, | 
					
						
							|  |  |  |                                locations: typing.List[str], | 
					
						
							|  |  |  |                                possible_items: typing.Iterable[typing.Iterable[str]]) -> None: | 
					
						
							| 
									
										
										
										
											2023-02-26 18:24:54 -06:00
										 |  |  |         """Asserts that the provided locations can't be reached without the listed items but can be reached with any
 | 
					
						
							|  |  |  |          one of the provided combinations"""
 | 
					
						
							| 
									
										
										
										
											2022-12-11 13:15:23 +01:00
										 |  |  |         all_items = [item_name for item_names in possible_items for item_name in item_names] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.collect_all_but(all_items) | 
					
						
							|  |  |  |         for location in self.multiworld.get_locations(): | 
					
						
							|  |  |  |             self.assertEqual(self.multiworld.state.can_reach(location), location.name not in locations) | 
					
						
							|  |  |  |         for item_names in possible_items: | 
					
						
							|  |  |  |             items = self.collect_by_name(item_names) | 
					
						
							|  |  |  |             for location in locations: | 
					
						
							|  |  |  |                 self.assertTrue(self.can_reach_location(location)) | 
					
						
							|  |  |  |             self.remove(items) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def assertBeatable(self, beatable: bool): | 
					
						
							| 
									
										
										
										
											2023-02-26 18:24:54 -06:00
										 |  |  |         """Asserts that the game can be beaten with the current state""" | 
					
						
							| 
									
										
										
										
											2022-12-11 13:15:23 +01:00
										 |  |  |         self.assertEqual(self.multiworld.can_beat_game(self.multiworld.state), beatable) | 
					
						
							| 
									
										
										
										
											2023-02-26 18:24:54 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # following tests are automatically run | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def skip_default_tests(self) -> bool: | 
					
						
							|  |  |  |         """Not possible or identical to the base test that's always being run already""" | 
					
						
							|  |  |  |         constructed = hasattr(self, "game") and hasattr(self, "multiworld") | 
					
						
							|  |  |  |         return not constructed or (not self.options | 
					
						
							|  |  |  |                                    and self.setUp is WorldTestBase.setUp | 
					
						
							|  |  |  |                                    and self.world_setup is WorldTestBase.world_setup) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def testAllStateCanReachEverything(self): | 
					
						
							|  |  |  |         """Ensure all state can reach everything with the defined options""" | 
					
						
							|  |  |  |         if self.skip_default_tests: | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  |         with self.subTest("Game", game=self.game): | 
					
						
							|  |  |  |             excluded = self.multiworld.exclude_locations[1].value | 
					
						
							|  |  |  |             state = self.multiworld.get_all_state(False) | 
					
						
							|  |  |  |             for location in self.multiworld.get_locations(): | 
					
						
							|  |  |  |                 if location.name not in excluded: | 
					
						
							|  |  |  |                     with self.subTest("Location should be reached", location=location): | 
					
						
							|  |  |  |                         self.assertTrue(location.can_reach(state), f"{location.name} unreachable") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def testEmptyStateCanReachSomething(self): | 
					
						
							|  |  |  |         """Ensure empty state can reach at least one location with the defined options""" | 
					
						
							|  |  |  |         if self.skip_default_tests: | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  |         with self.subTest("Game", game=self.game): | 
					
						
							|  |  |  |             state = CollectionState(self.multiworld) | 
					
						
							|  |  |  |             locations = self.multiworld.get_reachable_locations(state, 1) | 
					
						
							|  |  |  |             self.assertGreater(len(locations), 0, | 
					
						
							|  |  |  |                                "Need to be able to reach at least one location to get started.") |