Skip to content

Commit

Permalink
Merge branch 'main' into 123-vim-mode-not-configured-for-monthlyyearl…
Browse files Browse the repository at this point in the history
…y-shift-in-dateselect
  • Loading branch information
mikaelmello committed Dec 27, 2023
2 parents f2e8cb1 + 1503f57 commit db654a7
Show file tree
Hide file tree
Showing 8 changed files with 345 additions and 438 deletions.
26 changes: 6 additions & 20 deletions inquire/src/prompts/dateselect/test.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use crate::{
date_utils::get_current_date,
terminal::crossterm::CrosstermTerminal,
ui::{Backend, RenderConfig},
test::fake_backend,
ui::{Key, KeyModifiers},
validator::Validation,
DateSelect,
};
use chrono::NaiveDate;
use crossterm::event::{KeyCode, KeyEvent};

fn default<'a>() -> DateSelect<'a> {
DateSelect::new("Question?")
Expand All @@ -20,12 +19,7 @@ macro_rules! date_test {
($name:ident,$input:expr,$output:expr,$prompt:expr) => {
#[test]
fn $name() {
let read: Vec<KeyEvent> = $input.into_iter().map(KeyEvent::from).collect();
let mut read = read.iter();

let mut write: Vec<u8> = Vec::new();
let terminal = CrosstermTerminal::new_with_io(&mut write, &mut read);
let mut backend = Backend::new(terminal, RenderConfig::default()).unwrap();
let mut backend = fake_backend($input);

let ans = $prompt.prompt_with_backend(&mut backend).unwrap();

Expand All @@ -34,11 +28,11 @@ macro_rules! date_test {
};
}

date_test!(today_date, vec![KeyCode::Enter], get_current_date());
date_test!(today_date, vec![Key::Enter], get_current_date());

date_test!(
custom_default_date,
vec![KeyCode::Enter],
vec![Key::Enter],
NaiveDate::from_ymd_opt(2021, 1, 9).unwrap(),
DateSelect::new("Date").with_default(NaiveDate::from_ymd_opt(2021, 1, 9).unwrap())
);
Expand All @@ -47,11 +41,7 @@ date_test!(
/// Tests that a closure that actually closes on a variable can be used
/// as a DateSelect validator.
fn closure_validator() {
let read: Vec<KeyEvent> = vec![KeyCode::Enter, KeyCode::Left, KeyCode::Enter]
.into_iter()
.map(KeyEvent::from)
.collect();
let mut read = read.iter();
let mut backend = fake_backend(vec![Key::Enter, Key::Left(KeyModifiers::NONE), Key::Enter]);

let today_date = get_current_date();

Expand All @@ -63,10 +53,6 @@ fn closure_validator() {
}
};

let mut write: Vec<u8> = Vec::new();
let terminal = CrosstermTerminal::new_with_io(&mut write, &mut read);
let mut backend = Backend::new(terminal, RenderConfig::default()).unwrap();

let ans = DateSelect::new("Question")
.with_validator(validator)
.prompt_with_backend(&mut backend)
Expand Down
2 changes: 2 additions & 0 deletions inquire/src/prompts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ mod one_liners;
mod password;
mod prompt;
mod select;
#[cfg(test)]
pub(crate) mod test;
mod text;

pub use action::*;
Expand Down
138 changes: 37 additions & 101 deletions inquire/src/prompts/multiselect/test.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,22 @@
use crate::{
formatter::MultiOptionFormatter,
list_option::ListOption,
terminal::crossterm::CrosstermTerminal,
ui::{Backend, RenderConfig},
test::fake_backend,
ui::{Key, KeyModifiers},
MultiSelect,
};
use crossterm::event::{KeyCode, KeyEvent};

#[test]
/// Tests that a closure that actually closes on a variable can be used
/// as a Select formatter.
fn closure_formatter() {
let read: Vec<KeyEvent> = vec![KeyCode::Char(' '), KeyCode::Enter]
.into_iter()
.map(KeyEvent::from)
.collect();
let mut read = read.iter();
let mut backend = fake_backend(vec![Key::Char(' ', KeyModifiers::NONE), Key::Enter]);

let formatted = String::from("Thanks!");
let formatter: MultiOptionFormatter<'_, i32> = &|_| formatted.clone();

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

let mut write: Vec<u8> = Vec::new();
let terminal = CrosstermTerminal::new_with_io(&mut write, &mut read);
let mut backend = Backend::new(terminal, RenderConfig::default()).unwrap();

let ans = MultiSelect::new("Question", options)
.with_formatter(formatter)
.prompt_with_backend(&mut backend)
Expand All @@ -37,27 +28,18 @@ fn closure_formatter() {
#[test]
// Anti-regression test: https://github.com/mikaelmello/inquire/issues/30
fn down_arrow_on_empty_list_does_not_panic() {
let read: Vec<KeyEvent> = [
KeyCode::Char('9'),
KeyCode::Down,
KeyCode::Backspace,
KeyCode::Char('3'),
KeyCode::Down,
KeyCode::Backspace,
KeyCode::Enter,
]
.iter()
.map(|c| KeyEvent::from(*c))
.collect();

let mut read = read.iter();
let mut backend = fake_backend(vec![
Key::Char('9', KeyModifiers::NONE),
Key::Down(KeyModifiers::NONE),
Key::Backspace,
Key::Char('3', KeyModifiers::NONE),
Key::Down(KeyModifiers::NONE),
Key::Backspace,
Key::Enter,
]);

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

let mut write: Vec<u8> = Vec::new();
let terminal = CrosstermTerminal::new_with_io(&mut write, &mut read);
let mut backend = Backend::new(terminal, RenderConfig::default()).unwrap();

let ans = MultiSelect::new("Question", options)
.prompt_with_backend(&mut backend)
.unwrap();
Expand All @@ -67,19 +49,9 @@ fn down_arrow_on_empty_list_does_not_panic() {

#[test]
fn selecting_all_by_default_behavior() {
let read: Vec<KeyEvent> = [KeyCode::Enter, KeyCode::Enter]
.iter()
.map(|c| KeyEvent::from(*c))
.collect();

let mut read = read.iter();

let mut backend = fake_backend(vec![Key::Enter, Key::Enter]);
let options = vec![1, 2, 3];

let mut write: Vec<u8> = Vec::new();
let terminal = CrosstermTerminal::new_with_io(&mut write, &mut read);
let mut backend = Backend::new(terminal, RenderConfig::default()).unwrap();

let answer_with_all_selected_by_default = MultiSelect::new("Question", options.clone())
.with_all_selected_by_default()
.prompt_with_backend(&mut backend)
Expand All @@ -105,24 +77,16 @@ fn selecting_all_by_default_behavior() {
#[test]
// Anti-regression test: https://github.com/mikaelmello/inquire/issues/31
fn list_option_indexes_are_relative_to_input_vec() {
let read: Vec<KeyEvent> = vec![
KeyCode::Down,
KeyCode::Char(' '),
KeyCode::Down,
KeyCode::Char(' '),
KeyCode::Enter,
]
.into_iter()
.map(KeyEvent::from)
.collect();
let mut read = read.iter();
let mut backend = fake_backend(vec![
Key::Down(KeyModifiers::NONE),
Key::Char(' ', KeyModifiers::NONE),
Key::Down(KeyModifiers::NONE),
Key::Char(' ', KeyModifiers::NONE),
Key::Enter,
]);

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

let mut write: Vec<u8> = Vec::new();
let terminal = CrosstermTerminal::new_with_io(&mut write, &mut read);
let mut backend = Backend::new(terminal, RenderConfig::default()).unwrap();

let ans = MultiSelect::new("Question", options)
.prompt_with_backend(&mut backend)
.unwrap();
Expand All @@ -133,19 +97,9 @@ fn list_option_indexes_are_relative_to_input_vec() {
#[test]
// Anti-regression test: https://github.com/mikaelmello/inquire/issues/195
fn starting_cursor_is_respected() {
let read: Vec<KeyEvent> = [KeyCode::Char(' '), KeyCode::Enter]
.iter()
.map(|c| KeyEvent::from(*c))
.collect();

let mut read = read.iter();

let mut backend = fake_backend(vec![Key::Char(' ', KeyModifiers::NONE), Key::Enter]);
let options = vec![1, 2, 3];

let mut write: Vec<u8> = Vec::new();
let terminal = CrosstermTerminal::new_with_io(&mut write, &mut read);
let mut backend = Backend::new(terminal, RenderConfig::default()).unwrap();

let ans = MultiSelect::new("Question", options)
.with_starting_cursor(2)
.prompt_with_backend(&mut backend)
Expand All @@ -156,19 +110,14 @@ fn starting_cursor_is_respected() {

#[test]
fn naive_assert_fuzzy_match_as_default_scorer() {
let read: Vec<KeyEvent> = [
KeyCode::Char('w'),
KeyCode::Char('r'),
KeyCode::Char('r'),
KeyCode::Char('y'),
KeyCode::Char(' '),
KeyCode::Enter,
]
.iter()
.map(|c| KeyEvent::from(*c))
.collect();

let mut read = read.iter();
let mut backend = fake_backend(vec![
Key::Char('w', KeyModifiers::NONE),
Key::Char('r', KeyModifiers::NONE),
Key::Char('r', KeyModifiers::NONE),
Key::Char('y', KeyModifiers::NONE),
Key::Char(' ', KeyModifiers::NONE),
Key::Enter,
]);

let options = vec![
"Banana",
Expand All @@ -184,10 +133,6 @@ fn naive_assert_fuzzy_match_as_default_scorer() {
"Pineapple",
];

let mut write: Vec<u8> = Vec::new();
let terminal = CrosstermTerminal::new_with_io(&mut write, &mut read);
let mut backend = Backend::new(terminal, RenderConfig::default()).unwrap();

let ans = MultiSelect::new("Question", options)
.prompt_with_backend(&mut backend)
.unwrap();
Expand All @@ -197,19 +142,14 @@ fn naive_assert_fuzzy_match_as_default_scorer() {

#[test]
fn chars_do_not_affect_prompt_without_filtering() {
let read: Vec<KeyEvent> = [
KeyCode::Char('w'),
KeyCode::Char('r'),
KeyCode::Char('r'),
KeyCode::Char('y'),
KeyCode::Char(' '),
KeyCode::Enter,
]
.iter()
.map(|c| KeyEvent::from(*c))
.collect();

let mut read = read.iter();
let mut backend = fake_backend(vec![
Key::Char('w', KeyModifiers::NONE),
Key::Char('r', KeyModifiers::NONE),
Key::Char('r', KeyModifiers::NONE),
Key::Char('y', KeyModifiers::NONE),
Key::Char(' ', KeyModifiers::NONE),
Key::Enter,
]);

let options = vec![
"Banana",
Expand All @@ -225,10 +165,6 @@ fn chars_do_not_affect_prompt_without_filtering() {
"Pineapple",
];

let mut write: Vec<u8> = Vec::new();
let terminal = CrosstermTerminal::new_with_io(&mut write, &mut read);
let mut backend = Backend::new(terminal, RenderConfig::default()).unwrap();

let ans = MultiSelect::new("Question", options)
.without_filtering()
.prompt_with_backend(&mut backend)
Expand Down
Loading

0 comments on commit db654a7

Please sign in to comment.