test(ui): lookups as Languages methods tested on a catalog, tool status lines, number fields stepped (M8)

This commit is contained in:
Tanish Manku committed 2026-11-10 14:16:16 -07:00
1 parent 8c1d63d5ce
commit 27f77a992f
2 files changed
+104 -38

No files matched your search

+44 -32
View File
@@ -153,6 +153,7 @@ impl Languages {
candidates.iter().filter_map(|c| self.find(c)).find(|i| self.get(*i).code != PSEUDO)
}
#[cfg(test)]
fn lookup<'a>(&'a self, i: usize, context: &str, source: &'a str) -> Option<&'a str> {
match &self.get(i).kind {
Kind::Catalog(c) => c.get(context, source),
@@ -267,16 +268,48 @@ fn pseudo_static(source: &'static str) -> &'static str {
cache.entry(source).or_insert_with(|| Box::leak(pseudo::transform(source).into_boxed_str()))
}
impl Languages {
/// `source` in language `i` (English is the key itself: no lookup).
fn text<'a>(&'a self, i: usize, context: &str, source: &'static str) -> &'a str {
match &self.get(i).kind {
Kind::English => source,
Kind::Pseudo => pseudo_static(source),
Kind::Catalog(c) => c.get(context, source).unwrap_or(source),
}
}
/// Run-time English in language `i`, translated on an exact match.
fn text_dyn<'a>(&'a self, i: usize, source: &'a str) -> Cow<'a, str> {
match &self.get(i).kind {
Kind::English => Cow::Borrowed(source),
Kind::Pseudo => Cow::Owned(pseudo::transform(source)),
Kind::Catalog(c) => Cow::Borrowed(c.get("", source).unwrap_or(source)),
}
}
/// A command's label in language `i`: its `@id` row, else its label's row.
fn command<'a>(&'a self, i: usize, id: &str, label: &'static str) -> &'a str {
match &self.get(i).kind {
Kind::Catalog(c) => c.id(id).unwrap_or_else(|| c.get("", label).unwrap_or(label)),
_ => self.text(i, "", label),
}
}
/// The plural form for `n` in language `i`.
fn count<'a>(&'a self, i: usize, one: &'static str, other: &'static str, n: u64) -> &'a str {
match self.get(i).kind {
Kind::English => {
if n == 1 { one } else { other }
}
Kind::Pseudo => pseudo_static(if n == 1 { one } else { other }),
Kind::Catalog(_) => self.plural(i, one, other, n),
}
}
}
fn translate(context: &'static str, source: &'static str) -> &'static str {
let i = current();
if i == 0 {
return source;
}
let languages: &'static Languages = &LANGUAGES;
if matches!(languages.get(i).kind, Kind::Pseudo) {
return pseudo_static(source);
}
languages.lookup(i, context, source).unwrap_or(source)
languages.text(current(), context, source)
}
/// `source` in the interface language. Use [`tr!`] for literals; this is for `'static` strings
@@ -293,41 +326,20 @@ pub fn tc(context: &'static str, source: &'static str) -> &'static str {
/// English that arrives at run time from below the shell (an undo step's label, a
/// confirmation's title): translated when a catalog has that exact text.
pub fn t_dyn(source: &str) -> Cow<'_, str> {
let i = current();
if i == 0 {
return Cow::Borrowed(source);
}
let languages: &'static Languages = &LANGUAGES;
match &languages.get(i).kind {
Kind::Pseudo => Cow::Owned(pseudo::transform(source)),
Kind::Catalog(c) => c.get("", source).map_or(Cow::Borrowed(source), |t| Cow::Owned(t.to_string())),
Kind::English => Cow::Borrowed(source),
}
languages.text_dyn(current(), source)
}
/// A registry command's label: an `@id` row for its id first, else the label's own translation.
pub fn t_cmd(id: &str, label: &'static str) -> &'static str {
let i = current();
let languages: &'static Languages = &LANGUAGES;
if let Kind::Catalog(c) = &languages.get(i).kind
&& let Some(t) = c.id(id)
{
return t;
}
t(label)
languages.command(current(), id, label)
}
/// The plural form for `n`, placeholders not yet filled (see [`trn!`]).
pub fn tn(one: &'static str, other: &'static str, n: u64) -> &'static str {
let i = current();
let languages: &'static Languages = &LANGUAGES;
match languages.get(i).kind {
Kind::English => {
if n == 1 { one } else { other }
}
Kind::Pseudo => pseudo_static(if n == 1 { one } else { other }),
Kind::Catalog(_) => languages.plural(i, one, other, n),
}
languages.count(current(), one, other, n)
}
/// `template` with its `{name}` placeholders filled from `args`, in one pass: a value that
+60 -6
View File
@@ -311,6 +311,28 @@ fn lookups_fall_back_from_app_to_shared_to_base_language_to_english() {
assert_eq!(l.plural(0, "{n} frame", "{n} frames", 1), "{n} frame");
}
#[test]
fn every_lookup_kind_reads_the_catalog() {
// The same paths `tr!`, `t_dyn`, `t_cmd` and `trn!` take with a catalog built in.
let l = fixture();
let (de, pseudo) = (1, l.list.len() - 1);
assert_eq!(l.text(de, "", "Save"), "Speichern");
assert_eq!(l.text(de, "menu", "Nothing"), "Nothing");
assert_eq!(l.text(0, "", "Save"), "Save");
assert_eq!(l.text(pseudo, "", "Save"), "[Šàvé ~~]");
assert_eq!(l.text_dyn(de, "Open"), "Datei öffnen");
assert_eq!(l.text_dyn(de, "Delete Layer"), "Delete Layer");
assert_eq!(l.text_dyn(0, "Open"), "Open");
assert_eq!(l.text_dyn(pseudo, "Open"), "[Öpéñ ~~]");
assert_eq!(l.command(de, "edit.undo", "Undo"), "Rückgängig", "an @id row first");
assert_eq!(l.command(de, "file.save", "Save"), "Speichern", "then the label");
assert_eq!(l.command(de, "file.other", "Other"), "Other");
assert_eq!(l.command(0, "edit.undo", "Undo"), "Undo");
assert_eq!(l.count(de, "{n} frame", "{n} frames", 2), "{n} Bilder");
assert_eq!(l.count(0, "{n} frame", "{n} frames", 1), "{n} frame");
assert_eq!(l.count(pseudo, "{n} frame", "{n} frames", 1), "[{n} fŕàmé ~~]");
}
#[test]
fn system_locales_and_preferences_resolve() {
let l = fixture();
@@ -322,6 +344,8 @@ fn system_locales_and_preferences_resolve() {
assert_eq!(code(AUTO, Some("ru")), "ru");
assert_eq!(code(AUTO, Some("ar-EG")), "en", "not offered yet");
assert_eq!(code(AUTO, Some("C")), "en");
assert_eq!(code(AUTO, Some("zh-TW")), "en", "Chinese isn't offered yet (it needs a font)");
assert_eq!(code(AUTO, Some("zh_CN")), "en");
assert_eq!(code(AUTO, Some("")), "en");
assert_eq!(code(AUTO, None), "en");
assert_eq!(code("DE", Some("ru-RU")), "de", "a chosen language wins over the system's");
@@ -497,16 +521,15 @@ fn click_everything(what: &str, setup: impl FnOnce(&mut crate::app::App, &egui::
// The next control not clicked yet (by role and name: ids change as panels redraw).
let next = nodes.iter().find_map(|(id, n)| {
let key = format!("{:?} {}", n.role(), n.label().unwrap_or_default());
let clickable = n.supports_action(Action::Click) && !matches!(n.role(), Role::Window | Role::TitleBar | Role::ScrollBar);
(clickable && !done.contains(&key)).then(|| (*id, key, matches!(n.role(), Role::SpinButton | Role::Slider)))
let steps = n.supports_action(Action::Increment);
let clickable = (steps || n.supports_action(Action::Click)) && !matches!(n.role(), Role::Window | Role::TitleBar | Role::ScrollBar);
(clickable && !done.contains(&key)).then_some((*id, key, steps))
});
let Some((id, key, steps)) = next else { break };
done.insert(key);
let request = |action| egui::Event::AccessKitActionRequest(ActionRequest { action, target_tree: TreeId::ROOT, target_node: id, data: None });
let mut events = vec![request(Action::Click)];
if steps {
events.push(request(Action::Increment));
}
// A number field steps up (as an arrow key does); anything else is clicked.
let events = vec![request(if steps { Action::Increment } else { Action::Click })];
app.status = None;
frame(&mut app, events);
app.prompt = None;
@@ -561,6 +584,37 @@ fn every_control_can_be_used_in_the_pseudo_locale() {
assert!(found.is_empty(), "{} things in plain English after using the controls:\n{}", found.len(), found.join("\n"));
}
#[test]
fn tool_status_lines_are_translated() {
// The Eraser's and Eyedropper's results on the sample (on the sun and the hills, and on
// empty stage), as the status bar shows them.
let (mut app, _ctx) = pseudo_app(|_, _| {});
let mut plain = Vec::new();
let mut check = |app: &mut crate::app::App, what: &str| {
if let Some(s) = app.status.take()
&& !s.text.chars().any(pseudo::is_accented)
{
plain.push(format!("{what}: {:?}", s.text));
}
};
let sun = anicraft_engine::geom::Vec2::new(1000.0, 160.0);
let hill = anicraft_engine::geom::Vec2::new(300.0, 600.0);
let empty = anicraft_engine::geom::Vec2::new(-500.0, -500.0);
for (p, shift) in [(sun, false), (sun, true), (hill, false), (empty, false)] {
crate::erasertool::eyedropper(&mut app, p, shift, 1.0);
check(&mut app, "eyedropper");
}
for p in [hill, empty] {
crate::erasertool::faucet(&mut app, p, 1.0);
check(&mut app, "faucet");
}
crate::erasertool::erase_everything(&mut app);
check(&mut app, "erase everything");
crate::erasertool::erase_everything(&mut app);
check(&mut app, "erase everything again");
assert!(plain.is_empty(), "tool status lines in plain English:\n{}", plain.join("\n"));
}
#[test]
fn every_menu_item_is_translated() {
// The menus are built from the command registry when they open; each is drawn here as it