requirements.definitions.declaration
Requirements index · View definition
Requirement
An mdBook chapter MAY declare a named definition using a fenced
{definition}directive. Definitions MUST use the same chapter-qualified names, identifier rules, fence rules, and nonempty-body rules as requirements and examples. Names MUST be unique across all three kinds.Each definition MUST declare exactly one nonempty
:phrase:option before its explanatory body. The phrase MUST be interpreted as plain text. Definitions MUST NOT require their own evidence records or determinations.
Rationale
I judged this from the four supplied tests in tests/definitions.rs. I did not read the implementation or the fixture() helper. The only fixture-dependent assertion is the no-glossary. check described below. The MAY sentence imposes nothing to test. The obligations map to tests as follows.
-
Shared name rules.
definitions_share_name_and_fence_rules_and_need_no_evidenceputs a definition under the directorymd/bad dir. It asserts the errorinvalid definition name "bad dir.terms.a", which exercises the identifier rules.- Valid definitions are accepted under chapter-qualified names.
long.fencedandbad.badappear as<chapter>.<id>, and the error textdefinition bad.bad:confirms the qualified form.
-
Shared fence rules.
- In
invalid_declarations_and_reference_targets_are_reported, an unclosed definition fence yieldsmissing a closing colon fence. - In
definitions_share_name_and_fence_rules_and_need_no_evidence, a four-colon opening fence containing a three-colon line in its body parses cleanly. The test asserts no error mentionslong.fenced.
- In
-
Nonempty body. The case
":phrase: only"has a phrase and no body. The test asserts adefinition bad.bad:error for it. -
Unique names across all three kinds.
duplicates_involving_named_definitions_name_the_first_declarationcovers requirement-then-definition and definition-then-example. It asserts the exact message with both locations.invalid_declarations_and_reference_targets_are_reportedalso assertsduplicate definition bad.xfor requirement-then-definition. -
Exactly one nonempty
:phrase:. The loop ininvalid_declarations_and_reference_targets_are_reportedcovers three failure shapes:- no phrase option:
Body only. - an empty phrase:
:phrase:followed by a body - two phrase options
Each asserts an error for the definition. The valid case
:phrase: fencedis accepted. - no phrase option:
-
Phrase is plain text.
phrases_are_plain_links_and_markdown_literals_are_preserveduses the phrase*alpha* & <beta>. It asserts the rendered link text is*alpha* & <beta>, so the phrase is neither interpreted as Markdown nor as HTML. It also asserts the:phrase:line is stripped from the rendered definition. -
No evidence records or determinations.
definitions_share_name_and_fence_rules_and_need_no_evidenceasserts three things:- The valid definition
long.fencedproduces no error. - No error mentions
glossary.. - The requirement
rules.readystill gets anevidence/rules.ready.json: missingerror. That shows the evidence check is active and definitions are exempt.
- The valid definition
I found no required behavior without a test and assertion. Two untested variants seem to need no separate test, since the text does not distinguish them: a definition duplicating another definition, and a phrase placed after the body.
Inputs
Tests
Input 2: outline tests/definitions.rs fn[invalid_declarations_and_reference_targets_are_reported]
- File:
tests/definitions.rs - Selector:
fn[invalid_declarations_and_reference_targets_are_reported] - Kind:
fn tests/definitions.rs:419–473
// {test}`requirements.definitions.declaration`
// {test}`requirements.definitions.references`
#[test]
fn invalid_declarations_and_reference_targets_are_reported() {
for body in [
"Body only.",
":phrase: \n\nBody.",
":phrase: one\n:phrase: two\n\nBody.",
":phrase: only",
] {
let temp = fixture();
fs::write(
temp.path().join("md/bad.md"),
format!(":::{{definition}} bad\n{body}\n:::\n"),
)
.unwrap();
assert!(
validate_repository(temp.path())
.unwrap()
.errors
.iter()
.any(|e| e.contains("definition bad.bad:"))
);
}
let temp = fixture();
fs::write(
temp.path().join("md/bad.md"),
":::{definition} open\n:phrase: open\n\nNo closing fence.\n",
)
.unwrap();
assert!(
validate_repository(temp.path())
.unwrap()
.errors
.iter()
.any(|e| e.contains("missing a closing colon fence"))
);
fs::write(temp.path().join("md/bad.md"), ":::{req} x\nRequirement.\n:::\n:::{definition} x\n:phrase: x\n\nDefinition.\n:::\n{term}`rules.ready` {term}`missing.name`").unwrap();
let errors = validate_repository(temp.path()).unwrap().errors;
assert!(
errors
.iter()
.any(|e| e.contains("duplicate definition bad.x"))
);
assert!(
errors
.iter()
.any(|e| e.contains("term rules.ready has no matching named definition"))
);
assert!(
errors
.iter()
.any(|e| e.contains("term missing.name has no matching named definition"))
);
}
Input 3: outline tests/definitions.rs fn[definitions_share_name_and_fence_rules_and_need_no_evidence]
- File:
tests/definitions.rs - Selector:
fn[definitions_share_name_and_fence_rules_and_need_no_evidence] - Kind:
fn tests/definitions.rs:475–515
// {test}`requirements.definitions.declaration`
#[test]
fn definitions_share_name_and_fence_rules_and_need_no_evidence() {
let temp = fixture();
let root = temp.path();
// A longer opening fence holds a shorter colon line in its body.
fs::write(
root.join("md/long.md"),
"::::{definition} fenced\n:phrase: fenced\n\nBody with\n:::\na shorter colon line.\n::::\n",
)
.unwrap();
fs::create_dir_all(root.join("md/bad dir")).unwrap();
fs::write(
root.join("md/bad dir/terms.md"),
":::{definition} a\n:phrase: a\n\nBody.\n:::\n",
)
.unwrap();
let errors = validate_repository(root).unwrap().errors;
assert!(
errors
.iter()
.any(|e| e.contains("invalid definition name \"bad dir.terms.a\"")),
"{errors:#?}"
);
// The long fence closes only at four colons, and definitions need no
// evidence records, unlike the requirement.
assert!(
!errors.iter().any(|e| e.contains("long.fenced")),
"{errors:#?}"
);
assert!(
!errors.iter().any(|e| e.contains("glossary.")),
"{errors:#?}"
);
assert!(
errors
.iter()
.any(|e| e.contains("evidence/rules.ready.json: missing")),
"{errors:#?}"
);
}
Input 4: outline tests/definitions.rs fn[duplicates_involving_named_definitions_name_the_first_declaration]
- File:
tests/definitions.rs - Selector:
fn[duplicates_involving_named_definitions_name_the_first_declaration] - Kind:
fn tests/definitions.rs:517–544
// {test}`requirements.ci.definition_errors`
// {test}`requirements.definitions.declaration`
#[test]
fn duplicates_involving_named_definitions_name_the_first_declaration() {
for (first, second, kind) in [
(
":::{req} x\nRequirement.\n:::\n",
":::{definition} x\n:phrase: x\n\nDefinition.\n:::\n",
"definition",
),
(
":::{definition} x\n:phrase: x\n\nDefinition.\n:::\n",
":::{example} x\nExample.\n:::\n",
"example",
),
] {
let temp = fixture();
fs::write(temp.path().join("md/bad.md"), format!("{first}\n{second}")).unwrap();
let errors = validate_repository(temp.path()).unwrap().errors;
let line = first.lines().count() + 2;
let expected =
format!("md/bad.md:{line}: duplicate {kind} bad.x; first declared at md/bad.md:1");
assert!(
errors.iter().any(|error| error.contains(&expected)),
"{expected}\n{errors:#?}"
);
}
}
Input 5: outline tests/definitions.rs fn[phrases_are_plain_links_and_markdown_literals_are_preserved]
- File:
tests/definitions.rs - Selector:
fn[phrases_are_plain_links_and_markdown_literals_are_preserved] - Kind:
fn tests/definitions.rs:590–633
// {test}`requirements.definitions.phrase_rendering`
// {test}`requirements.definitions.references`
// {test}`requirements.definitions.declaration`
#[test]
fn phrases_are_plain_links_and_markdown_literals_are_preserved() {
let temp = fixture();
let mut book = Book::new();
book.push_item(Chapter::new(
"Glossary",
":::{definition} a\n:phrase: *alpha* & <beta>\n\nThe body.\n:::\n".into(),
"glossary.md",
Vec::new(),
));
let source = "Use {term}`glossary.a`.\n\n`{term}`glossary.a``\n\n```text\n{term}`glossary.a`\n```\n\n\\{term}`glossary.a`".to_owned();
book.push_item(Chapter::new("Guide", source, "guide/deep.md", Vec::new()));
habeas::preprocess_book(&mut book, temp.path()).unwrap();
let chapters: Vec<_> = book.chapters().collect();
assert!(
chapters[0]
.content
.contains("id=\"habeas-definition-glossary.a\"")
);
assert!(!chapters[0].content.contains(":phrase:"));
assert!(!chapters[1].content.contains("shields.io"));
assert_eq!(
chapters[1]
.content
.matches("href=\"../glossary.html#habeas-definition-glossary.a\"")
.count(),
1
);
assert!(
chapters[1]
.content
.contains("*alpha* & <beta>")
);
for literal in [
"`{term}`glossary.a``",
"```text\n{term}`glossary.a`\n```",
r"\{term}`glossary.a`",
] {
assert!(chapters[1].content.contains(literal), "{literal:?}");
}
}
Other sources
Input 1: requirements selection requirements.definitions.declaration
requirements.definitions.declaration:md/requirements/definitions.md:3–7
:::{req} declaration
An mdBook chapter MAY declare a named definition using a fenced `{definition}` directive. Definitions MUST use the same chapter-qualified names, identifier rules, fence rules, and nonempty-body rules as requirements and examples. Names MUST be unique across all three kinds.
Each definition MUST declare exactly one nonempty `:phrase:` option before its explanatory body. The phrase MUST be interpreted as plain text. Definitions MUST NOT require their own evidence records or determinations.
:::