#[cfg(test)]
mod tests {
fn rust_files_under(dir: &std::path::Path) -> Vec<std::path::PathBuf> {
let mut files = Vec::new();
let Ok(entries) = std::fs::read_dir(dir) else {
return files;
};
for entry in entries.flatten() {
let path = entry.path();
if path.is_dir() {
files.extend(rust_files_under(&path));
} else if path.extension().is_some_and(|ext| ext == "rs") {
files.push(path);
}
}
files
}
fn scan_dirs() -> Vec<std::path::PathBuf> {
let manifest = std::path::Path::new(env!("CARGO_MANIFEST_DIR"));
vec![
manifest.join("src/view"),
manifest.join("../knotra-ui/src/widget"),
]
}
fn is_scan_target(path: &std::path::Path) -> bool {
!path.ends_with("text_input_height_guard.rs")
}
fn is_comment_line(trimmed: &str) -> bool {
trimmed.starts_with("//")
}
fn blank_comment_lines(source: &str) -> String {
source
.lines()
.map(|line| {
if is_comment_line(line.trim_start()) {
" ".repeat(line.len())
} else {
line.to_owned()
}
})
.collect::<Vec<_>>()
.join("\n")
}
fn is_text_input_callee(callee: &str) -> bool {
let Some(rest) = callee.strip_suffix("text_input") else {
return false;
};
if rest.ends_with("::") {
return false;
}
rest.is_empty() || !rest.ends_with(|c: char| c.is_alphanumeric() || c == '_')
}
fn matching_close_paren(source: &str, open_paren: usize) -> Option<usize> {
let bytes = source.as_bytes();
let mut depth = 1i32;
let mut i = open_paren + 1;
while i < bytes.len() {
match bytes[i] {
b'(' => depth += 1,
b')' => {
depth -= 1;
if depth == 0 {
return Some(i);
}
}
_ => {}
}
i += 1;
}
None
}
#[derive(Debug)]
struct Violation {
location: String,
}
fn check_source(path: &std::path::Path, clean_source: &str, out: &mut Vec<Violation>) {
let mut search_from = 0;
while let Some(rel) = clean_source[search_from..].find("text_input(") {
let word_start = search_from + rel;
let paren_idx = word_start + "text_input".len();
search_from = paren_idx + 1;
let callee = clean_source[..paren_idx].trim_end();
if !is_text_input_callee(callee) {
continue;
}
let Some(close_paren) = matching_close_paren(clean_source, paren_idx) else {
continue;
};
let line = clean_source[..paren_idx].matches('\n').count() + 1;
let location = format!("{}:{line}", path.display());
let bytes = clean_source.as_bytes();
let mut depth: i32 = 0;
let mut i = close_paren + 1;
let mut has_height = false;
while i < bytes.len() {
let c = bytes[i];
match c {
b'(' | b'[' | b'{' => depth += 1,
b')' | b']' | b'}' => {
if depth == 0 {
break;
}
depth -= 1;
}
b';' | b',' if depth == 0 => break,
b'B' if clean_source[i..].starts_with("BUTTON_HEIGHT") => {
has_height = true;
break;
}
_ => {}
}
i += 1;
}
if !has_height {
out.push(Violation { location });
}
}
}
#[test]
fn every_text_input_reaches_button_height() {
let mut actual: Vec<String> = Vec::new();
let mut total_files = 0;
for dir in scan_dirs() {
let files = rust_files_under(&dir);
total_files += files.len();
for file in files.iter().filter(|f| is_scan_target(f)) {
let Ok(source) = std::fs::read_to_string(file) else {
continue;
};
let clean = blank_comment_lines(&source);
let mut violations = Vec::new();
check_source(file, &clean, &mut violations);
for v in violations {
actual.push(v.location);
}
}
}
assert!(
total_files > 20,
"found only {total_files} .rs files under the two scanned \
directories -- path resolution is broken (expected 20+ across \
view/ and widget/), not that nothing needed checking"
);
assert!(
actual.is_empty(),
"text_input(...) site(s) found with no BUTTON_HEIGHT anywhere in \
their own builder chain -- RFC-073 handoff Amendment A3 §2 \
requires every bare text_input(...) in view/ or widget/ to reach \
BUTTON_HEIGHT by padding, directly or through styled_text_input: \
{actual:?}"
);
}
}