1
use ratatui::layout::{Position, Rect};
2

            
3
use super::*;
4
use crate::widgets::SelectOption;
5

            
6
8
fn two_lines() -> Grid<'static> {
7
8
    let mut grid = Grid::default();
8
8
    grid.push_cells(0, vec![Item::label("ab"), Item::text("x", 1, false)]);
9
8
    grid.push_cells(
10
8
        0,
11
8
        vec![Item::label("abcd"), Item::text("hello world", 0, true)],
12
8
    );
13
8
    grid
14
8
}
15

            
16
6
fn select(labels: &[&str]) -> SelectWidget {
17
6
    let mut select = SelectWidget::new();
18
6
    select.set_options(
19
6
        labels
20
6
            .iter()
21
            .map(|label| SelectOption {
22
9
                id: label.to_lowercase(),
23
9
                label: (*label).to_string(),
24
9
            })
25
            .collect(),
26
    );
27
6
    select
28
6
}
29

            
30
#[test]
31
1
fn columns_take_the_widest_cell_and_a_gap_apart() {
32
1
    let rects = two_lines().cell_rects(Rect::new(2, 3, 40, 5), 0);
33
1
    assert_eq!(rects[0], [Rect::new(2, 3, 4, 1), Rect::new(7, 3, 13, 1)]);
34
1
    assert_eq!(rects[1], [Rect::new(2, 4, 4, 1), Rect::new(7, 4, 13, 1)]);
35
1
}
36

            
37
#[test]
38
1
fn a_text_cell_reserves_twelve_and_a_select_has_room_for_its_mark() {
39
1
    assert_eq!(Item::text("", 0, false).width(), 12);
40
1
    assert_eq!(Item::select(&select(&["Apple"]), false).width(), 9);
41
1
    assert_eq!(Item::select(&select(&["Banana"]), false).width(), 9);
42
1
}
43

            
44
#[test]
45
1
fn an_empty_label_takes_no_column_and_no_gap() {
46
1
    let mut grid = Grid::default();
47
1
    grid.push_cells(0, vec![Item::label(""), Item::label("a"), Item::label("")]);
48
1
    let rects = grid.cell_rects(Rect::new(0, 0, 10, 1), 0);
49
1
    assert_eq!(rects[0][1], Rect::new(0, 0, 1, 1));
50
1
    assert_eq!(rects[0][2].width, 0);
51
1
    assert_eq!(grid.width(), 1);
52
1
}
53

            
54
#[test]
55
1
fn indents_align_their_own_columns() {
56
1
    let mut grid = Grid::default();
57
1
    grid.push_cells(0, vec![Item::label("wide label")]);
58
1
    grid.push_cells(4, vec![Item::label("a"), Item::label("b")]);
59
1
    let rects = grid.cell_rects(Rect::new(0, 0, 40, 2), 0);
60
1
    assert_eq!(rects[1], [Rect::new(4, 1, 1, 1), Rect::new(6, 1, 1, 1)]);
61
1
}
62

            
63
#[test]
64
1
fn scrolling_moves_the_lines_up_and_hides_those_out_of_view() {
65
1
    let rects = two_lines().cell_rects(Rect::new(0, 0, 40, 1), 1);
66
2
    assert!(rects[0].iter().all(|rect| rect.width == 0));
67
1
    assert_eq!(rects[1][0], Rect::new(0, 0, 4, 1));
68
1
    let rects = two_lines().cell_rects(Rect::new(0, 0, 40, 1), 0);
69
2
    assert!(rects[1].iter().all(|rect| rect.height == 0));
70
1
}
71

            
72
#[test]
73
1
fn a_narrow_area_clips_the_cells_instead_of_panicking() {
74
1
    let area = Rect::new(1, 1, 7, 2);
75
1
    let rects = two_lines().cell_rects(area, 0);
76
1
    for rect in rects.iter().flatten() {
77
4
        assert_eq!(rect.intersection(area), *rect, "{rect:?}");
78
    }
79
1
    assert_eq!(rects[0][1].x, rects[1][1].x, "the group shifts together");
80
1
    let nothing = two_lines().cell_rects(Rect::new(0, 0, 0, 0), 0);
81
4
    assert!(nothing.iter().flatten().all(|rect| rect.area() == 0));
82
1
}
83

            
84
#[test]
85
1
fn a_text_line_spans_from_its_indent_to_the_edge() {
86
1
    let mut grid = Grid::default();
87
1
    grid.push_note(4, "(no tags)");
88
1
    let rects = grid.cell_rects(Rect::new(0, 0, 30, 1), 0);
89
1
    assert_eq!(rects[0], [Rect::new(4, 0, 26, 1)]);
90
1
    assert_eq!(grid.width(), 13);
91
1
}
92

            
93
#[test]
94
1
fn a_title_goes_first_and_pushes_the_lines_in_and_the_focus_down() {
95
1
    let mut grid = two_lines();
96
1
    grid.focus_line = 1;
97
1
    let grid = grid.titled("  Splits:", true);
98
1
    assert_eq!(grid.focus_line, 2);
99
1
    let rects = grid.cell_rects(Rect::new(0, 0, 40, 3), 0);
100
1
    assert_eq!(rects[0], [Rect::new(0, 0, 40, 1)]);
101
1
    assert_eq!(rects[1][0].x, GRID_INDENT);
102
1
}
103

            
104
#[test]
105
1
fn the_focused_text_cell_has_the_caret_at_its_cursor() {
106
1
    let grid = two_lines();
107
1
    let rects = grid.cell_rects(Rect::new(0, 0, 40, 2), 0);
108
1
    assert_eq!(grid.caret(&rects), Some(Position::new(6, 1)));
109
1
    assert_eq!(grid.anchor(&rects), None);
110
1
}
111

            
112
#[test]
113
1
fn a_select_has_a_caret_only_while_open_and_anchors_its_list_at_its_cell() {
114
1
    let mut open = select(&["Apple", "Banana"]);
115
1
    let mut grid = Grid::default();
116
1
    grid.push_cells(0, vec![Item::label("1."), Item::select(&open, true)]);
117
1
    let rects = grid.cell_rects(Rect::new(0, 0, 40, 1), 0);
118
1
    assert_eq!(grid.caret(&rects), None);
119
1
    assert_eq!(grid.anchor(&rects), Some(Rect::new(3, 0, 9, 1)));
120
1
    open.open_list();
121
1
    open.filter_push('b');
122
1
    let mut grid = Grid::default();
123
1
    grid.push_cells(0, vec![Item::label("1."), Item::select(&open, true)]);
124
1
    let rects = grid.cell_rects(Rect::new(0, 0, 40, 1), 0);
125
1
    assert_eq!(grid.caret(&rects), Some(Position::new(5, 0)));
126
1
}
127

            
128
#[test]
129
1
fn a_select_shows_its_filter_and_keeps_the_width_of_its_label() {
130
1
    let mut select = select(&["Banana"]);
131
1
    select.open_list();
132
1
    select.filter_push('b');
133
1
    let item = Item::select(&select, true);
134
1
    assert_eq!(item.text, "b");
135
1
    assert_eq!(item.width(), 9);
136
1
    assert_eq!(Item::select(&SelectWidget::new(), false).text, "?");
137
1
}
138

            
139
#[test]
140
1
fn the_caret_is_gone_when_the_focused_cell_has_no_room() {
141
1
    let grid = two_lines();
142
1
    let rects = grid.cell_rects(Rect::new(0, 0, 0, 2), 0);
143
1
    assert_eq!(grid.caret(&rects), None);
144
1
}
145

            
146
#[test]
147
1
fn a_select_keeps_the_width_of_its_widest_option_whatever_is_typed() {
148
1
    let mut pick = select(&["Cash", "Cafe", "Expenses:Food"]);
149
1
    let closed = Item::select(&pick, false).width();
150
1
    pick.open_list();
151
1
    pick.filter_push('c');
152
1
    pick.filter_push('a');
153
1
    assert_eq!(Item::select(&pick, true).width(), closed);
154
1
    assert_eq!(closed, 16);
155
1
}
156

            
157
#[test]
158
1
fn a_text_cell_has_a_column_for_its_end_cursor() {
159
1
    assert_eq!(Item::text("1234567", 7, true).width(), 12);
160
1
    assert_eq!(Item::text("123456789012", 12, true).width(), 14);
161
1
}
162

            
163
7
fn long_row(focus: Option<usize>) -> Grid<'static> {
164
7
    let mut grid = Grid::default();
165
28
    let item = |text: &str, at: usize| Item::text(text, 0, focus == Some(at));
166
7
    grid.push_cells(
167
7
        0,
168
7
        vec![
169
7
            Item::label("1."),
170
7
            item("aaaaaaaaaaaaaaaaaaaa", 1),
171
7
            item("bbbbbbbbbbbbbbbbbbbb", 2),
172
7
            item("cccccccccccccccccccc", 3),
173
7
        ],
174
7
    );
175
7
    grid.push_cells(2, vec![Item::label("2."), item("x", 9)]);
176
7
    grid
177
7
}
178

            
179
#[test]
180
1
fn a_focused_cell_past_the_edge_shifts_its_line_just_enough_and_its_caret_follows() {
181
1
    let area = Rect::new(0, 0, 40, 2);
182
1
    let grid = long_row(Some(3));
183
1
    let rects = grid.cell_rects(area, 0);
184
1
    assert_eq!(rects[0][3].right(), 40);
185
1
    assert_eq!(rects[0][3].width, 22);
186
1
    assert_eq!(grid.caret(&rects), Some(Position::new(19, 0)));
187
1
    assert_eq!(
188
1
        rects[1],
189
1
        long_row(None).cell_rects(area, 0)[1],
190
        "other lines stay"
191
    );
192
1
}
193

            
194
#[test]
195
1
fn a_cell_cut_at_the_left_edge_is_hidden_and_one_cut_at_the_right_is_clipped() {
196
1
    let rects = long_row(Some(3)).cell_rects(Rect::new(5, 0, 40, 2), 0);
197
1
    assert_eq!(rects[0][0].width, 0);
198
1
    assert_eq!(rects[0][2].width, 0, "the cell cut on the left");
199
1
    assert_eq!(rects[0][3].right(), 45);
200
1
    let rects = long_row(None).cell_rects(Rect::new(5, 0, 40, 2), 0);
201
1
    assert!(
202
1
        rects[0][2].width > 0 && rects[0][2].width < 21,
203
        "clipped on the right"
204
    );
205
1
}
206

            
207
#[test]
208
1
fn focus_back_on_the_first_cell_returns_the_shift_to_zero() {
209
1
    let area = Rect::new(0, 0, 40, 2);
210
1
    assert_eq!(
211
1
        long_row(Some(1)).cell_rects(area, 0),
212
1
        long_row(None).cell_rects(area, 0)
213
    );
214
1
}
215

            
216
#[test]
217
1
fn a_cell_wider_than_the_area_starts_at_its_edge() {
218
1
    let rects = long_row(Some(1)).cell_rects(Rect::new(0, 0, 10, 2), 0);
219
1
    assert_eq!(rects[0][1], Rect::new(0, 0, 10, 1));
220
1
}
221

            
222
#[test]
223
1
fn the_list_anchor_follows_the_shift() {
224
1
    let mut pick = select(&["Apple"]);
225
1
    pick.open_list();
226
1
    let mut grid = Grid::default();
227
1
    let wide = || Item::text("w".repeat(40), 0, false);
228
1
    grid.push_cells(0, vec![wide(), Item::select(&pick, true)]);
229
1
    let rects = grid.cell_rects(Rect::new(0, 0, 40, 1), 0);
230
1
    let anchor = grid.anchor(&rects).expect("an anchor");
231
1
    assert_eq!(anchor.right(), 40);
232
1
    assert!(anchor.x < 42);
233
1
}
234

            
235
#[test]
236
1
fn a_shifted_group_moves_whole_and_other_groups_stay() {
237
5
    let text = |text: &str, focused: bool| Item::text(text, 0, focused);
238
1
    let wide = "w".repeat(30);
239
1
    let mut grid = Grid::default();
240
1
    grid.push_cells(0, vec![Item::label("split"), text("s", false)]);
241
1
    grid.push_cells(4, vec![Item::label("Name"), Item::label("Value")]);
242
1
    grid.push_cells(4, vec![text(&wide, false), text("v", true)]);
243
1
    grid.push_cells(4, vec![text("a", false), text("b", false)]);
244
1
    let rects = grid.cell_rects(Rect::new(0, 0, 40, 4), 0);
245
1
    let flat = grid.clone();
246
1
    let area_far = flat.cell_rects(Rect::new(0, 0, 400, 4), 0);
247
1
    assert_eq!(rects[0], area_far[0], "the split row above stays");
248
1
    let shifts: Vec<u16> = (1..4)
249
3
        .map(|line| area_far[line][1].x - rects[line][1].x)
250
        .collect();
251
1
    assert!(
252
3
        shifts.iter().all(|&by| by == shifts[0] && by > 0),
253
        "{shifts:?}"
254
    );
255
1
    assert_eq!(rects[1][1].x, rects[2][1].x);
256
1
    assert_eq!(rects[3][1].x, rects[2][1].x);
257
1
}
258

            
259
#[test]
260
1
fn a_clipped_tail_item_shows_the_end_after_an_ellipsis_until_focused() {
261
1
    let rect = Rect::new(0, 0, 12, 1);
262
1
    let tail = Item::text("Expenses:Food:Groceries", 0, false).tail();
263
1
    assert_eq!(tail.shown(rect), "…Groceries");
264
1
    let mut focused = Item::text("Expenses:Food:Groceries", 0, true).tail();
265
1
    assert_eq!(focused.shown(rect), "Expenses:Food:Groceries");
266
1
    focused.focused = false;
267
1
    assert_eq!(focused.shown(rect), "…Groceries");
268
1
    assert_eq!(
269
1
        tail.shown(Rect::new(0, 0, 40, 1)),
270
1
        "Expenses:Food:Groceries"
271
    );
272
1
}
273

            
274
#[test]
275
1
fn an_item_without_the_tail_rule_keeps_its_text() {
276
1
    let item = Item::text("Expenses:Food:Groceries", 0, false);
277
1
    assert_eq!(
278
1
        item.shown(Rect::new(0, 0, 12, 1)),
279
1
        "Expenses:Food:Groceries"
280
    );
281
1
}
282

            
283
#[test]
284
1
fn a_combining_accent_at_the_cut_is_dropped_not_put_on_the_ellipsis() {
285
1
    let item = Item::text("Expenses:Cafe\u{301}s", 0, false).tail();
286
1
    let shown = item.shown(Rect::new(0, 0, 4, 1));
287
1
    assert_eq!(shown, "…s");
288
1
}
289

            
290
#[test]
291
1
fn a_wide_text_never_exceeds_the_room() {
292
1
    let item = Item::text("資産:銀行:普通預金口座", 0, false).tail();
293
1
    for width in 3..30 {
294
27
        let shown = item.shown(Rect::new(0, 0, width, 1));
295
27
        let cells = Span::raw(shown.as_ref()).width();
296
27
        assert!(cells <= usize::from(width - 2), "{width}: {shown}");
297
    }
298
1
}