#[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_size_guard.rs")
}
fn is_comment_line(trimmed: &str) -> bool {
trimmed.starts_with("//")
}
fn count_raw_size_literals(source: &str) -> usize {
let mut count = 0;
for line in source.lines() {
let trimmed = line.trim_start();
if is_comment_line(trimmed) {
continue;
}
let mut rest = line;
while let Some(pos) = rest.find(".size(") {
let after = &rest[pos + ".size(".len()..];
if after.trim_start().starts_with(|c: char| c.is_ascii_digit()) {
count += 1;
}
rest = after;
}
}
count
}
#[test]
fn no_raw_text_size_literal_remains_in_the_view_tree() {
let mut actual: std::collections::BTreeMap<String, usize> =
std::collections::BTreeMap::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 n = count_raw_size_literals(&source);
if n > 0 {
actual.insert(file.display().to_string(), n);
}
}
}
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(),
"raw `.size(<numeric literal>)` call sites found outside \
comments -- RFC-056 R8 requires every text size to come from a \
`snora::design::style::text::*_size(tokens)` role helper, not \
a literal:\n{actual:#?}"
);
}
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 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
}
const PAIRED_ROLES: &[(&str, &str)] = &[
("body_size", "body_line_height"),
("body_small_size", "body_small_line_height"),
];
fn starts_with_line_height_call(after: &str, lh_fn: &str) -> bool {
let Some(rest) = after.strip_prefix(".line_height(") else {
return false;
};
rest.trim_start()
.starts_with(&format!("snora::design::style::text::{lh_fn}("))
}
fn count_unpaired_body_roles(clean_source: &str) -> usize {
let mut count = 0;
for (size_fn, lh_fn) in PAIRED_ROLES {
let size_prefix = format!(".size(snora::design::style::text::{size_fn}(");
let mut search_from = 0;
while let Some(rel) = clean_source[search_from..].find(&size_prefix) {
let call_start = search_from + rel;
let outer_open = call_start + ".size".len();
let Some(outer_close) = matching_close_paren(clean_source, outer_open) else {
search_from = call_start + size_prefix.len();
continue;
};
let after = clean_source[outer_close + 1..].trim_start();
if !starts_with_line_height_call(after, lh_fn) {
count += 1;
}
search_from = outer_close + 1;
}
}
count
}
#[test]
fn every_body_role_size_call_carries_a_matching_line_height() {
let mut actual: std::collections::BTreeMap<String, usize> =
std::collections::BTreeMap::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 n = count_unpaired_body_roles(&clean);
if n > 0 {
actual.insert(file.display().to_string(), n);
}
}
}
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(),
"`body_size(`/`body_small_size(` call sites found with no \
matching `.line_height(...)` -- RFC-056 Stage 3 R4 requires \
every body/body_small text size to carry its role's \
line-height (label/title/heading/display are correctly \
unpaired -- see this file's module doc comment):\n{actual:#?}"
);
}
}