blob: aeca0b4361a1eb8d4b1a6692e3fff1d27efd30ca [file] [log] [blame]
Bram Moolenaarf9660b52016-04-16 22:19:15 +02001" Test commands that jump somewhere.
2
Bram Moolenaar226630a2016-10-08 19:21:31 +02003" Create a new buffer using "lines" and place the cursor on the word after the
4" first occurrence of return and invoke "cmd". The cursor should now be
5" positioned at the given line and col.
6func XTest_goto_decl(cmd, lines, line, col)
Bram Moolenaarf9660b52016-04-16 22:19:15 +02007 new
Bram Moolenaar226630a2016-10-08 19:21:31 +02008 call setline(1, a:lines)
9 /return/
10 normal! W
11 execute 'norm! ' . a:cmd
12 call assert_equal(a:line, line('.'))
13 call assert_equal(a:col, col('.'))
Bram Moolenaarf9660b52016-04-16 22:19:15 +020014 quit!
15endfunc
Bram Moolenaar23c60f22016-06-15 22:03:48 +020016
Bram Moolenaar226630a2016-10-08 19:21:31 +020017func Test_gD()
Bram Moolenaarc79745a2019-05-20 22:12:34 +020018 let lines =<< trim [CODE]
Bram Moolenaare7eb9272019-06-24 00:58:07 +020019 int x;
20
21 int func(void)
22 {
23 return x;
24 }
Bram Moolenaarc79745a2019-05-20 22:12:34 +020025 [CODE]
26
Bram Moolenaar226630a2016-10-08 19:21:31 +020027 call XTest_goto_decl('gD', lines, 1, 5)
28endfunc
29
30func Test_gD_too()
Bram Moolenaarc79745a2019-05-20 22:12:34 +020031 let lines =<< trim [CODE]
Bram Moolenaarc79745a2019-05-20 22:12:34 +020032 Filename x;
Bram Moolenaare7eb9272019-06-24 00:58:07 +020033
34 int Filename
35 int func() {
36 Filename x;
37 return x;
Bram Moolenaarc79745a2019-05-20 22:12:34 +020038 [CODE]
39
Bram Moolenaar226630a2016-10-08 19:21:31 +020040 call XTest_goto_decl('gD', lines, 1, 10)
41endfunc
42
43func Test_gD_comment()
Bram Moolenaarc79745a2019-05-20 22:12:34 +020044 let lines =<< trim [CODE]
Bram Moolenaare7eb9272019-06-24 00:58:07 +020045 /* int x; */
46 int x;
47
48 int func(void)
49 {
50 return x;
51 }
Bram Moolenaarc79745a2019-05-20 22:12:34 +020052 [CODE]
53
Bram Moolenaar226630a2016-10-08 19:21:31 +020054 call XTest_goto_decl('gD', lines, 2, 5)
55endfunc
56
57func Test_gD_inline_comment()
Bram Moolenaarc79745a2019-05-20 22:12:34 +020058 let lines =<< trim [CODE]
Bram Moolenaare7eb9272019-06-24 00:58:07 +020059 int y /* , x */;
60 int x;
61
62 int func(void)
63 {
64 return x;
65 }
Bram Moolenaarc79745a2019-05-20 22:12:34 +020066 [CODE]
67
Bram Moolenaar226630a2016-10-08 19:21:31 +020068 call XTest_goto_decl('gD', lines, 2, 5)
69endfunc
70
71func Test_gD_string()
Bram Moolenaarc79745a2019-05-20 22:12:34 +020072 let lines =<< trim [CODE]
Bram Moolenaare7eb9272019-06-24 00:58:07 +020073 char *s[] = "x";
74 int x = 1;
75
76 int func(void)
77 {
78 return x;
79 }
Bram Moolenaarc79745a2019-05-20 22:12:34 +020080 [CODE]
81
Bram Moolenaar226630a2016-10-08 19:21:31 +020082 call XTest_goto_decl('gD', lines, 2, 5)
83endfunc
84
85func Test_gD_string_same_line()
Bram Moolenaarc79745a2019-05-20 22:12:34 +020086 let lines =<< trim [CODE]
Bram Moolenaare7eb9272019-06-24 00:58:07 +020087 char *s[] = "x", int x = 1;
88
89 int func(void)
90 {
91 return x;
92 }
Bram Moolenaarc79745a2019-05-20 22:12:34 +020093 [CODE]
94
Bram Moolenaar226630a2016-10-08 19:21:31 +020095 call XTest_goto_decl('gD', lines, 1, 22)
96endfunc
97
98func Test_gD_char()
Bram Moolenaarc79745a2019-05-20 22:12:34 +020099 let lines =<< trim [CODE]
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200100 char c = 'x';
101 int x = 1;
102
103 int func(void)
104 {
105 return x;
106 }
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200107 [CODE]
108
Bram Moolenaar226630a2016-10-08 19:21:31 +0200109 call XTest_goto_decl('gD', lines, 2, 5)
110endfunc
111
112func Test_gd()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200113 let lines =<< trim [CODE]
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200114 int x;
115
116 int func(int x)
117 {
118 return x;
119 }
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200120 [CODE]
121
Bram Moolenaar226630a2016-10-08 19:21:31 +0200122 call XTest_goto_decl('gd', lines, 3, 14)
123endfunc
124
125func Test_gd_not_local()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200126 let lines =<< trim [CODE]
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200127 int func1(void)
128 {
129 return x;
130 }
131
132 int func2(int x)
133 {
134 return x;
135 }
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200136 [CODE]
137
Bram Moolenaar226630a2016-10-08 19:21:31 +0200138 call XTest_goto_decl('gd', lines, 3, 10)
139endfunc
140
141func Test_gd_kr_style()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200142 let lines =<< trim [CODE]
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200143 int func(x)
144 int x;
145 {
146 return x;
147 }
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200148 [CODE]
149
Bram Moolenaar226630a2016-10-08 19:21:31 +0200150 call XTest_goto_decl('gd', lines, 2, 7)
151endfunc
152
153func Test_gd_missing_braces()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200154 let lines =<< trim [CODE]
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200155 def func1(a)
156 a + 1
157 end
158
159 a = 1
160
161 def func2()
162 return a
163 end
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200164 [CODE]
165
Bram Moolenaar226630a2016-10-08 19:21:31 +0200166 call XTest_goto_decl('gd', lines, 1, 11)
167endfunc
168
169func Test_gd_comment()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200170 let lines =<< trim [CODE]
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200171 int func(void)
172 {
173 /* int x; */
174 int x;
175 return x;
176 }
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200177 [CODE]
178
Bram Moolenaar226630a2016-10-08 19:21:31 +0200179 call XTest_goto_decl('gd', lines, 4, 7)
180endfunc
181
182func Test_gd_comment_in_string()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200183 let lines =<< trim [CODE]
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200184 int func(void)
185 {
186 char *s ="//"; int x;
187 int x;
188 return x;
189 }
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200190 [CODE]
191
Bram Moolenaar226630a2016-10-08 19:21:31 +0200192 call XTest_goto_decl('gd', lines, 3, 22)
193endfunc
194
195func Test_gd_string_in_comment()
196 set comments=
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200197 let lines =<< trim [CODE]
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200198 int func(void)
199 {
200 /* " */ int x;
201 int x;
202 return x;
203 }
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200204 [CODE]
205
Bram Moolenaar226630a2016-10-08 19:21:31 +0200206 call XTest_goto_decl('gd', lines, 3, 15)
207 set comments&
208endfunc
209
210func Test_gd_inline_comment()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200211 let lines =<< trim [CODE]
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200212 int func(/* x is an int */ int x)
213 {
214 return x;
215 }
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200216 [CODE]
217
Bram Moolenaar226630a2016-10-08 19:21:31 +0200218 call XTest_goto_decl('gd', lines, 1, 32)
219endfunc
220
221func Test_gd_inline_comment_only()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200222 let lines =<< trim [CODE]
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200223 int func(void) /* one lonely x */
224 {
225 return x;
226 }
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200227 [CODE]
228
Bram Moolenaar226630a2016-10-08 19:21:31 +0200229 call XTest_goto_decl('gd', lines, 3, 10)
230endfunc
231
232func Test_gd_inline_comment_body()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200233 let lines =<< trim [CODE]
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200234 int func(void)
235 {
236 int y /* , x */;
237
238 for (/* int x = 0 */; y < 2; y++);
239
240 int x = 0;
241
242 return x;
243 }
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200244 [CODE]
245
Bram Moolenaar226630a2016-10-08 19:21:31 +0200246 call XTest_goto_decl('gd', lines, 7, 7)
247endfunc
248
249func Test_gd_trailing_multiline_comment()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200250 let lines =<< trim [CODE]
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200251 int func(int x) /* x is an int */
252 {
253 return x;
254 }
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200255 [CODE]
256
Bram Moolenaar226630a2016-10-08 19:21:31 +0200257 call XTest_goto_decl('gd', lines, 1, 14)
258endfunc
259
260func Test_gd_trailing_comment()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200261 let lines =<< trim [CODE]
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200262 int func(int x) // x is an int
263 {
264 return x;
265 }
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200266 [CODE]
267
Bram Moolenaar226630a2016-10-08 19:21:31 +0200268 call XTest_goto_decl('gd', lines, 1, 14)
269endfunc
270
271func Test_gd_string()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200272 let lines =<< trim [CODE]
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200273 int func(void)
274 {
275 char *s = "x";
276 int x = 1;
277
278 return x;
279 }
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200280 [CODE]
Bram Moolenaar226630a2016-10-08 19:21:31 +0200281 call XTest_goto_decl('gd', lines, 4, 7)
282endfunc
283
284func Test_gd_string_only()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200285 let lines =<< trim [CODE]
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200286 int func(void)
287 {
288 char *s = "x";
289
290 return x;
291 }
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200292 [CODE]
293
Bram Moolenaar226630a2016-10-08 19:21:31 +0200294 call XTest_goto_decl('gd', lines, 5, 10)
Bram Moolenaar23c60f22016-06-15 22:03:48 +0200295endfunc
Bram Moolenaara2477fd2016-12-03 15:13:20 +0100296
297" Check that setting 'cursorline' does not change curswant
298func Test_cursorline_keep_col()
299 new
300 call setline(1, ['long long long line', 'short line'])
301 normal ggfi
302 let pos = getcurpos()
303 normal j
304 set cursorline
305 normal k
306 call assert_equal(pos, getcurpos())
307 bwipe!
308 set nocursorline
309endfunc
310
Bram Moolenaar60402d62017-04-20 18:54:50 +0200311func Test_gd_local_block()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200312 let lines =<< trim [CODE]
313 int main()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200314 {
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200315 char *a = "NOT NULL";
316 if(a)
317 {
318 char *b = a;
319 printf("%s\n", b);
320 }
321 else
322 {
323 char *b = "NULL";
324 return b;
325 }
326
327 return 0;
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200328 }
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200329 [CODE]
330
Bram Moolenaar60402d62017-04-20 18:54:50 +0200331 call XTest_goto_decl('1gd', lines, 11, 11)
332endfunc
Bram Moolenaar6b69e5c2018-05-26 18:39:32 +0200333
334func Test_motion_if_elif_else_endif()
335 new
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200336 let lines =<< trim END
337 /* Test pressing % on #if, #else #elsif and #endif,
338 * with nested #if
339 */
340 #if FOO
341 /* ... */
342 # if BAR
343 /* ... */
344 # endif
345 #elif BAR
346 /* ... */
347 #else
348 /* ... */
349 #endif
350
351 #define FOO 1
352 END
353 call setline(1, lines)
Bram Moolenaar6b69e5c2018-05-26 18:39:32 +0200354 /#if FOO
355 norm %
356 call assert_equal([9, 1], getpos('.')[1:2])
357 norm %
358 call assert_equal([11, 1], getpos('.')[1:2])
359 norm %
360 call assert_equal([13, 1], getpos('.')[1:2])
361 norm %
362 call assert_equal([4, 1], getpos('.')[1:2])
363 /# if BAR
364 norm $%
365 call assert_equal([8, 1], getpos('.')[1:2])
366 norm $%
367 call assert_equal([6, 1], getpos('.')[1:2])
368
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200369 " Test for [# and ]# command
370 call cursor(5, 1)
371 normal [#
372 call assert_equal([4, 1], getpos('.')[1:2])
373 call cursor(5, 1)
374 normal ]#
375 call assert_equal([9, 1], getpos('.')[1:2])
376 call cursor(10, 1)
377 normal [#
378 call assert_equal([9, 1], getpos('.')[1:2])
379 call cursor(10, 1)
380 normal ]#
381 call assert_equal([11, 1], getpos('.')[1:2])
382
383 " Finding a match before the first line or after the last line should fail
384 normal gg
385 call assert_beeps('normal [#')
386 normal G
387 call assert_beeps('normal ]#')
388
389 " Finding a match for a macro definition (#define) should fail
390 normal G
391 call assert_beeps('normal %')
392
Bram Moolenaar6b69e5c2018-05-26 18:39:32 +0200393 bw!
394endfunc
395
396func Test_motion_c_comment()
397 new
398 a
399/*
400 * Test pressing % on beginning/end
401 * of C comments.
402 */
403/* Another comment */
404.
405 norm gg0%
406 call assert_equal([4, 3], getpos('.')[1:2])
407 norm %
408 call assert_equal([1, 1], getpos('.')[1:2])
409 norm gg0l%
410 call assert_equal([4, 3], getpos('.')[1:2])
411 norm h%
412 call assert_equal([1, 1], getpos('.')[1:2])
413
414 norm G^
415 norm %
416 call assert_equal([5, 21], getpos('.')[1:2])
417 norm %
418 call assert_equal([5, 1], getpos('.')[1:2])
419
420 bw!
421endfunc
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200422
423" vim: shiftwidth=2 sts=2 expandtab