Lines
100 %
Functions
Branches
use super::*;
fn make_opts(n: usize) -> Vec<SelectOption> {
(0..n)
.map(|i| SelectOption {
id: format!("id-{i}"),
label: format!("Label {i}"),
})
.collect()
}
#[test]
fn empty_widget_returns_empty_strings() {
let w = SelectWidget::new();
assert_eq!(w.value(), "");
assert_eq!(w.display(), "");
fn zero_match_confirm_is_noop_and_keeps_query() {
let mut w = SelectWidget::new();
w.set_options(make_opts(3));
for c in "zzz".chars() {
w.filter_push(c);
assert!(w.matches().is_empty(), "query matches nothing");
w.confirm();
assert_eq!(
w.value(),
"",
"confirm with no match must not silently select option 0"
);
w.query(),
"zzz",
"query kept so the user can fix the filter"
fn value_returns_focused_id() {
assert_eq!(w.value(), "id-0");
w.next();
assert_eq!(w.value(), "id-1");
fn display_returns_focused_label() {
assert_eq!(w.display(), "Label 0");
assert_eq!(w.display(), "Label 1");
fn next_wraps_around() {
w.next(); // 0 → 1 → 2
assert_eq!(w.value(), "id-2");
w.next(); // wraps to 0
fn prev_wraps_around() {
w.prev(); // 0 wraps to 2
w.prev(); // 2 → 1
fn next_and_prev_noop_on_empty() {
w.prev();
fn an_id_chosen_before_the_options_exist_is_selected_when_they_arrive() {
w.select_id("id-2");
assert_eq!(w.value(), "id-0", "the remembered id is used once");
fn value_keeps_a_pending_id_before_the_options_arrive() {
fn set_options_resets_focus() {
w.set_options(make_opts(2));
fn set_options_preserving_id_keeps_selected_uuid() {
w.set_options(vec![SelectOption {
id: "uuid-42".to_string(),
label: "uuid-42".to_string(),
}]);
assert_eq!(w.value(), "uuid-42");
let full_list = vec![
SelectOption {
id: "uuid-10".to_string(),
label: "First".to_string(),
},
label: "Second (proper label)".to_string(),
id: "uuid-99".to_string(),
label: "Third".to_string(),
];
w.set_options_preserving_id(full_list);
"uuid-42",
"uuid preserved after set_options_preserving_id"
w.display(),
"Second (proper label)",
"label updated to full label"
fn set_options_preserving_id_falls_back_to_zero_when_not_found() {
id: "old".to_string(),
label: "Old".to_string(),
let new_list = vec![
id: "new-a".to_string(),
label: "A".to_string(),
id: "new-b".to_string(),
label: "B".to_string(),
w.set_options_preserving_id(new_list);
assert_eq!(w.value(), "new-a");
fn confirm_closes_open_dropdown() {
w.open = true;
assert!(!w.open);
fn filter_push_narrows_matches() {
w.set_options(vec![
id: "a".to_string(),
label: "Apple".to_string(),
id: "b".to_string(),
label: "Grape".to_string(),
id: "c".to_string(),
label: "Apricot".to_string(),
]);
// 'p' matches "Apple" (has 'p') and "Apricot" (has 'p'), not "Grape" (has 'p' too!)
// Use 'i' instead: "Apricot" has 'i', "Apple" has no 'i', "Grape" has no 'i'
w.filter_push('i');
let m = w.matches();
assert_eq!(m.len(), 1, "only Apricot matches 'i'");
assert_eq!(w.value(), "c", "first (only) match is Apricot");
fn filter_next_wraps_within_filtered_set() {
id: "id-0".to_string(),
label: "Sword".to_string(),
id: "id-1".to_string(),
label: "Tuna".to_string(),
id: "id-2".to_string(),
label: "Squid".to_string(),
// 's' matches "Sword" (idx 0) and "Squid" (idx 2), not "Tuna"
w.filter_push('s');
assert_eq!(w.value(), "id-0", "first match is Sword");
assert_eq!(w.value(), "id-2", "second match is Squid");
assert_eq!(w.value(), "id-0", "wraps back to first match");
fn filter_pop_widens_matches() {
w.filter_push('1'); // only "Label 1" matches
assert_eq!(w.matches().len(), 1);
w.filter_pop();
assert_eq!(w.matches().len(), 3, "empty query matches all");
fn set_options_clears_query() {
w.filter_push('x');
assert_eq!(w.query(), "x");
assert_eq!(w.query(), "", "set_options must clear query");
fn set_options_preserving_id_clears_query() {
w.set_options_preserving_id(make_opts(2));
assert_eq!(w.query(), "", "set_options_preserving_id must clear query");
fn zero_match_value_and_display_empty() {
w.filter_push('z'); // no "Label N" contains 'z'
assert_eq!(w.matches().len(), 0);
assert_eq!(w.value(), "", "zero match → value is empty string");
assert_eq!(w.display(), "", "zero match → display is empty string");
fn zero_match_next_and_prev_no_panic() {
w.filter_push('z'); // zero matches
w.next(); // must not panic or index out of bounds
w.prev(); // must not panic or index out of bounds
fn confirm_resolves_filtered_selection_to_raw_index() {
id: "id-x".to_string(),
label: "Xenon".to_string(),
id: "id-a".to_string(),
id: "id-b".to_string(),
label: "Banana".to_string(),
// 'a' matches "Apple" (idx 1) and "Banana" (idx 2); focused=0 → "Apple"
w.filter_push('a');
assert_eq!(w.value(), "id-a");
assert_eq!(w.query(), "");
// After confirm, focused resolved to raw index of "Apple" (idx 1)
// With empty query, matches=[0,1,2], focused=1 → options[1] = "Apple"
"id-a",
"selection preserved after confirm+query clear"
fn a_step_before_the_options_keeps_the_pending_value() {
w.set_options_preserving_id(make_opts(3));
fn a_step_over_options_drops_the_pending_value() {
w.set_options_preserving_id(make_opts(1));
fn backspacing_a_query_to_empty_keeps_the_chosen_value() {
w.filter_push('1');
fn cancel_goes_back_to_the_option_the_list_opened_on_and_drops_the_query() {
w.open_list();
w.filter_push('0');
w.cancel();
assert_eq!((w.value(), w.query(), w.open), ("id-2", "", false));
fn a_longer_query_popped_once_keeps_filtering() {
w.set_options(make_opts(12));
w.select_id("id-5");
assert_eq!(w.query(), "1");
assert_eq!(w.value(), "id-5");
fn pick_closes_the_list_on_the_filtered_option() {
w.set_options(make_opts(5));
w.filter_push('4');
w.pick(0);
assert_eq!(w.value(), "id-4");
fn pick_past_the_options_changes_nothing() {
w.pick(3);
assert!(w.open);
fn page_moves_a_window_of_rows_and_stops_at_the_ends() {
w.set_options(make_opts(10));
let _ = w.window().fit(10, 4);
w.page(true);
assert_eq!(w.value(), "id-9");
w.page(false);
(0..3).for_each(|_| w.page(false));