1
use super::*;
2

            
3
24
fn area(text: &str) -> Editor {
4
24
    Editor::with_buffer(text).multiline(true)
5
24
}
6

            
7
20
fn at(text: &str, cursor: usize) -> Editor {
8
20
    let mut editor = area(text);
9
20
    editor.cursor = cursor;
10
20
    editor
11
20
}
12

            
13
7
fn numbered(count: usize) -> String {
14
7
    (0..count)
15
350
        .map(|i| format!("l{i}"))
16
        .collect::<Vec<_>>()
17
7
        .join("\n")
18
7
}
19

            
20
#[test]
21
1
fn typing_newlines_builds_several_lines() {
22
1
    let mut editor = Editor::default().multiline(true);
23
1
    for c in "ab".chars() {
24
2
        editor.insert_char(c);
25
    }
26
1
    editor.insert_newline();
27
1
    editor.insert_char('c');
28
1
    assert_eq!(editor.buffer(), "ab\nc");
29
1
    assert_eq!(editor.text_lines(), ["ab", "c"]);
30
1
}
31

            
32
#[test]
33
1
fn a_single_line_editor_ignores_the_newline_and_moves_as_before() {
34
1
    let mut editor = Editor::with_buffer("ab\ncd");
35
1
    editor.insert_newline();
36
1
    editor.move_up();
37
1
    editor.page_down();
38
1
    assert_eq!(editor.buffer(), "ab\ncd");
39
1
    assert_eq!(editor.cursor(), 5);
40
1
    editor.move_home();
41
1
    assert_eq!(editor.cursor(), 0);
42
1
    editor.move_end();
43
1
    assert_eq!(editor.cursor(), 5);
44
1
}
45

            
46
#[test]
47
1
fn up_and_down_keep_the_column_and_clamp_on_short_lines() {
48
1
    let mut editor = at("abcdef\nab\nabcdef", 5);
49
1
    editor.move_down();
50
1
    assert_eq!(editor.cursor(), 9);
51
1
    editor.move_down();
52
1
    assert_eq!(editor.cursor(), 12);
53
1
    editor.move_up();
54
1
    editor.move_up();
55
1
    assert_eq!(editor.cursor(), 2);
56
1
}
57

            
58
#[test]
59
1
fn up_on_the_first_and_down_on_the_last_line_stay_put() {
60
1
    let mut editor = at("ab\ncd", 1);
61
1
    editor.move_up();
62
1
    assert_eq!(editor.cursor(), 1);
63
1
    editor.move_down();
64
1
    editor.move_down();
65
1
    assert_eq!(editor.cursor(), 4);
66
1
}
67

            
68
#[test]
69
1
fn home_and_end_go_to_the_line_edges() {
70
1
    let mut editor = at("ab\ncde\nf", 4);
71
1
    editor.move_home();
72
1
    assert_eq!(editor.cursor(), 3);
73
1
    editor.move_end();
74
1
    assert_eq!(editor.cursor(), 6);
75
1
}
76

            
77
#[test]
78
1
fn wide_characters_keep_the_display_column() {
79
1
    let mut editor = at("日本語\nabcdef", 2);
80
1
    editor.move_down();
81
1
    assert_eq!(editor.cursor(), 4 + 4);
82
1
    editor.move_up();
83
1
    assert_eq!(editor.cursor(), 2);
84
1
}
85

            
86
#[test]
87
1
fn a_target_wide_character_straddling_the_column_is_not_entered() {
88
1
    let mut editor = at("abc\n日本", 3);
89
1
    editor.move_down();
90
1
    assert_eq!(editor.cursor(), 5);
91
1
}
92

            
93
#[test]
94
1
fn multi_byte_characters_count_as_one_cursor_step() {
95
1
    let mut editor = at("é\nüber", 1);
96
1
    editor.move_down();
97
1
    assert_eq!(editor.cursor(), 3);
98
1
    assert_eq!(editor.cursor_position(), (1, 1));
99
1
}
100

            
101
#[test]
102
1
fn the_lines_and_the_cursor_position_follow_the_text() {
103
1
    let mut editor = at(&numbered(50), 0);
104
1
    assert_eq!(editor.text_lines().len(), 50);
105
1
    assert_eq!(editor.text_lines()[49], "l49");
106
1
    assert_eq!(editor.cursor_position(), (0, 0));
107
1
    editor.move_down();
108
1
    editor.move_right();
109
1
    assert_eq!(editor.cursor_position(), (1, 1));
110
1
    assert_eq!(
111
1
        at(&numbered(50), numbered(50).chars().count()).cursor_position(),
112
1
        (49, 3)
113
    );
114
1
}
115

            
116
#[test]
117
1
fn page_keys_move_a_window_at_a_time() {
118
1
    let mut editor = at(&numbered(50), 0);
119
1
    editor.page_down();
120
1
    assert_eq!(editor.cursor_position().0, TEXT_AREA_ROWS);
121
1
    editor.page_up();
122
1
    editor.page_up();
123
1
    assert_eq!(editor.cursor(), 0);
124
1
}
125

            
126
#[test]
127
1
fn the_scroll_is_kept_between_reads_and_a_clone_has_its_own() {
128
1
    let editor = area("a");
129
1
    assert_eq!(editor.viewport(), tvui::Viewport::default());
130
1
    editor.set_viewport(tvui::Viewport { top: 2, left: 3 });
131
1
    assert_eq!(editor.viewport(), tvui::Viewport { top: 2, left: 3 });
132
1
    let copy = editor.clone();
133
1
    copy.set_viewport(tvui::Viewport { top: 7, left: 0 });
134
1
    assert_eq!(editor.viewport(), tvui::Viewport { top: 2, left: 3 });
135
1
}
136

            
137
#[test]
138
1
fn a_loaded_multiline_text_starts_with_the_cursor_at_its_top() {
139
1
    let editor = area(&numbered(50));
140
1
    assert_eq!(editor.cursor_position(), (0, 0));
141
1
    assert_eq!(editor.text_lines().len(), 50);
142
1
}
143

            
144
#[test]
145
1
fn cursor_positions_count_crlf_lines_once() {
146
1
    let editor = at("ab\r\ncd", 5);
147
1
    assert_eq!(editor.cursor_position(), (1, 1));
148
1
    assert_eq!(editor.text_lines(), ["ab", "cd"]);
149
1
}
150

            
151
#[test]
152
1
fn moving_without_editing_leaves_the_text_byte_for_byte() {
153
1
    for text in [numbered(50), numbered(50).replace('\n', "\r\n")] {
154
2
        let mut editor = area(&text);
155
2
        for _ in 0..3 {
156
6
            editor.page_up();
157
6
            editor.move_home();
158
6
            editor.move_end();
159
6
            editor.move_left();
160
6
            editor.move_right();
161
6
            editor.move_down();
162
6
            editor.page_down();
163
        }
164
2
        assert_eq!(editor.buffer(), text);
165
    }
166
1
}
167

            
168
#[test]
169
1
fn the_cursor_never_rests_inside_a_crlf() {
170
1
    let mut editor = at("ab\r\ncd", 2);
171
1
    editor.move_right();
172
1
    assert_eq!(editor.cursor(), 4);
173
1
    editor.move_left();
174
1
    assert_eq!(editor.cursor(), 2);
175
1
    assert_eq!(editor.text_lines(), ["ab", "cd"]);
176
1
}
177

            
178
#[test]
179
1
fn deleting_across_a_crlf_removes_both_characters() {
180
1
    let mut editor = at("ab\r\ncd", 4);
181
1
    editor.delete_backward();
182
1
    assert_eq!(editor.buffer(), "abcd");
183

            
184
1
    let mut editor = at("ab\r\ncd", 2);
185
1
    editor.delete_forward();
186
1
    assert_eq!(editor.buffer(), "abcd");
187
1
}
188

            
189
#[test]
190
1
fn a_typed_newline_is_lf_in_a_crlf_text() {
191
1
    let mut editor = at("ab\r\ncd", 2);
192
1
    editor.insert_newline();
193
1
    assert_eq!(editor.buffer(), "ab\n\r\ncd");
194
1
}
195

            
196
#[test]
197
1
fn the_text_area_flag_survives_a_reload_of_its_text() {
198
1
    let mut widget = crate::widgets::Widget::Text(Editor::default().multiline(true));
199
1
    widget.set_text("a\nb");
200
1
    assert_eq!(widget.kind(), crate::widgets::WidgetKind::TextArea);
201
1
}
202

            
203
#[test]
204
1
fn kill_to_end_stops_at_the_line_end() {
205
1
    let mut editor = at("abcd\nef", 2);
206
1
    editor.kill_to_end();
207
1
    assert_eq!(editor.buffer(), "ab\nef");
208
1
    assert_eq!(editor.cursor(), 2);
209
1
}
210

            
211
#[test]
212
1
fn kill_to_end_at_a_line_end_joins_the_next_line() {
213
1
    for (text, cursor) in [("ab\ncd", 2), ("ab\r\ncd", 2)] {
214
2
        let mut editor = at(text, cursor);
215
2
        editor.kill_to_end();
216
2
        assert_eq!(editor.buffer(), "abcd", "{text:?}");
217
    }
218
1
}
219

            
220
#[test]
221
1
fn kill_to_end_on_the_last_line_deletes_the_rest_and_then_nothing() {
222
1
    let mut editor = at("ab\ncdef", 5);
223
1
    editor.kill_to_end();
224
1
    assert_eq!(editor.buffer(), "ab\ncd");
225
1
    editor.kill_to_end();
226
1
    assert_eq!(editor.buffer(), "ab\ncd");
227
1
}
228

            
229
#[test]
230
1
fn kill_to_end_of_a_single_line_editor_is_unchanged() {
231
1
    let mut editor = Editor::with_buffer("ab\ncd");
232
1
    editor.cursor = 1;
233
1
    editor.kill_to_end();
234
1
    assert_eq!(editor.buffer(), "a");
235
1
}
236

            
237
#[test]
238
1
fn set_cursor_position_puts_the_cursor_on_a_line_and_column() {
239
1
    for text in ["ab\ncdef\ngh", "ab\r\ncdef\r\ngh"] {
240
2
        let mut editor = at(text, 0);
241
2
        editor.set_cursor_position((1, 3));
242
2
        assert_eq!(editor.cursor_position(), (1, 3), "{text:?}");
243
2
        editor.set_cursor_position((0, 9));
244
2
        assert_eq!(editor.cursor_position(), (0, 2), "{text:?}");
245
2
        editor.set_cursor_position((9, 1));
246
2
        assert_eq!(editor.cursor_position(), (2, 1), "{text:?}");
247
    }
248
1
}