1
use ratatui::crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
2

            
3
use super::Chord;
4

            
5
59
fn chord(text: &str) -> Chord {
6
59
    match text.parse() {
7
59
        Ok(chord) => chord,
8
        Err(message) => panic!("`{text}` should parse: {message}"),
9
    }
10
59
}
11

            
12
#[test]
13
1
fn every_written_form_round_trips() {
14
1
    let forms = [
15
1
        "Enter",
16
1
        "Esc",
17
1
        "Tab",
18
1
        "Backspace",
19
1
        "Delete",
20
1
        "Insert",
21
1
        "Up",
22
1
        "Down",
23
1
        "Left",
24
1
        "Right",
25
1
        "Home",
26
1
        "End",
27
1
        "PageUp",
28
1
        "PageDown",
29
1
        "Space",
30
1
        "Semicolon",
31
1
        "Equals",
32
1
        "F1",
33
1
        "F12",
34
1
        "q",
35
1
        "N",
36
1
        "?",
37
1
        ":",
38
1
        "+",
39
1
        "-",
40
1
        "#",
41
1
        "C-x",
42
1
        "M-x",
43
1
        "S-Tab",
44
1
        "C-M-x",
45
1
        "C-S-Up",
46
1
        "C-Space",
47
1
        "C--",
48
1
    ];
49
1
    for form in forms {
50
33
        assert_eq!(chord(form).to_string(), form, "{form}");
51
    }
52
1
}
53

            
54
#[test]
55
1
fn modifier_order_is_canonical_on_display() {
56
1
    assert_eq!(chord("M-C-x").to_string(), "C-M-x");
57
1
}
58

            
59
#[test]
60
1
fn the_parts_are_read() {
61
1
    let parsed = chord("C-M-x");
62
1
    assert_eq!(parsed.code, KeyCode::Char('x'));
63
1
    assert_eq!(parsed.modifiers, KeyModifiers::CONTROL | KeyModifiers::ALT);
64
1
    assert_eq!(chord("F5").code, KeyCode::F(5));
65
1
    assert_eq!(chord("Space").code, KeyCode::Char(' '));
66
1
    assert_eq!(chord("N").code, KeyCode::Char('N'));
67
1
}
68

            
69
#[test]
70
1
fn bad_chords_are_refused() {
71
1
    for bad in [
72
1
        "", "Fnord", "F0", "F13", "C-", "X-x", "ab", "S-a", "C-S-q", "C-A",
73
1
    ] {
74
10
        assert!(bad.parse::<Chord>().is_err(), "`{bad}`");
75
    }
76
1
}
77

            
78
#[test]
79
1
fn shift_on_a_character_is_explained() {
80
1
    let message = "S-n".parse::<Chord>().err().unwrap_or_default();
81
1
    assert!(message.contains("shifted character"), "{message}");
82
1
}
83

            
84
9
fn event(code: KeyCode, modifiers: KeyModifiers) -> Chord {
85
9
    Chord::from_event(KeyEvent::new(code, modifiers))
86
9
}
87

            
88
#[test]
89
1
fn shift_on_a_character_is_the_case() {
90
1
    assert_eq!(event(KeyCode::Char('A'), KeyModifiers::SHIFT), chord("A"));
91
1
    assert_eq!(event(KeyCode::Char('A'), KeyModifiers::NONE), chord("A"));
92
1
}
93

            
94
#[test]
95
1
fn a_ctrl_letter_matches_in_either_case() {
96
1
    let both = KeyModifiers::CONTROL | KeyModifiers::SHIFT;
97
1
    assert_eq!(event(KeyCode::Char('n'), both), chord("C-n"));
98
1
    assert_eq!(event(KeyCode::Char('N'), both), chord("C-n"));
99
1
    assert_eq!(
100
1
        event(KeyCode::Char('N'), KeyModifiers::CONTROL),
101
1
        chord("C-n")
102
    );
103
1
}
104

            
105
#[test]
106
1
fn shift_on_other_keys_is_kept() {
107
1
    assert_eq!(event(KeyCode::Up, KeyModifiers::SHIFT), chord("S-Up"));
108
1
    assert_eq!(event(KeyCode::Tab, KeyModifiers::NONE), chord("Tab"));
109
1
}
110

            
111
#[test]
112
1
fn back_tab_is_shift_tab() {
113
1
    assert_eq!(event(KeyCode::BackTab, KeyModifiers::SHIFT), chord("S-Tab"));
114
1
    assert_eq!(event(KeyCode::BackTab, KeyModifiers::NONE), chord("S-Tab"));
115
1
}
116

            
117
#[test]
118
1
fn only_an_unmodified_character_is_printable() {
119
1
    for text in ["q", "N", ":", "+", "0"] {
120
5
        assert!(chord(text).is_printable(), "{text}");
121
    }
122
1
    for text in ["C-q", "M-x", "F1", "Tab", "Enter", "Insert", "Left"] {
123
7
        assert!(!chord(text).is_printable(), "{text}");
124
    }
125
1
}