slices.py¶
clew/wiki/slices.py
A test is a slice of a master flow (round 5, R5-4).
_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.
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
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.
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
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.
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
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.
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
Requirements
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.
Called by none
Tested by 5 tests
test_a_slice_is_drawn_as_a_window_of_the_master_flow, test_a_step_is_exercised_when_its_call_site_ran_in_the_test, test_a_test_that_calls_into_the_middle_enters_there, test_a_test_that_ran_none_of_the_flow_has_an_empty_slice, test_every_test_s_slice_of_a_flow_comes_from_one_pass_over_its_steps
Calls _slice
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
Requirements
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.
Called by _slices_html
Tested by
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
Requirements
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.
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
Requirements
Flows
test_name
str clew.wiki.slices.test_name(str key)
The bare name a per-test key shows: the part after the file.
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]