| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
use std::collections::BTreeSet; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
const RENDERER_OWN: &[&str] = &[ |
| 66 |
"act-submit", |
| 67 |
"chip-remove", |
| 68 |
"field-writes", |
| 69 |
"figure-act", |
| 70 |
"figures", |
| 71 |
"form", |
| 72 |
"heading", |
| 73 |
"notices", |
| 74 |
"region", |
| 75 |
"rest", |
| 76 |
"rest-more", |
| 77 |
"rich", |
| 78 |
"row-activate", |
| 79 |
"row-menu", |
| 80 |
"row-select", |
| 81 |
"selector", |
| 82 |
"table-sort", |
| 83 |
"text", |
| 84 |
]; |
| 85 |
|
| 86 |
|
| 87 |
const SOURCES: &[(&str, &str)] = &[ |
| 88 |
("src/node.rs", include_str!("../src/node.rs")), |
| 89 |
("src/lib.rs", include_str!("../src/lib.rs")), |
| 90 |
("src/shell.rs", include_str!("../src/shell.rs")), |
| 91 |
]; |
| 92 |
|
| 93 |
#[test] |
| 94 |
fn every_class_this_renderer_emits_is_makeovers_or_declared_as_its_own() { |
| 95 |
let opts = makeover_webview::Emit::default(); |
| 96 |
let makeover = makeover_webview::vocabulary::names(&opts); |
| 97 |
let mut stray: Vec<String> = Vec::new(); |
| 98 |
|
| 99 |
for (name, src) in SOURCES { |
| 100 |
for (line, literal) in class_literals(src) { |
| 101 |
if makeover.contains(&literal) || RENDERER_OWN.contains(&literal.as_str()) { |
| 102 |
continue; |
| 103 |
} |
| 104 |
stray.push(format!(" {name}:{line} \"{literal}\"")); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
assert!( |
| 109 |
stray.is_empty(), |
| 110 |
"{} class name(s) are neither makeover's nor declared in RENDERER_OWN:\n{}\n\n\ |
| 111 |
If makeover already answers for this thing, call its naming function \ |
| 112 |
(`class`, `option_class`, `part_class`, `cell_part_class`) instead of \ |
| 113 |
spelling the name here -- that is the 0.27.0 defect, where `tabs` and \ |
| 114 |
`segmented` rendered flat because makeover's rules say `tab` and \ |
| 115 |
`segment`. If it is genuinely this renderer's, a behavioural hook or a \ |
| 116 |
container makeover has no word for, add it to RENDERER_OWN and say which.", |
| 117 |
stray.len(), |
| 118 |
stray.join("\n") |
| 119 |
); |
| 120 |
} |
| 121 |
|
| 122 |
#[test] |
| 123 |
fn nothing_this_renderer_claims_as_its_own_is_something_makeover_already_names() { |
| 124 |
|
| 125 |
|
| 126 |
let opts = makeover_webview::Emit::default(); |
| 127 |
let makeover = makeover_webview::vocabulary::names(&opts); |
| 128 |
let overlap: Vec<&&str> = RENDERER_OWN |
| 129 |
.iter() |
| 130 |
.filter(|name| makeover.contains(**name)) |
| 131 |
.collect(); |
| 132 |
assert!( |
| 133 |
overlap.is_empty(), |
| 134 |
"makeover defines {overlap:?}, so this renderer must not claim to own it. \ |
| 135 |
Delete the entry from RENDERER_OWN; the emitted name is already correct." |
| 136 |
); |
| 137 |
} |
| 138 |
|
| 139 |
#[test] |
| 140 |
fn renderer_own_carries_nothing_that_stopped_being_emitted() { |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
let emitted: BTreeSet<String> = SOURCES |
| 145 |
.iter() |
| 146 |
.flat_map(|(_, src)| class_literals(src).into_iter().map(|(_, l)| l)) |
| 147 |
.collect(); |
| 148 |
let dead: Vec<&&str> = RENDERER_OWN |
| 149 |
.iter() |
| 150 |
.filter(|name| !emitted.contains(**name)) |
| 151 |
.collect(); |
| 152 |
assert!( |
| 153 |
dead.is_empty(), |
| 154 |
"RENDERER_OWN declares {dead:?}, which nothing emits any more. Delete them." |
| 155 |
); |
| 156 |
} |
| 157 |
|
| 158 |
#[test] |
| 159 |
fn the_reader_finds_every_call_shape_and_ignores_prose() { |
| 160 |
let src = r#" |
| 161 |
// class_attr(&["not-a-real-one"]) in a comment |
| 162 |
class_attr(&["alpha"], opts, out); |
| 163 |
class_attr(&["beta", "gamma"], opts, out); |
| 164 |
out.push_str(&escape(&class("delta", opts))); |
| 165 |
class_into("epsilon", opts, out); |
| 166 |
class_attr(&[part_class(layout::RowPart::Primary)], opts, out); |
| 167 |
class_into(option_class(kind), opts, out); |
| 168 |
"#; |
| 169 |
let found: BTreeSet<String> = class_literals(src).into_iter().map(|(_, l)| l).collect(); |
| 170 |
let expected: BTreeSet<String> = ["alpha", "beta", "gamma", "delta", "epsilon"] |
| 171 |
.into_iter() |
| 172 |
.map(String::from) |
| 173 |
.collect(); |
| 174 |
|
| 175 |
|
| 176 |
assert_eq!(found, expected); |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
fn class_literals(src: &str) -> Vec<(usize, String)> { |
| 185 |
let mut out = Vec::new(); |
| 186 |
for (i, line) in src.lines().enumerate() { |
| 187 |
let code = line.trim_start(); |
| 188 |
|
| 189 |
if code.starts_with("//") { |
| 190 |
continue; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
for (call, open) in [ |
| 195 |
("class_attr(&[", ']'), |
| 196 |
("class_into(", ')'), |
| 197 |
("class(", ')'), |
| 198 |
] { |
| 199 |
let mut at = 0; |
| 200 |
while let Some(found) = line[at..].find(call) { |
| 201 |
let start = at + found + call.len(); |
| 202 |
|
| 203 |
|
| 204 |
let is_suffix = call == "class(" |
| 205 |
&& line[..at + found] |
| 206 |
.chars() |
| 207 |
.next_back() |
| 208 |
.is_some_and(|c| c.is_alphanumeric() || c == '_'); |
| 209 |
at = start; |
| 210 |
if is_suffix { |
| 211 |
continue; |
| 212 |
} |
| 213 |
let Some(end) = line[start..].find(open) else { |
| 214 |
continue; |
| 215 |
}; |
| 216 |
for literal in string_literals(&line[start..start + end]) { |
| 217 |
out.push((i + 1, literal)); |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
} |
| 222 |
out |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
fn string_literals(fragment: &str) -> Vec<String> { |
| 227 |
let mut out = Vec::new(); |
| 228 |
let mut rest = fragment; |
| 229 |
while let Some(open) = rest.find('"') { |
| 230 |
rest = &rest[open + 1..]; |
| 231 |
let Some(close) = rest.find('"') else { break }; |
| 232 |
out.push(rest[..close].to_string()); |
| 233 |
rest = &rest[close + 1..]; |
| 234 |
} |
| 235 |
out |
| 236 |
} |
| 237 |
|