refactor(tests): make cases dir configurable in subclass

This commit is contained in:
2026-05-29 17:52:10 +02:00
parent 89ec63cb05
commit 86e4763a12
2 changed files with 15 additions and 13 deletions

View File

@@ -7,8 +7,6 @@ from abc import ABC, abstractmethod
from pathlib import Path from pathlib import Path
from typing import Iterator, Protocol from typing import Iterator, Protocol
DEFAULT_BASE_DIR: Path = Path() / "tests"
class CaseResult(Protocol): class CaseResult(Protocol):
def dumps(self) -> str: ... def dumps(self) -> str: ...
@@ -17,8 +15,15 @@ class CaseResult(Protocol):
class Tester(ABC): class Tester(ABC):
"""A test runner to check for regressions in the lexer and parser""" """A test runner to check for regressions in the lexer and parser"""
def __init__(self, base_dir: Path): CASES_DIR: Path = Path(__file__).parent / "cases"
self.base_dir: Path = base_dir
@property
@abstractmethod
def namespace(self) -> str: ...
@property
def base_dir(self) -> Path:
return self.CASES_DIR / self.namespace
def _list_tests(self) -> list[Path]: def _list_tests(self) -> list[Path]:
return list(self.base_dir.rglob("*.midas")) return list(self.base_dir.rglob("*.midas"))
@@ -35,7 +40,7 @@ class Tester(ABC):
print(rule) print(rule)
for i, test in enumerate(tests): for i, test in enumerate(tests):
print(f"Case {i+1}/{n}: {test}") print(f"Case {i+1}/{n}: {test.relative_to(self.CASES_DIR)}")
success: bool = self._run_test(test) success: bool = self._run_test(test)
if success: if success:
successes += 1 successes += 1
@@ -106,13 +111,6 @@ class Tester(ABC):
@classmethod @classmethod
def main(cls): def main(cls):
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument(
"-D",
"--base-dir",
help="Base directory containing test files",
type=Path,
default=DEFAULT_BASE_DIR,
)
subparsers = parser.add_subparsers(dest="subcommand") subparsers = parser.add_subparsers(dest="subcommand")
update = subparsers.add_parser("update") update = subparsers.add_parser("update")
@@ -124,7 +122,7 @@ class Tester(ABC):
run.add_argument("FILE", type=Path, nargs="*") run.add_argument("FILE", type=Path, nargs="*")
args = parser.parse_args() args = parser.parse_args()
tester: Tester = cls(args.base_dir) tester: Tester = cls()
match args.subcommand: match args.subcommand:
case "update": case "update":

View File

@@ -23,6 +23,10 @@ class CaseResult:
class MidasTester(Tester): class MidasTester(Tester):
@property
def namespace(self) -> str:
return "parser"
def _exec_case(self, path: Path) -> CaseResult: def _exec_case(self, path: Path) -> CaseResult:
if not path.exists(): if not path.exists():
raise FileNotFoundError(f"Could not find test '{path}'") raise FileNotFoundError(f"Could not find test '{path}'")