checkpins.py¶
clew/wiki/checkpins.py
A test's checks, pinned by syntax tree to the function each one observes (round 5, P5.5).
pin_checks
list[Pin] clew.wiki.checkpins.pin_checks(list[str] lines, str rel, set[str] candidates, int first_line=1)
Pin each assertion in a test body to the candidate function it observes.
Called by _definition, _pins, _unmarked
Tested by 6 tests
test_a_pin_carries_its_lines_for_pairing_with_a_run, test_a_real_run_gives_a_pinned_check_its_value, test_a_traced_run_gives_each_pinned_check_the_value_it_saw, test_an_unknown_language_pins_nothing, test_cpp_checks_are_pinned_through_the_assertion_macros, test_python_checks_are_pinned_direct_via_variable_or_not
Calls _c_family, _python
Source · 9 lines
def pin_checks(lines: list[str], rel: str, candidates: set[str], first_line: int = 1) -> list[Pin]:
"""@brief Dispatch by language. @return Pins."""
suffix = PurePosixPath(rel).suffix
if suffix == ".py":
return _python(lines, candidates, first_line)
lang = _C_SUFFIXES.get(suffix) or _CPP_SUFFIXES.get(suffix)
return _c_family(lines, lang, candidates, first_line) if lang else []
Activity · 5 steps
Requirements
traced_values
dict[int, str] clew.wiki.checkpins.traced_values(list[Pin] pins, dict otlp, str test)
The value each pinned check observed in a recorded run, by pin index.
Source · 25 lines
def traced_values(pins: list[Pin], otlp: dict, test: str) -> dict[int, str]:
"""PAIRED BY CALL SITE, NEVER BY ORDER. The pinned function's span called FROM THE TEST at the
pin's line — the assertion's own line when direct, the binding line when via a variable — is
the call the check observed. A run recorded before spans carried `clew.call_line` pairs
nothing: a value shown against the wrong call would be worse than none.
@brief Pair checks with a run's return values.
@return Values by pin index.
"""
from .tracerun import spans_of
spans = spans_of(otlp)
tests = {s.get("spanId") for s in spans if s.get("name") == test}
calls: dict[tuple[str, str], str] = {}
for span in spans:
attrs = {a["key"]: next(iter(a["value"].values()), "") for a in span.get("attributes", [])}
if span.get("parentSpanId") in tests and attrs.get("clew.call_line"):
calls[(span.get("name", ""), attrs["clew.call_line"])] = attrs.get("clew.return", "")
out: dict[int, str] = {}
for i, pin in enumerate(pins):
site = pin.line if pin.kind == DIRECT else pin.bound_line
key = (pin.target or "", str(site))
if pin.target and site is not None and key in calls:
out[i] = calls[key]
return out1 more lines, not shown