1
use super::body::field_rects;
2
use super::*;
3
use crate::modal::Modal;
4
use crate::widgets::WidgetKind;
5
use ratatui::Terminal;
6
use ratatui::backend::TestBackend;
7
use ratatui::layout::Position;
8
use ratatui::layout::Rect;
9

            
10
8
fn drawn_cursor(
11
8
    app: &crate::app::App,
12
8
    size: (u16, u16),
13
8
) -> (Option<Position>, Terminal<TestBackend>) {
14
8
    let mut terminal = Terminal::new(TestBackend::new(size.0, size.1)).expect("test terminal");
15
8
    terminal
16
8
        .draw(|frame| crate::draw::draw(frame, app, &mut crate::hits::Hits::default()))
17
8
        .expect("the frame draws");
18
8
    let backend = terminal.backend();
19
8
    let shown = backend.cursor_visible().then(|| backend.cursor_position());
20
8
    (shown, terminal)
21
8
}
22

            
23
7
fn app_with(form: Form) -> crate::app::App {
24
7
    let mut app = crate::test_support::app();
25
7
    app.overlays.push(Modal::Form(form));
26
7
    app
27
7
}
28

            
29
2
fn kind_at(form: &Form, focus: usize) -> Option<WidgetKind> {
30
2
    form.fields.get(focus).map(|f| f.widget.kind())
31
2
}
32

            
33
#[test]
34
1
fn every_field_has_its_own_rects_in_order() {
35
1
    let app = crate::test_support::app();
36
1
    let form = Form::transaction_create();
37
1
    let body = form_body(&form, (false, true), &app);
38
1
    let area = Rect::new(0, 0, 60, 40);
39
1
    let rects = field_rects(&body, area);
40
1
    assert_eq!(rects.fields.len(), 4);
41
1
    assert_eq!(rects.fields[0].height, 1);
42
1
    assert_eq!(rects.fields[0].y, rects.notices.bottom());
43
1
    for pair in rects.fields.windows(2) {
44
3
        assert_eq!(pair[0].bottom(), pair[1].y);
45
    }
46
1
    assert_eq!(rects.tail.y, rects.fields[3].bottom());
47
1
    assert_eq!(
48
1
        rects.tail.bottom(),
49
1
        area.y + u16::try_from(body.height()).unwrap_or(0)
50
    );
51
1
}
52

            
53
#[test]
54
1
fn a_short_area_scrolls_by_whole_fields_to_keep_the_focus_in_view() {
55
1
    let app = crate::test_support::app();
56
1
    let mut form = Form::config_set();
57
1
    let area = Rect::new(2, 3, 40, 1);
58
1
    form.focus = 1;
59
1
    let rects = field_rects(&form_body(&form, (false, true), &app), area);
60
1
    assert_eq!(rects.fields[0].height, 0);
61
1
    assert_eq!(rects.fields[1], Rect::new(2, 3, 40, 1));
62
1
    form.focus = 0;
63
1
    let rects = field_rects(&form_body(&form, (false, true), &app), area);
64
1
    assert_eq!(rects.fields[0], Rect::new(2, 3, 40, 1));
65
1
    assert_eq!(rects.fields[1].height, 0);
66
1
}
67

            
68
4
fn typed(form: &mut Form, focus: usize, text: &str, back: usize) {
69
4
    let Some(editor) = form.fields[focus].widget.as_text_mut() else {
70
        panic!("a text-like field");
71
    };
72
4
    editor.replace_buffer(text);
73
4
    for _ in 0..back {
74
1
        editor.move_left();
75
    }
76
4
}
77

            
78
5
fn cell(terminal: &Terminal<TestBackend>, at: Position) -> String {
79
5
    terminal.backend().buffer()[(at.x, at.y)]
80
5
        .symbol()
81
5
        .to_string()
82
5
}
83

            
84
#[test]
85
1
fn the_cursor_is_at_the_edit_position_of_a_text_field() {
86
1
    let mut form = Form::config_set();
87
1
    form.focus = 1;
88
1
    typed(&mut form, 1, "abc", 1);
89
1
    let (shown, terminal) = drawn_cursor(&app_with(form), (80, 24));
90
1
    let Some(at) = shown else {
91
        panic!("a focused text field shows the cursor");
92
    };
93
1
    assert_eq!(cell(&terminal, at), "c");
94
1
    assert_eq!(cell(&terminal, Position::new(at.x - 1, at.y)), "b");
95
1
}
96

            
97
#[test]
98
1
fn the_cursor_follows_the_focus_to_an_amount_and_a_date_field() {
99
1
    for (mut form, label) in [
100
1
        (Form::commodity_convert(), "Amount"),
101
1
        (Form::transaction_create(), "Date"),
102
1
    ] {
103
2
        let focus = form
104
2
            .fields
105
2
            .iter()
106
2
            .position(|f| matches!(f.widget, Widget::Amount(_) | Widget::Date(_)))
107
2
            .expect("a one-line field");
108
2
        form.focus = focus;
109
2
        typed(&mut form, focus, "12/5", 0);
110
2
        let (shown, terminal) = drawn_cursor(&app_with(form), (120, 36));
111
2
        let Some(at) = shown else {
112
            panic!("{label}: the cursor is shown");
113
        };
114
2
        assert_eq!(
115
2
            cell(&terminal, Position::new(at.x - 1, at.y)),
116
2
            "5",
117
            "{label}"
118
        );
119
    }
120
1
}
121

            
122
#[test]
123
1
fn a_long_value_scrolls_and_the_cursor_stays_in_the_field() {
124
1
    let mut form = Form::config_set();
125
1
    typed(&mut form, 0, &"x".repeat(200), 0);
126
1
    let (shown, terminal) = drawn_cursor(&app_with(form), (80, 24));
127
1
    let Some(at) = shown else {
128
        panic!("the cursor is shown");
129
    };
130
1
    assert!(at.x < 80);
131
1
    assert_eq!(cell(&terminal, Position::new(at.x - 1, at.y)), "x");
132
1
}
133

            
134
#[test]
135
1
fn no_cursor_on_a_select_or_splits_field() {
136
1
    let mut form = Form::transaction_create();
137
1
    form.focus = 3;
138
1
    assert_eq!(kind_at(&form, 3), Some(WidgetKind::Splits));
139
1
    assert_eq!(drawn_cursor(&app_with(form), (80, 24)).0, None);
140
1
    let mut form = Form::account_create();
141
1
    form.focus = 1;
142
1
    assert_eq!(kind_at(&form, 1), Some(WidgetKind::Select));
143
1
    assert_eq!(drawn_cursor(&app_with(form), (80, 24)).0, None);
144
1
}
145

            
146
#[test]
147
1
fn no_cursor_while_a_pending_form_covers_the_overlay() {
148
1
    let mut app = app_with(Form::config_set());
149
1
    app.lock_pending_form(1, Form::config_set());
150
1
    assert_eq!(drawn_cursor(&app, (80, 24)).0, None);
151
1
}
152

            
153
#[test]
154
1
fn no_cursor_without_a_form() {
155
1
    let app = crate::test_support::app();
156
1
    assert_eq!(drawn_cursor(&app, (80, 24)).0, None);
157
1
}