flowchart.py¶
clew/wiki/flowchart.py
A test as a flowchart: each pinned assertion a diamond, each failure ending in a declared mode.
chart
list[Node] clew.wiki.flowchart.chart(list[Any] pins, list[str] lines, int first_line, list[str] modes)
A test's flowchart nodes, in pass-path order.
Source · 14 lines
def chart(pins: list[Any], lines: list[str], first_line: int, modes: list[str]) -> list[Node]:
"""@brief Derive the chart. @return Nodes."""
only = modes[0] if len(modes) == 1 else None
nodes = [Node("start", "start")]
for pin in pins:
if pin.target:
nodes.append(Node("step", f"{pin.target}(…)", pin.bound_line or pin.line))
index = (pin.line or 0) - first_line
source = lines[index] if 0 <= index < len(lines) else ""
nodes.append(Node("check", pin.statement, pin.line, marker(source) or only or UNASSIGNED))
nodes.append(Node("passed", "passed"))
return nodes
Activity · 10 steps
Requirements
flowchart_html
str clew.wiki.flowchart.flowchart_html(list[Node] nodes, dict[str, str] meanings, int|None observed_line=None)
The flowchart as an inline SVG.
Source · 41 lines
def flowchart_html(
nodes: list[Node], meanings: dict[str, str], observed_line: int | None = None
) -> str:
"""A MODE IS DRAWN ONCE, however many checks fail into it, so the right column reads as the
test's distinct ways to fail. An undeclared id still draws, with its id and no meaning.
@brief Render the chart.
@return HTML.
"""
if not any(n.kind == "check" for n in nodes):
return ""
## EACH MODE BESIDE THE FIRST CHECK THAT FAILS INTO IT, so a fail edge runs across rather
## than climbing the chart (found looking at the first render).
rows_of: dict[str, int] = {}
for i, n in enumerate(nodes):
if n.kind == "check":
rows_of.setdefault(str(n.fails_into), i)
## F3: THE BRANCH A RECORDED RUN TOOK — the check on the failing line, and its mode.
hit = next(
(i for i, n in enumerate(nodes) if n.kind == "check" and n.line == observed_line), None
)
hit_mode = str(nodes[hit].fails_into) if hit is not None else None
parts = [_edges(nodes, rows_of, hit)]
parts += [_node(i, n, i == hit) for i, n in enumerate(nodes)]
parts += [_mode(row, m, meanings, m == hit_mode) for m, row in rows_of.items()]
height = len(nodes) * _ROW + 20
observed = f' data-observed="{hit}"' if hit is not None else ""
note = (
f"<figcaption>The recorded run failed at line {observed_line}, into {escape(hit_mode)}."
"</figcaption>"
if hit is not None and hit_mode
else ""
)
return (
f'<figure class="flowchart"{observed}>'
'<button type="button" class="fc-play">play</button>'
f'<svg viewBox="0 0 {_MX + _W // 2 + 20} {height}" '
f'role="img" aria-label="test flowchart">{"".join(parts)}</svg>{note}{_PLAYER}</figure>'
)
Activity · 15 steps
Requirements
flowchart_section
str clew.wiki.flowchart.flowchart_section(list[Any] pins, list[str] lines, int first_line, list[str] modes, dict[str, str] meanings, int|None failed_at=None)
A test's flowchart section, or "" when the test names no failure mode.
Source · 21 lines
def flowchart_section(
pins: list[Any],
lines: list[str],
first_line: int,
modes: list[str],
meanings: dict[str, str],
failed_at: int | None = None,
) -> str:
"""DRAWN ONLY FOR A TEST THAT NAMES A MODE (in its doc or in a marker): on any other test
every end would read "unassigned", which says nothing and costs a figure on every test page.
A requirement's page lists its declared modes no test names.
@brief Flowchart section.
@return HTML.
"""
nodes = chart(pins, lines, first_line, modes)
if all(n.fails_into in (None, UNASSIGNED) for n in nodes):
return ""
return "<h2>How it can fail</h2>" + flowchart_html(nodes, meanings, failed_at)
Activity · 4 steps
Requirements
load_modes
tuple[dict[int, list[str]], dict[str, str]] clew.wiki.flowchart.load_modes(Any conn)
The failure modes the index holds: each test's, and every mode's meaning.
Source · 17 lines
def load_modes(conn: Any) -> tuple[dict[int, list[str]], dict[str, str]]:
"""@brief Load modes. @return Pair."""
tables = {r[0] for r in conn.execute("SELECT name FROM sqlite_master WHERE type = 'table'")}
meanings = (
dict(conn.execute("SELECT mode_id, meaning FROM failure_modes"))
if "failure_modes" in tables
else {}
)
by_test: dict[int, list[str]] = {}
if "test_failure_modes" in tables:
for mode, rowid in conn.execute(
"SELECT mode_id, test_memberdef_rowid FROM test_failure_modes ORDER BY mode_id"
):
by_test.setdefault(int(rowid), []).append(str(mode))
return by_test, meanings
Activity · 7 steps
Flows
marker
str | None clew.wiki.flowchart.marker(str text)
The failure-mode id an inline marker on this source line names.
Called by _apply_file, _unmarked, chart, check_fails_when_markers, test_a_marker_is_read_in_both_comment_styles
Calls none
Source · 6 lines
def marker(text: str) -> str | None:
"""@brief Marker id. @return Id or None."""
found = _MARKER.search(text)
return found.group(1) if found else None