Skip to content

Commit

Permalink
fix typing in mod testing decorators
Browse files Browse the repository at this point in the history
  • Loading branch information
Jouramie committed Jan 27, 2025
1 parent 7a66885 commit a1bad3b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 24 deletions.
24 changes: 12 additions & 12 deletions worlds/stardew_valley/test/mods/TestMods.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,51 +44,51 @@ class TestModsEntranceRandomization(WorldAssertMixin, SVTestCase):
Not all ER settings are tested, because 'buildings' is, essentially, a superset of all others
"""

@mod_testing(mod=ModNames.deepwoods)
@mod_testing(ModNames.deepwoods)
def test_deepwoods_entrance_randomization_buildings(self):
self.perform_basic_checks_on_mod_with_er(ModNames.deepwoods, options.EntranceRandomization.option_buildings)

@mod_testing(mod=ModNames.juna)
@mod_testing(ModNames.juna)
def test_juna_entrance_randomization_buildings(self):
self.perform_basic_checks_on_mod_with_er(ModNames.juna, options.EntranceRandomization.option_buildings)

@mod_testing(mod=ModNames.jasper)
@mod_testing(ModNames.jasper)
def test_jasper_entrance_randomization_buildings(self):
self.perform_basic_checks_on_mod_with_er(ModNames.jasper, options.EntranceRandomization.option_buildings)

@mod_testing(mod=ModNames.alec)
@mod_testing(ModNames.alec)
def test_alec_entrance_randomization_buildings(self):
self.perform_basic_checks_on_mod_with_er(ModNames.alec, options.EntranceRandomization.option_buildings)

@mod_testing(mod=ModNames.yoba)
@mod_testing(ModNames.yoba)
def test_yoba_entrance_randomization_buildings(self):
self.perform_basic_checks_on_mod_with_er(ModNames.yoba, options.EntranceRandomization.option_buildings)

@mod_testing(mod=ModNames.eugene)
@mod_testing(ModNames.eugene)
def test_eugene_entrance_randomization_buildings(self):
self.perform_basic_checks_on_mod_with_er(ModNames.eugene, options.EntranceRandomization.option_buildings)

@mod_testing(mod=ModNames.ayeisha)
@mod_testing(ModNames.ayeisha)
def test_ayeisha_entrance_randomization_buildings(self):
self.perform_basic_checks_on_mod_with_er(ModNames.ayeisha, options.EntranceRandomization.option_buildings)

@mod_testing(mod=ModNames.riley)
@mod_testing(ModNames.riley)
def test_riley_entrance_randomization_buildings(self):
self.perform_basic_checks_on_mod_with_er(ModNames.riley, options.EntranceRandomization.option_buildings)

@mod_testing(mod=ModNames.sve)
@mod_testing(ModNames.sve)
def test_sve_entrance_randomization_buildings(self):
self.perform_basic_checks_on_mod_with_er(ModNames.sve, options.EntranceRandomization.option_buildings)

@mod_testing(mod=ModNames.alecto)
@mod_testing(ModNames.alecto)
def test_alecto_entrance_randomization_buildings(self):
self.perform_basic_checks_on_mod_with_er(ModNames.alecto, options.EntranceRandomization.option_buildings)

@mod_testing(mod=ModNames.lacey)
@mod_testing(ModNames.lacey)
def test_lacey_entrance_randomization_buildings(self):
self.perform_basic_checks_on_mod_with_er(ModNames.lacey, options.EntranceRandomization.option_buildings)

@mod_testing(mod=ModNames.boarding_house)
@mod_testing(ModNames.boarding_house)
def test_boarding_house_entrance_randomization_buildings(self):
self.perform_basic_checks_on_mod_with_er(ModNames.boarding_house, options.EntranceRandomization.option_buildings)

Expand Down
28 changes: 16 additions & 12 deletions worlds/stardew_valley/test/mods/mod_testing_decorators.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
import unittest
from collections.abc import Collection
from collections.abc import Callable, Iterable
from functools import wraps, partial
from typing import Type

from ... import options


def must_test_all_mods(cls: Type[unittest.TestCase] | None = None, /, *, excluded_mods: Collection[str] = None):
def must_test_all_mods(cls: Type[unittest.TestCase] | None = None, /, *, excluded_mods: Iterable[str] | None = None) \
-> partial[Type[unittest.TestCase]] | Type[unittest.TestCase]:
if excluded_mods is None:
excluded_mods = set()

if cls is None:
return partial(must_test_all_mods, excluded_mods=excluded_mods)
return partial(_must_test_all_mods, excluded_mods=excluded_mods)
return _must_test_all_mods(cls, excluded_mods)

if excluded_mods is None:
setattr(cls, "tested_mods", set())
else:
setattr(cls, "tested_mods", set(excluded_mods))

def _must_test_all_mods(cls: Type[unittest.TestCase], excluded_mods: Iterable[str]) -> Type[unittest.TestCase]:
setattr(cls, "tested_mods", set(excluded_mods))
orignal_tear_down_class = cls.tearDownClass

@wraps(cls.tearDownClass)
def wrapper():
def wrapper() -> None:
tested_mods: set[str] = getattr(cls, "tested_mods")

diff = set(options.Mods.valid_keys) - tested_mods
diff = options.Mods.valid_keys - tested_mods
if diff:
raise AssertionError(f"Mods {diff} were not tested")

Expand All @@ -32,10 +35,11 @@ def wrapper():
return cls


def mod_testing(func=None, /, *, mod: str):
if func is None:
return partial(mod_testing, mod=mod)
def mod_testing(mod: str) -> partial[Callable]:
return partial(_mod_testing, mod=mod)


def _mod_testing(func: Callable, mod: str) -> Callable:
@wraps(func)
def wrapper(self: unittest.TestCase, *args, **kwargs):
getattr(self, "tested_mods").add(mod)
Expand Down

0 comments on commit a1bad3b

Please sign in to comment.