From 4a385b40ff778b4f23b4a989da94d45a88137ba0 Mon Sep 17 00:00:00 2001 From: unspeaker Date: Fri, 9 May 2025 22:43:21 +0300 Subject: [PATCH] test subcommand handling --- Cargo.lock | 3 +++ tengri/Cargo.toml | 5 +++++ tengri/src/lib.rs | 56 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index d9ccc29..0ba0ef4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -936,9 +936,12 @@ dependencies = [ name = "tengri" version = "0.13.0" dependencies = [ + "crossterm", + "tengri", "tengri_dsl", "tengri_input", "tengri_output", + "tengri_proc", "tengri_tui", ] diff --git a/tengri/Cargo.toml b/tengri/Cargo.toml index 1c7d4eb..23e87f8 100644 --- a/tengri/Cargo.toml +++ b/tengri/Cargo.toml @@ -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" ] diff --git a/tengri/src/lib.rs b/tengri/src/lib.rs index c1c72a0..7703a14 100644 --- a/tengri/src/lib.rs +++ b/tengri/src/lib.rs @@ -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 { + Ok(None) + } + fn do_sub (state: &mut Test, command: TestSubcommand) -> Perhaps { + Ok(command.execute(state)?.map(|command|Self::DoSub { command })) + } + } + #[tengri_proc::command(Test)] impl TestSubcommand { + fn do_other_thing (state: &mut Test) -> Perhaps { + 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(()) +}