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.

clew/wiki/checkpins.py:67

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
lines 69–69suffix = PurePosixPath(rel).suffixlines 70–70◇ if suffix == ".py"lines 71–71return _python(lines, candidates, first_line)lines 72–72lang = _C_SUFFIXES.get(suffix) or _CPP_SUFFIXES.get(suffix)lines 73–73return _c_family(lines, lang, candidates, first_line) if lang else []

Requirements

REQ-DDB-WIKI-043

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.

clew/wiki/checkpins.py:292

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 out

1 more lines, not shown

Activity · 15 steps
lines 301–301from .tracerun import spans_oflines 303–303spans = spans_of(otlp)lines 304–304tests = {s.get("spanId") for s in spans if s.get("name") == test}lines 305–305calls: dict[tuple[str, str], str] = {}lines 306–306◇ for span in spanslines 307–307attrs = {a["key"]: next(iter(a["value"].values()), "") for a in span.get("attributes", [])}lines 308–308◇ if span.get("parentSpanId") in tests and attrs.get("clew.call_line")lines 309–309calls[(span.get("name", ""), attrs["clew.call_line"])] = attrs.get("clew.return", "")lines 310–310out: dict[int, str] = {}lines 311–311◇ for i, pin in enumerate(pins)lines 312–312site = pin.line if pin.kind == DIRECT else pin.bound_linelines 313–313key = (pin.target or "", str(site))lines 314–314◇ if pin.target and site is not None and key in callslines 315–315out[i] = calls[key]lines 316–316return out

Requirements

REQ-DDB-WIKI-043