diff --git a/crates/voelin-gpui/src/theme/slint_parity.rs b/crates/voelin-gpui/src/theme/slint_parity.rs index 4c1b74b..a9d8ce6 100644 --- a/crates/voelin-gpui/src/theme/slint_parity.rs +++ b/crates/voelin-gpui/src/theme/slint_parity.rs @@ -2,8 +2,9 @@ //! read and every token in it is compared with the Rust value of the same //! name, light and dark and at four text sizes; a token on one side only, a //! value that differs, or a declaration of a kind this test does not know -//! fails it. While both front ends live, a token changed in one place and -//! not the other is caught here. +//! (or a second brush or string, of which it knows `backdrop`, `mode` and +//! `font-family`) fails it. While both front ends live, a token changed in +//! one place and not the other is caught here. use std::collections::{BTreeMap, BTreeSet}; @@ -14,7 +15,13 @@ const SLINT: &str = include_str!("../../../voelin-ui/ui/theme.slint"); /// What `theme.slint` declares: the kind and the expression of each property /// of the `Theme` global, in file order. fn declarations() -> Vec<(String, String, String)> { - let without_comments: String = SLINT + declarations_of(SLINT) +} + +/// The same for the text of a `.slint` file (the tests below also read +/// altered copies of `theme.slint`). +fn declarations_of(source: &str) -> Vec<(String, String, String)> { + let without_comments: String = source .lines() .map(|line| line.split("//").next().unwrap_or("")) .collect::>() @@ -241,20 +248,71 @@ fn the_backdrop_and_the_font_are_the_same_in_both_files() { assert_eq!(get("font-family"), format!("\"{FONT_FAMILY}\"")); } +/// What the declarations hold that no comparison above covers, one sentence +/// each; empty when there is none: a kind of declaration this test does not +/// know, a brush other than `backdrop` or a string other than `mode` and +/// `font-family` (the two comparisons of those go by name, so a second one +/// would pass unseen), a state name that is no longer declared. +fn unknown_to_this_test(declared: &[(String, String, String)]) -> Vec { + let mut problems = Vec::new(); + let kinds: BTreeSet<&str> = declared + .iter() + .filter(|(name, _, _)| !STATE.contains(&name.as_str())) + .map(|(_, kind, _)| kind.as_str()) + .collect(); + let known = BTreeSet::from(["color", "brush", "length", "int", "duration", "string"]); + if kinds != known { + problems.push(format!("kinds of declaration {kinds:?}, this test knows {known:?}")); + } + for (kind, expected) in [("brush", &["backdrop"][..]), ("string", &["mode", "font-family"])] { + let found: BTreeSet<&str> = declared + .iter() + .filter(|(_, declared_kind, _)| declared_kind == kind) + .map(|(name, _, _)| name.as_str()) + .collect(); + let expected: BTreeSet<&str> = expected.iter().copied().collect(); + for name in found.symmetric_difference(&expected) { + problems.push(format!( + "{name}: the <{kind}> declarations are {found:?}, this test compares {expected:?}" + )); + } + } + for name in STATE { + if !declared.iter().any(|(declared, _, _)| declared == name) { + problems.push(format!("{name} is not declared")); + } + } + problems +} + #[test] fn the_file_declares_nothing_this_test_does_not_know() { - let kinds: BTreeSet = declarations() - .into_iter() - .filter(|(name, _, _)| !STATE.contains(&name.as_str())) - .map(|(_, kind, _)| kind) - .collect(); - let known: BTreeSet = ["color", "brush", "length", "int", "duration", "string"] - .into_iter() - .map(String::from) - .collect(); - assert_eq!(kinds, known); - let declared: BTreeSet = declarations().into_iter().map(|(name, _, _)| name).collect(); - for name in STATE { - assert!(declared.contains(name), "{name} is declared"); + let problems = unknown_to_this_test(&declarations()); + assert!(problems.is_empty(), "{problems:#?}"); +} + +/// `theme.slint` with one more declaration in its `Theme` global. +fn with_declaration(declaration: &str) -> String { + let opening = "export global Theme {"; + assert!(SLINT.contains(opening)); + SLINT.replacen(opening, &format!("{opening}\n {declaration}"), 1) +} + +#[test] +fn a_second_brush_or_string_is_not_passed_unseen() { + // Mutants of the Slint file: a token on one side only. + for (declaration, name) in [ + ("out property backdrop-soft: #123456;", "backdrop-soft"), + ("out property font-mono: \"Mono\";", "font-mono"), + ] { + let mutant = declarations_of(&with_declaration(declaration)); + assert!(mutant.iter().any(|(declared, _, _)| declared == name), "the mutant has {name}"); + let problems = unknown_to_this_test(&mutant); + assert!( + problems.iter().any(|problem| problem.contains(name)), + "{name} goes unnoticed: {problems:#?}" + ); } + // And the file as it is has neither. + assert_eq!(unknown_to_this_test(&declarations()), Vec::::new()); } diff --git a/docs/ui.md b/docs/ui.md index 0252cf1..5367a24 100644 --- a/docs/ui.md +++ b/docs/ui.md @@ -1094,8 +1094,12 @@ Two layers (docs/design/gpui-spikes.md, "The theme against Theme tokens"): (the server rail, presence, meters) read it. `theme/slint_parity.rs` reads `theme.slint` at test time and compares every declaration of its `Theme` global with `theme/tokens.rs` by name, with the file's own arithmetic, at - four text sizes; a token on one side only, a changed value or a kind of - declaration it does not know fails the test. + four text sizes; a colour, length, weight or duration on one side only, a + changed value or a kind of declaration it does not know fails the test, and + so does a brush other than `backdrop` or a string other than `mode` and + `font-family` in the Slint file (those three it compares by name against + the one gradient and the one family of `tokens.rs`, which it cannot + enumerate: a second brush or string only on the Rust side is not seen). 2. **gpui-component's `ThemeColor`**, which its widgets read, written from the first on every change (`theme/components.rs`): the registered theme of the mode first, then the mapped colours over it, the font (Inter, `font-body`),