Lines
96.55 %
Functions
100 %
Branches
use super::*;
#[derive(Debug, Clone, PartialEq, Eq)]
enum Toy {
Quit,
Save,
}
impl Action for Toy {
fn parse(name: &str, arg: Option<&str>) -> Result<Toy, String> {
match (name, arg) {
("quit", None) => Ok(Toy::Quit),
("save", None) => Ok(Toy::Save),
_ => Err(format!("unknown action `{name}`")),
fn keymap(text: &str) -> Keymap<Toy> {
match Keymap::parse(text) {
Ok(keymap) => keymap,
Err(error) => panic!("should parse: {error}"),
fn error_of(text: &str) -> ParseError {
match Keymap::<Toy>::parse(text) {
Ok(_) => panic!("should not parse"),
Err(error) => error,
fn hinted(keymap: &Keymap<Toy>, scope: &str) -> Vec<Toy> {
keymap.hinted(scope).to_vec()
#[test]
fn a_hint_list_names_actions_in_its_order() {
let map = keymap("g: q = quit\ng: F2 = save\ng: hint = save quit");
assert_eq!(hinted(&map, "g"), [Toy::Save, Toy::Quit]);
assert!(hinted(&map, "other").is_empty());
fn bindings_come_in_file_order() {
let map = keymap("g: q = quit; g: C-x C-s = save; g: hint = quit");
let all: Vec<_> = map
.bindings("g")
.map(|(chords, action)| (show(chords), action.clone()))
.collect();
assert_eq!(
all,
[
("q".to_string(), Toy::Quit),
("C-x C-s".to_string(), Toy::Save)
]
);
fn a_hint_list_survives_rebinding_its_key() {
let map = keymap("g: q = quit; g: hint = quit");
let changed = map.overridden("g: q = none; g: F2 = quit").expect("valid");
assert_eq!(hinted(&changed, "g"), [Toy::Quit]);
fn an_override_replaces_the_whole_list_and_none_empties_it() {
let map = keymap("g: hint = quit save");
let changed = map.overridden("g: hint = save").expect("valid");
assert_eq!(hinted(&changed, "g"), [Toy::Save]);
let emptied = map.overridden("g: hint = none").expect("valid");
assert!(hinted(&emptied, "g").is_empty());
fn a_bad_hint_list_names_its_line() {
for text in ["g: hint =", "g: hint = bogus", "g: hint = quit 1"] {
let error = error_of(&format!("# one\n{text}"));
assert_eq!(error.line, Some(2), "{text}");
fn a_hint_list_written_twice_is_a_duplicate() {
let error = error_of("g: hint = quit\ng: hint = save");
assert_eq!(error.line, Some(2));
assert!(error.message.contains("duplicate hint list"), "{error}");