test subcommand handling

This commit is contained in:
🪞👃🪞 2025-05-09 22:43:21 +03:00
parent fe8ecf8a98
commit 4a385b40ff
3 changed files with 64 additions and 0 deletions

3
Cargo.lock generated
View file

@ -936,9 +936,12 @@ dependencies = [
name = "tengri"
version = "0.13.0"
dependencies = [
"crossterm",
"tengri",
"tengri_dsl",
"tengri_input",
"tengri_output",
"tengri_proc",
"tengri_tui",
]

View file

@ -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" ]

View file

@ -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(())
}