testfacts.py¶
clew/wiki/testfacts.py
What a test tests (round 3, P2a).
_context
list[str] clew.wiki.testfacts._context(list[str] lines, int at)
The Feature and Rule lines (with their tags) above a scenario.
Source · 14 lines
def _context(lines: list[str], at: int) -> list[str]:
"""@brief Heading lines. @return Lines."""
keep: list[str] = []
for i in range(at):
match = _BLOCK.match(lines[i].strip())
if match and match.group(1) in ("Feature", "Rule"):
if match.group(1) == "Feature":
keep = []
keep = [k for k in keep if not _is_rule(k)] if match.group(1) == "Rule" else keep
keep += _tags_above(lines, i) + [lines[i]]
tags = _tags_above(lines, at)
return keep + tags
Activity · 10 steps
Flows
_is_rule
bool clew.wiki.testfacts._is_rule(str line)
Whether a line opens a Rule.
Source · 5 lines
def _is_rule(line: str) -> bool:
"""@brief Rule? @return Bool."""
return line.strip().startswith("Rule:")
Activity · 1 steps
Flows
_scenario_end
int clew.wiki.testfacts._scenario_end(list[str] lines, int at)
How many lines the scenario at at spans.
Source · 18 lines
def _scenario_end(lines: list[str], at: int) -> int:
"""THE NEXT SCENARIO, RULE OR FEATURE ends it — and the tag lines just above that block
belong to it, not to this scenario.
@brief Span.
@return Count.
"""
end = len(lines)
for i in range(at + 1, len(lines)):
match = _BLOCK.match(lines[i].strip())
if match and match.group(1) not in ("Examples", "Scenarios"):
end = i
break
while end > at + 1 and lines[end - 1].strip().startswith("@"):
end -= 1
return end - at
Activity · 9 steps
Flows
_tags_above
list[str] clew.wiki.testfacts._tags_above(list[str] lines, int at)
The tag lines directly above a line.
Source · 10 lines
def _tags_above(lines: list[str], at: int) -> list[str]:
"""@brief Tags. @return Lines."""
out = []
i = at - 1
while i >= 0 and lines[i].strip().startswith("@"):
out.insert(0, lines[i])
i -= 1
return out
Activity · 6 steps
Flows
assertions
list[str] clew.wiki.testfacts.assertions(list[str] lines)
The assertion statements in a body, per framework, each read whole.
Source · 24 lines
def assertions(lines: list[str]) -> list[str]:
"""A STATEMENT OPEN AT ITS LINE'S END continues: lines are joined while parentheses are
unbalanced, at most `_MAX_SPAN` lines — never into the next statement.
@brief Read assertions.
@return Statements.
"""
out, i = [], 0
while i < len(lines):
if not _ASSERT.match(lines[i]):
i += 1
continue
parts, depth, j = [], 0, i
while j < len(lines) and j < i + _MAX_SPAN:
parts.append(lines[j].strip())
depth += lines[j].count("(") - lines[j].count(")")
j += 1
if depth <= 0:
break
out.append(" ".join(" ".join(parts).replace("( ", "(").split()))
i = j
return out
Activity · 15 steps
Requirements
Flows
feature_pages
dict[tuple[str, str], str] clew.wiki.testfacts.feature_pages(Path repo_root, list scenarios, dict[str, TestFacts] facts, Path source)
Write a page per feature file, a section per scenario the site's tests name.
Called by _build_site
Calls anchor, page_for, render_scenario, symbol_href, verifies_block
Source · 39 lines
def feature_pages(
repo_root: Path, scenarios: list, facts: dict[str, TestFacts], source: Path
) -> dict[tuple[str, str], str]:
"""@brief Write feature pages. @return Hrefs."""
from .symbols import anchor, page_for, symbol_href
by_file: dict[str, list] = {}
for sc in scenarios:
by_file.setdefault(sc.file, []).append(sc)
written: dict[tuple[str, str], str] = {}
for rel, items in sorted(by_file.items()):
try:
text = (Path(repo_root) / rel).read_text(encoding="utf-8", errors="replace")
except OSError:
continue
sections = []
for sc in items:
f = facts.get(sc.test_id, TestFacts())
sections.append(
f'<section class="symbol" id="{anchor(sc.name)}"><h2>{escape(sc.name)}</h2>'
f"{verifies_block(f, {})}{render_scenario(text, sc.line)}</section>"
)
written[(sc.test_id, rel)] = symbol_href(sc.name, rel)
page = [
"---",
"orphan: true",
"---",
"",
f"# `{rel}`",
"",
"```{raw} html",
*sections,
"```",
"",
]
(Path(source) / f"{page_for(rel)}.md").write_text("\n".join(page), encoding="utf-8")
return written
Activity · 18 steps
Requirements
Flows
gather
dict[str, TestFacts] clew.wiki.testfacts.gather(list evidence)
Invert the evidence: each test's status, requirements and mutation outcomes.
Source · 12 lines
def gather(evidence: list) -> dict[str, TestFacts]:
"""@brief Facts per test. @return Map."""
out: dict[str, TestFacts] = {}
for row in evidence:
for test, status in row.tests:
facts = out.setdefault(test, TestFacts(status=status))
facts.verifies.append((row.req_id, link_kind(row, test)))
for mechanism, test, outcome in row.mutations or ():
out.setdefault(test, TestFacts()).mutations.append((mechanism, outcome))
return out
Activity · 8 steps
Requirements
Flows
link_kind
str clew.wiki.testfacts.link_kind(Any row, str test)
How a test was linked to a requirement.
Source · 12 lines
def link_kind(row: Any, test: str) -> str:
"""@brief Link kind. @return Kind."""
for kind, tests in (
("measured", row.measured_tests),
("catalog", row.declared_tests),
("inferred", row.inferred_tests),
):
if test in tests:
return kind
return "tag"
Activity · 4 steps
Flows
render_scenario
str clew.wiki.testfacts.render_scenario(str text, int line)
One scenario of a feature file, rendered as Gherkin with its Feature and Rule.
Source · 15 lines
def render_scenario(text: str, line: int) -> str:
"""THE SCENARIO IN ITS CONTEXT: the Feature line and the Rule it sits under head it, with
their tags; the scenario runs until the next block at its level.
@brief Render Gherkin.
@return HTML.
"""
lines = text.splitlines()
head = _context(lines, line - 1)
body = []
for raw in lines[line - 1 :][: _scenario_end(lines, line - 1)]:
body.append(raw)
return f'<div class="gherkin">{_render(head + body)}</div>'
Activity · 6 steps
Requirements
Flows
verifies_block
str clew.wiki.testfacts.verifies_block(TestFacts facts, dict[str, list[str]] paths)
A test's result, the requirements it verifies (with the call path to an implementer when one is known) and its mutation outcomes.
Source · 23 lines
def verifies_block(facts: TestFacts, paths: dict[str, list[str]]) -> str:
"""@brief Verifies block. @return HTML."""
from .htmltable import tag
rows = "".join(
f'<tr><td><a class="req-badge" href="requirements.html#{escape(r)}">{escape(r)}</a></td>'
f'<td><span class="via via-{kind}">{kind}</span></td>'
f"<td>{' → '.join(f'<code>{escape(n)}</code>' for n in paths.get(r, []))}</td></tr>"
for r, kind in facts.verifies
)
out = f'<p class="result">Result: {tag(facts.status)}</p>'
if rows:
out += (
'<h4>Verifies</h4><table class="verifies"><thead><tr><th>requirement</th><th>linked'
f"</th><th>path to the implementer</th></tr></thead><tbody>{rows}</tbody></table>"
)
if facts.mutations:
out += (
'<h4>Mutations</h4><ul class="modal-list">'
+ "".join(f"<li><code>{escape(m)}</code> {escape(o)}</li>" for m, o in facts.mutations)
+ "</ul>"
)
return out1 more lines, not shown