mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 11:46:42 +01:00
test subcommand handling
This commit is contained in:
parent
fe8ecf8a98
commit
4a385b40ff
3 changed files with 64 additions and 0 deletions
3
Cargo.lock
generated
3
Cargo.lock
generated
|
|
@ -936,9 +936,12 @@ dependencies = [
|
|||
name = "tengri"
|
||||
version = "0.13.0"
|
||||
dependencies = [
|
||||
"crossterm",
|
||||
"tengri",
|
||||
"tengri_dsl",
|
||||
"tengri_input",
|
||||
"tengri_output",
|
||||
"tengri_proc",
|
||||
"tengri_tui",
|
||||
]
|
||||
|
||||
|
|
|
|||
|
|
@ -10,6 +10,11 @@ tengri_input = { optional = true, path = "../input" }
|
|||
tengri_output = { optional = true, path = "../output" }
|
||||
tengri_tui = { optional = true, path = "../tui" }
|
||||
|
||||
[dev-dependencies]
|
||||
tengri_proc = { path = "../proc" }
|
||||
tengri = { path = ".", features = [ "dsl" ] }
|
||||
crossterm = "0.28.1"
|
||||
|
||||
[features]
|
||||
default = [ "input", "output", "tui" ]
|
||||
input = [ "tengri_input" ]
|
||||
|
|
|
|||
|
|
@ -2,3 +2,59 @@
|
|||
#[cfg(feature="input")] pub use ::tengri_input as input;
|
||||
#[cfg(feature="dsl")] pub use ::tengri_dsl as dsl;
|
||||
#[cfg(feature="tui")] pub use ::tengri_tui as tui;
|
||||
|
||||
#[cfg(test)] extern crate tengri_proc;
|
||||
#[cfg(test)] #[test] fn test_subcommand () -> crate::output::Usually<()> {
|
||||
use crate::output::Perhaps;
|
||||
use crate::input::{Command, InputMap, KeyMap, Handle, handle};
|
||||
use crate::dsl::{TryFromDsl, TokenIter};
|
||||
use crate::tui::TuiIn;
|
||||
use crossterm::event::{Event, KeyEvent, KeyCode, KeyModifiers, KeyEventKind, KeyEventState};
|
||||
//use crate::input::*;
|
||||
//use crate::dsl::*;
|
||||
struct Test {
|
||||
keys: InputMap<'static, Test, TestCommand, TuiIn, TokenIter<'static>>
|
||||
}
|
||||
handle!(TuiIn: |self: Test, input|if let Some(command) = self.keys.command(self, input) {
|
||||
Ok(Some(true))
|
||||
} else {
|
||||
Ok(None)
|
||||
});
|
||||
#[tengri_proc::command(Test)] impl TestCommand {
|
||||
fn do_thing (state: &mut Test) -> Perhaps<Self> {
|
||||
Ok(None)
|
||||
}
|
||||
fn do_sub (state: &mut Test, command: TestSubcommand) -> Perhaps<Self> {
|
||||
Ok(command.execute(state)?.map(|command|Self::DoSub { command }))
|
||||
}
|
||||
}
|
||||
#[tengri_proc::command(Test)] impl TestSubcommand {
|
||||
fn do_other_thing (state: &mut Test) -> Perhaps<Self> {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
let mut test = Test {
|
||||
keys: InputMap::new(
|
||||
"(@a do-thing) (@b do-sub do-other-thing)".into()
|
||||
)
|
||||
};
|
||||
assert_eq!(Some(true), test.handle(&TuiIn(Default::default(), Event::Key(KeyEvent {
|
||||
kind: KeyEventKind::Press,
|
||||
code: KeyCode::Char('a'),
|
||||
modifiers: KeyModifiers::NONE,
|
||||
state: KeyEventState::NONE,
|
||||
})))?);
|
||||
assert_eq!(Some(true), test.handle(&TuiIn(Default::default(), Event::Key(KeyEvent {
|
||||
kind: KeyEventKind::Press,
|
||||
code: KeyCode::Char('b'),
|
||||
modifiers: KeyModifiers::NONE,
|
||||
state: KeyEventState::NONE,
|
||||
})))?);
|
||||
assert_eq!(None, test.handle(&TuiIn(Default::default(), Event::Key(KeyEvent {
|
||||
kind: KeyEventKind::Press,
|
||||
code: KeyCode::Char('c'),
|
||||
modifiers: KeyModifiers::NONE,
|
||||
state: KeyEventState::NONE,
|
||||
})))?);
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue