use pretty_assertions::assert_eq;
use crate::key_repr::{Key, KeyModifiers, KeyValue, MediaKeyCode};
#[cfg(not(feature = "serde"))]
use serde_json as _;
#[cfg(feature = "mouse-keys")]
use crate::key_repr::MouseKeyValue;
#[cfg(feature = "modifier-keys")]
use crate::key_repr::ModifierKeyCode;
#[test]
fn test_simple() {
let keys: Key = "<C-a>".parse().unwrap();
assert_eq!(
keys,
Key {
modifiers: KeyModifiers {
alt: false,
ctrl: true,
meta: false,
shift: false
},
value: KeyValue::Char('a'),
}
);
}
#[test]
fn test_dash_after_abc() {
let keys: Vec<Key> = Key::parse_multiple("<DASH><C-<DASH>>").unwrap();
assert_eq!(
keys,
vec![
Key {
modifiers: KeyModifiers {
alt: false,
ctrl: false,
meta: false,
shift: false
},
value: KeyValue::Char('-'),
},
Key {
modifiers: KeyModifiers {
alt: false,
ctrl: true,
meta: false,
shift: false
},
value: KeyValue::Char('-'),
},
]
);
}
#[test]
fn test_media_keys() {
let keys: Vec<Key> = Key::parse_multiple("<MEDIA_PAUSE><C-<PAUSE>><A-<MEDIA_STOP>>").unwrap();
assert_eq!(
keys,
vec![
Key {
modifiers: KeyModifiers {
alt: false,
ctrl: false,
meta: false,
shift: false
},
value: KeyValue::Media(MediaKeyCode::Pause),
},
Key {
modifiers: KeyModifiers {
alt: false,
ctrl: true,
meta: false,
shift: false
},
value: KeyValue::Pause,
},
Key {
modifiers: KeyModifiers {
alt: true,
ctrl: false,
meta: false,
shift: false
},
value: KeyValue::Media(MediaKeyCode::Stop),
},
]
);
}
#[test]
fn test_alias_meta() {
let keys: Key = "<Super+Ctrl-A>"
.parse()
.map_err(|e| panic!("{}", e))
.unwrap();
assert_eq!(
keys,
Key {
modifiers: KeyModifiers {
alt: false,
ctrl: true,
meta: true,
shift: false
},
value: KeyValue::Char('A'),
}
);
}
#[test]
fn test_super_only_alias() {
let output = "<Super+Meta-A>".parse::<Key>().is_err();
assert!(output);
}
#[test]
#[cfg(feature = "mouse-keys")]
fn test_mouse_keys() {
let keys: Key = "<C-<MOUSE_LEFT>>"
.parse()
.map_err(|e| panic!("{}", e))
.unwrap();
assert_eq!(
keys,
Key {
modifiers: KeyModifiers {
alt: false,
ctrl: true,
meta: false,
shift: false
},
value: KeyValue::MouseKey(MouseKeyValue::Left),
}
);
}
#[test]
#[cfg(feature = "modifier-keys")]
fn test_modifier_keys() {
let keys: Key = "<C-<LEFT_META>>"
.parse()
.map_err(|e| panic!("{}", e))
.unwrap();
assert_eq!(
keys,
Key {
modifiers: KeyModifiers {
alt: false,
ctrl: true,
meta: false,
shift: false
},
value: KeyValue::ModifierKey(ModifierKeyCode::LeftMeta),
}
);
}
#[test]
#[cfg(feature = "modifier-keys")]
fn test_modifier_key_super_alias() {
let keys: Key = "<C-<LEFT_SUPER>>"
.parse()
.map_err(|e| panic!("{}", e))
.unwrap();
assert_eq!(
keys,
Key {
modifiers: KeyModifiers {
alt: false,
ctrl: true,
meta: false,
shift: false
},
value: KeyValue::ModifierKey(ModifierKeyCode::LeftMeta),
}
);
}
#[test]
#[cfg(feature = "modifier-keys")]
fn test_modifier_keys_layer() {
use crate::key_repr::Keys;
let keys: Keys = "<Meta-<LEFT_META>><RIGHT_ALT><Alt+Meta+Ctrl-<LEFT_SHIFT>><RIGHT_CTRL>"
.parse()
.map_err(|e| panic!("{}", e))
.unwrap();
assert_eq!(
keys,
vec![
Key {
modifiers: KeyModifiers {
alt: false,
ctrl: false,
meta: true,
shift: false
},
value: KeyValue::ModifierKey(ModifierKeyCode::LeftMeta),
},
Key {
modifiers: KeyModifiers {
alt: false,
ctrl: false,
meta: false,
shift: false
},
value: KeyValue::ModifierKey(ModifierKeyCode::RightAlt),
},
Key {
modifiers: KeyModifiers {
alt: true,
ctrl: true,
meta: true,
shift: false
},
value: KeyValue::ModifierKey(ModifierKeyCode::LeftShift),
},
Key {
modifiers: KeyModifiers {
alt: false,
ctrl: false,
meta: false,
shift: false
},
value: KeyValue::ModifierKey(ModifierKeyCode::RightCtrl),
}
]
.into()
);
}
#[test]
fn test_shift_a() {
let keys: Key = "<S-a>".parse().unwrap();
assert_eq!(
keys,
Key {
modifiers: KeyModifiers {
alt: false,
ctrl: false,
meta: false,
shift: true
},
value: KeyValue::Char('a'),
}
);
}
#[test]
fn test_string_repr() {
let key = Key {
modifiers: KeyModifiers {
alt: true,
ctrl: false,
meta: true,
shift: true,
},
value: KeyValue::Up,
};
assert_eq!("<AMS-<UP>>".to_owned(), key.to_string_repr());
}
#[test]
fn test_string_repr_special() {
let key = Key {
modifiers: KeyModifiers {
alt: true,
ctrl: false,
meta: true,
shift: true,
},
value: KeyValue::Char('<'),
};
assert_eq!(
"<AMS-<ANGULAR_BRACKET_OPEN>>".to_owned(),
key.to_string_repr()
);
}
#[test]
fn test_extra_special_keys() {
let keys: Vec<Key> = Key::parse_multiple("<C--><C-<DASH>><A-<ABO>><ABC>").unwrap();
assert_eq!(
keys,
vec![
Key {
modifiers: KeyModifiers {
alt: false,
ctrl: true,
meta: false,
shift: false
},
value: KeyValue::Char('-'),
},
Key {
modifiers: KeyModifiers {
alt: false,
ctrl: true,
meta: false,
shift: false
},
value: KeyValue::Char('-'),
},
Key {
modifiers: KeyModifiers {
alt: true,
ctrl: false,
meta: false,
shift: false
},
value: KeyValue::Char('<'),
},
Key {
modifiers: KeyModifiers {
alt: false,
ctrl: false,
meta: false,
shift: false
},
value: KeyValue::Char('>'),
},
]
);
}
#[test]
fn test_false_pattern() {
let keys: Result<Key, _> = "<c->".parse();
assert!(keys.is_err());
}
#[test]
fn test_single_long_modifier() {
let keys: Vec<Key> = Key::parse_multiple("<Alt-Q><Ctrl-Q><Meta-Q><Shift-Q>")
.map_err(|e| panic!("{}", e))
.unwrap();
assert_eq!(
keys,
vec![
Key {
modifiers: KeyModifiers {
alt: true,
ctrl: false,
meta: false,
shift: false
},
value: KeyValue::Char('Q')
},
Key {
modifiers: KeyModifiers {
alt: false,
ctrl: true,
meta: false,
shift: false
},
value: KeyValue::Char('Q')
},
Key {
modifiers: KeyModifiers {
alt: false,
ctrl: false,
meta: true,
shift: false
},
value: KeyValue::Char('Q')
},
Key {
modifiers: KeyModifiers {
alt: false,
ctrl: false,
meta: false,
shift: true
},
value: KeyValue::Char('Q')
},
]
);
}
#[test]
fn test_long_modifiers_other() {
let key: Key = "<Alt+Ctrl+Meta+Shift-Q>".parse().unwrap();
assert_eq!(
key,
Key {
modifiers: KeyModifiers {
alt: true,
ctrl: true,
meta: true,
shift: true
},
value: KeyValue::Char('Q')
}
);
}
#[test]
fn test_long_modifiers() {
let keys: Vec<Key> = Key::parse_multiple("<Ctrl+Shift+Alt+Meta-C><CSAM-A>").unwrap();
assert_eq!(
keys,
vec![
Key {
modifiers: KeyModifiers {
alt: true,
ctrl: true,
meta: true,
shift: true
},
value: KeyValue::Char('C'),
},
Key {
modifiers: KeyModifiers {
alt: true,
ctrl: true,
meta: true,
shift: true
},
value: KeyValue::Char('A'),
}
]
);
}
#[test]
fn test_f_keys() {
let key: Key = "<C-<F255>>".parse().unwrap_or_else(|e| panic!("{}", e));
let expected = Key {
modifiers: KeyModifiers {
alt: false,
ctrl: true,
meta: false,
shift: false,
},
value: KeyValue::F(255),
};
assert_eq!(key, expected);
}
#[test]
fn test_complex() {
let keys: Vec<Key> =
Key::parse_multiple("<C-a><ACMS-<ESC>>a 🙂").unwrap_or_else(|e| panic!("{}", e));
assert_eq!(
keys,
vec![
Key {
modifiers: KeyModifiers {
alt: false,
ctrl: true,
meta: false,
shift: false
},
value: KeyValue::Char('a'),
},
Key {
modifiers: KeyModifiers {
alt: true,
ctrl: true,
meta: true,
shift: true
},
value: KeyValue::Esc,
},
Key {
modifiers: KeyModifiers {
alt: false,
ctrl: false,
meta: false,
shift: false
},
value: KeyValue::Char('a'),
},
Key {
modifiers: KeyModifiers {
alt: false,
ctrl: false,
meta: false,
shift: false
},
value: KeyValue::Char(' '),
},
Key {
modifiers: KeyModifiers {
alt: false,
ctrl: false,
meta: false,
shift: false
},
value: KeyValue::Char('🙂'),
},
]
);
}