1
//! Help is the focused chain's bindings, then the `:` commands.
2

            
3
use super::help_lines;
4
use crate::app::App;
5
use crate::form::Form;
6
use crate::modal::Modal;
7
use crate::view::Tab;
8

            
9
5
fn help(app: &App) -> String {
10
5
    help_lines(app).join("\n")
11
5
}
12

            
13
10
fn line_with(app: &App, keys: &str, name: &str, text: &str) -> bool {
14
10
    let wanted = format!("  {keys:<16}{name:<26}{text}");
15
10
    help_lines(app).contains(&wanted)
16
10
}
17

            
18
#[test]
19
1
fn help_lists_the_keys_of_the_preset_with_their_descriptions() {
20
1
    let cua = crate::test_support::app();
21
1
    assert!(line_with(&cua, "F1", "open-help", "Show the key help"));
22
1
    assert!(line_with(
23
1
        &cua,
24
1
        "M-x",
25
1
        "quit",
26
1
        "Ask, then leave the application"
27
1
    ));
28
1
    assert!(line_with(
29
1
        &cua,
30
1
        "F10, M-m",
31
1
        "open-menu",
32
1
        "Open the menu bar"
33
1
    ));
34
1
    let emacs = crate::test_support::emacs_app();
35
1
    assert!(line_with(
36
1
        &emacs,
37
1
        "q",
38
1
        "close-or-quit",
39
1
        "Close the front window, or ask to quit when none is open"
40
1
    ));
41
1
    assert!(!line_with(&emacs, "F1", "open-help", "Show the key help"));
42
1
    assert!(line_with(&emacs, "F10", "open-menu", "Open the menu bar"));
43
1
}
44

            
45
#[test]
46
1
fn a_binding_of_one_action_gathers_its_chords_on_a_row() {
47
1
    let mut app = crate::test_support::app();
48
1
    app.switch_tab(Tab::Accounts);
49
1
    assert!(line_with(
50
1
        &app,
51
1
        "Down, j",
52
1
        "next-row",
53
1
        "Select the next row"
54
1
    ));
55
1
}
56

            
57
#[test]
58
1
fn a_payload_action_names_its_argument() {
59
1
    let app = crate::test_support::app();
60
1
    assert!(line_with(
61
1
        &app,
62
1
        "2",
63
1
        "select-tab transactions",
64
1
        "Go to a tab"
65
1
    ));
66
1
}
67

            
68
#[test]
69
1
fn each_scope_of_the_chain_is_a_titled_section() {
70
1
    let mut app = crate::test_support::app();
71
1
    app.switch_tab(Tab::Transactions);
72
1
    let text = help(&app);
73
1
    let order: Vec<_> = ["Transactions\n", "Lists\n", "Views\n", "Everywhere\n"]
74
1
        .iter()
75
4
        .map(|title| text.find(title))
76
        .collect();
77
1
    assert!(order.iter().all(Option::is_some), "{text}");
78
3
    assert!(order.windows(2).all(|pair| pair[0] < pair[1]), "{text}");
79
1
}
80

            
81
#[test]
82
1
fn help_describes_the_context_it_is_opened_in() {
83
1
    let mut app = crate::test_support::app();
84
1
    app.overlays.push(Modal::Form(Form::config_set()));
85
1
    let text = help(&app);
86
1
    assert!(text.contains("Forms and dialogs\n"), "{text}");
87
1
    assert!(line_with(
88
1
        &app,
89
1
        "M-o, F2",
90
1
        "submit",
91
1
        "Submit the form or the command line"
92
1
    ));
93
1
    assert!(!text.contains("Lists\n"), "{text}");
94
1
}
95

            
96
#[test]
97
1
fn help_follows_an_override() {
98
1
    let mut app = crate::test_support::app();
99
1
    app.keys = match app.keys.overridden("list: F9 = new-row; list: c = none") {
100
1
        Ok(keys) => keys,
101
        Err(err) => panic!("the override parses: {err}"),
102
    };
103
1
    app.switch_tab(Tab::Accounts);
104
1
    assert!(line_with(
105
1
        &app,
106
1
        "Insert, F9",
107
1
        "new-row",
108
1
        "Create a row in the active tab"
109
1
    ));
110
1
    assert!(!help(&app).contains("  n "), "{}", help(&app));
111
1
}
112

            
113
#[test]
114
1
fn the_commands_follow_the_keys_and_come_from_the_command_tree() {
115
1
    let app = crate::test_support::app();
116
1
    let lines = help_lines(&app);
117
1
    assert!(
118
1
        lines.contains(&"Commands, after :".to_string()),
119
        "{lines:?}"
120
    );
121
1
    assert!(lines.contains(&"  version".to_string()), "{lines:?}");
122
1
    assert!(
123
1
        lines
124
1
            .iter()
125
56
            .any(|line| line.starts_with("  account list|balance")),
126
        "{lines:?}"
127
    );
128
1
}
129

            
130
#[test]
131
1
fn without_a_command_line_key_the_commands_have_a_plain_title() {
132
1
    let mut app = crate::test_support::app();
133
1
    app.overlays.push(Modal::Form(Form::config_set()));
134
1
    assert!(help_lines(&app).contains(&"Commands".to_string()));
135
1
}
136

            
137
#[test]
138
1
fn help_lists_only_keys_that_act_in_the_context() {
139
1
    let mut app = crate::test_support::app();
140
1
    app.switch_tab(Tab::Accounts);
141
1
    let text = help(&app);
142
1
    assert!(!text.contains("delete-row"), "{text}");
143
1
    app.overlays.push(Modal::Form(Form::config_set()));
144
1
    let text = help(&app);
145
1
    for hidden in ["M-x", "C-b", "C-t"] {
146
3
        assert!(!text.contains(hidden), "{hidden}: {text}");
147
    }
148
1
}