diff --git a/examples/action.rs b/examples/action.rs index 1dbd152..275a0df 100644 --- a/examples/action.rs +++ b/examples/action.rs @@ -7,19 +7,19 @@ pub(crate) enum Action { Jump, /// Climb or move up - #[key("up", symbol = "↑", help = "move up")] + #[key("up", symbol = "↑", help = "up")] Up, /// Drop or crouch down - #[key("down", symbol = "↓", help = "move down")] + #[key("down", symbol = "↓", help = "down")] Down, /// Move leftward - #[key("left", symbol = "←", help = "move left")] + #[key("left", symbol = "←", help = "left")] Left, /// Move rightward - #[key("right", symbol = "→", help = "move right")] + #[key("right", symbol = "→", help = "right")] Right, /// Exit or pause game diff --git a/examples/reload.gif b/examples/reload.gif index 4b0afc9..bbeda8b 100644 Binary files a/examples/reload.gif and b/examples/reload.gif differ diff --git a/keymap_derive/tests/derive.rs b/keymap_derive/tests/derive.rs index 379b6a3..e20e6e2 100644 --- a/keymap_derive/tests/derive.rs +++ b/keymap_derive/tests/derive.rs @@ -66,6 +66,14 @@ mod tests { struct Wrapper(keymap_parser::Node); + fn wrap(s: &str) -> Vec { + keymap_parser::parse_seq(s) + .unwrap() + .into_iter() + .map(Wrapper) + .collect() + } + impl ToKeyMap for Wrapper { fn to_keymap(&self) -> Result { Ok(self.0.clone()) @@ -156,45 +164,28 @@ mod tests { fn test_bound_payload_extraction() { let config = Action::keymap_config(); - // When we press '1', it matches @digit, and we should extract '1' - let keys = keymap_parser::parse_seq("1") - .unwrap() - .into_iter() - .map(Wrapper) - .collect::>(); - let bound_action = config.get_bound_seq(&keys).unwrap(); - assert_eq!(bound_action, Action::Digit('1')); - - // When we press 'A', it matches @any, and we should extract 'A' - let keys = keymap_parser::parse_seq("A") - .unwrap() - .into_iter() - .map(Wrapper) - .collect::>(); - let bound_action = config.get_bound_seq(&keys).unwrap(); - - assert_eq!(bound_action, Action::Jump('A')); + [ + ("1", Action::Digit('1')), + ("A", Action::Jump('A')), + ("enter", Action::Create), + ] + .into_iter() + .for_each(|(input, expected)| { + assert_eq!(expected, config.get_bound_seq(&wrap(input)).unwrap()); + }); - // When we press 'Q', it matches @any, and we should extract 'Q' - let keys = keymap_parser::parse_seq("Q") - .unwrap() - .into_iter() - .map(Wrapper) - .collect::>(); - let nodes = keys.iter().map(|k| k.0.clone()).collect::>(); + // get_bound_item_by_keymaps also returns the item + let nodes = wrap("Q").into_iter().map(|w| w.0).collect::>(); let (bound_action, item) = config.get_bound_item_by_keymaps(&nodes).unwrap(); - assert_eq!(bound_action, Action::Jump('Q')); assert_eq!(item.description, "Jump with char argument"); - // Standard keys should extract as well using get_bound_seq - let keys = keymap_parser::parse_seq("enter") - .unwrap() - .into_iter() - .map(Wrapper) - .collect::>(); - let bound_action = config.get_bound_seq(&keys).unwrap(); - assert_eq!(bound_action, Action::Create); + // Key::Space extracted as ' ' via @any + let space = vec![Wrapper(keymap_parser::Node::new( + 0, + keymap_parser::node::Key::Space, + ))]; + assert_eq!(Action::Jump(' '), config.get_bound_seq(&space).unwrap()); } #[test] diff --git a/src/keymap.rs b/src/keymap.rs index f514f17..6e1faf5 100644 --- a/src/keymap.rs +++ b/src/keymap.rs @@ -106,6 +106,7 @@ impl KeyGroupValue for char { fn from_keymap_node(node: &KeyMap) -> Self { match node.key { Key::Char(c) => c, + Key::Space => ' ', _ => '\0', } }