1
//! `q` closes windows and every quit asks first, except the pending C-c.
2

            
3
use crate::app::{App, PendingForm};
4
use crate::event::{Intent, apply};
5
use crate::form::Form;
6
use crate::modal::{ConfirmAction, Modal};
7
use crate::test_support::{alt, app, ctrl, empty_app, key, press};
8
use ratatui::crossterm::event::KeyCode;
9

            
10
3
fn asks_to_quit(app: &App) -> bool {
11
3
    matches!(
12
3
        app.overlays.top(),
13
        Some(Modal::Confirm {
14
            action: ConfirmAction::Quit,
15
            ..
16
        })
17
    )
18
3
}
19

            
20
#[test]
21
1
fn q_with_a_window_open_closes_it_and_does_not_quit() {
22
1
    let mut app = app();
23
1
    let front = app.front_window();
24
1
    press(&mut app, key(KeyCode::Char('q')));
25
1
    assert_ne!(app.front_window(), front);
26
1
    assert!(app.overlays.is_empty());
27
1
    assert!(!app.should_quit);
28
1
}
29

            
30
#[test]
31
1
fn q_on_the_empty_desktop_asks_and_y_quits() {
32
1
    let mut app = empty_app();
33
1
    press(&mut app, key(KeyCode::Char('q')));
34
1
    assert!(asks_to_quit(&app));
35
1
    assert!(!app.should_quit);
36
1
    press(&mut app, key(KeyCode::Char('y')));
37
1
    assert!(app.should_quit);
38
1
}
39

            
40
#[test]
41
1
fn n_keeps_the_application_running() {
42
1
    let mut app = empty_app();
43
1
    press(&mut app, key(KeyCode::Char('q')));
44
1
    press(&mut app, key(KeyCode::Char('n')));
45
1
    assert!(app.overlays.is_empty());
46
1
    assert!(!app.should_quit);
47
1
}
48

            
49
#[test]
50
1
fn the_quit_intent_asks_first() {
51
1
    let mut app = app();
52
1
    apply(&mut app, Intent::Quit);
53
1
    assert!(asks_to_quit(&app));
54
1
    assert!(!app.should_quit);
55
1
}
56

            
57
#[test]
58
1
fn alt_x_in_cua_asks_to_quit() {
59
1
    let mut app = app();
60
1
    press(&mut app, alt(KeyCode::Char('x')));
61
1
    assert!(asks_to_quit(&app));
62
1
    assert!(!app.should_quit);
63
1
}
64

            
65
#[test]
66
1
fn ctrl_c_quits_at_once_while_a_request_is_pending() {
67
1
    let mut app = app();
68
1
    app.pending_form = Some(PendingForm {
69
1
        request_id: 1,
70
1
        form: Form::commodity_create(),
71
1
    });
72
1
    press(&mut app, ctrl(KeyCode::Char('c')));
73
1
    assert!(app.should_quit);
74
1
    assert!(app.overlays.is_empty());
75
1
}
76

            
77
#[test]
78
1
fn alt_x_while_a_request_is_pending_opens_no_box_and_does_not_quit() {
79
1
    let mut app = app();
80
1
    app.pending_form = Some(PendingForm {
81
1
        request_id: 1,
82
1
        form: Form::commodity_create(),
83
1
    });
84
1
    press(&mut app, alt(KeyCode::Char('x')));
85
1
    assert!(app.overlays.is_empty());
86
1
    assert!(!app.should_quit);
87
1
}
88

            
89
#[test]
90
1
fn q_typed_in_the_command_line_is_text_not_close_or_quit() {
91
1
    let mut app = app();
92
1
    apply(&mut app, Intent::OpenCommandLine);
93
1
    let front = app.front_window();
94
1
    press(&mut app, key(KeyCode::Char('q')));
95
1
    assert_eq!(app.cmdline.editor.buffer(), "q");
96
1
    assert_eq!(app.front_window(), front);
97
1
    assert!(app.overlays.is_empty());
98
1
    assert!(!app.should_quit);
99
1
}