108 lines
3.5 KiB
Python
108 lines
3.5 KiB
Python
"""Reference Manual section and keyword coverage tests."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from src.cpp_transpiler import transpile_to_cpp
|
|
from src.keywords import PROTEL_KEYWORDS
|
|
from src.parser import parse_protel
|
|
from tests.ref_manual_manifest import (
|
|
EXAMPLES,
|
|
KEYWORD_EXTERNAL_SOURCES,
|
|
REFERENCE_MANUAL_SECTIONS,
|
|
REF_EXAMPLE_SOURCES,
|
|
ROOT,
|
|
)
|
|
|
|
_KEYWORD_BOUNDARY = re.compile(
|
|
r"(?<![A-Za-z0-9_$])("
|
|
+ "|".join(re.escape(k) for k in sorted(PROTEL_KEYWORDS, key=len, reverse=True))
|
|
+ r")(?![A-Za-z0-9_$])",
|
|
re.IGNORECASE,
|
|
)
|
|
|
|
|
|
def _corpus_files() -> list[Path]:
|
|
patterns = (
|
|
"examples/**/*.protel",
|
|
"examples/**/*.aa01",
|
|
"examples/**/*.P",
|
|
"vdi.aa01",
|
|
"Pb",
|
|
"tests/test_pb.py",
|
|
"tests/test_vdi_aa01.py",
|
|
"tests/ref_manual_manifest.py",
|
|
)
|
|
files: list[Path] = []
|
|
for pattern in patterns:
|
|
files.extend(ROOT.glob(pattern))
|
|
return sorted({p.resolve() for p in files if p.is_file()})
|
|
|
|
|
|
def _file_contains_keyword(path: Path, keyword: str) -> bool:
|
|
try:
|
|
text = path.read_text(encoding="utf-8", errors="replace")
|
|
except OSError:
|
|
return False
|
|
return bool(_KEYWORD_BOUNDARY.search(text))
|
|
|
|
|
|
@pytest.mark.parametrize("section", sorted(REFERENCE_MANUAL_SECTIONS))
|
|
def test_reference_manual_section_has_example_mapping(section: str):
|
|
mapped = REFERENCE_MANUAL_SECTIONS[section]
|
|
assert mapped, f"section {section} has no mapped sources"
|
|
for rel in mapped:
|
|
if rel.startswith("tests/"):
|
|
assert (ROOT / rel).is_file(), f"missing test corpus for §{section}: {rel}"
|
|
else:
|
|
assert (EXAMPLES / rel).is_file() or (ROOT / rel).is_file(), (
|
|
f"missing example for §{section}: {rel}"
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize("rel_source", REF_EXAMPLE_SOURCES)
|
|
def test_example_parses(rel_source: str):
|
|
source = (EXAMPLES / rel_source).read_text(encoding="utf-8")
|
|
tree = parse_protel(source)
|
|
assert tree
|
|
|
|
|
|
@pytest.mark.parametrize("rel_source", [s for s in REF_EXAMPLE_SOURCES if s.startswith("ref/")])
|
|
def test_ref_example_transpiles(rel_source: str):
|
|
path = EXAMPLES / rel_source
|
|
source = path.read_text(encoding="utf-8")
|
|
tree = parse_protel(source)
|
|
cpp = transpile_to_cpp(tree, source_name=path.name)
|
|
assert "Generated by PROTEL" in cpp
|
|
|
|
|
|
@pytest.mark.parametrize("keyword", sorted(PROTEL_KEYWORDS))
|
|
def test_reserved_keyword_exercised_in_corpus(keyword: str):
|
|
if keyword in KEYWORD_EXTERNAL_SOURCES:
|
|
for rel in KEYWORD_EXTERNAL_SOURCES[keyword]:
|
|
path = ROOT / rel if (ROOT / rel).exists() else EXAMPLES / rel
|
|
assert path.is_file(), f"missing external keyword source for {keyword}: {rel}"
|
|
assert _file_contains_keyword(path, keyword), (
|
|
f"keyword {keyword!r} not found in {rel}"
|
|
)
|
|
return
|
|
|
|
for path in _corpus_files():
|
|
if _file_contains_keyword(path, keyword):
|
|
return
|
|
|
|
pytest.fail(f"reserved keyword {keyword!r} not found in examples or test corpus")
|
|
|
|
|
|
def test_keyword_manifest_covers_all_keywords():
|
|
covered = set(KEYWORD_EXTERNAL_SOURCES)
|
|
for path in _corpus_files():
|
|
text = path.read_text(encoding="utf-8", errors="replace")
|
|
for match in _KEYWORD_BOUNDARY.finditer(text):
|
|
covered.add(match.group(1).upper())
|
|
missing = PROTEL_KEYWORDS - {k.upper() for k in covered}
|
|
assert not missing, f"keywords missing from corpus: {sorted(missing)}" |