diff --git a/crates/app/src/tests/editing.rs b/crates/app/src/tests/editing.rs index 616f8d2..4213a7b 100644 --- a/crates/app/src/tests/editing.rs +++ b/crates/app/src/tests/editing.rs @@ -839,16 +839,6 @@ fn every_action_is_reachable_without_the_keyboard() { /// the day it is added, which is the only way the two stay in step. #[test] fn every_card_closes_without_the_keyboard() { - let source = ui_source(); - let source = source.as_str(); - // A fn's source, up to the next one: enough of a body to scan. - let body = |name: &str| -> &str { - let at = source - .find(&format!("fn {name}(")) - .unwrap_or_else(|| panic!("no fn {name} in main.rs")); - let rest = &source[at..]; - &rest[..rest[1..].find("\n fn ").map_or(rest.len(), |e| e + 1)] - }; for card in [ "keys_overlay", "export_card", @@ -858,7 +848,7 @@ fn every_card_closes_without_the_keyboard() { "mix_card", "silence_card", ] { - let src = body(card); + let src = fn_body(card); assert!( src.contains("this.close_card()"), "{card}'s scrim swallows the press without closing the card: \ @@ -873,8 +863,8 @@ fn every_card_closes_without_the_keyboard() { // ...and every state that makes the window modal is a state that press // clears. `exporting()` is not one of them: a running export is a job // with a cancel button, not a card with a scrim. - let close = body("close_card"); - for field in body("modal").split("self.").skip(1).filter_map(|rest| { + let close = fn_body("close_card"); + for field in fn_body("modal").split("self.").skip(1).filter_map(|rest| { let name: String = rest .chars() .take_while(|c| c.is_alphanumeric() || *c == '_') diff --git a/crates/app/src/tests/layout.rs b/crates/app/src/tests/layout.rs index eed39e7..0bd583a 100644 --- a/crates/app/src/tests/layout.rs +++ b/crates/app/src/tests/layout.rs @@ -147,11 +147,7 @@ fn an_inspector_section_occludes_no_timeline() { // Whichever file the root render has come to live in, from the impl to the // end of it -- found rather than named, so moving the root does not quietly // leave this rule reading a file the render is no longer in. - let render = source_files() - .iter() - .map(|path| std::fs::read_to_string(path).expect("a source file")) - .find_map(|text| text.find("impl Render for Player").map(|at| text[at..].to_string())) - .expect("the root render"); + let render = source_from("impl Render for Player"); for card in [ "eq_card", "color_card", "speed_card", "silence_card", "mix_card", ] { diff --git a/crates/app/src/tests/mod.rs b/crates/app/src/tests/mod.rs index 98d0c55..268a0aa 100644 --- a/crates/app/src/tests/mod.rs +++ b/crates/app/src/tests/mod.rs @@ -77,6 +77,11 @@ use std::time::{Duration, Instant}; /// and a scan that names its files passes a rule by simply not looking where /// the code went. So the files are found rather than listed: every `.rs` under /// `src/`, read at run time. +/// +/// For asking *whether* a line is written anywhere. Never slice it: the files +/// are joined end to end, so a cut that runs to "the next `fn`" runs out of +/// the file it started in and reads the following one as more of the same +/// ([`source_from`]). fn ui_source() -> String { source_files() .iter() @@ -84,6 +89,32 @@ fn ui_source() -> String { .collect() } +/// The one file `needle` is written in, from the needle to the end of *that* +/// file. Every scan that slices starts here, because the alternative is +/// slicing [`ui_source()`]: 42 files with 42 seams between them, and the last +/// fn of any of them -- `silence_card` in `ui/cards.rs` -- read as running on +/// into the file that happens to follow it. The file is found and not named, +/// so the code may move house without this going blind. +fn source_from(needle: &str) -> String { + source_files() + .iter() + .map(|path| std::fs::read_to_string(path).expect("a source file")) + .find_map(|text| text.find(needle).map(|at| text[at..].to_string())) + .unwrap_or_else(|| panic!("no {needle} in the window's source")) +} + +/// One method's body: from its `fn` line to the ` }` that closes it. The +/// brace at the impl's own indent is where a method ends and nothing inside +/// one is written that far out -- which the old cut, "up to the next +/// `\n fn `", was not: these methods are `pub(crate) fn`, so it matched +/// none of them and read every card's body as running to the end of the +/// window instead. +fn fn_body(name: &str) -> String { + let text = source_from(&format!("fn {name}(")); + let end = text.find("\n }\n").map_or(text.len(), |at| at + "\n }\n".len()); + text[..end].to_string() +} + /// One named file of that source, for the scans about a single region. fn src_text(rel: &str) -> String { std::fs::read_to_string(src_dir().join(rel)).expect("a source file")