forge
Explore
Log in

Don't you dare go Hollow

A card's body ends where the card does

Browse files at this commit

f6f8156c32a8eefc36c859ae8ab0e27ea26daddcAhmet Kaan GümüşParent: b593558
  • fix
    • a card's body ends where the card does
Show full message
🐛 fix(tests): a card's body ends where the card does

`every_card_closes_without_the_keyboard` read a card's source "up to the
next `\n    fn `" out of every source file joined end to end -- and these
methods are `pub(crate) fn`, which that cut matches none of. So each body
ran tens of thousands of characters past its own end, through the rest of
its file and into the next one: `mix_card`'s scrim could stop closing the
card and the guard still passed, on five other cards' `close_card` calls
downstream of it.

Two helpers in the tests' own module now, because a slice that starts at
`ui_source()` spills by construction and there are 42 seams to spill at:
`source_from` cuts inside the one file its needle is in, and `fn_body`
ends a method at the `    }` at the impl's own indent. The inspector's
occlusion rule reuses the first; the concatenation keeps its old job of
answering whether a line is written anywhere, and says so.
crates/app/src/tests/editing.rsmodified+3 -13
@@ -839,16 +839,6 @@ fn every_action_is_reachable_without_the_keyboard() {
839 /// the day it is added, which is the only way the two stay in step.839 /// the day it is added, which is the only way the two stay in step.
840 #[test]840 #[test]
841 fn every_card_closes_without_the_keyboard() {841 fn every_card_closes_without_the_keyboard() {
842- let source = ui_source();
843- let source = source.as_str();
844- // A fn's source, up to the next one: enough of a body to scan.
845- let body = |name: &str| -> &str {
846- let at = source
847- .find(&format!("fn {name}("))
848- .unwrap_or_else(|| panic!("no fn {name} in main.rs"));
849- let rest = &source[at..];
850- &rest[..rest[1..].find("\n fn ").map_or(rest.len(), |e| e + 1)]
851- };
852 for card in [842 for card in [
853 "keys_overlay",843 "keys_overlay",
854 "export_card",844 "export_card",
@@ -858,7 +848,7 @@ fn every_card_closes_without_the_keyboard() {
858 "mix_card",848 "mix_card",
859 "silence_card",849 "silence_card",
860 ] {850 ] {
861- let src = body(card);851+ let src = fn_body(card);
862 assert!(852 assert!(
863 src.contains("this.close_card()"),853 src.contains("this.close_card()"),
864 "{card}'s scrim swallows the press without closing the card: \854 "{card}'s scrim swallows the press without closing the card: \
@@ -873,8 +863,8 @@ fn every_card_closes_without_the_keyboard() {
873 // ...and every state that makes the window modal is a state that press863 // ...and every state that makes the window modal is a state that press
874 // clears. `exporting()` is not one of them: a running export is a job864 // clears. `exporting()` is not one of them: a running export is a job
875 // with a cancel button, not a card with a scrim.865 // with a cancel button, not a card with a scrim.
876- let close = body("close_card");866+ let close = fn_body("close_card");
877- for field in body("modal").split("self.").skip(1).filter_map(|rest| {867+ for field in fn_body("modal").split("self.").skip(1).filter_map(|rest| {
878 let name: String = rest868 let name: String = rest
879 .chars()869 .chars()
880 .take_while(|c| c.is_alphanumeric() || *c == '_')870 .take_while(|c| c.is_alphanumeric() || *c == '_')
crates/app/src/tests/layout.rsmodified+1 -5
@@ -147,11 +147,7 @@ fn an_inspector_section_occludes_no_timeline() {
147 // Whichever file the root render has come to live in, from the impl to the147 // Whichever file the root render has come to live in, from the impl to the
148 // end of it -- found rather than named, so moving the root does not quietly148 // end of it -- found rather than named, so moving the root does not quietly
149 // leave this rule reading a file the render is no longer in.149 // leave this rule reading a file the render is no longer in.
150- let render = source_files()150+ let render = source_from("impl Render for Player");
151- .iter()
152- .map(|path| std::fs::read_to_string(path).expect("a source file"))
153- .find_map(|text| text.find("impl Render for Player").map(|at| text[at..].to_string()))
154- .expect("the root render");
155 for card in [151 for card in [
156 "eq_card", "color_card", "speed_card", "silence_card", "mix_card",152 "eq_card", "color_card", "speed_card", "silence_card", "mix_card",
157 ] {153 ] {
crates/app/src/tests/mod.rsmodified+31 -0
@@ -77,6 +77,11 @@ use std::time::{Duration, Instant};
77 /// and a scan that names its files passes a rule by simply not looking where77 /// and a scan that names its files passes a rule by simply not looking where
78 /// the code went. So the files are found rather than listed: every `.rs` under78 /// the code went. So the files are found rather than listed: every `.rs` under
79 /// `src/`, read at run time.79 /// `src/`, read at run time.
80+///
81+/// For asking *whether* a line is written anywhere. Never slice it: the files
82+/// are joined end to end, so a cut that runs to "the next `fn`" runs out of
83+/// the file it started in and reads the following one as more of the same
84+/// ([`source_from`]).
80 fn ui_source() -> String {85 fn ui_source() -> String {
81 source_files()86 source_files()
82 .iter()87 .iter()
@@ -84,6 +89,32 @@ fn ui_source() -> String {
84 .collect()89 .collect()
85 }90 }
86 91
92+/// The one file `needle` is written in, from the needle to the end of *that*
93+/// file. Every scan that slices starts here, because the alternative is
94+/// slicing [`ui_source()`]: 42 files with 42 seams between them, and the last
95+/// fn of any of them -- `silence_card` in `ui/cards.rs` -- read as running on
96+/// into the file that happens to follow it. The file is found and not named,
97+/// so the code may move house without this going blind.
98+fn source_from(needle: &str) -> String {
99+ source_files()
100+ .iter()
101+ .map(|path| std::fs::read_to_string(path).expect("a source file"))
102+ .find_map(|text| text.find(needle).map(|at| text[at..].to_string()))
103+ .unwrap_or_else(|| panic!("no {needle} in the window's source"))
104+}
105+
106+/// One method's body: from its `fn` line to the ` }` that closes it. The
107+/// brace at the impl's own indent is where a method ends and nothing inside
108+/// one is written that far out -- which the old cut, "up to the next
109+/// `\n fn `", was not: these methods are `pub(crate) fn`, so it matched
110+/// none of them and read every card's body as running to the end of the
111+/// window instead.
112+fn fn_body(name: &str) -> String {
113+ let text = source_from(&format!("fn {name}("));
114+ let end = text.find("\n }\n").map_or(text.len(), |at| at + "\n }\n".len());
115+ text[..end].to_string()
116+}
117+
87 /// One named file of that source, for the scans about a single region.118 /// One named file of that source, for the scans about a single region.
88 fn src_text(rel: &str) -> String {119 fn src_text(rel: &str) -> String {
89 std::fs::read_to_string(src_dir().join(rel)).expect("a source file")120 std::fs::read_to_string(src_dir().join(rel)).expect("a source file")