1
use ratatui::style::Color;
2
use ratatui::widgets::StatefulWidget;
3

            
4
use super::*;
5

            
6
const PRESETS: [&str; 2] = ["dn", "turbo"];
7

            
8
238
fn theme(preset: &str) -> Theme {
9
238
    Theme::preset(preset).expect("the preset parses")
10
238
}
11

            
12
236
fn render(line: InputLine<'_>, width: u16, from: Viewport) -> (Buffer, Viewport) {
13
236
    let mut buf = Buffer::empty(Rect::new(0, 0, width, 1));
14
236
    let mut viewport = from;
15
236
    line.render(buf.area, &mut buf, &mut viewport);
16
236
    (buf, viewport)
17
236
}
18

            
19
228
fn drawn_from(
20
228
    text: &str,
21
228
    cursor: usize,
22
228
    width: u16,
23
228
    focused: bool,
24
228
    from: Viewport,
25
228
) -> (Buffer, Viewport) {
26
228
    let theme = theme("dn");
27
228
    render(InputLine::new(&theme, text, cursor, focused), width, from)
28
228
}
29

            
30
13
fn drawn(text: &str, cursor: usize, width: u16, focused: bool) -> Buffer {
31
13
    drawn_from(text, cursor, width, focused, Viewport::default()).0
32
13
}
33

            
34
204
fn scrolled(text: &str, cursor: usize, width: u16) -> Viewport {
35
204
    drawn_from(text, cursor, width, false, Viewport::default()).1
36
204
}
37

            
38
23
fn row(buf: &Buffer) -> String {
39
23
    (0..buf.area.width)
40
133
        .map(|x| buf[(x, 0)].symbol().to_string())
41
        .collect()
42
23
}
43

            
44
12
fn col(text: &str, cursor: usize, width: u16) -> u16 {
45
12
    let viewport = scrolled(text, cursor, width);
46
12
    InputLine::cursor(Rect::new(3, 5, width, 1), text, cursor, viewport).x - 3
47
12
}
48

            
49
#[test]
50
1
fn short_text_starts_one_cell_in_and_the_cursor_follows_it() {
51
1
    assert_eq!(row(&drawn("abc", 3, 8, false)), " abc    ");
52
1
    assert_eq!(col("abc", 3, 8), 4);
53
1
    assert_eq!(col("abc", 1, 8), 2);
54
1
}
55

            
56
#[test]
57
1
fn empty_text_puts_the_cursor_after_the_margin() {
58
1
    assert_eq!(row(&drawn("", 0, 4, true)), "    ");
59
1
    assert_eq!(col("", 0, 4), 1);
60
1
}
61

            
62
#[test]
63
1
fn text_exactly_filling_the_row_leaves_the_cursor_on_the_last_cell_unscrolled() {
64
1
    assert_eq!(row(&drawn("abcd", 4, 6, true)), " abcd ");
65
1
    assert_eq!(col("abcd", 4, 6), 5);
66
1
    assert_eq!(row(&drawn("abcde", 5, 6, true)), "\u{25c4}bcde ");
67
1
    assert_eq!(col("abcde", 5, 6), 5);
68
1
}
69

            
70
#[test]
71
1
fn wide_characters_count_two_columns() {
72
1
    assert_eq!(col("日本語", 2, 10), 5);
73
1
    assert_eq!(col("日本語", 3, 6), 5);
74
1
    let buf = drawn("日本語", 3, 6, true);
75
1
    assert_eq!(buf[(1, 0)].symbol(), "本");
76
1
    assert_eq!(buf[(3, 0)].symbol(), "語");
77
1
}
78

            
79
#[test]
80
1
fn a_wide_character_that_does_not_fit_is_left_out() {
81
1
    assert_eq!(row(&drawn("a日", 0, 2, false)), "a ");
82
1
    assert_eq!(row(&drawn("ab日", 0, 5, false)), " ab \u{25ba}");
83
1
}
84

            
85
#[test]
86
1
fn a_one_cell_area_keeps_the_cursor_in_it() {
87
1
    assert_eq!(col("abc", 3, 1), 0);
88
1
    assert_eq!(row(&drawn("abc", 3, 1, true)), " ");
89
1
    assert_eq!(row(&drawn("abc", 3, 2, true)), "c ");
90
1
}
91

            
92
#[test]
93
1
fn an_empty_area_draws_nothing_and_keeps_the_cursor_at_its_edge() {
94
1
    assert_eq!(col("abc", 3, 0), 0);
95
1
    let theme = theme("dn");
96
1
    let mut buf = Buffer::empty(Rect::new(0, 0, 3, 1));
97
1
    let mut viewport = Viewport::default();
98
1
    InputLine::new(&theme, "abc", 3, true).render(Rect::new(0, 0, 0, 1), &mut buf, &mut viewport);
99
1
    assert_eq!(row(&buf), "   ");
100
1
}
101

            
102
#[test]
103
1
fn the_cursor_is_placed_inside_the_area_rows_and_columns() {
104
1
    let at = InputLine::cursor(Rect::new(10, 4, 6, 1), "ab", 2, Viewport::default());
105
1
    assert_eq!((at.x, at.y), (13, 4));
106
1
}
107

            
108
#[test]
109
1
fn a_combining_mark_joins_the_cell_before_it() {
110
1
    let buf = drawn("e\u{301}x", 0, 6, false);
111
1
    assert_eq!(buf[(1, 0)].symbol(), "e\u{301}");
112
1
    assert_eq!(buf[(2, 0)].symbol(), "x");
113
1
    assert_eq!(col("e\u{301}x", 2, 6), 2);
114
1
}
115

            
116
#[test]
117
1
fn a_huge_text_does_not_overflow_the_column_sum() {
118
1
    let text = "日".repeat(40_000);
119
1
    assert!(col(&text, 40_000, 10) < 10);
120
1
}
121

            
122
#[test]
123
1
fn a_control_character_is_drawn_as_a_blank_one_cell_wide() {
124
1
    assert_eq!(row(&drawn("a\tb", 0, 8, false)), " a b    ");
125
1
    assert_eq!(col("a\tb", 3, 8), 4);
126
1
}
127

            
128
10
fn at(text: &str, cursor: usize, width: u16, x: u16) -> usize {
129
10
    let area = Rect::new(3, 5, width, 1);
130
10
    let viewport = scrolled(text, cursor, width);
131
10
    InputLine::position_at(area, text, viewport, Position::new(3 + x, 5))
132
10
}
133

            
134
#[test]
135
1
fn a_click_lands_on_the_character_under_it() {
136
1
    assert_eq!(at("abcdef", 0, 8, 1), 0);
137
1
    assert_eq!(at("abcdef", 0, 8, 4), 3);
138
1
}
139

            
140
#[test]
141
1
fn a_click_past_the_end_lands_at_the_end() {
142
1
    assert_eq!(at("abc", 3, 8, 7), 3);
143
1
    assert_eq!(at("", 0, 8, 5), 0);
144
1
}
145

            
146
#[test]
147
1
fn a_click_left_of_the_area_lands_on_the_first_visible_character() {
148
1
    let area = Rect::new(3, 5, 8, 1);
149
1
    assert_eq!(
150
1
        InputLine::position_at(area, "abc", Viewport::default(), Position::new(0, 5)),
151
1
        0
152
    );
153
1
}
154

            
155
#[test]
156
1
fn a_click_in_a_scrolled_field_counts_from_the_scroll() {
157
1
    let text = "abcdefghij";
158
1
    assert_eq!(at(text, 10, 6, 1), 6);
159
1
    assert_eq!(at(text, 10, 6, 3), 8);
160
1
    assert_eq!(at(text, 10, 6, 5), 10);
161
1
}
162

            
163
#[test]
164
1
fn a_wide_character_takes_a_click_on_either_of_its_cells() {
165
1
    let text = "a\u{4e2d}b";
166
1
    assert_eq!(at(text, 0, 8, 2), 1);
167
1
    assert_eq!(at(text, 0, 8, 3), 1);
168
1
    assert_eq!(at(text, 0, 8, 4), 2);
169
1
}
170

            
171
#[test]
172
1
fn position_at_is_the_inverse_of_cursor() {
173
1
    for text in [
174
1
        "",
175
1
        "abc",
176
1
        "abcdefghijkl",
177
1
        "a\u{4e2d}b\u{4e2d}cdefg",
178
1
        "\u{4e2d}\u{4e2d}\u{4e2d}\u{4e2d}",
179
1
    ] {
180
5
        for width in [1, 2, 3, 5, 8] {
181
25
            for cursor in 0..=text.chars().count() {
182
165
                let area = Rect::new(3, 5, width, 1);
183
165
                let viewport = scrolled(text, cursor, width);
184
165
                let point = InputLine::cursor(area, text, cursor, viewport);
185
165
                let found = InputLine::position_at(area, text, viewport, point);
186
165
                assert_eq!(found, cursor, "{text:?} width {width} cursor {cursor}");
187
            }
188
        }
189
    }
190
1
}
191

            
192
#[test]
193
1
fn the_scroll_stays_while_the_cursor_moves_inside_the_view() {
194
1
    let text = "abcdefghijklmnop";
195
1
    let (_, scrolled) = drawn_from(text, 16, 8, true, Viewport::default());
196
1
    assert_eq!(scrolled.left, 10);
197
1
    let (buf, kept) = drawn_from(text, 13, 8, true, scrolled);
198
1
    assert_eq!(kept, scrolled);
199
1
    assert_eq!(row(&buf), "\u{25c4}klmnop ");
200
1
}
201

            
202
#[test]
203
1
fn the_scroll_moves_the_least_to_bring_the_cursor_back() {
204
1
    let text = "abcdefghijklmnop";
205
1
    let from = Viewport { top: 0, left: 11 };
206
1
    let (buf, moved) = drawn_from(text, 8, 8, true, from);
207
1
    assert_eq!(moved.left, 8);
208
1
    assert_eq!(row(&buf), "\u{25c4}ijklmn\u{25ba}");
209
1
}
210

            
211
#[test]
212
1
fn a_click_in_a_scrolled_field_keeps_the_view_when_drawn_again() {
213
1
    let text = "abcdefghijklmnop";
214
1
    let area = Rect::new(3, 5, 8, 1);
215
1
    let (_, viewport) = drawn_from(text, 16, 8, true, Viewport::default());
216
1
    let clicked = InputLine::position_at(area, text, viewport, Position::new(3 + 3, 5));
217
1
    assert_eq!(clicked, 12);
218
1
    let (_, again) = drawn_from(text, clicked, 8, true, viewport);
219
1
    assert_eq!(again, viewport);
220
1
    assert_eq!(InputLine::cursor(area, text, clicked, again).x, 3 + 3);
221
1
}
222

            
223
18
fn style_of(buf: &Buffer, x: u16) -> (Option<Color>, Option<Color>) {
224
18
    let style = buf[(x, 0)].style();
225
18
    (style.fg, style.bg)
226
18
}
227

            
228
14
fn want(theme: &Theme, role: Role) -> (Option<Color>, Option<Color>) {
229
14
    let style = theme.style(role);
230
14
    (style.fg, style.bg)
231
14
}
232

            
233
5
fn long(cursor: usize, left: usize, width: u16, preset: &str) -> (Buffer, Theme) {
234
5
    let theme = theme(preset);
235
5
    let line = InputLine::new(&theme, "abcdefghij", cursor, true);
236
5
    let (buf, _) = render(line, width, Viewport { top: 0, left });
237
5
    (buf, theme)
238
5
}
239

            
240
#[test]
241
1
fn an_empty_field_is_all_field_colour_in_both_presets() {
242
1
    for preset in PRESETS {
243
2
        let theme = theme(preset);
244
2
        let line = InputLine::new(&theme, "", 0, true);
245
2
        let (buf, _) = render(line, 6, Viewport::default());
246
2
        assert_eq!(row(&buf), "      ", "{preset}");
247
2
        for x in 0..6 {
248
12
            assert_eq!(
249
12
                style_of(&buf, x),
250
12
                want(&theme, Role::InputFocused),
251
                "{preset} {x}"
252
            );
253
        }
254
    }
255
1
}
256

            
257
4
fn arrow_style(theme: &Theme) -> (Option<Color>, Option<Color>) {
258
4
    (
259
4
        theme.style(Role::InputArrow).fg,
260
4
        theme.style(Role::InputFocused).bg,
261
4
    )
262
4
}
263

            
264
#[test]
265
1
fn the_left_arrow_marks_text_scrolled_off_the_left() {
266
1
    for preset in PRESETS {
267
2
        let (buf, theme) = long(10, 7, 6, preset);
268
2
        assert_eq!(row(&buf), "\u{25c4}ghij ", "{preset}");
269
2
        assert_eq!(style_of(&buf, 0), arrow_style(&theme), "{preset}");
270
2
        assert_eq!(style_of(&buf, 1), want(&theme, Role::InputFocused));
271
    }
272
1
}
273

            
274
#[test]
275
1
fn the_right_arrow_marks_text_running_off_the_right() {
276
1
    for preset in PRESETS {
277
2
        let (buf, theme) = long(0, 0, 6, preset);
278
2
        assert_eq!(row(&buf), " abcd\u{25ba}", "{preset}");
279
2
        assert_eq!(style_of(&buf, 5), arrow_style(&theme), "{preset}");
280
    }
281
1
}
282

            
283
#[test]
284
1
fn both_arrows_show_while_text_runs_off_both_sides() {
285
1
    let (buf, _) = long(5, 0, 6, "turbo");
286
1
    assert_eq!(row(&buf), "\u{25c4}bcde\u{25ba}");
287
1
}
288

            
289
#[test]
290
1
fn text_that_just_fits_shows_no_arrow() {
291
1
    let theme = theme("dn");
292
1
    let line = InputLine::new(&theme, "abcd", 0, true);
293
1
    let (buf, _) = render(line, 6, Viewport::default());
294
1
    assert_eq!(row(&buf), " abcd ");
295
1
}
296

            
297
#[test]
298
1
fn the_cursor_stays_between_the_margin_and_the_last_cell_at_both_ends_of_the_scroll() {
299
1
    let text = "abcdefghij";
300
1
    let area = Rect::new(0, 0, 6, 1);
301
1
    let (_, right) = drawn_from(text, 10, 6, true, Viewport::default());
302
1
    assert_eq!(InputLine::cursor(area, text, 10, right).x, 5);
303
1
    let (_, left) = drawn_from(text, 6, 6, true, right);
304
1
    assert_eq!(left.left, 6);
305
1
    assert_eq!(InputLine::cursor(area, text, 6, left).x, 1);
306
1
    let (_, back) = drawn_from(text, 5, 6, true, left);
307
1
    assert_eq!(InputLine::cursor(area, text, 5, back).x, 1);
308
1
}
309

            
310
#[test]
311
1
fn the_cursor_never_leaves_the_text_cells() {
312
1
    let text = "abcdefghijklmnop";
313
1
    for cursor in 0..=text.len() {
314
17
        let area = Rect::new(0, 0, 7, 1);
315
17
        let viewport = scrolled(text, cursor, 7);
316
17
        let x = InputLine::cursor(area, text, cursor, viewport).x;
317
17
        assert!((1..=6).contains(&x), "cursor {cursor} at {x}");
318
    }
319
1
}
320

            
321
#[test]
322
1
fn a_field_too_narrow_for_two_arrows_draws_none() {
323
1
    assert_eq!(row(&drawn("abcdef", 6, 2, true)), "f ");
324
1
    assert_eq!(row(&drawn("abcdef", 0, 2, true)), "ab");
325
1
}
326

            
327
1
fn typed_then_erased() -> (Buffer, Viewport) {
328
1
    let theme = theme("dn");
329
1
    let full = "abcdefghijklmnopqrstuvwxyz0123456789ABCD";
330
1
    let (_, scrolled) = drawn_from(full, 40, 20, true, Viewport::default());
331
1
    let kept = &full[..22];
332
1
    let mut buf = Buffer::empty(Rect::new(0, 0, 20, 1));
333
1
    let mut viewport = scrolled;
334
1
    InputLine::new(&theme, kept, 22, true).render(buf.area, &mut buf, &mut viewport);
335
1
    (buf, viewport)
336
1
}
337

            
338
#[test]
339
1
fn erasing_scrolls_the_text_back_so_it_fills_the_field() {
340
1
    let (buf, viewport) = typed_then_erased();
341
1
    assert_eq!(viewport.left, 4);
342
1
    assert_eq!(row(&buf), "\u{25c4}efghijklmnopqrstuv ");
343
1
}
344

            
345
#[test]
346
1
fn a_wide_character_under_the_cursor_at_the_right_edge_is_drawn_whole() {
347
1
    let (buf, viewport) = drawn_from("abcde\u{4e2d}", 5, 8, true, Viewport::default());
348
1
    assert_eq!(viewport.left, 1);
349
1
    assert_eq!(buf[(5, 0)].symbol(), "\u{4e2d}");
350
1
    let (buf, viewport) = drawn_from("abcdef\u{4e2d}", 6, 8, true, Viewport::default());
351
1
    assert_eq!(viewport.left, 2);
352
1
    assert_eq!(buf[(5, 0)].symbol(), "\u{4e2d}");
353
1
}