added tests for input

This commit is contained in:
Lutetium-Vanadium 2021-06-24 16:28:28 +05:30
parent d8905971cd
commit 43eb1531cd
84 changed files with 2553 additions and 12 deletions

View File

@ -0,0 +1,27 @@
---
source: src/question/input.rs
expression: backend
---
┌──────────────────────────────────────────────────┐
? message    │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: src/question/input.rs
expression: backend
---
┌──────────────────────────────────────────────────┐
? message  inputinputinputinputinputinputinp│
│utinputinputinput  │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: src/question/input.rs
expression: backend
---
┌──────────────────────────────────────────────────┐
? message  inputa │
 inputa │
│ inputb │
│ inputc │
│ inputd │
│ inpute │
  │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: src/question/input.rs
expression: backend
---
┌──────────────────────────────────────────────────┐
? message  inputb  │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: src/question/input.rs
expression: backend
---
┌──────────────────────────────────────────────────┐
? message (default)   │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: src/question/input.rs
expression: backend
---
┌──────────────────────────────────────────────────┐
? message (default) inputinputinputinputinput│
│inputinputinputinputinput  │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: src/question/input.rs
expression: backend
---
┌──────────────────────────────────────────────────┐
? message    │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: src/question/input.rs
expression: backend
---
┌──────────────────────────────────────────────────┐
? message  inputinputinputinputinputinputinp│
│utinputinputinput  │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
✔ message · string │
  │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message  s  │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message  st  │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message  str  │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message  strg  │
 strg │
│ strh │
│ stri │
│ strj │
│ strk │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message  strh  │
│ strg │
 strh │
│ stri │
│ strj │
│ strk │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message  stri  │
│ strg │
│ strh │
 stri │
│ strj │
│ strk │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message  strin  │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message  string  │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message    │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message (default) s  │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message (default)   │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
✔ message · │
  │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message (default)   │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
✔ message · default │
  │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message (default)   │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message  s  │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message  st  │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message  str  │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
✔ message · str--suffix │
  │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message    │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message  s  │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message  st  │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message  str  │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
✔ message · "str" │
  │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message    │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message  t  │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message  tr  │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message  tr  │
✖ The string must be more than 2 characters long │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message  tr │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message  str │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
✔ message · str │
  │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message    │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -171,7 +171,21 @@ impl Prompt for InputPrompt<'_, '_> {
}
}
impl Input<'_> {
impl<'i> Input<'i> {
fn into_input_prompt<'a>(
mut self,
message: &'a str,
answers: &'a Answers,
) -> InputPrompt<'i, 'a> {
InputPrompt {
prompt: widgets::Prompt::new(message).with_optional_hint(self.default.take()),
input_opts: self,
input: widgets::StringInput::default(),
select: None,
answers,
}
}
pub(crate) fn ask<B: Backend, E: Iterator<Item = error::Result<KeyEvent>>>(
mut self,
message: String,
@ -181,17 +195,7 @@ impl Input<'_> {
) -> error::Result<Answer> {
let transform = self.transform.take();
let ans = ui::Input::new(
InputPrompt {
prompt: widgets::Prompt::new(&message[..]).with_optional_hint(self.default.take()),
input_opts: self,
input: widgets::StringInput::default(),
select: None,
answers,
},
b,
)
.run(events)?;
let ans = ui::Input::new(self.into_input_prompt(&message, answers), b).run(events)?;
crate::write_final!(
transform,
@ -240,3 +244,189 @@ impl<'a> From<InputBuilder<'a>> for super::Question<'a> {
builder.build()
}
}
#[cfg(test)]
mod tests {
use ui::{backend::TestBackend, layout::Layout};
use super::*;
const NINPUTS: usize = 3;
static INPUT_IDS: [&str; NINPUTS] = ["no_default", "default", "auto_complete"];
const AUTO_COMPLETE_IDX: usize = 2;
fn inputs(answers: &Answers) -> [(InputPrompt<'static, '_>, u16); NINPUTS] {
[
(Input::default().into_input_prompt("message", &answers), 17),
(
Input {
default: Some("default".into()),
..Input::default()
}
.into_input_prompt("message", &answers),
25,
),
(
Input {
auto_complete: AutoComplete::Sync(Box::new(|s, _| {
let mut completions: Vec<_> = ('a'..='d')
.map(|c| {
let mut s = s.clone();
s.push(c);
s
})
.collect();
completions.push(s + "e");
completions
})),
..Input::default()
}
.into_input_prompt("message", &answers),
17,
),
]
}
#[test]
fn test_render() {
let size = (50, 20).into();
let base_layout = Layout::new(5, size);
let answers = Answers::default();
let mut inputs = inputs(&answers);
let mut backend = TestBackend::new_with_layout(size, base_layout);
for (i, (prompt, line_offset)) in inputs.iter_mut().enumerate() {
let line_offset = *line_offset;
let mut layout = base_layout;
backend.reset_with_layout(layout);
assert!(prompt.render(&mut layout, &mut backend).is_ok());
assert_eq!(layout, base_layout.with_line_offset(line_offset));
ui::assert_backend_snapshot!(format!("{}-1", INPUT_IDS[i]), backend);
prompt.input.set_value("input".repeat(10));
layout = base_layout;
backend.reset_with_layout(layout);
assert!(prompt.render(&mut layout, &mut backend).is_ok());
assert_eq!(
layout,
base_layout.with_offset(0, 1).with_line_offset(line_offset)
);
ui::assert_backend_snapshot!(format!("{}-2", INPUT_IDS[i]), backend);
}
let prompt = &mut inputs[AUTO_COMPLETE_IDX].0;
prompt.input.replace_with(|mut s| {
s.truncate(5);
s
});
assert!(prompt.handle_key(KeyCode::Tab.into()));
let mut layout = base_layout;
backend.reset_with_layout(layout);
assert!(prompt.render(&mut layout, &mut backend).is_ok());
assert_eq!(layout, base_layout.with_offset(0, 6).with_line_offset(0));
ui::assert_backend_snapshot!(format!("{}-3", INPUT_IDS[AUTO_COMPLETE_IDX]), backend);
assert!(prompt.handle_key(KeyCode::Tab.into()));
assert_eq!(prompt.validate(), Ok(Validation::Continue));
layout = base_layout;
backend.reset_with_layout(layout);
assert!(prompt.render(&mut layout, &mut backend).is_ok());
assert_eq!(
layout,
base_layout
.with_offset(0, 0)
.with_line_offset(inputs[AUTO_COMPLETE_IDX].1 + 6)
);
ui::assert_backend_snapshot!(format!("{}-4", INPUT_IDS[AUTO_COMPLETE_IDX]), backend);
}
#[test]
fn test_height() {
let size = (50, 20).into();
let base_layout = Layout::new(5, size);
let answers = Answers::default();
let mut inputs = inputs(&answers);
for (prompt, line_offset) in inputs.iter_mut() {
let line_offset = *line_offset;
let mut layout = base_layout;
assert_eq!(prompt.height(&mut layout), 1);
assert_eq!(layout, base_layout.with_line_offset(line_offset));
prompt.input.set_value("input".repeat(10));
layout = base_layout;
assert_eq!(prompt.height(&mut layout), 2);
assert_eq!(
layout,
base_layout.with_offset(0, 1).with_line_offset(line_offset)
);
}
let prompt = &mut inputs[AUTO_COMPLETE_IDX].0;
prompt.input.replace_with(|mut s| {
s.truncate(5);
s
});
assert!(prompt.handle_key(KeyCode::Tab.into()));
let mut layout = base_layout;
assert_eq!(prompt.height(&mut layout), 6);
assert_eq!(layout, base_layout.with_offset(0, 6).with_line_offset(0));
assert!(prompt.handle_key(KeyCode::Tab.into()));
assert_eq!(prompt.validate(), Ok(Validation::Continue));
layout = base_layout;
assert_eq!(prompt.height(&mut layout), 1);
assert_eq!(
layout,
base_layout
.with_offset(0, 0)
.with_line_offset(inputs[AUTO_COMPLETE_IDX].1 + 6)
);
}
#[test]
fn test_cursor_pos() {
let size = (50, 20).into();
let layout = Layout::new(5, size);
let answers = Answers::default();
let mut inputs = inputs(&answers);
for (prompt, line_offset) in inputs.iter_mut() {
let line_offset = *line_offset;
assert_eq!(prompt.cursor_pos(layout), (line_offset, 0));
prompt.input.set_value("input".repeat(10));
prompt.input.set_at(50);
assert_eq!(prompt.cursor_pos(layout), (line_offset, 1));
}
let prompt = &mut inputs[AUTO_COMPLETE_IDX].0;
let line_offset = inputs[AUTO_COMPLETE_IDX].1;
prompt.input.replace_with(|mut s| {
s.truncate(5);
s
});
assert!(prompt.handle_key(KeyCode::Tab.into()));
assert_eq!(prompt.cursor_pos(layout), (line_offset + 6, 0));
assert!(prompt.handle_key(KeyCode::Tab.into()));
assert_eq!(prompt.validate(), Ok(Validation::Continue));
assert_eq!(prompt.cursor_pos(layout), (line_offset + 6, 0));
}
}

View File

@ -0,0 +1,27 @@
---
source: src/question/input.rs
expression: backend
---
┌──────────────────────────────────────────────────┐
? message    │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: src/question/input.rs
expression: backend
---
┌──────────────────────────────────────────────────┐
? message  inputinputinputinputinputinputinp│
│utinputinputinput  │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: src/question/input.rs
expression: backend
---
┌──────────────────────────────────────────────────┐
? message  inputa │
 inputa │
│ inputb │
│ inputc │
│ inputd │
│ inpute │
  │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: src/question/input.rs
expression: backend
---
┌──────────────────────────────────────────────────┐
? message  inputb  │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: src/question/input.rs
expression: backend
---
┌──────────────────────────────────────────────────┐
? message (default)   │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: src/question/input.rs
expression: backend
---
┌──────────────────────────────────────────────────┐
? message (default) inputinputinputinputinput│
│inputinputinputinputinput  │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: src/question/input.rs
expression: backend
---
┌──────────────────────────────────────────────────┐
? message    │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: src/question/input.rs
expression: backend
---
┌──────────────────────────────────────────────────┐
? message  inputinputinputinputinputinputinp│
│utinputinputinput  │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
✔ message · string │
  │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message  s  │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message  st  │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message  str  │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message  strg  │
 strg │
│ strh │
│ stri │
│ strj │
│ strk │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message  strh  │
│ strg │
 strh │
│ stri │
│ strj │
│ strk │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message  stri  │
│ strg │
│ strh │
 stri │
│ strj │
│ strk │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message  strin  │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message  string  │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message    │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message (default) s  │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message (default)   │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
✔ message · │
  │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message (default)   │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
✔ message · default │
  │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message (default)   │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message  s  │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message  st  │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message  str  │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
✔ message · str--suffix │
  │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message    │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message  s  │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message  st  │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message  str  │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
✔ message · "str" │
  │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message    │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message  t  │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message  tr  │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message  tr  │
✖ The string must be more than 2 characters long │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message  tr │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message  str │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
✔ message · str │
  │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,27 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message    │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

137
tests/input.rs Normal file
View File

@ -0,0 +1,137 @@
use discourse::{Answer, Question};
use ui::{
events::{KeyCode, TestEvents},
style::Color,
};
mod helpers;
#[test]
fn test_validate() {
let prompt = Question::input("name").message("message").validate(|s, _| {
if s.len() > 2 {
Ok(())
} else {
Err("The string must be more than 2 characters long".into())
}
});
let mut backend = helpers::SnapshotOnFlushBackend::new((50, 20).into());
let mut events = TestEvents::new(vec![
KeyCode::Char('t').into(),
KeyCode::Char('r').into(),
KeyCode::Enter.into(),
KeyCode::Home.into(),
KeyCode::Char('s').into(),
KeyCode::Enter.into(),
]);
let ans = discourse::prompt_one_with(prompt, &mut backend, &mut events).unwrap();
assert_eq!(ans, Answer::String("str".into()));
assert!(events.next().is_none());
}
#[test]
fn test_filter() {
let prompt = Question::input("name")
.message("message")
.filter(|s, _| s + "--suffix");
let mut backend = helpers::SnapshotOnFlushBackend::new((50, 20).into());
let mut events = TestEvents::new(vec![
KeyCode::Char('s').into(),
KeyCode::Char('t').into(),
KeyCode::Char('r').into(),
KeyCode::Enter.into(),
]);
let ans = discourse::prompt_one_with(prompt, &mut backend, &mut events).unwrap();
assert_eq!(ans, Answer::String("str--suffix".into()));
assert!(events.next().is_none());
}
#[test]
fn test_transform() {
let prompt = Question::input("name")
.message("message")
.transform(|s, _, b| {
b.set_fg(Color::Magenta)?;
write!(b, "{:?}", s)?;
b.set_fg(Color::Reset)
});
let mut backend = helpers::SnapshotOnFlushBackend::new((50, 20).into());
let mut events = TestEvents::new(vec![
KeyCode::Char('s').into(),
KeyCode::Char('t').into(),
KeyCode::Char('r').into(),
KeyCode::Enter.into(),
]);
let ans = discourse::prompt_one_with(prompt, &mut backend, &mut events).unwrap();
assert_eq!(ans, Answer::String("str".into()));
assert!(events.next().is_none());
}
#[test]
fn test_default() {
let prompt = Question::input("name")
.message("message")
.default("default");
let mut backend = helpers::SnapshotOnFlushBackend::new((50, 20).into());
let mut events = TestEvents::new(vec![
KeyCode::Char('s').into(),
KeyCode::Backspace.into(),
KeyCode::Enter.into(),
]);
let ans = discourse::prompt_one_with(prompt, &mut backend, &mut events).unwrap();
assert_eq!(ans, Answer::String("".into()));
assert!(events.next().is_none());
let prompt = Question::input("name")
.message("message")
.default("default");
let mut backend = helpers::SnapshotOnFlushBackend::new((50, 20).into());
let mut events = TestEvents::new(Some(KeyCode::Enter.into()));
let ans = discourse::prompt_one_with(prompt, &mut backend, &mut events).unwrap();
assert_eq!(ans, Answer::String("default".into()));
assert!(events.next().is_none());
}
#[test]
fn test_auto_complete() {
let prompt = Question::input("name")
.message("message")
.auto_complete(|s, _| {
let mut completions: Vec<_> = ('g'..='j')
.map(|c| {
let mut s = s.clone();
s.push(c);
s
})
.collect();
completions.push(s + "k");
completions
});
let mut backend = helpers::SnapshotOnFlushBackend::new((50, 20).into());
let mut events = TestEvents::new(vec![
KeyCode::Char('s').into(),
KeyCode::Char('t').into(),
KeyCode::Char('r').into(),
KeyCode::Tab.into(),
KeyCode::Tab.into(),
KeyCode::Tab.into(),
KeyCode::Char('n').into(),
KeyCode::Char('g').into(),
KeyCode::Enter.into(),
]);
let ans = discourse::prompt_one_with(prompt, &mut backend, &mut events).unwrap();
assert_eq!(ans, Answer::String("string".into()));
assert!(events.next().is_none());
}