Skip to content

Commit

Permalink
Fix unexpected behaviour of keep_filter option in MultiSelect promp…
Browse files Browse the repository at this point in the history
…ts (#239)
  • Loading branch information
mikaelmello committed Mar 25, 2024
1 parent 82ab0e4 commit 62ecc2e
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

## [Unreleased] <!-- ReleaseDate -->

- No changes since the latest release below.
- Fix unexpected behaviour of `keep_filter` option in MultiSelect prompts, where the resulting behaviour was the opposite of what was expected.

## [0.7.3] - 2024-03-21

Expand Down
2 changes: 1 addition & 1 deletion inquire/src/prompts/multiselect/prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ where
}

fn clear_input_if_needed(&mut self, action: MultiSelectPromptAction) -> ActionResult {
if !self.config.keep_filter {
if self.config.keep_filter {
return ActionResult::Clean;
}

Expand Down
62 changes: 62 additions & 0 deletions inquire/src/prompts/multiselect/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,65 @@ fn chars_do_not_affect_prompt_without_filtering() {

assert_eq!(vec![ListOption::new(0, "Banana")], ans);
}

#[test]
fn keep_filter_false_behavior() {
let mut backend = fake_backend(vec![
Key::Char('1', KeyModifiers::NONE), // filter to option 1
Key::Char(' ', KeyModifiers::NONE), // toggle, filter input is reset to empty
Key::Char('2', KeyModifiers::NONE), // filter is now '2' and shows option 2
Key::Char(' ', KeyModifiers::NONE), // toggle
Key::Enter,
]);

let options = vec![1, 2, 3, 4, 5];

let ans = MultiSelect::new("Question", options)
.with_keep_filter(false)
.prompt_with_backend(&mut backend)
.unwrap();

let expected_answer = vec![ListOption::new(0, 1), ListOption::new(1, 2)];
assert_eq!(expected_answer, ans);
}

#[test]
fn keep_filter_true_behavior() {
let mut backend = fake_backend(vec![
Key::Char('1', KeyModifiers::NONE), // filter to option 1
Key::Char(' ', KeyModifiers::NONE), // toggle, filter input is NOT reset
Key::Char('2', KeyModifiers::NONE), // filter is now '12' and shows no option
Key::Char(' ', KeyModifiers::NONE), // should be no-op
Key::Enter,
]);

let options = vec![1, 2, 3, 4, 5];

let ans = MultiSelect::new("Question", options)
.with_keep_filter(true)
.prompt_with_backend(&mut backend)
.unwrap();

let expected_answer = vec![ListOption::new(0, 1)];
assert_eq!(expected_answer, ans);
}

#[test]
fn keep_filter_should_be_true_by_default() {
let mut backend = fake_backend(vec![
Key::Char('1', KeyModifiers::NONE), // filter to option 1
Key::Char(' ', KeyModifiers::NONE), // toggle, filter input is NOT reset
Key::Char('2', KeyModifiers::NONE), // filter is now '12' and shows no option
Key::Char(' ', KeyModifiers::NONE), // should be no-op
Key::Enter,
]);

let options = vec![1, 2, 3, 4, 5];

let prompt = MultiSelect::new("Question", options);
assert!(prompt.keep_filter);
let ans = prompt.prompt_with_backend(&mut backend).unwrap();

let expected_answer = vec![ListOption::new(0, 1)];
assert_eq!(expected_answer, ans);
}

0 comments on commit 62ecc2e

Please sign in to comment.