-
Notifications
You must be signed in to change notification settings - Fork 164
Implementing basic integration testing using Enigo #97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
5df1cf8
bc6bc67
e23d48e
03c9655
3fd5466
0a68ff1
b0a8e13
b3770c9
60b8989
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,3 +20,4 @@ console = "0.13.0" | |
| lazy_static = "1" | ||
| tempfile = "3" | ||
| zeroize = "1.1.1" | ||
| enigo = "0.0.14" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -37,6 +37,7 @@ pub struct Select<'a> { | |
| default: usize, | ||
| items: Vec<String>, | ||
| prompt: Option<String>, | ||
| before: Box<dyn FnMut() + 'a>, | ||
| clear: bool, | ||
| theme: &'a dyn Theme, | ||
| paged: bool, | ||
|
|
@@ -77,6 +78,7 @@ impl<'a> Select<'a> { | |
| default: !0, | ||
| items: vec![], | ||
| prompt: None, | ||
| before: Box::new(|| ()), | ||
| clear: true, | ||
| theme, | ||
| paged: false, | ||
|
|
@@ -180,7 +182,7 @@ impl<'a> Select<'a> { | |
| /// Similar to [interact_on](#method.interact_on) except for the fact that it does not allow selection of the terminal. | ||
| /// The dialog is rendered on stderr. | ||
| /// Result contains index of a selected item. | ||
| pub fn interact(&self) -> io::Result<usize> { | ||
| pub fn interact(&mut self) -> io::Result<usize> { | ||
| self.interact_on(&Term::stderr()) | ||
| } | ||
|
|
||
|
|
@@ -189,10 +191,15 @@ impl<'a> Select<'a> { | |
| /// This method is similar to [interact_on_opt](#method.interact_on_opt) except for the fact that it does not allow selection of the terminal. | ||
| /// The dialog is rendered on stderr. | ||
| /// Result contains `Some(index)` if user selected one of items or `None` if user cancelled with 'Esc' or 'q'. | ||
| pub fn interact_opt(&self) -> io::Result<Option<usize>> { | ||
| pub fn interact_opt(&mut self) -> io::Result<Option<usize>> { | ||
| self.interact_on_opt(&Term::stderr()) | ||
| } | ||
|
|
||
| pub fn set_before(&mut self, f: impl FnMut() + 'a) -> &mut Select<'a> { | ||
|
||
| self.before = Box::new(f); | ||
| self | ||
| } | ||
|
|
||
| /// Like [interact](#method.interact) but allows a specific terminal to be set. | ||
| /// | ||
| /// ## Examples | ||
|
|
@@ -211,7 +218,7 @@ impl<'a> Select<'a> { | |
| /// Ok(()) | ||
| /// } | ||
| ///``` | ||
| pub fn interact_on(&self, term: &Term) -> io::Result<usize> { | ||
| pub fn interact_on(&mut self, term: &Term) -> io::Result<usize> { | ||
| self._interact_on(term, false)? | ||
| .ok_or_else(|| io::Error::new(io::ErrorKind::Other, "Quit not allowed in this case")) | ||
| } | ||
|
|
@@ -238,12 +245,12 @@ impl<'a> Select<'a> { | |
| /// } | ||
| /// ``` | ||
| #[inline] | ||
| pub fn interact_on_opt(&self, term: &Term) -> io::Result<Option<usize>> { | ||
| pub fn interact_on_opt(&mut self, term: &Term) -> io::Result<Option<usize>> { | ||
| self._interact_on(term, true) | ||
| } | ||
|
|
||
| /// Like `interact` but allows a specific terminal to be set. | ||
| fn _interact_on(&self, term: &Term, allow_quit: bool) -> io::Result<Option<usize>> { | ||
| fn _interact_on(&mut self, term: &Term, allow_quit: bool) -> io::Result<Option<usize>> { | ||
| let mut page = 0; | ||
|
|
||
| if self.items.is_empty() { | ||
|
|
@@ -280,6 +287,8 @@ impl<'a> Select<'a> { | |
| size_vec.push(*size); | ||
| } | ||
|
|
||
| let mut is_first_update = true; | ||
|
|
||
| loop { | ||
| for (idx, item) in self | ||
| .items | ||
|
|
@@ -294,6 +303,11 @@ impl<'a> Select<'a> { | |
| term.hide_cursor()?; | ||
| term.flush()?; | ||
|
|
||
| if is_first_update { | ||
| (self.before)(); | ||
| is_first_update = false; | ||
| } | ||
|
|
||
| match term.read_key()? { | ||
| Key::ArrowDown | Key::Char('j') => { | ||
| if sel == !0 { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| use dialoguer::{theme::ColorfulTheme, Select}; | ||
|
|
||
| use enigo::{Enigo, Key, KeyboardControllable}; | ||
| use std::thread; | ||
| use std::time::Duration; | ||
|
|
||
| #[test] | ||
| fn basic_navigation_produces_correct_selection() { | ||
| let selections = &[ | ||
| "Ice Cream", | ||
| "Vanilla Cupcake", | ||
| "Chocolate Muffin", | ||
| "A Pile of sweet, sweet mustard", | ||
| ]; | ||
|
|
||
| let selection = Select::with_theme(&ColorfulTheme::default()) | ||
| .with_prompt("Optionally pick your flavor") | ||
| .set_before(|| { | ||
| let mut enigo = Enigo::new(); | ||
| enigo.key_click(Key::Layout('j')); | ||
| enigo.key_down(Key::Return); | ||
| thread::sleep(Duration::from_millis(10)); | ||
| enigo.key_up(Key::Return); | ||
| }) | ||
| .default(0) | ||
| .items(&selections[..]) | ||
| .interact_opt() | ||
| .unwrap(); | ||
|
|
||
| assert_eq!(Some(1), selection); | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.