diff options
| author | Joe Carstairs <me@joeac.net> | 2026-02-01 20:39:39 +0000 |
|---|---|---|
| committer | Joe Carstairs <me@joeac.net> | 2026-02-01 20:39:39 +0000 |
| commit | 2c11ff0935ecf8f63d98509965c1a3ae94edce93 (patch) | |
| tree | cc06b78b776936164f4ee595cba31512c59604e9 /schist_desktop_gui/src/gui/components | |
| parent | 650cc444930bb82341ac6dedf94000ce1bce6503 (diff) | |
macrifies navigation replace test util functions
Diffstat (limited to 'schist_desktop_gui/src/gui/components')
| -rw-r--r-- | schist_desktop_gui/src/gui/components/navigation/navigation_replace_options.rs | 211 |
1 files changed, 113 insertions, 98 deletions
diff --git a/schist_desktop_gui/src/gui/components/navigation/navigation_replace_options.rs b/schist_desktop_gui/src/gui/components/navigation/navigation_replace_options.rs index 11ef380..e24059d 100644 --- a/schist_desktop_gui/src/gui/components/navigation/navigation_replace_options.rs +++ b/schist_desktop_gui/src/gui/components/navigation/navigation_replace_options.rs @@ -110,6 +110,55 @@ mod test { traits::Component, }; + macro_rules! assert_active_option_eq { + ($nav:expr, $option:expr) => { + if !$nav + .active_option + .as_ref() + .is_some_and(|o| o.name == $option) + { + panic!( + "navigation active option should have been Some(\"{}\"), but was {:?}", + $option, $nav.active_option + ); + } + }; + } + + macro_rules! assert_options_before_eq { + ($nav:expr, $options:expr) => { + let options_before_eq = $options.len() == $nav.options_before_active.len() + && (|| { + (0..$options.len()).all(|i| $options[i] == $nav.options_before_active[i].name) + })(); + + if !options_before_eq { + panic!( + "navigation should have had options ({}) before active option, but were ({})", + $options.iter().join(", "), + $nav.options_before_active.iter().join(", "), + ); + } + }; + } + + macro_rules! assert_options_after_eq { + ($nav:expr, $options:expr) => { + let options_after_eq = $options.len() == $nav.options_after_active.len() + && (|| { + (0..$options.len()).all(|i| $options[i] == $nav.options_after_active[i].name) + })(); + + if !options_after_eq { + panic!( + "navigation should have had options ({}) after active option, but were ({})", + $options.iter().join(", "), + $nav.options_after_active.iter().join(", "), + ); + } + }; + } + fn make_dummy_nav_with_options_a_to_g() -> Navigation<DummyNavigationOption> { Navigation::new( None, @@ -135,9 +184,9 @@ mod test { fn select_dummy_nav_option_selects_dummy_nav_option() { let mut nav = make_dummy_nav_with_options_a_to_g(); select_dummy_nav_option(&mut nav, "d"); - assert_options_before_eq(&nav, &["a", "b", "c"]); - assert_active_option_eq(&nav, "d"); - assert_options_after_eq(&nav, &["e", "f", "g"]); + assert_options_before_eq!(nav, ["a", "b", "c"]); + assert_active_option_eq!(nav, "d"); + assert_options_after_eq!(nav, ["e", "f", "g"]); } #[test] @@ -146,9 +195,9 @@ mod test { select_dummy_nav_option(&mut nav, "d"); let new = vec![DummyNavigationOption::new("z")]; nav.replace_first(|o| o.name == "a", &new); - assert_options_before_eq(&nav, &["z", "b", "c"]); - assert_active_option_eq(&nav, "d"); - assert_options_after_eq(&nav, &["e", "f", "g"]); + assert_options_before_eq!(nav, ["z", "b", "c"]); + assert_active_option_eq!(nav, "d"); + assert_options_after_eq!(nav, ["e", "f", "g"]); } #[test] @@ -161,9 +210,9 @@ mod test { DummyNavigationOption::new("z"), ]; nav.replace_first(|o| o.name == "a", &new); - assert_options_before_eq(&nav, &["x", "y", "z", "b", "c"]); - assert_active_option_eq(&nav, "d"); - assert_options_after_eq(&nav, &["e", "f", "g"]); + assert_options_before_eq!(nav, ["x", "y", "z", "b", "c"]); + assert_active_option_eq!(nav, "d"); + assert_options_after_eq!(nav, ["e", "f", "g"]); } #[test] @@ -172,9 +221,9 @@ mod test { select_dummy_nav_option(&mut nav, "d"); let new = vec![DummyNavigationOption::new("z")]; nav.replace_first(|o| o.name == "c", &new); - assert_options_before_eq(&nav, &["a", "b", "z"]); - assert_active_option_eq(&nav, "d"); - assert_options_after_eq(&nav, &["e", "f", "g"]); + assert_options_before_eq!(nav, ["a", "b", "z"]); + assert_active_option_eq!(nav, "d"); + assert_options_after_eq!(nav, ["e", "f", "g"]); } #[test] @@ -187,9 +236,9 @@ mod test { DummyNavigationOption::new("z"), ]; nav.replace_first(|o| o.name == "c", &new); - assert_options_before_eq(&nav, &["a", "b", "x", "y", "z"]); - assert_active_option_eq(&nav, "d"); - assert_options_after_eq(&nav, &["e", "f", "g"]); + assert_options_before_eq!(nav, ["a", "b", "x", "y", "z"]); + assert_active_option_eq!(nav, "d"); + assert_options_after_eq!(nav, ["e", "f", "g"]); } #[test] @@ -198,9 +247,9 @@ mod test { select_dummy_nav_option(&mut nav, "d"); let new = vec![DummyNavigationOption::new("z")]; nav.replace_first(|o| o.name == "d", &new); - assert_options_before_eq(&nav, &["a", "b", "c"]); - assert_active_option_eq(&nav, "z"); - assert_options_after_eq(&nav, &["e", "f", "g"]); + assert_options_before_eq!(nav, ["a", "b", "c"]); + assert_active_option_eq!(nav, "z"); + assert_options_after_eq!(nav, ["e", "f", "g"]); } #[test] @@ -209,9 +258,9 @@ mod test { select_dummy_nav_option(&mut nav, "a"); let new = vec![DummyNavigationOption::new("z")]; nav.replace_first(|o| o.name == "a", &new); - assert_options_before_eq(&nav, &[]); - assert_active_option_eq(&nav, "z"); - assert_options_after_eq(&nav, &["b", "c", "d", "e", "f", "g"]); + assert_options_before_eq!(nav, [] as [String; 0]); + assert_active_option_eq!(nav, "z"); + assert_options_after_eq!(nav, ["b", "c", "d", "e", "f", "g"]); } #[test] @@ -220,9 +269,9 @@ mod test { select_dummy_nav_option(&mut nav, "g"); let new = vec![DummyNavigationOption::new("z")]; nav.replace_first(|o| o.name == "g", &new); - assert_options_before_eq(&nav, &["a", "b", "c", "d", "e", "f"]); - assert_active_option_eq(&nav, "z"); - assert_options_after_eq(&nav, &[]); + assert_options_before_eq!(nav, ["a", "b", "c", "d", "e", "f"]); + assert_active_option_eq!(nav, "z"); + assert_options_after_eq!(nav, [] as [String; 0]); } #[test] @@ -231,9 +280,9 @@ mod test { let mut nav = Navigation::new(Some(active_option.clone()), vec![active_option]); let new = vec![DummyNavigationOption::new("z")]; nav.replace_first(|o| o.name == "a", &new); - assert_options_before_eq(&nav, &[]); - assert_active_option_eq(&nav, "z"); - assert_options_after_eq(&nav, &[]); + assert_options_before_eq!(nav, [] as [String; 0]); + assert_active_option_eq!(nav, "z"); + assert_options_after_eq!(nav, [] as [String; 0]); } #[test] @@ -246,9 +295,9 @@ mod test { DummyNavigationOption::new("z"), ]; nav.replace_first(|o| o.name == "d", &new); - assert_options_before_eq(&nav, &["a", "b", "c"]); - assert_active_option_eq(&nav, "x"); - assert_options_after_eq(&nav, &["y", "z", "e", "f", "g"]); + assert_options_before_eq!(nav, ["a", "b", "c"]); + assert_active_option_eq!(nav, "x"); + assert_options_after_eq!(nav, ["y", "z", "e", "f", "g"]); } #[test] @@ -261,9 +310,9 @@ mod test { DummyNavigationOption::new("z"), ]; nav.replace_first(|o| o.name == "a", &new); - assert_options_before_eq(&nav, &[]); - assert_active_option_eq(&nav, "x"); - assert_options_after_eq(&nav, &["y", "z", "b", "c", "d", "e", "f", "g"]); + assert_options_before_eq!(nav, [] as [String; 0]); + assert_active_option_eq!(nav, "x"); + assert_options_after_eq!(nav, ["y", "z", "b", "c", "d", "e", "f", "g"]); } #[test] @@ -276,10 +325,11 @@ mod test { DummyNavigationOption::new("z"), ]; nav.replace_first(|o| o.name == "g", &new); - assert_options_before_eq(&nav, &["a", "b", "c", "d", "e", "f"]); - assert_active_option_eq(&nav, "x"); - assert_options_after_eq(&nav, &["y", "z"]); + assert_options_before_eq!(nav, ["a", "b", "c", "d", "e", "f"]); + assert_active_option_eq!(nav, "x"); + assert_options_after_eq!(nav, ["y", "z"]); } + #[test] fn replace_active_option_with_many_when_active_option_is_only() { let active_option = DummyNavigationOption::new("a"); @@ -290,9 +340,9 @@ mod test { DummyNavigationOption::new("z"), ]; nav.replace_first(|o| o.name == "a", &new); - assert_options_before_eq(&nav, &[]); - assert_active_option_eq(&nav, "x"); - assert_options_after_eq(&nav, &["y", "z"]); + assert_options_before_eq!(nav, [] as [String; 0]); + assert_active_option_eq!(nav, "x"); + assert_options_after_eq!(nav, ["y", "z"]); } #[test] @@ -301,9 +351,9 @@ mod test { select_dummy_nav_option(&mut nav, "d"); let new = vec![DummyNavigationOption::new("z")]; nav.replace_first(|o| o.name == "e", &new); - assert_options_before_eq(&nav, &["a", "b", "c"]); - assert_active_option_eq(&nav, "d"); - assert_options_after_eq(&nav, &["z", "f", "g"]); + assert_options_before_eq!(nav, ["a", "b", "c"]); + assert_active_option_eq!(nav, "d"); + assert_options_after_eq!(nav, ["z", "f", "g"]); } #[test] @@ -316,9 +366,9 @@ mod test { DummyNavigationOption::new("z"), ]; nav.replace_first(|o| o.name == "e", &new); - assert_options_before_eq(&nav, &["a", "b", "c"]); - assert_active_option_eq(&nav, "d"); - assert_options_after_eq(&nav, &["x", "y", "z", "f", "g"]); + assert_options_before_eq!(nav, ["a", "b", "c"]); + assert_active_option_eq!(nav, "d"); + assert_options_after_eq!(nav, ["x", "y", "z", "f", "g"]); } #[test] @@ -327,9 +377,9 @@ mod test { select_dummy_nav_option(&mut nav, "d"); let new = vec![DummyNavigationOption::new("z")]; nav.replace_first(|o| o.name == "g", &new); - assert_options_before_eq(&nav, &["a", "b", "c"]); - assert_active_option_eq(&nav, "d"); - assert_options_after_eq(&nav, &["e", "f", "z"]); + assert_options_before_eq!(nav, ["a", "b", "c"]); + assert_active_option_eq!(nav, "d"); + assert_options_after_eq!(nav, ["e", "f", "z"]); } #[test] @@ -342,9 +392,9 @@ mod test { DummyNavigationOption::new("z"), ]; nav.replace_first(|o| o.name == "g", &new); - assert_options_before_eq(&nav, &["a", "b", "c"]); - assert_active_option_eq(&nav, "d"); - assert_options_after_eq(&nav, &["e", "f", "x", "y", "z"]); + assert_options_before_eq!(nav, ["a", "b", "c"]); + assert_active_option_eq!(nav, "d"); + assert_options_after_eq!(nav, ["e", "f", "x", "y", "z"]); } #[test] @@ -354,9 +404,9 @@ mod test { nav.replace_first(|o| o.name == "c", &vec![DummyNavigationOption::new("a")]); let new = vec![DummyNavigationOption::new("z")]; nav.replace_first(|o| o.name == "a", &new); - assert_options_before_eq(&nav, &["z", "b", "a"]); - assert_active_option_eq(&nav, "d"); - assert_options_after_eq(&nav, &["e", "f", "g"]); + assert_options_before_eq!(nav, ["z", "b", "a"]); + assert_active_option_eq!(nav, "d"); + assert_options_after_eq!(nav, ["e", "f", "g"]); } #[test] @@ -366,9 +416,9 @@ mod test { nav.replace_first(|o| o.name == "d", &vec![DummyNavigationOption::new("a")]); let new = vec![DummyNavigationOption::new("z")]; nav.replace_first(|o| o.name == "a", &new); - assert_options_before_eq(&nav, &["z", "b", "c"]); - assert_active_option_eq(&nav, "a"); - assert_options_after_eq(&nav, &["e", "f", "g"]); + assert_options_before_eq!(nav, ["z", "b", "c"]); + assert_active_option_eq!(nav, "a"); + assert_options_after_eq!(nav, ["e", "f", "g"]); } #[test] @@ -378,9 +428,9 @@ mod test { nav.replace_first(|o| o.name == "f", &vec![DummyNavigationOption::new("a")]); let new = vec![DummyNavigationOption::new("z")]; nav.replace_first(|o| o.name == "a", &new); - assert_options_before_eq(&nav, &["z", "b", "c"]); - assert_active_option_eq(&nav, "d"); - assert_options_after_eq(&nav, &["e", "a", "g"]); + assert_options_before_eq!(nav, ["z", "b", "c"]); + assert_active_option_eq!(nav, "d"); + assert_options_after_eq!(nav, ["e", "a", "g"]); } #[test] @@ -390,43 +440,8 @@ mod test { nav.replace_first(|o| o.name == "f", &vec![DummyNavigationOption::new("d")]); let new = vec![DummyNavigationOption::new("z")]; nav.replace_first(|o| o.name == "d", &new); - assert_options_before_eq(&nav, &["a", "b", "c"]); - assert_active_option_eq(&nav, "z"); - assert_options_after_eq(&nav, &["e", "d", "g"]); - } - - fn assert_options_before_eq(nav: &Navigation<DummyNavigationOption>, options: &[&str]) { - let options_before_eq = options.len() == nav.options_before_active.len() - && (|| (0..options.len()).all(|i| options[i] == nav.options_before_active[i].name))(); - - if !options_before_eq { - panic!( - "navigation should have had options ({}) before active option, but were ({})", - options.iter().join(", "), - nav.options_before_active.iter().join(", "), - ); - } - } - - fn assert_active_option_eq(nav: &Navigation<DummyNavigationOption>, option: &str) { - if !nav.active_option.as_ref().is_some_and(|o| o.name == option) { - panic!( - "navigation active option should have been Some(\"{}\"), but was {:?}", - option, nav.active_option - ); - } - } - - fn assert_options_after_eq(nav: &Navigation<DummyNavigationOption>, options: &[&str]) { - let options_after_eq = options.len() == nav.options_after_active.len() - && (|| (0..options.len()).all(|i| options[i] == nav.options_after_active[i].name))(); - - if !options_after_eq { - panic!( - "navigation should have had options ({}) after active option, but were ({})", - options.iter().join(", "), - nav.options_after_active.iter().join(", "), - ); - } + assert_options_before_eq!(nav, ["a", "b", "c"]); + assert_active_option_eq!(nav, "z"); + assert_options_after_eq!(nav, ["e", "d", "g"]); } } |
