Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

requirements.definitions.declaration

Requirements index · View definition

evidence Valid

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_evidence puts a definition under the directory md/bad dir. It asserts the error invalid definition name "bad dir.terms.a", which exercises the identifier rules.
    • Valid definitions are accepted under chapter-qualified names. long.fenced and bad.bad appear as <chapter>.<id>, and the error text definition bad.bad: confirms the qualified form.
  • Shared fence rules.

    • In invalid_declarations_and_reference_targets_are_reported, an unclosed definition fence yields missing 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 mentions long.fenced.
  • Nonempty body. The case ":phrase: only" has a phrase and no body. The test asserts a definition bad.bad: error for it.

  • Unique names across all three kinds. duplicates_involving_named_definitions_name_the_first_declaration covers requirement-then-definition and definition-then-example. It asserts the exact message with both locations. invalid_declarations_and_reference_targets_are_reported also asserts duplicate definition bad.x for requirement-then-definition.

  • Exactly one nonempty :phrase:. The loop in invalid_declarations_and_reference_targets_are_reported covers 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: fenced is accepted.

  • Phrase is plain text. phrases_are_plain_links_and_markdown_literals_are_preserved uses the phrase *alpha* & <beta>. It asserts the rendered link text is &#42;alpha&#42; &amp; &lt;beta&gt;, 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_evidence asserts three things:

    • The valid definition long.fenced produces no error.
    • No error mentions glossary..
    • The requirement rules.ready still gets an evidence/rules.ready.json: missing error. That shows the evidence check is active and definitions are exempt.

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]
// {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]
// {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]
// {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]
// {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("&#42;alpha&#42; &amp; &lt;beta&gt;")
    );
    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
:::{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.
:::