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.cli.status_table_alignment

Requirements index · View definition

evidence Valid

Requirement

When habeas status or habeas status --all displays a Markdown table, it MUST left-align and space-pad the Definition and Evidence columns so every row, including the header and separator row, has its | column delimiters in the same positions. Column widths MUST be determined by the widest displayed cell in each column.

Rationale

The reviewed tests adequately check requirements.cli.status_table_alignment.

The only test marked for it is tests/commands.rs#fn[status_tables_align_each_column_to_its_widest_cell]. It runs both habeas status and habeas status --all, so both modes the requirement names are covered. It uses tests/commands.rs#fn[fixture], which has eight requirements with very different name lengths and evidence states.

Each obligation maps to an assertion as follows:

  • Aligned delimiters on every row. The test collects the contiguous | table lines. It requires more than two lines, so header, separator and data rows are all present. It takes the header’s | byte positions and requires exactly three of them. It then asserts that every row, including the separator, has identical | positions.
  • Left-aligned, space-padded cells. For both columns of every row, the cell must start with one space and end with a space. The character after that first space must not be a space. That rules out right-aligned or centred padding, and with the ends-with-space check it ensures padding is added.
  • Widths set by the widest displayed cell. For each column, the test computes the maximum trimmed content length over all displayed rows. It asserts that the distance between delimiters minus 3 equals that maximum. That means one leading space, one trailing space, and no extra padding beyond the widest cell. The column widths differ, from Evidence to Determination false and from short names to `requirements.stale_input`. A fixed width or a width taken from the wrong row would therefore fail.
  • Separator row. The test asserts that the separator cells trim to --- in both columns, so the separator is space-padded to the same width.
  • Label content. A final block checks the Evidence labels for the ready, absent, pending and malformed rows.

I found no obligation or boundary in the requirement text that these assertions miss. The test only inspects the first contiguous table. The requirement describes a single table, so this is not a gap.

Inputs

Tests

Input 2: outline tests/commands.rs fn[status_tables_align_each_column_to_its_widest_cell]
// {test}`requirements.cli.status_table_alignment`
// {test}`requirements.cli.shared_status_rendering`
#[test]
fn status_tables_align_each_column_to_its_widest_cell() {
    let temp = fixture();
    for args in [["status"].as_slice(), ["status", "--all"].as_slice()] {
        let output = habeas(temp.path(), args);
        assert!(output.status.success(), "{args:?}: {output:?}");
        let text = String::from_utf8(output.stdout).unwrap();
        let table = text
            .lines()
            .skip_while(|line| !line.starts_with("| "))
            .take_while(|line| line.starts_with("| "))
            .collect::<Vec<_>>();
        assert!(table.len() > 2, "{text}");
        let pipes = table[0]
            .match_indices('|')
            .map(|(index, _)| index)
            .collect::<Vec<_>>();
        assert_eq!(pipes.len(), 3, "{text}");
        for line in &table {
            let actual = line
                .match_indices('|')
                .map(|(index, _)| index)
                .collect::<Vec<_>>();
            assert_eq!(actual, pipes, "{text}");
            for cell in line.split('|').skip(1).take(2) {
                assert!(cell.starts_with(' ') && cell.ends_with(' '), "{text}");
                assert!(!cell[1..].starts_with(' '), "{text}");
            }
        }
        for column in 0..2 {
            let content_width = table
                .iter()
                .map(|line| line.split('|').nth(column + 1).unwrap().trim().len())
                .max()
                .unwrap();
            assert_eq!(
                pipes[column + 1] - pipes[column] - 3,
                content_width,
                "{text}"
            );
        }
        assert_eq!(table[1].split('|').nth(1).unwrap().trim(), "---");
        assert_eq!(table[1].split('|').nth(2).unwrap().trim(), "---");
    }
    // The labels are the evidence-state labels the mdBook index also shows.
    let output = habeas(temp.path(), &["status", "--all"]);
    let text = String::from_utf8(output.stdout).unwrap();
    for (name, state) in [
        ("ready", "Valid"),
        ("absent", "Missing"),
        ("pending", "Determination false"),
        ("malformed", "Invalid"),
    ] {
        let row = text
            .lines()
            .find(|line| line.starts_with(&format!("| `requirements.{name}` ")))
            .unwrap_or_else(|| panic!("missing {name} in:\n{text}"));
        assert_eq!(row.split('|').nth(2).unwrap().trim(), state, "{text}");
    }
}

Other sources

Input 1: requirements selection requirements.cli.status_table_alignment
:::{req} status_table_alignment
When `habeas status` or `habeas status --all` displays a Markdown table, it MUST left-align and space-pad the Definition and Evidence columns so every row, including the header and separator row, has its `|` column delimiters in the same positions. Column widths MUST be determined by the widest displayed cell in each column.
:::
Input 3: outline tests/commands.rs fn[fixture]
// {cite}`habeas:util`
fn fixture() -> TempDir {
    let temp = TempDir::new().unwrap();
    fs::create_dir_all(temp.path().join("md")).unwrap();
    fs::create_dir_all(temp.path().join("evidence")).unwrap();
    fs::create_dir_all(temp.path().join("src/nested")).unwrap();
    fs::write(
        temp.path().join("Cargo.toml"),
        "[package]\nname = \"fixture\"\n",
    )
    .unwrap();
    fs::write(temp.path().join("book.toml"), "[book]\nsrc = \"md\"\n").unwrap();
    fs::write(temp.path().join("md/SUMMARY.md"), "# Summary\n").unwrap();
    let requirements = [
        ("ready", "Ready MUST work."),
        ("xready", "Xready MUST work."),
        ("pending", "Pending MUST work."),
        ("stale", "Stale MUST work."),
        ("stale_input", "Stale input MUST work."),
        ("absent", "Absent MUST work."),
        ("malformed", "Malformed MUST work."),
        ("uncovered", "Uncovered MUST work."),
    ];
    let mut source = String::from("# Requirements\n\n");
    for (name, text) in requirements {
        source.push_str(&format!(":::{{req}} {name}\n{text}\n:::\n\n"));
    }
    source.push_str(":::{example} case\nGiven ready, it works. {cite}`requirements.ready`\n:::\n");
    let case_line = source
        .lines()
        .position(|line| line.contains("Given ready, it works."))
        .unwrap()
        + 1;
    fs::write(temp.path().join("md/requirements.md"), source).unwrap();
    fs::write(
        temp.path().join("src/main.rs"),
        // No outline node follows the comment, so it belongs to the root and
        // `main` stays uncited.
        "fn main() {}\n// {test}`requirements.uncovered`\n",
    )
    .unwrap();
    for (name, text, determination) in [
        ("ready", "Ready MUST work.", true),
        ("xready", "Xready MUST work.", true),
        ("pending", "Pending MUST work.", false),
        ("stale", "Old requirement.", true),
        ("stale_input", "Stale input MUST work.", true),
        ("uncovered", "Uncovered MUST work.", true),
    ] {
        let inputs = if name == "ready" {
            vec![
                habeas::outline_input(temp.path(), "src/main.rs", "fn[main]").unwrap(),
                habeas::outline_inputs_for_lines(
                    temp.path(),
                    "md/requirements.md",
                    case_line,
                    case_line,
                )
                .unwrap()
                .remove(0),
            ]
        } else if name == "stale_input" {
            vec![json!({
                "file": "src/main.rs",
                "item": "fn[main]",
                "hash": "0".repeat(64)
            })]
        } else {
            vec![]
        };
        fs::write(
            temp.path()
                .join(format!("evidence/requirements.{name}.json")),
            json!({
                "requirement": text,
                "determination": determination,
                "rationale": format!("**{name}** was inspected."),
                "references": habeas::reference_set_digest_for(
                    temp.path(),
                    &format!("requirements.{name}")
                )
                .unwrap(),
                "inputs": inputs
            })
            .to_string(),
        )
        .unwrap();
    }
    fs::write(
        temp.path().join("evidence/requirements.malformed.json"),
        "{broken",
    )
    .unwrap();
    common::coverage_setup(temp.path());
    temp
}