| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
use std::path::{Path, PathBuf}; |
| 15 |
|
| 16 |
use makeover_geometry::{Density, SizeClass}; |
| 17 |
use makeover_webview::Emit; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
const CONST_NAME: &str = "TOUCH_DENSITY"; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
const SNIFFS: &[&str] = &["ontouchstart", "maxTouchPoints"]; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
pub fn check_touch_density(js_dir: impl AsRef<Path>) { |
| 51 |
let js_dir = js_dir.as_ref(); |
| 52 |
let want = Density::Touch.media_condition(); |
| 53 |
let mut wrong: Vec<String> = Vec::new(); |
| 54 |
let mut found = 0usize; |
| 55 |
|
| 56 |
let files = js_files(js_dir); |
| 57 |
for path in &files { |
| 58 |
let src = std::fs::read_to_string(path).expect("read js file"); |
| 59 |
let name = path |
| 60 |
.strip_prefix(js_dir) |
| 61 |
.unwrap_or(path) |
| 62 |
.display() |
| 63 |
.to_string(); |
| 64 |
|
| 65 |
for (offset, literal) in touch_density_literals(&src) { |
| 66 |
found += 1; |
| 67 |
if literal != want { |
| 68 |
wrong.push(format!( |
| 69 |
" {name}:{} {CONST_NAME} = '{literal}'", |
| 70 |
line_of(&src, offset) |
| 71 |
)); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
for needle in SNIFFS { |
| 76 |
if let Some(offset) = src.find(needle) { |
| 77 |
wrong.push(format!( |
| 78 |
" {name}:{} {needle} -- device sniff, not a density question", |
| 79 |
line_of(&src, offset) |
| 80 |
)); |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
assert!( |
| 86 |
found > 0, |
| 87 |
"no {CONST_NAME} literal found under {}.\n\n\ |
| 88 |
A frontend that asks whether it is being touched states\n\ |
| 89 |
makeover_geometry::Density::Touch's media condition in a const of that\n\ |
| 90 |
name, and this check exists to keep every copy equal to it. If the\n\ |
| 91 |
const was renamed, rename it back rather than dropping the check; if\n\ |
| 92 |
this frontend genuinely asks no density question, drop the call.", |
| 93 |
js_dir.display() |
| 94 |
); |
| 95 |
|
| 96 |
assert!( |
| 97 |
wrong.is_empty(), |
| 98 |
"hand-written touch detection disagrees with makeover_geometry::Density.\n\n\ |
| 99 |
Density::Touch.media_condition() is: {want}\n\n\ |
| 100 |
Wrong:\n{}\n\n\ |
| 101 |
Fix the JS to state the crate's string. Never widen it to catch a\n\ |
| 102 |
device the query misses: density is what is pointing at the screen,\n\ |
| 103 |
and a laptop with a touchscreen and a mouse is a pointer device.", |
| 104 |
wrong.join("\n") |
| 105 |
); |
| 106 |
|
| 107 |
for path in &files { |
| 108 |
println!("cargo:rerun-if-changed={}", path.display()); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
fn js_files(dir: &Path) -> Vec<PathBuf> { |
| 114 |
files_with_extension(dir, "js") |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
fn files_with_extension(dir: &Path, ext: &str) -> Vec<PathBuf> { |
| 124 |
let mut out = Vec::new(); |
| 125 |
let mut stack = vec![dir.to_path_buf()]; |
| 126 |
while let Some(d) = stack.pop() { |
| 127 |
for entry in std::fs::read_dir(&d) |
| 128 |
.unwrap_or_else(|e| panic!("read {}: {e}", d.display())) |
| 129 |
.flatten() |
| 130 |
{ |
| 131 |
let path = entry.path(); |
| 132 |
if path.is_dir() { |
| 133 |
stack.push(path); |
| 134 |
} else if path.extension().is_some_and(|x| x == ext) { |
| 135 |
out.push(path); |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
out.sort(); |
| 140 |
out |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
fn touch_density_literals(src: &str) -> Vec<(usize, &str)> { |
| 146 |
let mut out = Vec::new(); |
| 147 |
let mut at = 0; |
| 148 |
while let Some(i) = src[at..].find(CONST_NAME) { |
| 149 |
let start = at + i; |
| 150 |
at = start + CONST_NAME.len(); |
| 151 |
|
| 152 |
let Some(rest) = src[at..].strip_prefix(" = ") else { |
| 153 |
continue; |
| 154 |
}; |
| 155 |
let open = at + " = ".len(); |
| 156 |
let Some(quote @ ('\'' | '"')) = rest.chars().next() else { |
| 157 |
continue; |
| 158 |
}; |
| 159 |
let body = open + 1; |
| 160 |
if let Some(j) = src[body..].find(quote) { |
| 161 |
out.push((start, &src[body..body + j])); |
| 162 |
at = body + j + 1; |
| 163 |
} |
| 164 |
} |
| 165 |
out |
| 166 |
} |
| 167 |
|
| 168 |
fn line_of(src: &str, offset: usize) -> usize { |
| 169 |
src[..offset].matches('\n').count() + 1 |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
pub fn check_breakpoints(frontend: impl AsRef<Path>, tuning_widths: &[u16]) { |
| 206 |
let frontend = frontend.as_ref(); |
| 207 |
let mut files = files_with_extension(&frontend.join("css"), "css"); |
| 208 |
files.extend(js_files(&frontend.join("js"))); |
| 209 |
check_paths(&files, tuning_widths, Some(frontend)); |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
pub fn check_breakpoints_files<P: AsRef<Path>>(paths: &[P], tuning_widths: &[u16]) { |
| 232 |
let paths: Vec<PathBuf> = paths.iter().map(|p| p.as_ref().to_path_buf()).collect(); |
| 233 |
check_paths(&paths, tuning_widths, None); |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
fn check_paths(paths: &[PathBuf], tuning_widths: &[u16], root: Option<&Path>) { |
| 238 |
let allowed = allowed_widths(tuning_widths); |
| 239 |
let mut stale: Vec<String> = Vec::new(); |
| 240 |
|
| 241 |
for path in paths { |
| 242 |
let raw = std::fs::read_to_string(path) |
| 243 |
.unwrap_or_else(|e| panic!("read {}: {e}", path.display())); |
| 244 |
let name = match root { |
| 245 |
Some(root) => display_name(root, path), |
| 246 |
None => path.display().to_string(), |
| 247 |
}; |
| 248 |
|
| 249 |
if path.extension().is_some_and(|x| x == "js") { |
| 250 |
|
| 251 |
for (offset, px) in js_widths(&raw) { |
| 252 |
if !allowed.contains(&px) { |
| 253 |
stale.push(format!(" {name}:{} ({px}px)", line_of(&raw, offset))); |
| 254 |
} |
| 255 |
} |
| 256 |
continue; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
let src = strip_block_comments(&raw); |
| 262 |
for (offset, condition) in media_conditions(&src) { |
| 263 |
for px in media_widths(condition) { |
| 264 |
if !allowed.contains(&px) { |
| 265 |
stale.push(format!( |
| 266 |
" {name}:{} @media{condition} ({px}px)", |
| 267 |
line_of(&src, offset) |
| 268 |
)); |
| 269 |
} |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
assert!( |
| 275 |
stale.is_empty(), |
| 276 |
"hand-written breakpoints disagree with makeover_geometry::SizeClass.\n\n\ |
| 277 |
Allowed: {allowed:?}\n\ |
| 278 |
({:?} come from SizeClass; {tuning_widths:?} were passed as tuning widths.)\n\n\ |
| 279 |
Stale:\n{}\n\n\ |
| 280 |
If a size class moved, update these to match. If one of these is a new\n\ |
| 281 |
tuning width inside the wide shell rather than a shell boundary, add it\n\ |
| 282 |
to the caller's tuning list with a note saying what it tunes.\n\n\ |
| 283 |
Best of all, make the rule dimensional so it needs no threshold: a grid\n\ |
| 284 |
wants repeat(auto-fit, minmax(<content floor>, 1fr)) and a size wants\n\ |
| 285 |
clamp(). A threshold is for what appears and disappears.", |
| 286 |
allowed |
| 287 |
.iter() |
| 288 |
.filter(|px| !tuning_widths.contains(px)) |
| 289 |
.collect::<Vec<_>>(), |
| 290 |
stale.join("\n") |
| 291 |
); |
| 292 |
|
| 293 |
for path in paths { |
| 294 |
println!("cargo:rerun-if-changed={}", path.display()); |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
fn display_name(frontend: &Path, path: &Path) -> String { |
| 300 |
path.strip_prefix(frontend) |
| 301 |
.unwrap_or(path) |
| 302 |
.display() |
| 303 |
.to_string() |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
fn allowed_widths(tuning_widths: &[u16]) -> Vec<u16> { |
| 312 |
let mut widths: Vec<u16> = SizeClass::all() |
| 313 |
.iter() |
| 314 |
.flat_map(|c| media_widths(&c.media_condition())) |
| 315 |
.collect(); |
| 316 |
widths.extend_from_slice(tuning_widths); |
| 317 |
widths.sort_unstable(); |
| 318 |
widths.dedup(); |
| 319 |
widths |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
fn media_widths(condition: &str) -> Vec<u16> { |
| 324 |
let mut out = Vec::new(); |
| 325 |
let mut rest = condition; |
| 326 |
while let Some(i) = rest.find("-width:") { |
| 327 |
rest = &rest[i + "-width:".len()..]; |
| 328 |
let digits: String = rest |
| 329 |
.trim_start() |
| 330 |
.chars() |
| 331 |
.take_while(char::is_ascii_digit) |
| 332 |
.collect(); |
| 333 |
if let Ok(px) = digits.parse() { |
| 334 |
out.push(px); |
| 335 |
} |
| 336 |
} |
| 337 |
out |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
fn media_conditions(css: &str) -> Vec<(usize, &str)> { |
| 342 |
let mut out = Vec::new(); |
| 343 |
let mut at = 0; |
| 344 |
while let Some(i) = css[at..].find("@media") { |
| 345 |
let start = at + i; |
| 346 |
let after = start + "@media".len(); |
| 347 |
match css[after..].find('{') { |
| 348 |
Some(j) => { |
| 349 |
out.push((start, &css[after..after + j])); |
| 350 |
at = after + j; |
| 351 |
} |
| 352 |
None => break, |
| 353 |
} |
| 354 |
} |
| 355 |
out |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
fn js_widths(src: &str) -> Vec<(usize, u16)> { |
| 366 |
let mut out = Vec::new(); |
| 367 |
for pat in ["(max-width:", "(min-width:"] { |
| 368 |
let mut at = 0; |
| 369 |
while let Some(i) = src[at..].find(pat) { |
| 370 |
let start = at + i; |
| 371 |
let rest = src[start + pat.len()..].trim_start(); |
| 372 |
let digits: String = rest.chars().take_while(char::is_ascii_digit).collect(); |
| 373 |
if let Ok(px) = digits.parse() |
| 374 |
&& rest[digits.len()..].starts_with("px)") |
| 375 |
{ |
| 376 |
out.push((start, px)); |
| 377 |
} |
| 378 |
at = start + pat.len(); |
| 379 |
} |
| 380 |
} |
| 381 |
out |
| 382 |
} |
| 383 |
|
| 384 |
|
| 385 |
fn strip_block_comments(css: &str) -> String { |
| 386 |
let bytes = css.as_bytes(); |
| 387 |
let mut out = String::with_capacity(css.len()); |
| 388 |
let mut i = 0; |
| 389 |
while i < bytes.len() { |
| 390 |
if bytes[i..].starts_with(b"/*") { |
| 391 |
let end = css[i..].find("*/").map_or(bytes.len(), |j| i + j + 2); |
| 392 |
for c in css[i..end].chars() { |
| 393 |
out.push(if c == '\n' { '\n' } else { ' ' }); |
| 394 |
} |
| 395 |
i = end; |
| 396 |
} else { |
| 397 |
let c = css[i..].chars().next().unwrap(); |
| 398 |
out.push(c); |
| 399 |
i += c.len_utf8(); |
| 400 |
} |
| 401 |
} |
| 402 |
out |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
pub fn check_vocabulary( |
| 457 |
frontend: impl AsRef<Path>, |
| 458 |
opts: &Emit, |
| 459 |
generated: &[&str], |
| 460 |
allowed: &[(&str, &str)], |
| 461 |
) { |
| 462 |
let frontend = frontend.as_ref(); |
| 463 |
let css = frontend.join("css"); |
| 464 |
let files: Vec<PathBuf> = files_with_extension(&css, "css") |
| 465 |
.into_iter() |
| 466 |
.filter(|p| { |
| 467 |
let name = p.strip_prefix(&css).unwrap_or(p).display().to_string(); |
| 468 |
!generated.contains(&name.as_str()) |
| 469 |
}) |
| 470 |
.collect(); |
| 471 |
check_vocabulary_paths(&files, opts, Some(frontend), allowed); |
| 472 |
} |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
pub fn check_vocabulary_files<P: AsRef<Path>>(paths: &[P], opts: &Emit, allowed: &[(&str, &str)]) { |
| 485 |
let paths: Vec<PathBuf> = paths.iter().map(|p| p.as_ref().to_path_buf()).collect(); |
| 486 |
check_vocabulary_paths(&paths, opts, None, allowed); |
| 487 |
} |
| 488 |
|
| 489 |
|
| 490 |
fn check_vocabulary_paths( |
| 491 |
paths: &[PathBuf], |
| 492 |
opts: &Emit, |
| 493 |
root: Option<&Path>, |
| 494 |
allowed: &[(&str, &str)], |
| 495 |
) { |
| 496 |
let generated = |
| 497 |
makeover_webview::vocabulary::declarations_by_class(&makeover_webview::stylesheet(opts)); |
| 498 |
let mut clashes: Vec<String> = Vec::new(); |
| 499 |
let mut seen: Vec<(String, String)> = Vec::new(); |
| 500 |
|
| 501 |
for path in paths { |
| 502 |
println!("cargo::rerun-if-changed={}", path.display()); |
| 503 |
let raw = std::fs::read_to_string(path) |
| 504 |
.unwrap_or_else(|e| panic!("read {}: {e}", path.display())); |
| 505 |
let name = match root { |
| 506 |
Some(root) => display_name(root, path), |
| 507 |
None => path.display().to_string(), |
| 508 |
}; |
| 509 |
|
| 510 |
|
| 511 |
let local = makeover_webview::vocabulary::declarations_by_class(&raw); |
| 512 |
for (class, properties) in &local { |
| 513 |
let Some(theirs) = generated.get(class) else { |
| 514 |
continue; |
| 515 |
}; |
| 516 |
for property in properties.intersection(theirs) { |
| 517 |
seen.push((class.clone(), property.clone())); |
| 518 |
if allowed.contains(&(class.as_str(), property.as_str())) { |
| 519 |
continue; |
| 520 |
} |
| 521 |
clashes.push(format!(" {name} .{class} {{ {property} }}")); |
| 522 |
} |
| 523 |
} |
| 524 |
} |
| 525 |
|
| 526 |
assert!( |
| 527 |
clashes.is_empty(), |
| 528 |
"{} hand-written declaration(s) take a property the generated stylesheet \ |
| 529 |
already sets on the same class. App CSS is unlayered and beats \ |
| 530 |
@layer makeover, so each of these wins over the design system \ |
| 531 |
silently:\n{}\n\nDelete the declaration, or, if it is a deliberate pairing \ |
| 532 |
on a different selector arm, add (class, property) to this check's \ |
| 533 |
allowed list and say why beside it. Count the consumers before deciding \ |
| 534 |
a divergence is worth keeping.", |
| 535 |
clashes.len(), |
| 536 |
clashes.join("\n") |
| 537 |
); |
| 538 |
|
| 539 |
let stale: Vec<&(&str, &str)> = allowed |
| 540 |
.iter() |
| 541 |
.filter(|(class, property)| { |
| 542 |
!seen.contains(&((*class).to_string(), (*property).to_string())) |
| 543 |
}) |
| 544 |
.collect(); |
| 545 |
assert!( |
| 546 |
stale.is_empty(), |
| 547 |
"the allowed list declares {stale:?}, which no longer collides with \ |
| 548 |
anything. Delete the entries: an exception nobody is using is where the \ |
| 549 |
next real collision lands and reads as company." |
| 550 |
); |
| 551 |
} |
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
pub fn check_vocabulary_use<P: AsRef<Path>>(markup: &[P], opts: &Emit, high_water: usize) { |
| 580 |
let generated = makeover_webview::vocabulary::names(opts); |
| 581 |
let mut haystack = String::new(); |
| 582 |
for path in markup { |
| 583 |
let path = path.as_ref(); |
| 584 |
println!("cargo::rerun-if-changed={}", path.display()); |
| 585 |
haystack.push_str( |
| 586 |
&std::fs::read_to_string(path) |
| 587 |
.unwrap_or_else(|e| panic!("read {}: {e}", path.display())), |
| 588 |
); |
| 589 |
haystack.push('\n'); |
| 590 |
} |
| 591 |
|
| 592 |
let unused: Vec<&String> = generated |
| 593 |
.iter() |
| 594 |
.filter(|class| !haystack.contains(class.as_str())) |
| 595 |
.collect(); |
| 596 |
|
| 597 |
assert!( |
| 598 |
unused.len() <= high_water, |
| 599 |
"{} of {} generated classes are emitted by no markup, above the recorded {}. \ |
| 600 |
The vocabulary grew or the markup stopped using it:\n{}", |
| 601 |
unused.len(), |
| 602 |
generated.len(), |
| 603 |
high_water, |
| 604 |
unused |
| 605 |
.iter() |
| 606 |
.map(|c| format!(" .{c}")) |
| 607 |
.collect::<Vec<_>>() |
| 608 |
.join("\n") |
| 609 |
); |
| 610 |
|
| 611 |
if unused.len() < high_water { |
| 612 |
println!( |
| 613 |
"cargo::warning=dead makeover vocabulary is down to {} from a sealed {}; \ |
| 614 |
lower the seal so it cannot grow back", |
| 615 |
unused.len(), |
| 616 |
high_water |
| 617 |
); |
| 618 |
} |
| 619 |
} |
| 620 |
|
| 621 |
#[cfg(test)] |
| 622 |
mod tests { |
| 623 |
use super::*; |
| 624 |
|
| 625 |
fn scratch(name: &str) -> PathBuf { |
| 626 |
let dir = |
| 627 |
std::env::temp_dir().join(format!("makeover-drift-{}-{name}", std::process::id())); |
| 628 |
let _ = std::fs::remove_dir_all(&dir); |
| 629 |
std::fs::create_dir_all(&dir).expect("create scratch"); |
| 630 |
dir |
| 631 |
} |
| 632 |
|
| 633 |
fn write(dir: &Path, name: &str, src: &str) { |
| 634 |
if let Some(parent) = dir.join(name).parent() { |
| 635 |
std::fs::create_dir_all(parent).unwrap(); |
| 636 |
} |
| 637 |
std::fs::write(dir.join(name), src).unwrap(); |
| 638 |
} |
| 639 |
|
| 640 |
fn declaring() -> String { |
| 641 |
format!( |
| 642 |
"const {CONST_NAME} = '{}';\n", |
| 643 |
Density::Touch.media_condition() |
| 644 |
) |
| 645 |
} |
| 646 |
|
| 647 |
#[test] |
| 648 |
fn the_crates_own_string_passes() { |
| 649 |
let dir = scratch("ok"); |
| 650 |
write(&dir, "touch.js", &declaring()); |
| 651 |
check_touch_density(&dir); |
| 652 |
} |
| 653 |
|
| 654 |
#[test] |
| 655 |
#[should_panic(expected = "disagrees with makeover_geometry::Density")] |
| 656 |
fn a_drifted_literal_fails() { |
| 657 |
let dir = scratch("drift"); |
| 658 |
write(&dir, "touch.js", &declaring()); |
| 659 |
write( |
| 660 |
&dir, |
| 661 |
"haptics.js", |
| 662 |
&format!("const {CONST_NAME} = '(pointer: coarse)';\n"), |
| 663 |
); |
| 664 |
check_touch_density(&dir); |
| 665 |
} |
| 666 |
|
| 667 |
#[test] |
| 668 |
#[should_panic(expected = "device sniff")] |
| 669 |
fn the_sniff_cannot_come_back() { |
| 670 |
let dir = scratch("sniff"); |
| 671 |
write(&dir, "touch.js", &declaring()); |
| 672 |
write(&dir, "legacy.js", "if ('ontouchstart' in window) {}\n"); |
| 673 |
check_touch_density(&dir); |
| 674 |
} |
| 675 |
|
| 676 |
#[test] |
| 677 |
#[should_panic(expected = "no TOUCH_DENSITY literal found")] |
| 678 |
fn a_frontend_that_states_nothing_fails() { |
| 679 |
let dir = scratch("empty"); |
| 680 |
write(&dir, "app.js", "export const x = 1;\n"); |
| 681 |
check_touch_density(&dir); |
| 682 |
} |
| 683 |
|
| 684 |
#[test] |
| 685 |
fn a_use_site_is_not_a_declaration() { |
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
let src = |
| 690 |
format!("import {{ {CONST_NAME} }} from './touch.js';\nmatchMedia({CONST_NAME});\n"); |
| 691 |
assert!(touch_density_literals(&src).is_empty()); |
| 692 |
} |
| 693 |
|
| 694 |
#[test] |
| 695 |
fn nested_files_are_read() { |
| 696 |
|
| 697 |
|
| 698 |
let dir = scratch("nested"); |
| 699 |
write(&dir, "touch.js", &declaring()); |
| 700 |
write(&dir, "screens/legacy.js", "navigator.maxTouchPoints > 0;\n"); |
| 701 |
let files = js_files(&dir); |
| 702 |
assert_eq!(files.len(), 2); |
| 703 |
} |
| 704 |
|
| 705 |
#[test] |
| 706 |
fn a_non_js_file_is_ignored() { |
| 707 |
let dir = scratch("nonjs"); |
| 708 |
write(&dir, "touch.js", &declaring()); |
| 709 |
write(&dir, "styles.css", "body { }\n"); |
| 710 |
assert_eq!(js_files(&dir).len(), 1); |
| 711 |
} |
| 712 |
|
| 713 |
fn frontend(name: &str) -> PathBuf { |
| 714 |
let dir = scratch(name); |
| 715 |
std::fs::create_dir_all(dir.join("css")).unwrap(); |
| 716 |
std::fs::create_dir_all(dir.join("js")).unwrap(); |
| 717 |
dir |
| 718 |
} |
| 719 |
|
| 720 |
|
| 721 |
fn boundary() -> u16 { |
| 722 |
SizeClass::Medium.min_px() |
| 723 |
} |
| 724 |
|
| 725 |
#[test] |
| 726 |
fn the_crates_own_boundaries_pass() { |
| 727 |
let dir = frontend("bp-ok"); |
| 728 |
write( |
| 729 |
&dir, |
| 730 |
"css/styles.css", |
| 731 |
&format!("@media (min-width: {}px) {{ body {{ }} }}\n", boundary()), |
| 732 |
); |
| 733 |
check_breakpoints(&dir, &[]); |
| 734 |
} |
| 735 |
|
| 736 |
#[test] |
| 737 |
#[should_panic(expected = "disagree with makeover_geometry::SizeClass")] |
| 738 |
fn a_stale_css_width_fails() { |
| 739 |
let dir = frontend("bp-css"); |
| 740 |
write(&dir, "css/styles.css", "@media (max-width: 768px) { }\n"); |
| 741 |
check_breakpoints(&dir, &[]); |
| 742 |
} |
| 743 |
|
| 744 |
#[test] |
| 745 |
#[should_panic(expected = "disagree with makeover_geometry::SizeClass")] |
| 746 |
fn a_stale_js_width_fails() { |
| 747 |
let dir = frontend("bp-js"); |
| 748 |
write(&dir, "js/shell.js", "matchMedia('(max-width: 768px)');\n"); |
| 749 |
check_breakpoints(&dir, &[]); |
| 750 |
} |
| 751 |
|
| 752 |
#[test] |
| 753 |
fn a_declared_tuning_width_passes() { |
| 754 |
let dir = frontend("bp-tuning"); |
| 755 |
write(&dir, "css/styles.css", "@media (min-width: 1400px) { }\n"); |
| 756 |
check_breakpoints(&dir, &[1400]); |
| 757 |
} |
| 758 |
|
| 759 |
#[test] |
| 760 |
fn a_width_in_a_comment_is_prose() { |
| 761 |
|
| 762 |
|
| 763 |
let dir = frontend("bp-comment"); |
| 764 |
write( |
| 765 |
&dir, |
| 766 |
"css/styles.css", |
| 767 |
"/* was @media (max-width: 768px) until the size classes landed */\n", |
| 768 |
); |
| 769 |
check_breakpoints(&dir, &[]); |
| 770 |
} |
| 771 |
|
| 772 |
#[test] |
| 773 |
fn an_unparenthesized_width_is_not_a_breakpoint() { |
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
let dir = frontend("bp-inline"); |
| 778 |
write( |
| 779 |
&dir, |
| 780 |
"js/style.js", |
| 781 |
"el.style.cssText = 'max-width: 320px; display: block';\n", |
| 782 |
); |
| 783 |
check_breakpoints(&dir, &[]); |
| 784 |
} |
| 785 |
|
| 786 |
#[test] |
| 787 |
fn nested_css_is_read() { |
| 788 |
|
| 789 |
|
| 790 |
let dir = frontend("bp-nested"); |
| 791 |
write( |
| 792 |
&dir, |
| 793 |
"css/screens/detail.css", |
| 794 |
"@media (max-width: 768px) { }\n", |
| 795 |
); |
| 796 |
let found = std::panic::catch_unwind(|| check_breakpoints(&dir, &[])); |
| 797 |
assert!(found.is_err(), "a nested stylesheet must be scanned"); |
| 798 |
} |
| 799 |
|
| 800 |
#[test] |
| 801 |
fn a_named_list_is_checked() { |
| 802 |
let dir = frontend("bp-list"); |
| 803 |
write(&dir, "css/style.css", "@media (max-width: 768px) { }\n"); |
| 804 |
let listed = dir.join("css/style.css"); |
| 805 |
let err = |
| 806 |
std::panic::catch_unwind(|| check_breakpoints_files(&[&listed], &[])).unwrap_err(); |
| 807 |
let msg = err.downcast_ref::<String>().expect("String payload"); |
| 808 |
assert!(msg.contains("style.css:1"), "got: {msg}"); |
| 809 |
} |
| 810 |
|
| 811 |
#[test] |
| 812 |
#[should_panic(expected = "read ")] |
| 813 |
fn a_listed_file_that_is_gone_fails() { |
| 814 |
|
| 815 |
|
| 816 |
let dir = frontend("bp-missing"); |
| 817 |
check_breakpoints_files(&[dir.join("css/never-written.css")], &[]); |
| 818 |
} |
| 819 |
|
| 820 |
#[test] |
| 821 |
fn a_listed_js_file_is_parsed_as_script() { |
| 822 |
|
| 823 |
|
| 824 |
let dir = frontend("bp-list-js"); |
| 825 |
write( |
| 826 |
&dir, |
| 827 |
"js/style.js", |
| 828 |
"el.style.cssText = 'max-width: 320px';\n", |
| 829 |
); |
| 830 |
check_breakpoints_files(&[dir.join("js/style.js")], &[]); |
| 831 |
} |
| 832 |
|
| 833 |
#[test] |
| 834 |
fn the_error_names_the_file_and_line() { |
| 835 |
let dir = frontend("bp-message"); |
| 836 |
write( |
| 837 |
&dir, |
| 838 |
"css/styles.css", |
| 839 |
"body { }\n@media (max-width: 768px) { }\n", |
| 840 |
); |
| 841 |
let err = std::panic::catch_unwind(|| check_breakpoints(&dir, &[])).unwrap_err(); |
| 842 |
let msg = err |
| 843 |
.downcast_ref::<String>() |
| 844 |
.expect("panic payload is a String"); |
| 845 |
assert!(msg.contains("css/styles.css:2"), "got: {msg}"); |
| 846 |
} |
| 847 |
|
| 848 |
#[test] |
| 849 |
fn a_rule_restating_a_generated_class_fails_and_names_it() { |
| 850 |
let dir = scratch("vocab-clash"); |
| 851 |
|
| 852 |
|
| 853 |
write( |
| 854 |
&dir, |
| 855 |
"css/styles.css", |
| 856 |
"body { color: red; }\n.card { box-shadow: none; }\n", |
| 857 |
); |
| 858 |
let err = std::panic::catch_unwind(|| check_vocabulary(&dir, &Emit::default(), &[], &[])) |
| 859 |
.unwrap_err(); |
| 860 |
let msg = err |
| 861 |
.downcast_ref::<String>() |
| 862 |
.expect("panic payload is a String"); |
| 863 |
assert!(msg.contains(".card"), "got: {msg}"); |
| 864 |
assert!(msg.contains("box-shadow"), "got: {msg}"); |
| 865 |
assert!(msg.contains("css/styles.css"), "got: {msg}"); |
| 866 |
} |
| 867 |
|
| 868 |
#[test] |
| 869 |
fn an_app_class_of_its_own_is_left_alone() { |
| 870 |
let dir = scratch("vocab-clean"); |
| 871 |
write( |
| 872 |
&dir, |
| 873 |
"css/styles.css", |
| 874 |
".task-list-container { overflow: auto; }\n.day-plan-slot { height: 1rem; }\n", |
| 875 |
); |
| 876 |
check_vocabulary(&dir, &Emit::default(), &[], &[]); |
| 877 |
} |
| 878 |
|
| 879 |
#[test] |
| 880 |
fn the_generated_sheet_is_skipped_rather_than_reported_against_itself() { |
| 881 |
let dir = scratch("vocab-generated"); |
| 882 |
let opts = Emit::default(); |
| 883 |
write(&dir, "css/layout.css", &makeover_webview::stylesheet(&opts)); |
| 884 |
|
| 885 |
|
| 886 |
check_vocabulary(&dir, &opts, &["layout.css"], &[]); |
| 887 |
} |
| 888 |
|
| 889 |
#[test] |
| 890 |
fn a_prefixed_app_is_checked_against_its_own_prefix() { |
| 891 |
let dir = scratch("vocab-prefix"); |
| 892 |
let opts = Emit { |
| 893 |
class_prefix: "mo-", |
| 894 |
..Emit::default() |
| 895 |
}; |
| 896 |
|
| 897 |
|
| 898 |
write(&dir, "css/styles.css", ".card { box-shadow: none; }\n"); |
| 899 |
check_vocabulary(&dir, &opts, &[], &[]); |
| 900 |
|
| 901 |
let dir = scratch("vocab-prefix-clash"); |
| 902 |
write(&dir, "css/styles.css", ".mo-card { box-shadow: none; }\n"); |
| 903 |
assert!(std::panic::catch_unwind(|| check_vocabulary(&dir, &opts, &[], &[])).is_err()); |
| 904 |
} |
| 905 |
|
| 906 |
#[test] |
| 907 |
fn a_class_shared_without_a_shared_property_is_left_alone() { |
| 908 |
let dir = scratch("vocab-additive"); |
| 909 |
|
| 910 |
|
| 911 |
write( |
| 912 |
&dir, |
| 913 |
"css/styles.css", |
| 914 |
".badge { padding: 2px; border-radius: 3px; font-weight: 600; }\n", |
| 915 |
); |
| 916 |
check_vocabulary(&dir, &Emit::default(), &[], &[]); |
| 917 |
} |
| 918 |
|
| 919 |
#[test] |
| 920 |
fn a_reviewed_pair_passes_and_stops_passing_when_it_stops_colliding() { |
| 921 |
let dir = scratch("vocab-allowed"); |
| 922 |
write(&dir, "css/styles.css", ".card { box-shadow: none; }\n"); |
| 923 |
check_vocabulary(&dir, &Emit::default(), &[], &[("card", "box-shadow")]); |
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
let dir = scratch("vocab-allowed-stale"); |
| 928 |
write(&dir, "css/styles.css", ".card { padding: 2px; }\n"); |
| 929 |
let err = std::panic::catch_unwind(|| { |
| 930 |
check_vocabulary(&dir, &Emit::default(), &[], &[("card", "box-shadow")]); |
| 931 |
}) |
| 932 |
.unwrap_err(); |
| 933 |
let msg = err |
| 934 |
.downcast_ref::<String>() |
| 935 |
.expect("panic payload is a String"); |
| 936 |
assert!(msg.contains("no longer collides"), "got: {msg}"); |
| 937 |
} |
| 938 |
|
| 939 |
#[test] |
| 940 |
fn dead_vocabulary_above_the_seal_fails_and_below_it_passes() { |
| 941 |
let dir = scratch("vocab-seal"); |
| 942 |
let opts = Emit::default(); |
| 943 |
let all = makeover_webview::vocabulary::names(&opts).len(); |
| 944 |
|
| 945 |
write(&dir, "index.html", "<div></div>\n"); |
| 946 |
let markup = [dir.join("index.html")]; |
| 947 |
|
| 948 |
check_vocabulary_use(&markup, &opts, all); |
| 949 |
assert!( |
| 950 |
std::panic::catch_unwind(|| check_vocabulary_use(&markup, &opts, all - 1)).is_err(), |
| 951 |
"a vocabulary deader than the seal has to fail" |
| 952 |
); |
| 953 |
} |
| 954 |
|
| 955 |
#[test] |
| 956 |
fn both_quote_styles_read() { |
| 957 |
let want = Density::Touch.media_condition(); |
| 958 |
for q in ['\'', '"'] { |
| 959 |
let src = format!("const {CONST_NAME} = {q}{want}{q};\n"); |
| 960 |
let found = touch_density_literals(&src); |
| 961 |
assert_eq!(found.len(), 1); |
| 962 |
assert_eq!(found[0].1, want); |
| 963 |
} |
| 964 |
} |
| 965 |
} |
| 966 |
|