1
//! The cursor keys inside the cells of the splits and rows grids, under every preset.
2

            
3
use super::tests::{key, press, type_text};
4
use crate::app::App;
5
use crate::form::Form;
6
use crate::modal::Modal;
7
use crate::widgets::splits::{COL_FROM, COL_VALUE};
8
use crate::widgets::{FocusedSubWidget, Widget};
9
use ratatui::crossterm::event::KeyCode;
10

            
11
const SPLITS: usize = 3;
12

            
13
4
fn presets() -> [(&'static str, App); 3] {
14
4
    [
15
4
        ("cua", crate::test_support::app()),
16
4
        ("emacs", crate::test_support::emacs_app()),
17
4
        ("vim", crate::test_support::vim_app()),
18
4
    ]
19
4
}
20

            
21
10
fn on_splits(mut app: App, col: usize) -> App {
22
10
    let mut form = Form::transaction_create();
23
10
    form.focus = SPLITS;
24
10
    if let Widget::Splits(sw) = &mut form.fields[SPLITS].widget {
25
10
        sw.col_focus = col;
26
    }
27
10
    app.overlays.push(Modal::Form(form));
28
10
    app
29
10
}
30

            
31
9
fn amount(app: &mut App) -> (String, usize) {
32
9
    let Some(form) = app.overlays.form_mut() else {
33
        panic!("the form is open");
34
    };
35
9
    let Widget::Splits(sw) = &mut form.fields[SPLITS].widget else {
36
        panic!("the splits field");
37
    };
38
9
    match sw.focused_subwidget_mut() {
39
9
        Some(FocusedSubWidget::Amount(a)) => (a.value().to_string(), a.editor().cursor()),
40
        _ => panic!("an amount cell"),
41
    }
42
9
}
43

            
44
#[test]
45
1
fn left_inserts_in_the_middle_of_an_amount_cell_under_every_preset() {
46
1
    for (name, app) in presets() {
47
3
        let mut app = on_splits(app, COL_VALUE);
48
3
        type_text(&mut app, "1234");
49
3
        press(&mut app, key(KeyCode::Left));
50
3
        press(&mut app, key(KeyCode::Left));
51
3
        type_text(&mut app, "9");
52
3
        assert_eq!(amount(&mut app), ("12934".to_string(), 3), "{name}");
53
    }
54
1
}
55

            
56
#[test]
57
1
fn home_then_delete_removes_the_first_character_under_every_preset() {
58
1
    for (name, app) in presets() {
59
3
        let mut app = on_splits(app, COL_VALUE);
60
3
        type_text(&mut app, "1234");
61
3
        press(&mut app, key(KeyCode::Home));
62
3
        press(&mut app, key(KeyCode::Delete));
63
3
        assert_eq!(amount(&mut app), ("234".to_string(), 0), "{name}");
64
3
        press(&mut app, key(KeyCode::End));
65
3
        press(&mut app, key(KeyCode::Right));
66
3
        assert_eq!(amount(&mut app).1, 3, "{name}");
67
    }
68
1
}
69

            
70
#[test]
71
1
fn the_cursor_keys_leave_a_select_cell_alone_and_tab_still_moves_on() {
72
1
    for (name, app) in presets() {
73
3
        let mut app = on_splits(app, COL_FROM);
74
3
        for code in [KeyCode::Left, KeyCode::Right, KeyCode::Home, KeyCode::End] {
75
12
            press(&mut app, key(code));
76
        }
77
3
        let Some(form) = app.overlays.form_mut() else {
78
            panic!("the form is open");
79
        };
80
3
        let Widget::Splits(sw) = &form.fields[SPLITS].widget else {
81
            panic!("the splits field");
82
        };
83
3
        assert_eq!((sw.row_focus, sw.col_focus), (0, COL_FROM), "{name}");
84
3
        press(&mut app, key(KeyCode::Tab));
85
3
        let Some(form) = app.overlays.form_mut() else {
86
            panic!("the form is open");
87
        };
88
3
        let Widget::Splits(sw) = &form.fields[SPLITS].widget else {
89
            panic!("the splits field");
90
        };
91
3
        assert_eq!(sw.col_focus, COL_FROM + 1, "{name}");
92
    }
93
1
}
94

            
95
#[test]
96
1
fn left_inserts_in_the_middle_of_a_text_cell_of_a_rows_grid_under_every_preset() {
97
1
    for (name, app) in presets() {
98
3
        let mut form = Form::transaction_create();
99
3
        let tags = form
100
3
            .fields
101
3
            .iter()
102
9
            .position(|f| matches!(f.widget, Widget::Rows(_)))
103
3
            .expect("a rows field");
104
3
        form.focus = tags;
105
3
        if let Widget::Rows(rw) = &mut form.fields[tags].widget {
106
3
            rw.add_row();
107
        }
108
3
        let mut app = app;
109
3
        app.overlays.push(Modal::Form(form));
110
3
        type_text(&mut app, "1234");
111
3
        press(&mut app, key(KeyCode::Left));
112
3
        press(&mut app, key(KeyCode::Left));
113
3
        type_text(&mut app, "9");
114
3
        let Some(form) = app.overlays.form_mut() else {
115
            panic!("the form is open");
116
        };
117
3
        let Widget::Rows(rw) = &form.fields[tags].widget else {
118
            panic!("the rows field");
119
        };
120
3
        assert_eq!(rw.focused_text("name"), "12934", "{name}");
121
    }
122
1
}
123

            
124
#[test]
125
1
fn help_lists_the_line_editing_keys_on_a_grid_cell() {
126
1
    let app = on_splits(crate::test_support::app(), COL_VALUE);
127
1
    let lines = super::help::help_lines(&app);
128
17
    assert!(lines.iter().any(|l| l == "Line editing"), "{lines:?}");
129
1
    assert!(
130
18
        lines.iter().any(|l| l.contains("Move the cursor left")),
131
        "{lines:?}"
132
    );
133
1
}