requirements.examples.index
Requirements index · View definition
Requirement
The requirements index MUST list examples as well as requirements, identify each entry’s kind, and show each example’s evidence state. Example detail pages and their links MUST follow
and
; references to an example MUST link to its definition.
Rationale
The reviewed tests adequately check requirements.examples.index.
Obligations derived from the text
- The index lists examples as well as requirements.
- The index identifies each entry’s kind.
- The index shows each example’s evidence state.
- Example detail pages and their links follow
requirements.index.detail_pagesandrequirements.index.navigation. This is context only, so I checked that the tests apply it to examples. - References to an example link to its definition.
Mapping to tests
- Listing and kind.
src/index.rs#mod[tests]/fn[index_lists_examples_and_links_their_detail_pages]asserts index rows of the form| [`rules.pending`](requirements-index/rules.pending.html) (example)and the same forrules.missing. It also asserts a(requirement)row forrules.ready.src/lib.rs#mod[tests]/fn[definitions_without_a_marker_get_a_default_index_and_detail_pages]asserts that the default index contains both(requirement)and(example).
- Evidence state per example.
src/index.rs#mod[tests]/fn[index_and_detail_pages_show_each_example_evidence_state]builds one example in each state: valid with a test, determination false, FIXME with an issue, changed text (Invalid), and missing evidence.- It asserts the exact state cell in each index row:
Valid,Determination false,FIXME,Invalid,Missing. - It asserts that each detail page carries the matching
alt="evidence <state>". - The
tests/outcomes.rs#fn[valid_fixme_remains_visible_in_status_info_and_book]test adds FIXME visibility on an index and a detail page.
- It asserts the exact state cell in each index row:
- Detail pages (
requirements.index.detail_pages) applied to examples.- The example detail pages are sub-items of the index chapter.
- They are titled by full name (
rules.alpha). - They follow definition order:
rules.zeta,rules.alpha,rules.cli.mdbook,rules.child.nested. The example sits between two requirements, so mixed ordering is exercised. - Their paths and numbering are asserted in
fn[definitions_without_a_marker_get_a_default_index_and_detail_pages]. - The example detail page content (
## Example, findings, rationale) is asserted infn[index_lists_examples_and_links_their_detail_pages].
- Navigation (
requirements.index.navigation) applied to examples.- The example badge has an anchored
idand a self-linkhref. This is asserted forhabeas-example-rules.pendingandhabeas-example-rules.alpha. - Every detail page, examples included, links back to the index (the
[Requirements index]check in the loop). - The example’s page links back to its definition:
[View definition](../rules.html#habeas-example-rules.pending). - The
.mdescaping, the arrow icon and the no-plaintext-link rule are checked infn[definitions_without_a_marker_get_a_default_index_and_detail_pages], with the escaping asserted oncli.mdbook. - Dedicated stylesheet and escaping tests cover the non-wrapping row and the arrow link’s target.
- The arrow-link code in
replace_definitionsdoes not branch on kind, so the example case runs the same code as the requirement case.
- The example badge has an anchored
- References to an example link to its definition.
src/lib.rs#mod[tests]/fn[examples_are_distinct_definitions_and_tests_link_to_them]references an example from another chapter. It asserts the link../requirements.html#habeas-example-requirements.something.foo. It also asserts the example badge’salt.
Gaps
I found none. The tests carry {test} markers for the subject and cover each clause, including the evidence-state distinctions, with concrete assertions. No fixme acknowledgments apply.
Inputs
Implementation
Input 9: outline src/lib.rs fn[replace_definitions]
- File:
src/lib.rs - Selector:
fn[replace_definitions] - Kind:
fn src/lib.rs:290–369
// {impl}`requirements.index.navigation`
// {impl}`requirements.references.book_badges`
// {impl}`requirements.examples.definition`
// {impl}`requirements.examples.index`
fn replace_definitions(
content: &str,
namespace: &str,
current_path: Option<&Path>,
plan: Option<&index::DetailPlan>,
) -> String {
let mut result = String::with_capacity(content.len());
let mut cursor = 0;
// A directive whose full name is invalid remains literal.
for block in definition_blocks(content)
.into_iter()
.filter(|block| block.closed && valid_name(&full_requirement_name(namespace, &block.name)))
{
if block.start < cursor {
continue;
}
result.push_str(&content[cursor..block.start]);
let full_name = full_requirement_name(namespace, &block.name);
// Valid names contain no HTML-special characters, so this escaping
// only guards the inserted HTML against future name rules.
// {impl}`requirements.references.safe_badges`
let escaped_name = escape_html(&full_name);
if block.kind == DefinitionKind::Term {
if let Ok((phrase, body)) =
terms::declaration(&content[block.body_start..block.body_end])
{
result.push_str(&format!(
"<strong id=\"{}\">{}</strong>\n\n",
escape_html(&block.kind.anchor(&full_name)),
terms::escaped_phrase(phrase)
));
result.push_str(body);
result.push('\n');
} else {
result.push_str(&content[block.start..block.end]);
}
cursor = block.end;
continue;
}
let color = if block.kind == DefinitionKind::Example {
"5b62a8"
} else {
"006ba8"
};
let badge = shield_badge(block.kind.badge(), &full_name, color);
let detail_href = current_path
.zip(plan)
.and_then(|(current, plan)| plan.detail_path(&full_name).map(|path| (current, path)))
.map(|(current, path)| escape_html(&chapter_href(current, path, None)));
let anchor = format!("habeas-{}-{escaped_name}", block.kind.badge());
let permalink = format!("#{anchor}").replace(".md", "%2Emd");
result.push_str(&format!(
"<span class=\"habeas-definition-badge\"><a class=\"habeas-req-badge\" id=\"{anchor}\" href=\"{permalink}\" title=\"Link to this definition\">{badge}</a>"
));
if let Some(href) = &detail_href {
result.push_str(&format!(
"<a class=\"habeas-detail-icon\" href=\"{href}\" style=\"background-color: #{color}\" title=\"View evidence\" aria-label=\"View evidence for {escaped_name}\"><svg width=\"14\" height=\"14\" viewBox=\"0 0 16 16\" fill=\"none\" stroke=\"#fff\" stroke-width=\"1.5\" aria-hidden=\"true\" focusable=\"false\"><path d=\"M3 8h10M8 3l5 5-5 5\"/></svg></a>"
));
}
result.push_str("</span>\n\n");
// Option lines such as anchors are metadata, not body text.
let body = &content[block.text_start..block.body_end];
let body = if block.kind == DefinitionKind::Requirement {
replace_query_directives(body)
} else {
body.to_owned()
};
result.push_str(&replace_definitions(&body, namespace, current_path, plan));
result.push('\n');
cursor = block.end;
}
result.push_str(&content[cursor..]);
result
}
Tests
Input 4: outline src/index.rs mod[tests]/fn[index_and_detail_pages_show_each_example_evidence_state]
- File:
src/index.rs - Selector:
mod[tests]/fn[index_and_detail_pages_show_each_example_evidence_state] - Kind:
fn src/index.rs:1919–2018
// {test}`requirements.examples.index`
#[test]
fn index_and_detail_pages_show_each_example_evidence_state() {
let temp = TempDir::new().unwrap();
let root = temp.path();
for dir in ["md", "evidence", "src"] {
fs::create_dir_all(root.join(dir)).unwrap();
}
fs::write(root.join("book.toml"), "[book]\nsrc = \"md\"\n").unwrap();
let body = |name: &str| format!("The {name} case. {{cite}}`rules.ready`");
let names = ["valid", "pending", "fixme", "changed", "missing"];
let mut source = ":::{req} ready\nReady.\n:::\n".to_owned();
for name in names {
source.push_str(&format!("\n:::{{example}} {name}\n{}\n:::\n", body(name)));
}
fs::write(root.join("md/rules.md"), &source).unwrap();
fs::write(
root.join("src/lib.rs"),
"// {test}`rules.valid`\nfn valid_case() {}\n\n// :::{fixme} rules.fixme\n// :github: example/rules#7\n// The case is untested.\n// :::\nfn fixme_case() {}\n",
)
.unwrap();
let test_input =
crate::inputs::outline_input(root, "src/lib.rs", "fn[valid_case]").unwrap();
let fixme_input =
crate::inputs::outline_input(root, "src/lib.rs", "fn[fixme_case]").unwrap();
for (name, determination, text, inputs) in [
("valid", json!(true), body("valid"), json!([test_input])),
("pending", json!(false), body("pending"), json!([])),
(
"fixme",
json!({"FIXME": ["example/rules#7"]}),
body("fixme"),
json!([fixme_input]),
),
(
"changed",
json!(true),
"An older case text.".to_owned(),
json!([]),
),
] {
fs::write(
root.join(format!("evidence/rules.{name}.json")),
json!({
"example": text,
"determination": determination,
"rationale": format!("The {name} fixture."),
"inputs": inputs,
})
.to_string(),
)
.unwrap();
}
let mut book = Book::new();
book.push_item(Chapter::new("Rules", source, "rules.md", Vec::new()));
book.push_item(Chapter::new(
"Index",
":::{requirements-index}\n:::\n".to_owned(),
"requirements-index.md",
Vec::new(),
));
crate::evidence::tests::stamp_reference_sets(root);
preprocess_book(&mut book, root).unwrap();
let index = book
.chapters()
.find(|chapter| chapter.name == "Index")
.unwrap();
for (name, state) in [
("valid", "Valid"),
("pending", "Determination false"),
("fixme", "FIXME"),
("changed", "Invalid"),
("missing", "Missing"),
] {
let link =
format!("| [`rules.{name}`](requirements-index/rules.{name}.html) (example)");
let row = index
.content
.lines()
.find(|line| line.starts_with(&link))
.unwrap_or_else(|| panic!("missing {link} in:\n{}", index.content));
assert_eq!(row.split('|').nth(2).unwrap().trim(), state, "{row}");
let page = index
.sub_items
.iter()
.find_map(|item| match item {
BookItem::Chapter(chapter) if chapter.name == format!("rules.{name}") => {
Some(chapter)
}
_ => None,
})
.unwrap();
assert!(
page.content.contains(&format!("alt=\"evidence {state}\"")),
"{name}: {}",
page.content
);
}
}
Input 5: outline src/index.rs mod[tests]/fn[index_lists_examples_and_links_their_detail_pages]
- File:
src/index.rs - Selector:
mod[tests]/fn[index_lists_examples_and_links_their_detail_pages] - Kind:
fn src/index.rs:2020–2112
// {test}`requirements.examples.index`
// {test}`requirements.index.detail_format`
// {test}`requirements.index.invalid_detail`
#[test]
fn index_lists_examples_and_links_their_detail_pages() {
let temp = TempDir::new().unwrap();
fs::create_dir_all(temp.path().join("md")).unwrap();
fs::create_dir_all(temp.path().join("evidence")).unwrap();
fs::write(temp.path().join("book.toml"), "[book]\nsrc = \"md\"\n").unwrap();
let source = ":::{req} ready\nReady.\n:::\n\n:::{example} pending\nPending case. {cite}`rules.ready`\n:::\n\n:::{example} missing\nMissing case. {cite}`rules.ready`\n:::\n";
fs::write(temp.path().join("md/rules.md"), source).unwrap();
fs::write(
temp.path().join("evidence/rules.pending.json"),
json!({
"example": "Pending case. {cite}`rules.ready`",
"determination": false,
"rationale": "The case still needs a test.",
"inputs": []
})
.to_string(),
)
.unwrap();
let mut book = Book::new();
book.push_item(Chapter::new(
"Rules",
source.to_owned(),
"rules.md",
Vec::new(),
));
book.push_item(Chapter::new(
"Index",
":::{requirements-index}\n:::\n".to_owned(),
"requirements-index.md",
Vec::new(),
));
preprocess_book(&mut book, temp.path()).unwrap();
let rules = book.chapters().next().unwrap();
assert!(rules.content.contains(
"id=\"habeas-example-rules.pending\" href=\"#habeas-example-rules.pending\""
));
let index = book
.chapters()
.find(|chapter| chapter.name == "Index")
.unwrap();
assert!(
index
.content
.contains("| [`rules.pending`](requirements-index/rules.pending.html) (example)")
);
assert!(
index
.content
.contains("| [`rules.missing`](requirements-index/rules.missing.html) (example)")
);
// The requirement's row is identified as one too.
assert!(
index
.content
.contains("| [`rules.ready`](requirements-index/rules.ready.html) (requirement)"),
"{}",
index.content
);
let pending = index
.sub_items
.iter()
.find_map(|item| match item {
BookItem::Chapter(chapter) if chapter.name == "rules.pending" => Some(chapter),
_ => None,
})
.unwrap();
assert!(pending.content.contains("## Example\n\n> Pending case."));
assert!(
pending
.content
.contains("[View definition](../rules.html#habeas-example-rules.pending)")
);
assert!(pending.content.contains("The case still needs a test."));
let missing = index
.sub_items
.iter()
.find_map(|item| match item {
BookItem::Chapter(chapter) if chapter.name == "rules.missing" => Some(chapter),
_ => None,
})
.unwrap();
assert!(missing.content.contains("## Example\n\n> Missing case."));
assert!(missing.content.contains("## Validation findings"));
assert!(missing.content.contains("missing for example"));
assert!(missing.content.contains("No rationale recorded."));
}
Input 6: outline src/lib.rs mod[tests]/fn[definitions_without_a_marker_get_a_default_index_and_detail_pages]
- File:
src/lib.rs - Selector:
mod[tests]/fn[definitions_without_a_marker_get_a_default_index_and_detail_pages] - Kind:
fn src/lib.rs:1050–1185
// {test}`requirements.index.detail_pages`
// {test}`requirements.index.chapter`
// {test}`requirements.index.chapter.default`
// {test}`requirements.index.navigation`
// {test}`requirements.examples.index`
// {test}`requirements.test-coverage.index`
// {test}`requirements.references.anchors`
#[test]
fn definitions_without_a_marker_get_a_default_index_and_detail_pages() {
let mut book = Book::new();
let mut chapter = Chapter::new(
"Rules",
"# Rules\n\n:::{req} zeta\n:impl: notes.txt\nZeta.\n:::\n\n:::{example} alpha\nAlpha case. {cite}`rules.zeta`\n:::\n\n:::{req} cli.mdbook\nMdbook.\n:::\n".to_owned(),
"rules.md",
Vec::new(),
);
chapter.number = Some(mdbook_preprocessor::book::SectionNumber::new(vec![2]));
chapter.sub_items.push(BookItem::Chapter(Chapter::new(
"Child",
":::{req} nested\nNested.\n:::\n".to_owned(),
"rules/child.md",
vec!["Rules".to_owned()],
)));
book.push_item(chapter);
let root = book_root();
preprocess_book(&mut book, root.path()).unwrap();
let rules = book.chapters().next().unwrap();
let pages = rules
.sub_items
.iter()
.filter_map(|item| match item {
BookItem::Chapter(chapter) => Some(chapter),
_ => None,
})
.collect::<Vec<_>>();
assert_eq!(pages.len(), 1);
assert_eq!(pages[0].name, "Child");
assert_eq!(book.items.len(), 2);
assert_eq!(book.chapters().count(), 8);
assert!(rules.content.contains("habeas-detail-icon"));
assert!(!rules.content.contains("See evidence"));
assert!(!rules.content.contains("notes.txt"));
for (kind, name) in [("req", "zeta"), ("example", "alpha"), ("req", "cli.mdbook")] {
assert!(rules.content.contains(&format!(
"id=\"habeas-{kind}-rules.{name}\" href=\"#habeas-{kind}-rules.{}\"",
name.replace(".md", "%2Emd")
)));
}
assert!(
rules
.content
.contains("href=\"requirements-index/rules.cli%2Emdbook.html\"")
);
assert!(
pages[0]
.content
.contains("href=\"../requirements-index/rules.child.nested.html\"")
);
let BookItem::Chapter(index) = book.items.last().unwrap() else {
panic!("the default index must be a top-level chapter");
};
assert_eq!(index.name, "Requirements index");
assert_eq!(
index.path.as_deref(),
Some(Path::new("requirements-index.md"))
);
assert!(index.source_path.is_none());
assert!(index.parent_names.is_empty());
assert_eq!(index.number.as_deref().map(Vec::as_slice), Some(&[3][..]));
assert!(!index.content.contains("{requirements-index}"));
assert!(index.content.contains("(requirement)"));
assert!(index.content.contains("(example)"));
assert!(index.content.contains("Missing"));
let coverage = index
.sub_items
.iter()
.filter_map(|item| match item {
BookItem::Chapter(chapter) if chapter.name == "Coverage gaps" => Some(chapter),
_ => None,
})
.collect::<Vec<_>>();
assert_eq!(coverage.len(), 1);
assert_eq!(
coverage[0].path.as_deref(),
Some(Path::new("requirements-index/coverage.md"))
);
assert_eq!(coverage[0].parent_names, ["Requirements index"]);
assert!(
index
.content
.contains("[Coverage gaps](requirements-index/coverage.html)")
);
let details = index
.sub_items
.iter()
.filter_map(|item| match item {
BookItem::Chapter(chapter) if chapter.name != "Coverage gaps" => Some(chapter),
_ => None,
})
.collect::<Vec<_>>();
assert_eq!(
details
.iter()
.map(|page| page.name.as_str())
.collect::<Vec<_>>(),
[
"rules.zeta",
"rules.alpha",
"rules.cli.mdbook",
"rules.child.nested"
]
);
for (offset, page) in details.iter().enumerate() {
assert_eq!(page.parent_names, ["Requirements index"]);
assert_eq!(
page.number.as_deref().map(Vec::as_slice),
Some(&[3, offset as u32 + 2][..])
);
assert!(
page.content
.contains("[Requirements index](../requirements-index.html)")
);
}
assert!(
details[2]
.content
.contains("[View definition](../rules.html#habeas-req-rules.cli%2Emdbook)")
);
assert!(
details[3]
.content
.contains("[View definition](../rules/child.html#habeas-req-rules.child.nested)")
);
assert!(!root.path().join("md/requirements-index.md").exists());
}
Input 7: outline src/lib.rs mod[tests]/fn[examples_are_distinct_definitions_and_tests_link_to_them]
- File:
src/lib.rs - Selector:
mod[tests]/fn[examples_are_distinct_definitions_and_tests_link_to_them] - Kind:
fn src/lib.rs:1941–1995
// {test}`requirements.examples.definition`
// {test}`requirements.references.book_badges`
// {test}`requirements.ci.identifiers`
// {test}`requirements.examples.index`
#[test]
fn examples_are_distinct_definitions_and_tests_link_to_them() {
let source = "::::{req} ready\nReady.\n\n:::{example} something.foo\nGiven ready, it succeeds. {cite}`requirements.ready`\n:::\n::::\n\n:::{example} bad name\nInvalid.\n:::\n\n```text\n:::{example} hidden\nLiteral.\n:::\n```".to_owned();
let blocks = definition_blocks(&source);
assert_eq!(blocks.len(), 2);
assert_eq!(blocks[0].kind, DefinitionKind::Requirement);
assert_eq!(blocks[1].kind, DefinitionKind::Example);
let mut book = Book::new();
book.push_item(Chapter::new(
"Requirements",
source,
"requirements.md",
Vec::new(),
));
book.push_item(Chapter::new(
"Guide",
"{test}`requirements.something.foo`".to_owned(),
"guides/deep.md",
Vec::new(),
));
let root = book_root();
preprocess_book(&mut book, root.path()).unwrap();
// Generated detail pages are titled by full name.
let chapters = book
.chapters()
.filter(|chapter| !chapter.name.contains('.'))
.collect::<Vec<_>>();
assert!(
chapters[0]
.content
.contains("id=\"habeas-req-requirements.ready\"")
);
assert!(
chapters[0]
.content
.contains("id=\"habeas-example-requirements.something.foo\"")
);
assert!(
chapters[0]
.content
.contains("alt=\"example requirements.something.foo\"")
);
assert!(chapters[0].content.contains(":::{example} bad name"));
assert!(chapters[0].content.contains(":::{example} hidden"));
assert!(
chapters[1].content.contains(
"href=\"../requirements.html#habeas-example-requirements.something.foo\""
)
);
}
Input 8: outline tests/outcomes.rs fn[valid_fixme_remains_visible_in_status_info_and_book]
- File:
tests/outcomes.rs - Selector:
fn[valid_fixme_remains_visible_in_status_info_and_book] - Kind:
fn tests/outcomes.rs:471–527
// {test}`requirements.cli.status`
// {test}`requirements.cli.fixme_summary`
// {test}`requirements.cli.info`
// {test}`requirements.index.state`
// {test}`requirements.index.detail_format`
// {test}`requirements.index.detail_pages`
// {test}`requirements.examples.index`
#[test]
fn valid_fixme_remains_visible_in_status_info_and_book() {
let temp = fixture();
for all in [false, true] {
let status = habeas::status_markdown(temp.path(), all).unwrap();
assert_eq!(status.contains("requirements.timeout"), all, "{status}");
assert!(status.contains("FIXME"));
assert!(status.contains("example/io#125"));
assert!(status.contains("rust-lang/rust#123"));
assert!(status.contains("affects 1 judgment"), "{status}");
assert!(!status.contains("## Validation findings"));
}
let verbose = habeas::status_markdown_with_options(temp.path(), false, None, true).unwrap();
assert!(verbose.contains("requirements.timeout"), "{verbose}");
assert!(verbose.contains("example"), "{verbose}");
let info = habeas::info_markdown(temp.path(), "requirements.timeout").unwrap();
assert!(info.contains("## FIXME references"));
assert!(info.contains("rust-lang/rust#123"));
let mut book = Book::new();
book.push_item(Chapter::new(
"Requirements",
fs::read_to_string(temp.path().join("md/requirements.md")).unwrap(),
"requirements.md",
Vec::new(),
));
book.push_item(Chapter::new(
"Index",
":::{requirements-index}\n:::\n".to_owned(),
"requirements-index.md",
Vec::new(),
));
habeas::preprocess_book(&mut book, temp.path()).unwrap();
let index = book
.chapters()
.find(|chapter| chapter.name == "Index")
.unwrap();
assert!(index.content.contains("FIXME"));
let detail = index
.sub_items
.iter()
.find_map(|item| match item {
BookItem::Chapter(chapter) if chapter.name == "requirements.timeout" => Some(chapter),
_ => None,
})
.unwrap();
assert!(detail.content.contains("evidence-FIXME"));
assert!(detail.content.contains("## FIXME references"));
assert!(detail.content.contains("rust-lang/rust#123"));
}
Referenced definitions
Input 2: requirements selection requirements.index.detail_pages
requirements.index.detail_pages:md/requirements/index/README.md:11–19
:::{req} detail_pages
`habeas mdbook` MUST generate one evidence detail
page for every requirement and named example defined in a rendered chapter.
Each page MUST be a subchapter of the index chapter, in definition order,
titled by the definition's full name, at a path derived from the index chapter's
path.
Books configured by {cite}`requirements.cli.setup` fold these subchapters in the
sidebar until a reader expands the index chapter.
:::
Input 3: requirements selection requirements.index.navigation
requirements.index.navigation:md/requirements/index/README.md:29–32
:::{req} navigation
Each requirement and example definition badge MUST link to its own anchored definition on the current page. The badge MUST have a separate adjacent link containing an accessible right-arrow icon in an integrated segment matching the badge color linking to its evidence detail page. The badge and arrow MUST remain aligned in one non-wrapping row. Definitions MUST NOT be followed by a separate plaintext evidence link. Each detail page MUST link back to the definition's anchored badge and to the index. Reference badges MUST continue to link to the definition. These links MUST resolve when a requirement name contains `.md`, as in `requirements.cli.mdbook`.
:::
Other sources
Input 1: requirements selection requirements.examples.index
requirements.examples.index:md/requirements/examples.md:11–13
:::{req} index
The requirements index MUST list examples as well as requirements, identify each entry's kind, and show each example's evidence state. Example detail pages and their links MUST follow {cite}`requirements.index.detail_pages` and {cite}`requirements.index.navigation`; references to an example MUST link to its definition.
:::