1
use ratatui::style::Color;
2

            
3
use super::*;
4

            
5
35
fn theme() -> Theme {
6
35
    Theme::preset("dn").expect("the dn preset parses")
7
35
}
8

            
9
const AREA: Rect = Rect::new(2, 3, 4, 3);
10

            
11
15
fn view(lines: &[&str], cursor: (usize, usize), previous: (usize, usize)) -> (usize, usize) {
12
15
    let previous = Viewport {
13
15
        top: previous.0,
14
15
        left: previous.1,
15
15
    };
16
15
    let at = follow_cursor(AREA, lines, cursor, previous);
17
15
    (at.top, at.left)
18
15
}
19

            
20
9
fn render(lines: &[&str], cursor: (usize, usize), state: &mut Viewport, focused: bool) -> Buffer {
21
9
    let theme = theme();
22
9
    let mut buf = Buffer::empty(Rect::new(0, 0, 4, 3));
23
9
    TextView::new(&theme, lines, cursor, focused).render(buf.area, &mut buf, state);
24
9
    buf
25
9
}
26

            
27
/// The lines drawn with the view starting at `viewport` and the cursor inside it.
28
5
fn drawn(lines: &[&str], viewport: (usize, usize), focused: bool) -> Buffer {
29
5
    let mut state = Viewport {
30
5
        top: viewport.0,
31
5
        left: viewport.1,
32
5
    };
33
    let cursor = (
34
5
        viewport.0,
35
5
        lines.get(viewport.0).map_or(0, |line| line.chars().count()),
36
    );
37
5
    let cursor = (cursor.0, cursor.1.min(viewport.1));
38
5
    render(lines, cursor, &mut state, focused)
39
5
}
40

            
41
10
fn row(buf: &Buffer, y: u16) -> String {
42
10
    (0..buf.area.width)
43
40
        .map(|x| buf[(x, y)].symbol().to_string())
44
        .collect()
45
10
}
46

            
47
const TEN: [&str; 10] = ["a"; 10];
48

            
49
#[test]
50
1
fn a_cursor_inside_the_view_leaves_it_where_it_was() {
51
1
    assert_eq!(view(&TEN, (4, 0), (2, 0)), (2, 0));
52
1
    assert_eq!(view(&["abcdefgh"], (0, 5), (0, 3)), (0, 3));
53
1
}
54

            
55
#[test]
56
1
fn a_cursor_below_or_above_scrolls_the_least() {
57
1
    assert_eq!(view(&TEN, (3, 0), (0, 0)), (1, 0));
58
1
    assert_eq!(view(&TEN, (9, 0), (0, 0)), (7, 0));
59
1
    assert_eq!(view(&TEN, (1, 0), (4, 0)), (1, 0));
60
1
}
61

            
62
#[test]
63
1
fn a_cursor_right_or_left_of_the_view_scrolls_the_least() {
64
1
    let lines = ["abcdefgh"];
65
1
    assert_eq!(view(&lines, (0, 4), (0, 0)), (0, 1));
66
1
    assert_eq!(view(&lines, (0, 8), (0, 0)), (0, 5));
67
1
    assert_eq!(view(&lines, (0, 1), (0, 5)), (0, 1));
68
1
}
69

            
70
#[test]
71
1
fn wide_characters_count_two_columns() {
72
1
    assert_eq!(view(&["日本語"], (0, 3), (0, 0)), (0, 3));
73
1
    let at = TextView::cursor(AREA, &["日本語"], (0, 2), Viewport::default());
74
1
    assert_eq!((at.x, at.y), (6, 3));
75
1
}
76

            
77
#[test]
78
1
fn empty_text_and_a_row_past_the_lines_stay_at_the_origin() {
79
1
    assert_eq!(view(&[], (0, 0), (0, 0)), (0, 0));
80
1
    assert_eq!(view(&[""], (0, 0), (0, 0)), (0, 0));
81
1
}
82

            
83
#[test]
84
1
fn a_tiny_area_keeps_the_cursor_in_it() {
85
1
    let area = Rect::new(0, 0, 0, 0);
86
1
    let at = follow_cursor(area, &["abc", "d"], (1, 1), Viewport::default());
87
1
    assert_eq!(at, Viewport { top: 1, left: 1 });
88
1
}
89

            
90
#[test]
91
1
fn the_cursor_cell_is_relative_to_the_view() {
92
1
    let lines = ["ab", "cdefgh", "ij"];
93
1
    let viewport = Viewport { top: 1, left: 2 };
94
1
    let at = TextView::cursor(AREA, &lines, (1, 5), viewport);
95
1
    assert_eq!((at.x, at.y), (5, 3));
96
1
    let at = TextView::cursor(AREA, &lines, (2, 0), Viewport::default());
97
1
    assert_eq!((at.x, at.y), (2, 5));
98
1
}
99

            
100
#[test]
101
1
fn the_lines_show_through_the_view() {
102
1
    let lines = ["abcdef", "gh", "ij", "kl"];
103
1
    let buf = drawn(&lines, (1, 0), false);
104
1
    assert_eq!([row(&buf, 0), row(&buf, 1)], ["gh  ", "ij  "]);
105
1
    let buf = drawn(&lines, (0, 2), false);
106
1
    assert_eq!(row(&buf, 0), "cdef");
107
1
    assert_eq!(row(&buf, 1), "    ");
108
1
}
109

            
110
#[test]
111
1
fn a_wide_character_cut_at_the_left_leaves_no_half_of_it() {
112
1
    let buf = drawn(&["日本x"], (0, 1), false);
113
1
    assert!(!row(&buf, 0).contains('日'), "{:?}", row(&buf, 0));
114
1
    assert!(row(&buf, 0).contains('本'), "{:?}", row(&buf, 0));
115
1
}
116

            
117
4
fn colours(buf: &Buffer, x: u16, y: u16) -> (Option<Color>, Option<Color>) {
118
4
    let style = buf[(x, y)].style();
119
4
    (style.fg, style.bg)
120
4
}
121

            
122
#[test]
123
1
fn text_and_empty_cells_use_the_input_roles() {
124
1
    let theme = theme();
125
4
    let want = |role| (theme.style(role).fg, theme.style(role).bg);
126
1
    let buf = drawn(&["a"], (0, 0), false);
127
1
    assert_eq!(colours(&buf, 0, 0), want(Role::Input));
128
1
    assert_eq!(colours(&buf, 3, 2), want(Role::Input));
129
1
    let buf = drawn(&["a"], (0, 0), true);
130
1
    assert_eq!(colours(&buf, 0, 0), want(Role::InputFocused));
131
1
    assert_eq!(colours(&buf, 3, 2), want(Role::InputFocused));
132
1
}
133

            
134
#[test]
135
1
fn rendering_follows_the_cursor_and_keeps_the_state() {
136
1
    let lines = ["abcdefgh", "x", "y", "z"];
137
1
    let mut state = Viewport::default();
138
1
    let buf = render(&lines, (0, 7), &mut state, true);
139
1
    assert_eq!(state, Viewport { top: 0, left: 4 });
140
1
    assert_eq!(row(&buf, 0), "efgh");
141
1
    let buf = render(&lines, (3, 0), &mut state, true);
142
1
    assert_eq!(state, Viewport { top: 1, left: 0 });
143
1
    assert_eq!(row(&buf, 0), "x   ");
144
1
}
145

            
146
#[test]
147
1
fn a_wide_character_under_the_cursor_is_kept_whole_in_view() {
148
1
    assert_eq!(view(&["abc日"], (0, 3), (0, 0)), (0, 1));
149
1
    assert_eq!(view(&["abc日"], (0, 3), (0, 3)), (0, 3));
150
1
    let mut state = Viewport::default();
151
1
    let buf = render(&["abc日"], (0, 3), &mut state, true);
152
1
    assert_eq!(row(&buf, 0), "bc日 ");
153
1
}
154

            
155
#[test]
156
1
fn a_character_wider_than_the_box_starts_at_its_edge() {
157
1
    let tiny = Rect::new(0, 0, 1, 1);
158
1
    assert_eq!(
159
1
        follow_cursor(tiny, &["a日"], (0, 1), Viewport::default()).left,
160
1
        1
161
    );
162
1
}
163

            
164
#[test]
165
1
fn the_top_is_pulled_back_when_the_text_got_shorter() {
166
1
    assert_eq!(view(&["a", "b", "c", "d"], (3, 0), (7, 0)), (1, 0));
167
1
    assert_eq!(view(&["a"], (0, 0), (5, 0)), (0, 0));
168
1
}
169

            
170
#[test]
171
1
fn control_characters_are_drawn_as_blanks() {
172
1
    let mut state = Viewport::default();
173
1
    let buf = render(&["a\tb\u{7f}c"], (0, 0), &mut state, false);
174
1
    assert_eq!(row(&buf, 0), "a b ");
175
1
    let at = TextView::cursor(AREA, &["a\tb"], (0, 3), Viewport::default());
176
1
    assert_eq!(at.x, AREA.x + 3);
177
1
}
178

            
179
9
fn at(lines: &[&str], viewport: (usize, usize), x: u16, y: u16) -> (usize, usize) {
180
9
    let viewport = Viewport {
181
9
        top: viewport.0,
182
9
        left: viewport.1,
183
9
    };
184
9
    TextView::position_at(AREA, lines, viewport, Position::new(AREA.x + x, AREA.y + y))
185
9
}
186

            
187
#[test]
188
1
fn a_click_lands_on_its_line_and_character() {
189
1
    let lines = ["abcd", "efgh", "ij"];
190
1
    assert_eq!(at(&lines, (0, 0), 2, 1), (1, 2));
191
1
    assert_eq!(at(&lines, (0, 0), 0, 0), (0, 0));
192
1
}
193

            
194
#[test]
195
1
fn a_click_past_a_line_end_lands_at_its_end() {
196
1
    let lines = ["abcd", "ij"];
197
1
    assert_eq!(at(&lines, (0, 0), 3, 1), (1, 2));
198
1
}
199

            
200
#[test]
201
1
fn a_click_below_the_text_lands_on_the_last_line() {
202
1
    let lines = ["abcd", "ij"];
203
1
    assert_eq!(at(&lines, (0, 0), 1, 2), (1, 1));
204
1
    assert_eq!(at(&[], (0, 0), 1, 2), (0, 0));
205
1
}
206

            
207
#[test]
208
1
fn a_click_in_a_scrolled_view_counts_from_the_viewport() {
209
1
    assert_eq!(at(&TEN, (4, 0), 0, 2), (6, 0));
210
1
    assert_eq!(at(&["abcdefgh"], (0, 3), 1, 0), (0, 4));
211
1
}
212

            
213
#[test]
214
1
fn a_wide_character_takes_a_click_on_either_of_its_cells() {
215
1
    let lines = ["a\u{4e2d}b"];
216
1
    assert_eq!(at(&lines, (0, 0), 2, 0), (0, 1));
217
1
    assert_eq!(at(&lines, (0, 1), 1, 0), (0, 1));
218
1
}
219

            
220
#[test]
221
1
fn position_at_is_the_inverse_of_cursor() {
222
1
    let lines = [
223
1
        "abcdefgh",
224
1
        "\u{4e2d}\u{4e2d}\u{4e2d}x",
225
1
        "",
226
1
        "ij",
227
1
        "klmn",
228
1
        "o",
229
1
    ];
230
1
    for row in 0..lines.len() {
231
6
        for col in 0..=lines[row].chars().count() {
232
25
            let mut viewport = Viewport::default();
233
25
            let theme = theme();
234
25
            let mut buf = Buffer::empty(AREA);
235
25
            TextView::new(&theme, &lines, (row, col), true).render(AREA, &mut buf, &mut viewport);
236
25
            let point = TextView::cursor(AREA, &lines, (row, col), viewport);
237
25
            let found = TextView::position_at(AREA, &lines, viewport, point);
238
25
            assert_eq!(found, (row, col), "{viewport:?}");
239
        }
240
    }
241
1
}