1
use super::*;
2

            
3
24
fn theme() -> Theme {
4
24
    Theme::preset("dn").expect("the dn preset parses")
5
24
}
6

            
7
4
fn screen() -> Rect {
8
4
    Rect::new(0, 0, 40, 20)
9
4
}
10

            
11
15
fn cells(buf: &Buffer, y: u16, from: u16, to: u16) -> String {
12
142
    (from..to).map(|x| buf[(x, y)].symbol()).collect()
13
15
}
14

            
15
12
fn labels(n: usize) -> Vec<String> {
16
146
    (0..n).map(|i| format!("Opt{i}")).collect()
17
12
}
18

            
19
18
fn filled() -> Buffer {
20
18
    let mut buf = Buffer::empty(Rect::new(0, 0, 30, 12));
21
18
    for position in buf.area.positions() {
22
6480
        buf[position].set_char('x');
23
    }
24
18
    buf
25
18
}
26

            
27
18
fn drawn_in(items: &[String], state: &mut ListState, area: Rect) -> Buffer {
28
18
    let mut buf = filled();
29
18
    DropDown::new(&theme(), items).render(area, &mut buf, state);
30
18
    buf
31
18
}
32

            
33
8
fn drawn(items: &[String], selected: usize, area: Rect) -> Buffer {
34
8
    let mut state = ListState::default().with_selected(Some(selected));
35
8
    drawn_in(items, &mut state, area)
36
8
}
37

            
38
#[test]
39
1
fn the_box_goes_below_the_anchor_at_its_column_and_width() {
40
1
    let area = dropdown_area(Rect::new(5, 3, 12, 1), 4, screen());
41
1
    assert_eq!(area, Rect::new(5, 4, 12, 6));
42
1
}
43

            
44
#[test]
45
1
fn the_box_goes_above_when_there_is_no_room_below() {
46
1
    let area = dropdown_area(Rect::new(5, 17, 12, 1), 4, screen());
47
1
    assert_eq!(area, Rect::new(5, 11, 12, 6));
48
1
}
49

            
50
#[test]
51
1
fn the_box_is_cut_to_the_larger_side_when_neither_fits() {
52
1
    let small = Rect::new(0, 0, 40, 8);
53
1
    assert_eq!(
54
1
        dropdown_area(Rect::new(0, 2, 10, 1), 20, small),
55
1
        Rect::new(0, 3, 10, 5)
56
    );
57
1
    assert_eq!(
58
1
        dropdown_area(Rect::new(0, 6, 10, 1), 20, small),
59
1
        Rect::new(0, 0, 10, 6)
60
    );
61
1
}
62

            
63
#[test]
64
1
fn the_box_is_kept_inside_a_narrow_screen() {
65
1
    let area = dropdown_area(Rect::new(30, 2, 20, 1), 2, screen());
66
1
    assert_eq!(area, Rect::new(20, 3, 20, 4));
67
1
    let narrow = dropdown_area(Rect::new(3, 2, 20, 1), 2, Rect::new(0, 0, 10, 10));
68
1
    assert_eq!(narrow, Rect::new(0, 3, 10, 4));
69
1
}
70

            
71
#[test]
72
1
fn the_box_under_the_last_row_goes_above() {
73
1
    let area = dropdown_area(Rect::new(0, 19, 10, 1), 3, screen());
74
1
    assert_eq!(area, Rect::new(0, 14, 10, 5));
75
1
}
76

            
77
#[test]
78
1
fn rows_are_framed_with_the_selected_one_in_its_role() {
79
1
    let items = labels(3);
80
1
    let buf = drawn(&items, 1, Rect::new(2, 1, 10, 5));
81
1
    assert_eq!(cells(&buf, 1, 2, 12), "┌────────┐");
82
1
    assert_eq!(cells(&buf, 2, 2, 12), "│ Opt0   │");
83
1
    assert_eq!(cells(&buf, 3, 2, 12), "│ Opt1   │");
84
1
    assert_eq!(cells(&buf, 4, 2, 12), "│ Opt2   │");
85
1
    assert_eq!(cells(&buf, 5, 2, 12), "└────────┘");
86
1
    let theme = theme();
87
1
    assert_eq!(buf[(4, 2)].style().bg, theme.style(Role::List).bg);
88
1
    assert_eq!(buf[(4, 3)].style().fg, theme.style(Role::ListSelected).fg);
89
1
    assert_eq!(buf[(10, 3)].style().bg, theme.style(Role::ListSelected).bg);
90
1
}
91

            
92
#[test]
93
1
fn the_list_scrolls_to_keep_the_selected_row_in_view() {
94
1
    let items = labels(20);
95
1
    let buf = drawn(&items, 15, Rect::new(0, 0, 10, 5));
96
1
    assert_eq!(cells(&buf, 3, 0, 9), "│ Opt15  ");
97
1
    let selected = theme().style(Role::ListSelected);
98
1
    assert_eq!(buf[(2, 3)].style().fg, selected.fg);
99
1
    assert_eq!(buf[(2, 3)].style().bg, selected.bg);
100
1
    assert_eq!(buf[(2, 2)].style().bg, theme().style(Role::List).bg);
101
1
}
102

            
103
#[test]
104
1
fn the_box_casts_a_shadow_right_and_below() {
105
1
    let items = labels(2);
106
1
    let buf = drawn(&items, 0, Rect::new(2, 1, 10, 4));
107
1
    let shade = theme().style(Role::Shadow);
108
1
    assert_eq!(buf[(12, 2)].style().bg, shade.bg);
109
1
    assert_eq!(buf[(13, 3)].style().bg, shade.bg);
110
1
    assert_eq!(buf[(5, 5)].style().bg, shade.bg);
111
1
    assert_eq!(buf[(5, 6)].symbol(), "x");
112
1
}
113

            
114
#[test]
115
1
fn a_box_partly_off_the_buffer_is_clipped() {
116
1
    let items = labels(2);
117
1
    let buf = drawn(&items, 0, Rect::new(25, 10, 10, 6));
118
1
    assert_eq!(cells(&buf, 10, 25, 30), "┌───┐");
119
1
}
120

            
121
#[test]
122
1
fn the_window_stays_put_while_the_selection_moves_inside_it() {
123
1
    let items = labels(20);
124
1
    let area = Rect::new(0, 0, 10, 5);
125
1
    let mut state = ListState::default().with_selected(Some(0));
126
1
    drawn_in(&items, &mut state, area);
127
1
    for selected in [1, 2, 1] {
128
3
        state.select(Some(selected));
129
3
        let buf = drawn_in(&items, &mut state, area);
130
3
        assert_eq!(state.offset(), 0);
131
3
        assert_eq!(cells(&buf, 1, 0, 10), "│ Opt0   ▲");
132
    }
133
1
}
134

            
135
#[test]
136
1
fn the_window_scrolls_by_the_minimum_past_either_edge() {
137
1
    let items = labels(20);
138
1
    let area = Rect::new(0, 0, 10, 5);
139
1
    let mut state = ListState::default().with_selected(Some(0));
140
1
    drawn_in(&items, &mut state, area);
141
1
    state.select(Some(3));
142
1
    drawn_in(&items, &mut state, area);
143
1
    assert_eq!(state.offset(), 1);
144
1
    state.select(Some(2));
145
1
    drawn_in(&items, &mut state, area);
146
1
    assert_eq!(state.offset(), 1);
147
1
    state.select(Some(0));
148
1
    drawn_in(&items, &mut state, area);
149
1
    assert_eq!(state.offset(), 0);
150
1
}
151

            
152
#[test]
153
1
fn an_offset_past_the_last_page_is_pulled_back_to_it() {
154
1
    let items = labels(6);
155
1
    let mut state = ListState::default().with_offset(5).with_selected(Some(5));
156
1
    let buf = drawn_in(&items, &mut state, Rect::new(0, 0, 10, 5));
157
1
    assert_eq!(state.offset(), 3);
158
1
    assert_eq!(cells(&buf, 3, 1, 9), " Opt5   ");
159
1
}
160

            
161
#[test]
162
1
fn the_scrollbar_shows_on_the_right_border_only_when_items_exceed_the_rows() {
163
1
    let theme = theme();
164
1
    let area = Rect::new(0, 0, 10, 5);
165
1
    let long = drawn(&labels(20), 0, area);
166
1
    assert_eq!(long[(9, 1)].symbol(), "\u{25B2}");
167
1
    assert_eq!(long[(9, 3)].symbol(), "\u{25BC}");
168
1
    assert_eq!(
169
1
        long[(9, 2)].style().fg,
170
1
        theme.style(Role::ScrollbarThumb).fg
171
    );
172
1
    assert_eq!(
173
1
        long[(9, 2)].style().bg,
174
1
        theme.style(Role::ScrollbarThumb).bg
175
    );
176
1
    let short = drawn(&labels(3), 0, area);
177
1
    assert_eq!(short[(9, 1)].symbol(), "\u{2502}");
178
1
    assert_eq!(short[(9, 2)].symbol(), "\u{2502}");
179
1
}
180

            
181
#[test]
182
1
fn the_scrollbar_thumb_follows_the_window() {
183
1
    let theme = theme();
184
1
    let area = Rect::new(0, 0, 10, 9);
185
9
    let thumb = |buf: &Buffer| (2..7).find(|&y| buf[(9, y)].symbol() != "\u{2592}");
186
1
    let top = drawn(&labels(20), 0, area);
187
1
    let end = drawn(&labels(20), 19, area);
188
1
    assert!(
189
1
        thumb(&top) < thumb(&end),
190
        "{:?} {:?}",
191
        thumb(&top),
192
        thumb(&end)
193
    );
194
1
    let y = thumb(&end).expect("the thumb is drawn");
195
1
    let thumb = theme.style(Role::ScrollbarThumb);
196
1
    assert_eq!(end[(9, y)].style().bg, thumb.bg);
197
1
    assert_eq!(end[(9, y)].style().fg, thumb.fg);
198
1
}
199

            
200
#[test]
201
1
fn the_rows_are_the_area_inside_the_border() {
202
1
    assert_eq!(
203
1
        DropDown::rows(Rect::new(2, 1, 10, 5)),
204
1
        Rect::new(3, 2, 8, 3)
205
    );
206
1
}
207

            
208
#[test]
209
1
fn the_option_rows_are_the_rows_inside_the_border_numbered_from_the_top() {
210
1
    let area = Rect::new(2, 3, 10, 5);
211
1
    assert_eq!(
212
1
        DropDown::option_rows(area, 4, 20),
213
1
        [
214
1
            (4, Rect::new(3, 4, 8, 1)),
215
1
            (5, Rect::new(3, 5, 8, 1)),
216
1
            (6, Rect::new(3, 6, 8, 1)),
217
1
        ]
218
    );
219
1
}
220

            
221
#[test]
222
1
fn a_short_list_has_a_row_per_option_and_none_past_the_end() {
223
1
    let area = Rect::new(0, 0, 10, 8);
224
1
    assert_eq!(DropDown::option_rows(area, 0, 2).len(), 2);
225
1
    assert_eq!(DropDown::option_rows(area, 5, 6).len(), 1);
226
1
    assert!(DropDown::option_rows(area, 6, 6).is_empty());
227
1
}
228

            
229
#[test]
230
1
fn the_option_rows_match_the_rows_the_list_draws() {
231
1
    let items = labels(10);
232
1
    let area = Rect::new(2, 1, 12, 6);
233
1
    let mut state = ListState::default().with_selected(Some(7));
234
1
    let buf = drawn_in(&items, &mut state, area);
235
1
    for (index, row) in DropDown::option_rows(area, state.offset(), items.len()) {
236
4
        let text = cells(&buf, row.y, row.x, row.right());
237
4
        assert!(
238
4
            text.trim_start().starts_with(&items[index]),
239
            "{index} {text:?}"
240
        );
241
    }
242
1
}