-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathvisual_mode.c
301 lines (237 loc) · 6.35 KB
/
visual_mode.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#include "libvim.h"
#include "minunit.h"
void test_setup(void)
{
vimKey("<esc>");
vimKey("<esc>");
vimExecute("e!");
vimInput("g");
vimInput("g");
vimInput("0");
}
void test_teardown(void) {}
MU_TEST(test_visual_is_active)
{
mu_check(vimVisualIsActive() == 0);
vimInput("v");
mu_check(vimVisualGetType() == 'v');
mu_check(vimVisualIsActive() == 1);
mu_check((vimGetMode() & VISUAL) == VISUAL);
vimKey("<esc>");
mu_check((vimGetMode() & NORMAL) == NORMAL);
mu_check(vimVisualIsActive() == 0);
vimKey("<c-v>");
mu_check(vimVisualGetType() == Ctrl_V);
mu_check(vimVisualIsActive() == 1);
mu_check((vimGetMode() & VISUAL) == VISUAL);
vimKey("<esc>");
mu_check((vimGetMode() & NORMAL) == NORMAL);
mu_check(vimVisualIsActive() == 0);
vimInput("V");
mu_check(vimVisualGetType() == 'V');
mu_check(vimVisualIsActive() == 1);
mu_check((vimGetMode() & VISUAL) == VISUAL);
}
MU_TEST(test_characterwise_range)
{
vimInput("v");
vimInput("l");
vimInput("l");
pos_T start;
pos_T end;
// Get current range
vimVisualGetRange(&start, &end);
mu_check(start.lnum == 1);
mu_check(start.col == 0);
mu_check(end.lnum == 1);
mu_check(end.col == 2);
vimKey("<esc>");
vimInput("j");
// Validate we still get previous range
vimVisualGetRange(&start, &end);
mu_check(start.lnum == 1);
mu_check(start.col == 0);
mu_check(end.lnum == 1);
mu_check(end.col == 2);
}
MU_TEST(test_ctrl_q)
{
vimKey("<c-q>");
mu_check((vimGetMode() & VISUAL) == VISUAL);
mu_check(vimVisualGetType() == Ctrl_V);
mu_check(vimVisualIsActive() == 1);
}
MU_TEST(test_ctrl_Q)
{
vimKey("<c-Q>");
mu_check((vimGetMode() & VISUAL) == VISUAL);
mu_check(vimVisualGetType() == Ctrl_V);
mu_check(vimVisualIsActive() == 1);
}
MU_TEST(test_insert_block_mode)
{
vimKey("<c-v>");
vimInput("j");
vimInput("j");
vimInput("j");
vimInput("I");
mu_check((vimGetMode() & INSERT) == INSERT);
vimInput("a");
vimInput("b");
vimInput("c");
}
/**
* This test case does a visual block select and then an "I" insert
* which should insert at the start of each line.
* this test fails and will be commented out.
*/
/*
MU_TEST(test_insert_block_mode_change)
{
char_u *lines[] = {"line1", "line2", "line3", "line4", "line5"};
vimBufferSetLines(curbuf, 0, 3, lines, 5);
vimInput("<c-v>");
vimInput("j");
vimInput("j");
vimInput("j");
vimInput("I");
vimInput("a");
vimInput("b");
vimInput("c");
vimInput("<esc>");
char_u *line = vimBufferGetLine(curbuf, 1);
mu_check(strcmp(line, "abcline1") == 0);
line = vimBufferGetLine(curbuf, 3);
mu_check(strcmp(line, "abcline3") == 0);
}
*/
/**
* This test case does a visual block select and then an "c" insert
* which should insert "abc" at the start of each line, replacing the l
*/
MU_TEST(test_change_block_mode_change)
{
char_u *lines[] = {"line1", "line2", "line3", "line4", "line5"};
vimBufferSetLines(curbuf, 0, 3, lines, 5);
vimKey("<c-v>");
vimInput("j");
vimInput("j");
vimInput("j");
vimInput("c");
vimInput("a");
vimInput("b");
vimInput("c");
vimKey("<esc>");
char_u *line = vimBufferGetLine(curbuf, 1);
mu_check(strcmp(line, "abcine1") == 0);
line = vimBufferGetLine(curbuf, 3);
mu_check(strcmp(line, "abcine3") == 0);
}
MU_TEST(test_in_parentheses)
{
char_u *lines[] = {"abc\"123\"def"};
vimBufferSetLines(curbuf, 0, 3, lines, 1);
vimInput("v");
vimInput("i");
vimInput("\"");
pos_T start;
pos_T end;
// Get current range, validate coordinates
vimVisualGetRange(&start, &end);
printf("start lnum: %ld col: %d end lnum: %ld col: %d\n", start.lnum, start.col, end.lnum, end.col);
mu_check(start.lnum == 1);
mu_check(start.col == 4);
mu_check(end.lnum == 1);
mu_check(end.col == 6);
}
MU_TEST(test_adjust_start_visual_line)
{
char_u *lines[] = {"line1", "line2", "line3", "line4", "line5"};
vimBufferSetLines(curbuf, 0, 3, lines, 5);
mu_check(vimBufferGetLineCount(curbuf) == 5);
vimInput("j");
vimInput("j");
vimInput("V");
pos_T start;
pos_T end;
// Get current range, validate coordinates
vimVisualGetRange(&start, &end);
mu_check(start.lnum == 3);
mu_check(start.col == 0);
mu_check(end.lnum == 3);
mu_check(end.col == 0);
pos_T newStart;
newStart.lnum = 1;
newStart.col = 0;
vimVisualSetStart(newStart);
vimVisualGetRange(&start, &end);
mu_check(start.lnum == 1);
mu_check(start.col == 0);
mu_check(end.lnum == 3);
mu_check(end.col == 0);
// Delete the lines - 1 through 3
vimInput("d");
// 3 lines should've been deleted
mu_check(vimBufferGetLineCount(curbuf) == 2);
mu_check(strcmp(vimBufferGetLine(curbuf, 1), "line4") == 0);
}
MU_TEST(test_adjust_start_select_character)
{
char_u *lines[] = {"line1", "line2", "line3", "line4", "line5"};
vimBufferSetLines(curbuf, 0, 3, lines, 5);
mu_check(vimBufferGetLineCount(curbuf) == 5);
// Move two characters to the right - cursor on 'n' in line1
vimInput("l");
vimInput("l");
// Switch to visual
vimInput("v");
// and then select
vimKey("<C-g>");
mu_check(vimSelectIsActive() == 1);
pos_T start;
pos_T end;
// Get current range, validate coordinates
vimVisualGetRange(&start, &end);
mu_check(start.lnum == 1);
mu_check(start.col == 2);
mu_check(end.lnum == 1);
mu_check(end.col == 2);
pos_T newStart;
newStart.lnum = 1;
newStart.col = 3;
vimVisualSetStart(newStart);
vimVisualGetRange(&start, &end);
mu_check(start.lnum == 1);
mu_check(start.col == 3);
mu_check(end.lnum == 1);
mu_check(end.col == 2);
// Delete the lines - 1 through 3
vimInput("t");
// 3 lines should've been deleted
mu_check(vimBufferGetLineCount(curbuf) == 5);
mu_check(strcmp(vimBufferGetLine(curbuf, 1), "lit1") == 0);
}
MU_TEST_SUITE(test_suite)
{
MU_SUITE_CONFIGURE(&test_setup, &test_teardown);
MU_RUN_TEST(test_visual_is_active);
MU_RUN_TEST(test_characterwise_range);
MU_RUN_TEST(test_ctrl_q);
MU_RUN_TEST(test_ctrl_Q);
MU_RUN_TEST(test_insert_block_mode);
// MU_RUN_TEST(test_insert_block_mode_change);
MU_RUN_TEST(test_change_block_mode_change);
MU_RUN_TEST(test_in_parentheses);
MU_RUN_TEST(test_adjust_start_visual_line);
MU_RUN_TEST(test_adjust_start_select_character);
}
int main(int argc, char **argv)
{
vimInit(argc, argv);
win_setwidth(5);
win_setheight(100);
vimBufferOpen("collateral/testfile.txt", 1, 0);
MU_RUN_SUITE(test_suite);
MU_REPORT();
MU_RETURN();
}