use std::path::{Path, PathBuf};
fn main_rs() -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR")).join("src/main.rs")
}
fn read_main() -> String {
let p = main_rs();
std::fs::read_to_string(&p).unwrap_or_else(|e| panic!("read {}: {e}", p.display()))
}
fn code_only(src: &str) -> String {
src.lines()
.map(|line| match line.find("//") {
Some(i) => &line[..i],
None => line,
})
.collect::<Vec<_>>()
.join("\n")
}
fn fn_body(src: &str, name: &str) -> String {
let sig = format!("fn {name}");
let start = src
.find(&sig)
.unwrap_or_else(|| panic!("`{sig}` must exist in main.rs"));
let open = src[start..]
.find('{')
.unwrap_or_else(|| panic!("`{sig}` must have a body"))
+ start;
let bytes = src.as_bytes();
let mut depth = 0usize;
for (i, b) in bytes.iter().enumerate().skip(open) {
match b {
b'{' => depth += 1,
b'}' => {
depth -= 1;
if depth == 0 {
return src[open..=i].to_string();
}
}
_ => {}
}
}
panic!("`{sig}` body is not brace-balanced");
}
#[test]
fn the_clean_verb_runs_the_state_file_diagnostic() {
let src = code_only(&read_main());
let body = fn_body(&src, "clean_iceoryx2_state");
assert!(
body.contains("report_shm_state_population("),
"`cerulion clean` must call the CER-1411 diagnostic; body was:\n{body}"
);
}
#[test]
fn the_dead_node_sweep_runs_before_the_state_file_diagnostic() {
let src = code_only(&read_main());
let body = fn_body(&src, "clean_iceoryx2_state");
let sweep = body
.find("clean_dead_nodes(")
.expect("the sweep must still be called");
let diagnostic = body
.find("report_shm_state_population(")
.expect("the diagnostic must still be called");
assert!(
sweep < diagnostic,
"the dead-node sweep must precede the state-file diagnostic; body was:\n{body}"
);
}
#[test]
fn the_reclamation_stands_down_when_dead_nodes_are_still_registered() {
let src = code_only(&read_main());
let body = fn_body(&src, "clean_iceoryx2_state");
let normalized = body.split_whitespace().collect::<Vec<_>>().join(" ");
assert!(
normalized.contains("let reclaim = !report_only && converged;"),
"`cerulion clean` must reclaim only when the operator asked AND the sweep left no \
dead nodes registered — an `||` would reclaim on a wedged desk; body was:\n{body}"
);
let call = body
.find("report_shm_state_population(")
.expect("the diagnostic must still be called");
let arg_end = body[call..]
.find(')')
.expect("the diagnostic call must be closed");
let arg = &body[call..call + arg_end];
assert!(
arg.contains("reclaim") || arg.contains("converged"),
"the diagnostic's report-only argument must carry the convergence gate, not just \
the flag; argument was:\n{arg}"
);
}
fn if_else_arms(body: &str, condition: &str) -> (String, String) {
let needle = format!("if {condition}");
let start = body
.find(&needle)
.unwrap_or_else(|| panic!("`{needle}` must exist in the body:\n{body}"));
let then = block_after(body, start);
let after_then = start + body[start..].find(&then).expect("then-block offset") + then.len();
let else_at = body[after_then..]
.find("else")
.unwrap_or_else(|| panic!("`{needle}` must have an else arm:\n{body}"))
+ after_then;
let otherwise = block_after(body, else_at);
(then, otherwise)
}
fn block_after(src: &str, from: usize) -> String {
let open = src[from..]
.find('{')
.unwrap_or_else(|| panic!("expected a block after byte {from}"))
+ from;
let bytes = src.as_bytes();
let mut depth = 0usize;
for (i, b) in bytes.iter().enumerate().skip(open) {
match b {
b'{' => depth += 1,
b'}' => {
depth -= 1;
if depth == 0 {
return src[open..=i].to_string();
}
}
_ => {}
}
}
panic!("unbalanced block after byte {from}");
}
#[test]
fn the_diagnostic_reclaims_only_when_the_operator_did_not_ask_for_a_report() {
let src = code_only(&read_main());
let body = fn_body(&src, "report_shm_state_population");
let (report_arm, reclaim_arm) = if_else_arms(&body, "report_only");
assert!(
report_arm.contains("ScanMode::ReportOnly") && !report_arm.contains("ScanMode::Reclaim"),
"the `--report-only` arm must choose ReportOnly and nothing else; arm was:\n{report_arm}"
);
assert!(
reclaim_arm.contains("ScanMode::Reclaim") && !reclaim_arm.contains("ScanMode::ReportOnly"),
"the default arm must choose Reclaim and nothing else; arm was:\n{reclaim_arm}"
);
assert!(
report_arm.contains("SHM_STATE_REPORT_BUDGET")
&& !report_arm.contains("SHM_STATE_RECLAIM_BUDGET"),
"the report arm must take the report budget; arm was:\n{report_arm}"
);
assert!(
reclaim_arm.contains("SHM_STATE_RECLAIM_BUDGET")
&& !reclaim_arm.contains("SHM_STATE_REPORT_BUDGET"),
"the reclaim arm must take the reclaim budget; arm was:\n{reclaim_arm}"
);
}
#[test]
fn the_walk_reads_the_code_it_claims_to_read() {
let raw = read_main();
let src = code_only(&raw);
assert!(
src.contains("fn clean_iceoryx2_state"),
"the stripped view must still hold the function under test"
);
assert!(
src.contains("fn report_shm_state_population"),
"and the diagnostic it calls"
);
assert!(
src.len() < raw.len(),
"the stripped view must be shorter than the source it came from"
);
}