Stardew Valley: add assert_can_reach_region_* for better tests (#4556)

* add assert_reach_region_*; refactor existing assert_reach_location_* to allow string

* rename asserts
This commit is contained in:
Jouramie
2025-02-04 02:27:23 -05:00
committed by GitHub
parent 19faaa4104
commit da48af60dc
7 changed files with 50 additions and 29 deletions

View File

@@ -65,7 +65,7 @@ class TestBooksanityNone(SVTestBase):
if item_to_ship not in power_books and item_to_ship not in skill_books:
continue
with self.subTest(location.name):
self.assert_reach_location_true(location, self.multiworld.state)
self.assert_can_reach_location(location, self.multiworld.state)
class TestBooksanityPowers(SVTestBase):
@@ -111,7 +111,7 @@ class TestBooksanityPowers(SVTestBase):
if item_to_ship not in power_books and item_to_ship not in skill_books:
continue
with self.subTest(location.name):
self.assert_reach_location_true(location, self.multiworld.state)
self.assert_can_reach_location(location, self.multiworld.state)
class TestBooksanityPowersAndSkills(SVTestBase):
@@ -157,7 +157,7 @@ class TestBooksanityPowersAndSkills(SVTestBase):
if item_to_ship not in power_books and item_to_ship not in skill_books:
continue
with self.subTest(location.name):
self.assert_reach_location_true(location, self.multiworld.state)
self.assert_can_reach_location(location, self.multiworld.state)
class TestBooksanityAll(SVTestBase):
@@ -203,4 +203,4 @@ class TestBooksanityAll(SVTestBase):
if item_to_ship not in power_books and item_to_ship not in skill_books:
continue
with self.subTest(location.name):
self.assert_reach_location_true(location, self.multiworld.state)
self.assert_can_reach_location(location, self.multiworld.state)

View File

@@ -81,10 +81,10 @@ class TestWalnutsanityPuzzles(SVTestBase):
self.collect("Combat Level", 10)
self.collect("Mining Level", 10)
for location in locations:
self.assert_reach_location_false(location, self.multiworld.state)
self.assert_cannot_reach_location(location, self.multiworld.state)
self.collect("Open Professor Snail Cave")
for location in locations:
self.assert_reach_location_true(location, self.multiworld.state)
self.assert_can_reach_location(location, self.multiworld.state)
class TestWalnutsanityBushes(SVTestBase):

View File

@@ -1,7 +1,7 @@
from typing import List
from unittest import TestCase
from BaseClasses import CollectionState, Location
from BaseClasses import CollectionState, Location, Region
from ...stardew_rule import StardewRule, false_, MISSING_ITEM, Reach
from ...stardew_rule.rule_explain import explain
@@ -40,19 +40,42 @@ class RuleAssertMixin(TestCase):
raise AssertionError(f"Error while checking rule {rule}: {e}"
f"\nExplanation: {expl}")
def assert_reach_location_true(self, location: Location, state: CollectionState):
expl = explain(Reach(location.name, "Location", 1), state)
def assert_can_reach_location(self, location: Location | str, state: CollectionState) -> None:
location_name = location.name if isinstance(location, Location) else location
expl = explain(Reach(location_name, "Location", 1), state)
try:
can_reach = location.can_reach(state)
can_reach = state.can_reach_location(location_name, 1)
self.assertTrue(can_reach, expl)
except KeyError as e:
raise AssertionError(f"Error while checking location {location.name}: {e}"
raise AssertionError(f"Error while checking location {location_name}: {e}"
f"\nExplanation: {expl}")
def assert_reach_location_false(self, location: Location, state: CollectionState):
expl = explain(Reach(location.name, "Location", 1), state, expected=False)
def assert_cannot_reach_location(self, location: Location | str, state: CollectionState) -> None:
location_name = location.name if isinstance(location, Location) else location
expl = explain(Reach(location_name, "Location", 1), state, expected=False)
try:
self.assertFalse(location.can_reach(state), expl)
can_reach = state.can_reach_location(location_name, 1)
self.assertFalse(can_reach, expl)
except KeyError as e:
raise AssertionError(f"Error while checking location {location.name}: {e}"
raise AssertionError(f"Error while checking location {location_name}: {e}"
f"\nExplanation: {expl}")
def assert_can_reach_region(self, region: Region | str, state: CollectionState) -> None:
region_name = region.name if isinstance(region, Region) else region
expl = explain(Reach(region_name, "Region", 1), state)
try:
can_reach = state.can_reach_region(region_name, 1)
self.assertTrue(can_reach, expl)
except KeyError as e:
raise AssertionError(f"Error while checking region {region_name}: {e}"
f"\nExplanation: {expl}")
def assert_cannot_reach_region(self, region: Region | str, state: CollectionState) -> None:
region_name = region.name if isinstance(region, Region) else region
expl = explain(Reach(region_name, "Region", 1), state, expected=False)
try:
can_reach = state.can_reach_region(region_name, 1)
self.assertFalse(can_reach, expl)
except KeyError as e:
raise AssertionError(f"Error while checking region {region_name}: {e}"
f"\nExplanation: {expl}")

View File

@@ -53,7 +53,7 @@ class WorldAssertMixin(RuleAssertMixin, TestCase):
def assert_can_reach_everything(self, multiworld: MultiWorld):
for location in multiworld.get_locations():
self.assert_reach_location_true(location, multiworld.state)
self.assert_can_reach_location(location, multiworld.state)
def assert_basic_checks(self, multiworld: MultiWorld):
self.assert_same_number_items_locations(multiworld)

View File

@@ -12,15 +12,13 @@ class TestBooksLogic(SVTestBase):
location = self.multiworld.get_location("Read Mapping Cave Systems", self.player)
self.assert_reach_location_false(location, self.multiworld.state)
self.assert_cannot_reach_location(location, self.multiworld.state)
self.collect("Progressive Mine Elevator")
self.collect("Progressive Mine Elevator")
self.collect("Progressive Mine Elevator")
self.collect("Progressive Mine Elevator")
self.assert_reach_location_false(location, self.multiworld.state)
self.assert_cannot_reach_location(location, self.multiworld.state)
self.collect("Progressive Weapon")
self.assert_reach_location_true(location, self.multiworld.state)
self.assert_can_reach_location(location, self.multiworld.state)

View File

@@ -43,18 +43,18 @@ class TestNeedRegionToCatchFish(SVTestBase):
self.collect_all_the_money()
item_names = fish_and_items[fish]
location = self.multiworld.get_location(f"Fishsanity: {fish}", self.player)
self.assert_reach_location_false(location, self.multiworld.state)
self.assert_cannot_reach_location(location, self.multiworld.state)
items = []
for item_name in item_names:
items.append(self.collect(item_name))
with self.subTest(f"{fish} can be reached with {item_names}"):
self.assert_reach_location_true(location, self.multiworld.state)
self.assert_can_reach_location(location, self.multiworld.state)
for item_required in items:
self.multiworld.state = self.original_state.copy()
with self.subTest(f"{fish} requires {item_required.name}"):
for item_to_collect in items:
if item_to_collect.name != item_required.name:
self.collect(item_to_collect)
self.assert_reach_location_false(location, self.multiworld.state)
self.assert_cannot_reach_location(location, self.multiworld.state)
self.multiworld.state = self.original_state.copy()

View File

@@ -39,10 +39,10 @@ class TestSkillProgressionProgressive(SVTestBase):
with self.subTest(location_name):
if level > 1:
self.assert_reach_location_false(location, self.multiworld.state)
self.assert_cannot_reach_location(location, self.multiworld.state)
self.collect(f"{skill} Level")
self.assert_reach_location_true(location, self.multiworld.state)
self.assert_can_reach_location(location, self.multiworld.state)
self.reset_collection_state()
@@ -88,7 +88,7 @@ class TestSkillProgressionProgressiveWithMasteryWithoutMods(SVTestBase):
for skill in all_vanilla_skills:
with self.subTest(skill):
location = self.multiworld.get_location(f"{skill} Mastery", self.player)
self.assert_reach_location_true(location, self.multiworld.state)
self.assert_can_reach_location(location, self.multiworld.state)
self.reset_collection_state()
@@ -99,7 +99,7 @@ class TestSkillProgressionProgressiveWithMasteryWithoutMods(SVTestBase):
self.remove_one_by_name(f"{skill} Level")
location = self.multiworld.get_location(f"{skill} Mastery", self.player)
self.assert_reach_location_false(location, self.multiworld.state)
self.assert_cannot_reach_location(location, self.multiworld.state)
self.reset_collection_state()
@@ -108,6 +108,6 @@ class TestSkillProgressionProgressiveWithMasteryWithoutMods(SVTestBase):
self.remove_one_by_name(f"Progressive Pickaxe")
location = self.multiworld.get_location("Mining Mastery", self.player)
self.assert_reach_location_false(location, self.multiworld.state)
self.assert_cannot_reach_location(location, self.multiworld.state)
self.reset_collection_state()