slices.py

clew/wiki/slices.py

A test is a slice of a master flow (round 5, R5-4).

_hidden

str clew.wiki.slices._hidden(int n, str where)

A line counting steps outside a window.

clew/wiki/slices.py:216

Show on the map →

Called by slice_svg

Calls none

Source · 5 lines
def _hidden(n: int, where: str) -> str:
    """@brief Count hidden steps. @return HTML."""
    steps = f"step{'s' if n != 1 else ''}"
    said = f"{n} {steps} between" if where == "between" else f"{n} {where} {steps}"
    return f'<p class="slice-more">… {said}</p>' if n else ""

1 more lines, not shown

Activity · 3 steps
lines 218–218steps = f"step{'s' if n != 1 else ''}"lines 219–219said = f"{n} {steps} between" if where == "between" else f"{n} {where} {steps}"lines 220–220return f'<p class="slice-more">… {said}</p>' if n else ""

Flows

_marks

dict[int, str] clew.wiki.slices._marks(Flow flow, Slice cut, int lo, int hi)

Classes per step of a window: lit, dimmed, and the first step of each entry.

clew/wiki/slices.py:195

Show on the map →

Called by slice_svg

Calls none

Source · 14 lines
def _marks(flow: Flow, cut: Slice, lo: int, hi: int) -> dict[int, str]:
    """@brief Mark steps. @return Classes."""
    lit, entered, seen = set(cut.exercised), set(cut.entered), set()
    out = {}
    for i in range(lo, hi):
        mark = "ex" if i in lit else "dim"
        caller = flow.steps[i].caller_qualified or flow.steps[i].caller
        if i in lit and caller in entered and caller not in seen:
            seen.add(caller)
            mark += " entry"
        out[i - lo] = mark
    return out

Activity · 10 steps
lines 197–197lit, entered, seen = set(cut.exercised), set(cut.entered), set()lines 198–198out = {}lines 199–199◇ for i in range(lo, hi)lines 200–200mark = "ex" if i in lit else "dim"lines 201–201caller = flow.steps[i].caller_qualified or flow.steps[i].callerlines 202–202◇ if i in lit and caller in entered and caller not in seenlines 203–203seen.add(caller)lines 204–204mark += " entry"lines 205–205out[i - lo] = marklines 206–206return out

Flows

_slice

Slice clew.wiki.slices._slice(Flow flow, tuple[int,...] exercised)

A slice from its exercised steps: where the test entered follows from them.

clew/wiki/slices.py:120

Source · 11 lines
def _slice(flow: Flow, exercised: tuple[int, ...]) -> Slice:
    """@brief Entries from exercised steps. @return Slice."""
    called = {flow.steps[i].callee_qualified or flow.steps[i].callee for i in exercised}
    entered: list[str] = []
    for i in exercised:
        caller = flow.steps[i].caller_qualified or flow.steps[i].caller
        if caller not in called and caller not in entered:
            entered.append(caller)
    return Slice(exercised, tuple(entered))

Activity · 7 steps
lines 122–122called = {flow.steps[i].callee_qualified or flow.steps[i].callee for i in exercised}lines 123–123entered: list[str] = []lines 124–124◇ for i in exercisedlines 125–125caller = flow.steps[i].caller_qualified or flow.steps[i].callerlines 126–126◇ if caller not in called and caller not in enteredlines 127–127entered.append(caller)lines 128–128return Slice(exercised, tuple(entered))

Flows

_windows

list[tuple[int, int]] clew.wiki.slices._windows(tuple[int,...] exercised, int pad, int n)

The windows a slice is drawn in: each exercised run padded, overlapping ones merged.

clew/wiki/slices.py:169

Show on the map →

Called by slice_svg

Calls none

Source · 18 lines
def _windows(exercised: tuple[int, ...], pad: int, n: int) -> list[tuple[int, int]]:
    """W10 (2.0.0): ONE window from the first exercised step to the last drew every dimmed step
    between, 38 of them for a test that ran steps 2 and 40. A gap the two paddings do not close
    now starts a new window.

    @brief Split a slice into windows.
    @return Windows.
    """
    out: list[tuple[int, int]] = []
    for i in exercised:
        lo, hi = max(0, i - pad), min(n, i + pad + 1)
        if out and lo <= out[-1][1]:
            out[-1] = (out[-1][0], max(out[-1][1], hi))
        else:
            out.append((lo, hi))
    return out

Activity · 8 steps
lines 177–177out: list[tuple[int, int]] = []lines 178–178◇ for i in exercisedlines 179–179lo, hi = max(0, i - pad), min(n, i + pad + 1)lines 180–180◇ if out and lo <= out[-1][1]lines 181–181out[-1] = (out[-1][0], max(out[-1][1], hi))lines 182–182elselines 183–183out.append((lo, hi))lines 184–184return out

Flows

lines_per_test

dict[str, Lines] clew.wiki.slices.lines_per_test(Contexts contexts)

Per-test coverage turned around: each test's lines per file.

clew/wiki/slices.py:66

Source · 10 lines
def lines_per_test(contexts: Contexts) -> dict[str, Lines]:
    """@brief Group lines by test. @return Lines per test."""
    out: dict[str, Lines] = {}
    for rel, lines in contexts.items():
        for line, tests in lines.items():
            for test in tests:
                out.setdefault(test, {}).setdefault(rel, set()).add(line)
    return out

Activity · 6 steps
lines 68–68out: dict[str, Lines] = {}lines 69–69◇ for rel, lines in contexts.items()lines 70–70◇ for line, tests in lines.items()lines 71–71◇ for test in testslines 72–72out.setdefault(test, {}).setdefault(rel, set()).add(line)lines 73–73return out

Requirements

REQ-DDB-WIKI-041

Flows

slice_of

Slice clew.wiki.slices.slice_of(Flow flow, Lines lines)

The steps of a flow one test exercised, and where it entered.

clew/wiki/slices.py:83

Source · 16 lines
def slice_of(flow: Flow, lines: Lines) -> Slice:
    """A STEP'S CALL SITE is `site_file:line` — the line in the caller where the call is made — so
    a step is exercised only if that call happened in this test. Entry points are listed in the
    order their first exercised step appears.

    @brief Slice a flow.
    @return The slice.
    """
    exercised = tuple(
        i
        for i, step in enumerate(flow.steps)
        if step.line in lines.get(step.site_file or step.file, ())
    )
    return _slice(flow, exercised)

Activity · 2 steps
lines 91–95exercised = tuple( i for i, step in enumerate(flow.steps) if step.line in lines.get(step.site_file or step.file, ()) )lines 96–96return _slice(flow, exercised)

Requirements

REQ-DDB-WIKI-041

slice_svg

str clew.wiki.slices.slice_svg(Flow flow, Slice cut, int pad=2)

A test's slice drawn as a window of its master flow.

clew/wiki/slices.py:139

Source · 23 lines
def slice_svg(flow: Flow, cut: Slice, pad: int = 2) -> str:
    """THE PORTION OF THE DIAGRAM, numbered as in the master flow so the reader can find it there:
    exercised steps lit, the rest of the window dimmed, the entry marked, only the lanes the window
    uses drawn, and what lies outside the window counted rather than drawn.

    @brief Draw a slice.
    @return HTML.
    """
    if not cut.exercised:
        return ""
    windows = _windows(cut.exercised, pad, len(flow.steps))
    parts = [_hidden(windows[0][0], "earlier")]
    for k, (lo, hi) in enumerate(windows):
        if k:
            parts.append(_hidden(lo - windows[k - 1][1], "between"))
        steps = flow.steps[lo:hi]
        used = {s.from_lane for s in steps} | {s.to_lane for s in steps}
        window = replace(flow, steps=steps, lanes=tuple(ln for ln in flow.lanes if ln.id in used))
        parts.append(flow_svg(window, marks=_marks(flow, cut, lo, hi), first=lo))
    parts.append(_hidden(len(flow.steps) - windows[-1][1], "later"))
    return f'<div class="slice">{"".join(parts)}</div>'

Activity · 13 steps
lines 147–147◇ if not cut.exercisedlines 148–148return ""lines 149–149windows = _windows(cut.exercised, pad, len(flow.steps))lines 150–150parts = [_hidden(windows[0][0], "earlier")]lines 151–151◇ for k, (lo, hi) in enumerate(windows)lines 152–152◇ if klines 153–153parts.append(_hidden(lo - windows[k - 1][1], "between"))lines 154–154steps = flow.steps[lo:hi]lines 155–155used = {s.from_lane for s in steps} | {s.to_lane for s in steps}lines 156–156window = replace(flow, steps=steps, lanes=tuple(ln for ln in flow.lanes if ln.id in used))lines 157–157parts.append(flow_svg(window, marks=_marks(flow, cut, lo, hi), first=lo))lines 158–158parts.append(_hidden(len(flow.steps) - windows[-1][1], "later"))lines 159–159return f'<div class="slice">{"".join(parts)}</div>'

Requirements

REQ-DDB-WIKI-041

Flows

slices_of

dict[str, Slice] clew.wiki.slices.slices_of(Flow flow, Contexts contexts)

Every test's slice of one flow, from its steps' call sites in one pass.

clew/wiki/slices.py:105

Source · 9 lines
def slices_of(flow: Flow, contexts: Contexts) -> dict[str, Slice]:
    """@brief Slice a flow for all its tests. @return Slices."""
    hit: dict[str, list[int]] = {}
    for i, step in enumerate(flow.steps):
        for test in (contexts.get(step.site_file or step.file) or {}).get(step.line, ()):
            hit.setdefault(test, []).append(i)
    return {test: _slice(flow, tuple(steps)) for test, steps in sorted(hit.items())}

Activity · 5 steps
lines 107–107hit: dict[str, list[int]] = {}lines 108–108◇ for i, step in enumerate(flow.steps)lines 109–109◇ for test in (contexts.get(step.site_file or step.file) or {}).get(step.line, ())lines 110–110hit.setdefault(test, []).append(i)lines 111–111return {test: _slice(flow, tuple(steps)) for test, steps in sorted(hit.items())}

Requirements

REQ-DDB-WIKI-041

Flows

test_name

str clew.wiki.slices.test_name(str key)

The bare name a per-test key shows: the part after the file.

clew/wiki/slices.py:38

Source · 10 lines
def test_name(key: str) -> str:
    """W11 (2.0.0): the key carries the file so two namesakes stay two tests; the name is what a
    reader sees and what a requirement's evidence lists.

    @brief Show a key's name.
    @return Name.
    """
    return key.rsplit("::", 1)[-1]

Activity · 1 steps
lines 45–45return key.rsplit("::", 1)[-1]

Requirements

REQ-DDB-WIKI-041

Flows