1
use super::*;
2

            
3
#[derive(Debug, Clone, PartialEq, Eq)]
4
enum Toy {
5
    Quit,
6
    Save,
7
}
8

            
9
impl Action for Toy {
10
18
    fn parse(name: &str, arg: Option<&str>) -> Result<Toy, String> {
11
18
        match (name, arg) {
12
10
            ("quit", None) => Ok(Toy::Quit),
13
6
            ("save", None) => Ok(Toy::Save),
14
2
            _ => Err(format!("unknown action `{name}`")),
15
        }
16
18
    }
17
}
18

            
19
4
fn keymap(text: &str) -> Keymap<Toy> {
20
4
    match Keymap::parse(text) {
21
4
        Ok(keymap) => keymap,
22
        Err(error) => panic!("should parse: {error}"),
23
    }
24
4
}
25

            
26
4
fn error_of(text: &str) -> ParseError {
27
4
    match Keymap::<Toy>::parse(text) {
28
        Ok(_) => panic!("should not parse"),
29
4
        Err(error) => error,
30
    }
31
4
}
32

            
33
5
fn hinted(keymap: &Keymap<Toy>, scope: &str) -> Vec<Toy> {
34
5
    keymap.hinted(scope).to_vec()
35
5
}
36

            
37
#[test]
38
1
fn a_hint_list_names_actions_in_its_order() {
39
1
    let map = keymap("g: q = quit\ng: F2 = save\ng: hint = save quit");
40
1
    assert_eq!(hinted(&map, "g"), [Toy::Save, Toy::Quit]);
41
1
    assert!(hinted(&map, "other").is_empty());
42
1
}
43

            
44
#[test]
45
1
fn bindings_come_in_file_order() {
46
1
    let map = keymap("g: q = quit; g: C-x C-s = save; g: hint = quit");
47
1
    let all: Vec<_> = map
48
1
        .bindings("g")
49
2
        .map(|(chords, action)| (show(chords), action.clone()))
50
        .collect();
51
1
    assert_eq!(
52
1
        all,
53
1
        [
54
1
            ("q".to_string(), Toy::Quit),
55
1
            ("C-x C-s".to_string(), Toy::Save)
56
1
        ]
57
    );
58
1
}
59

            
60
#[test]
61
1
fn a_hint_list_survives_rebinding_its_key() {
62
1
    let map = keymap("g: q = quit; g: hint = quit");
63
1
    let changed = map.overridden("g: q = none; g: F2 = quit").expect("valid");
64
1
    assert_eq!(hinted(&changed, "g"), [Toy::Quit]);
65
1
}
66

            
67
#[test]
68
1
fn an_override_replaces_the_whole_list_and_none_empties_it() {
69
1
    let map = keymap("g: hint = quit save");
70
1
    let changed = map.overridden("g: hint = save").expect("valid");
71
1
    assert_eq!(hinted(&changed, "g"), [Toy::Save]);
72
1
    let emptied = map.overridden("g: hint = none").expect("valid");
73
1
    assert!(hinted(&emptied, "g").is_empty());
74
1
}
75

            
76
#[test]
77
1
fn a_bad_hint_list_names_its_line() {
78
1
    for text in ["g: hint =", "g: hint = bogus", "g: hint = quit 1"] {
79
3
        let error = error_of(&format!("# one\n{text}"));
80
3
        assert_eq!(error.line, Some(2), "{text}");
81
    }
82
1
}
83

            
84
#[test]
85
1
fn a_hint_list_written_twice_is_a_duplicate() {
86
1
    let error = error_of("g: hint = quit\ng: hint = save");
87
1
    assert_eq!(error.line, Some(2));
88
1
    assert!(error.message.contains("duplicate hint list"), "{error}");
89
1
}