use super::*;
use crate::docs::test_helpers::{make_function, make_test_config};
const UNIFORM_HEADINGS: &str = "Does a thing.\n\n# Returns\n\nA value.\n\n# Observability\n\nEmits a metric.\n";
const DEEP_FIRST_HEADING: &str = "Does a thing.\n\n##### Deep First\n\nText.\n\n# Observability\n\nMore text.\n";
fn api_with_doc(doc: &str) -> ApiSurface {
let mut func = make_function(
"compute_total",
vec![],
TypeRef::Primitive(PrimitiveType::U32),
false,
None,
);
func.doc = doc.to_string();
let mut ty = empty_type("Widget");
ty.doc = doc.to_string();
let mut api = crate::docs::test_helpers::empty_api();
api.crate_name = "mylib".to_string();
api.functions = vec![func];
api.types = vec![ty];
api
}
fn heading_level(line: &str) -> Option<usize> {
if !line.starts_with('#') {
return None;
}
let level = line.chars().take_while(|&c| c == '#').count();
(1..=6).contains(&level).then_some(level)
}
fn generated_pages(doc: &str) -> Vec<crate::core::GeneratedFile> {
let api = api_with_doc(doc);
let config = make_test_config();
generate_docs(&api, &config, &[Language::Rust], "out").unwrap()
}
fn is_alef_heading(title: &str) -> bool {
title.ends_with("()")
|| title == "Widget"
|| title.starts_with("Rust API Reference")
|| matches!(
title,
"Functions" | "Types" | "Other Types" | "Types Reference" | "Error Reference" | "Configuration Reference"
)
}
#[test]
fn should_not_emit_top_level_heading_for_unrecognised_rustdoc_section() {
for doc in [UNIFORM_HEADINGS, DEEP_FIRST_HEADING] {
for file in generated_pages(doc) {
let offenders: Vec<&str> = file
.content
.lines()
.filter(|line| heading_level(line) == Some(1))
.collect();
assert!(
offenders.is_empty(),
"{}: every generated page is rooted at `##`, so an H1 can only come from a rustdoc \
heading that escaped re-levelling (MD025): {offenders:?}\n{}",
file.path.display(),
file.content
);
}
}
}
#[test]
fn should_nest_unrecognised_rustdoc_heading_under_its_own_section() {
for doc in [UNIFORM_HEADINGS, DEEP_FIRST_HEADING] {
for file in generated_pages(doc) {
let mut section_level: Option<usize> = None;
for line in file.content.lines() {
let Some(level) = heading_level(line) else {
continue;
};
let title = line.trim_start_matches('#').trim();
if is_alef_heading(title) {
section_level = Some(level);
continue;
}
let parent = section_level.expect("a doc heading always follows an alef-emitted heading");
assert!(
level > parent,
"{}: doc heading {line:?} sits at H{level} under an H{parent} section — it reads \
as a sibling of its own parent",
file.path.display()
);
}
}
}
}