Base: codex-main originale (prima del fork aKode)
This commit is contained in:
93
codex-rs/config/src/tui_keymap_chord_tests.rs
Normal file
93
codex-rs/config/src/tui_keymap_chord_tests.rs
Normal file
@@ -0,0 +1,93 @@
|
||||
use super::KeybindingSpec;
|
||||
use super::KeybindingsSpec;
|
||||
use super::TuiKeymap;
|
||||
use super::normalize_keybinding_spec;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
#[test]
|
||||
fn normalizes_each_stroke_in_a_key_chord() {
|
||||
assert_eq!(
|
||||
normalize_keybinding_spec(" CONTROL-X Option-Return "),
|
||||
Ok("ctrl-x alt-enter".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deserializes_a_two_stroke_global_key_chord() {
|
||||
let actual = toml::from_str::<TuiKeymap>(
|
||||
r#"
|
||||
[global]
|
||||
open_transcript = "ctrl-x ctrl-t"
|
||||
"#,
|
||||
)
|
||||
.expect("two-stroke key chords should deserialize");
|
||||
|
||||
let expected = TuiKeymap {
|
||||
global: super::TuiGlobalKeymap {
|
||||
open_transcript: Some(KeybindingsSpec::One(KeybindingSpec(
|
||||
"ctrl-x ctrl-t".to_string(),
|
||||
))),
|
||||
..Default::default()
|
||||
},
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
assert_eq!(actual, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn keeps_single_keys_and_chords_as_alternative_bindings() {
|
||||
let actual = toml::from_str::<TuiKeymap>(
|
||||
r#"
|
||||
[global]
|
||||
open_transcript = ["ctrl-t", "ctrl-x ctrl-t"]
|
||||
"#,
|
||||
)
|
||||
.expect("single keys and key chords should coexist");
|
||||
|
||||
let expected = TuiKeymap {
|
||||
global: super::TuiGlobalKeymap {
|
||||
open_transcript: Some(KeybindingsSpec::Many(vec![
|
||||
KeybindingSpec("ctrl-t".to_string()),
|
||||
KeybindingSpec("ctrl-x ctrl-t".to_string()),
|
||||
])),
|
||||
..Default::default()
|
||||
},
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
assert_eq!(actual, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_chords_longer_than_two_strokes() {
|
||||
let error = normalize_keybinding_spec("ctrl-x ctrl-s ctrl-t")
|
||||
.expect_err("key chords should have a bounded length");
|
||||
|
||||
assert!(error.contains("at most 2 strokes"), "{error}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_an_invalid_second_chord_stroke() {
|
||||
let error = normalize_keybinding_spec("ctrl-x ctrl-unknown")
|
||||
.expect_err("every key chord stroke should be valid");
|
||||
|
||||
assert!(error.contains("unknown key"), "{error}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn preserves_keybinding_validation_errors_when_deserializing_toml() {
|
||||
for invalid in ["backslash", "ctrl-x ctrl-unknown", "ctrl-x ctrl-s ctrl-t"] {
|
||||
for bindings in [format!("'{invalid}'"), format!("['ctrl-j', '{invalid}']")] {
|
||||
let error =
|
||||
toml::from_str::<TuiKeymap>(&format!("[editor]\ninsert_newline = {bindings}\n"))
|
||||
.expect_err("invalid bindings should retain the validation error");
|
||||
|
||||
assert_eq!(
|
||||
error.message(),
|
||||
normalize_keybinding_spec(invalid).unwrap_err()
|
||||
);
|
||||
assert!(error.to_string().contains("insert_newline"), "{error}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user