# SPDX-License-Identifier: MIT """docs/api.md: every name it lists exists, and the chips keep their import boundaries.""" from __future__ import annotations import ast import importlib import re from pathlib import Path ROOT = Path(__file__).resolve().parents[1] API = ROOT / "docs" / "api.md" SRC = ROOT / "src" / "a2600_python" def listed_names() -> list[str]: names = [] for line in API.read_text().splitlines(): m = re.match(r"\| `(a2600_python(?:\.\w+)+)` \|", line) if m: names.append(m.group(1)) return names def instances(): """Objects to look instance attributes up on (set in ``__init__``).""" from a2600_python.audio import Audio from a2600_python.board import Board from a2600_python.cartridge import load from a2600_python.machine import VCS_NTSC from a2600_python.riot import RIOT from a2600_python.system import System from a2600_python.tia import TIA cartridge = load(bytes(0x1000), scheme="4K").cartridge return { TIA: TIA(), RIOT: RIOT(0, 0, 1024, 1024), Audio: Audio(), Board: Board(VCS_NTSC, cartridge), System: System(cartridge), } def resolve(name: str, objects) -> bool: parts = name.split(".") for cut in range(len(parts), 0, -1): try: target = importlib.import_module(".".join(parts[:cut])) except ModuleNotFoundError: continue for attr in parts[cut:]: if hasattr(target, attr): target = getattr(target, attr) elif target in objects and hasattr(objects[target], attr): target = getattr(objects[target], attr) else: return False return True return False def test_every_listed_name_exists(): names = listed_names() assert len(names) > 80 objects = instances() missing = [n for n in names if not resolve(n, objects)] assert not missing, missing def imports_of(path: Path) -> set[str]: tree = ast.parse(path.read_text()) found = set() for node in ast.walk(tree): if isinstance(node, ast.ImportFrom) and node.level: found.add(node.module or "") return found def test_the_chips_import_nothing_from_the_board(): assert imports_of(SRC / "riot.py") == set() assert imports_of(SRC / "audio.py") == set() assert imports_of(SRC / "tia.py") <= {"audio", "machine"} # tia.py takes only the revision record from machine.py. text = (SRC / "tia.py").read_text() assert "from .machine import REFERENCE_TIA, TIARevision" in text