xref: /vim-8.2.3635/src/testdir/test_goto.vim (revision d1ad99b6)
1" Test commands that jump somewhere.
2
3" 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)
7  new
8  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('.'))
14  quit!
15endfunc
16
17func Test_gD()
18  let lines =<< trim [CODE]
19    int x;
20
21    int func(void)
22    {
23      return x;
24    }
25  [CODE]
26
27  call XTest_goto_decl('gD', lines, 1, 5)
28endfunc
29
30func Test_gD_too()
31  let lines =<< trim [CODE]
32    Filename x;
33
34    int Filename
35    int func() {
36      Filename x;
37      return x;
38  [CODE]
39
40  call XTest_goto_decl('gD', lines, 1, 10)
41endfunc
42
43func Test_gD_comment()
44  let lines =<< trim [CODE]
45    /* int x; */
46    int x;
47
48    int func(void)
49    {
50      return x;
51    }
52  [CODE]
53
54  call XTest_goto_decl('gD', lines, 2, 5)
55endfunc
56
57func Test_gD_inline_comment()
58  let lines =<< trim [CODE]
59    int y /* , x */;
60    int x;
61
62    int func(void)
63    {
64      return x;
65    }
66  [CODE]
67
68  call XTest_goto_decl('gD', lines, 2, 5)
69endfunc
70
71func Test_gD_string()
72  let lines =<< trim [CODE]
73    char *s[] = "x";
74    int x = 1;
75
76    int func(void)
77    {
78      return x;
79    }
80  [CODE]
81
82  call XTest_goto_decl('gD', lines, 2, 5)
83endfunc
84
85func Test_gD_string_same_line()
86  let lines =<< trim [CODE]
87    char *s[] = "x", int x = 1;
88
89    int func(void)
90    {
91      return x;
92    }
93  [CODE]
94
95  call XTest_goto_decl('gD', lines, 1, 22)
96endfunc
97
98func Test_gD_char()
99  let lines =<< trim [CODE]
100    char c = 'x';
101    int x = 1;
102
103    int func(void)
104    {
105      return x;
106    }
107  [CODE]
108
109  call XTest_goto_decl('gD', lines, 2, 5)
110endfunc
111
112func Test_gd()
113  let lines =<< trim [CODE]
114    int x;
115
116    int func(int x)
117    {
118      return x;
119    }
120  [CODE]
121
122  call XTest_goto_decl('gd', lines, 3, 14)
123endfunc
124
125" Using gd to jump to a declaration in a fold
126func Test_gd_with_fold()
127  new
128  let lines =<< trim END
129    #define ONE 1
130    #define TWO 2
131    #define THREE 3
132
133    TWO
134  END
135  call setline(1, lines)
136  1,3fold
137  call feedkeys('Ggd', 'xt')
138  call assert_equal(2, line('.'))
139  call assert_equal(-1, foldclosedend(2))
140  bw!
141endfunc
142
143func Test_gd_not_local()
144  let lines =<< trim [CODE]
145    int func1(void)
146    {
147      return x;
148    }
149
150    int func2(int x)
151    {
152      return x;
153    }
154  [CODE]
155
156  call XTest_goto_decl('gd', lines, 3, 10)
157endfunc
158
159func Test_gd_kr_style()
160  let lines =<< trim [CODE]
161    int func(x)
162      int x;
163    {
164      return x;
165    }
166  [CODE]
167
168  call XTest_goto_decl('gd', lines, 2, 7)
169endfunc
170
171func Test_gd_missing_braces()
172  let lines =<< trim [CODE]
173    def func1(a)
174      a + 1
175    end
176
177    a = 1
178
179    def func2()
180      return a
181    end
182  [CODE]
183
184  call XTest_goto_decl('gd', lines, 1, 11)
185endfunc
186
187func Test_gd_comment()
188  let lines =<< trim [CODE]
189    int func(void)
190    {
191      /* int x; */
192      int x;
193      return x;
194    }
195  [CODE]
196
197  call XTest_goto_decl('gd', lines, 4, 7)
198endfunc
199
200func Test_gd_comment_in_string()
201  let lines =<< trim [CODE]
202    int func(void)
203    {
204      char *s ="//"; int x;
205      int x;
206      return x;
207    }
208  [CODE]
209
210  call XTest_goto_decl('gd', lines, 3, 22)
211endfunc
212
213func Test_gd_string_in_comment()
214  set comments=
215  let lines =<< trim [CODE]
216    int func(void)
217    {
218      /* " */ int x;
219      int x;
220      return x;
221    }
222  [CODE]
223
224  call XTest_goto_decl('gd', lines, 3, 15)
225  set comments&
226endfunc
227
228func Test_gd_inline_comment()
229  let lines =<< trim [CODE]
230    int func(/* x is an int */ int x)
231    {
232      return x;
233    }
234  [CODE]
235
236  call XTest_goto_decl('gd', lines, 1, 32)
237endfunc
238
239func Test_gd_inline_comment_only()
240  let lines =<< trim [CODE]
241    int func(void) /* one lonely x */
242    {
243      return x;
244    }
245  [CODE]
246
247  call XTest_goto_decl('gd', lines, 3, 10)
248endfunc
249
250func Test_gd_inline_comment_body()
251  let lines =<< trim [CODE]
252    int func(void)
253    {
254      int y /* , x */;
255
256      for (/* int x = 0 */; y < 2; y++);
257
258      int x = 0;
259
260      return x;
261    }
262  [CODE]
263
264  call XTest_goto_decl('gd', lines, 7, 7)
265endfunc
266
267func Test_gd_trailing_multiline_comment()
268  let lines =<< trim [CODE]
269    int func(int x) /* x is an int */
270    {
271      return x;
272    }
273  [CODE]
274
275  call XTest_goto_decl('gd', lines, 1, 14)
276endfunc
277
278func Test_gd_trailing_comment()
279  let lines =<< trim [CODE]
280    int func(int x) // x is an int
281    {
282      return x;
283    }
284  [CODE]
285
286  call XTest_goto_decl('gd', lines, 1, 14)
287endfunc
288
289func Test_gd_string()
290  let lines =<< trim [CODE]
291    int func(void)
292    {
293      char *s = "x";
294      int x = 1;
295
296      return x;
297    }
298  [CODE]
299  call XTest_goto_decl('gd', lines, 4, 7)
300endfunc
301
302func Test_gd_string_only()
303  let lines =<< trim [CODE]
304    int func(void)
305    {
306      char *s = "x";
307
308      return x;
309    }
310  [CODE]
311
312  call XTest_goto_decl('gd', lines, 5, 10)
313endfunc
314
315" Check that setting 'cursorline' does not change curswant
316func Test_cursorline_keep_col()
317  new
318  call setline(1, ['long long long line', 'short line'])
319  normal ggfi
320  let pos = getcurpos()
321  normal j
322  set cursorline
323  normal k
324  call assert_equal(pos, getcurpos())
325  bwipe!
326  set nocursorline
327endfunc
328
329func Test_gd_local_block()
330  let lines =<< trim [CODE]
331    int main()
332    {
333      char *a = "NOT NULL";
334      if(a)
335      {
336        char *b = a;
337        printf("%s\n", b);
338      }
339      else
340      {
341        char *b = "NULL";
342        return b;
343      }
344
345      return 0;
346    }
347  [CODE]
348
349  call XTest_goto_decl('1gd', lines, 11, 11)
350endfunc
351
352func Test_motion_if_elif_else_endif()
353  new
354  let lines =<< trim END
355    /* Test pressing % on #if, #else #elsif and #endif,
356     * with nested #if
357     */
358    #if FOO
359    /* ... */
360    #  if BAR
361    /* ... */
362    #  endif
363    #elif BAR
364    /* ... */
365    #else
366    /* ... */
367    #endif
368
369    #define FOO 1
370  END
371  call setline(1, lines)
372  /#if FOO
373  norm %
374  call assert_equal([9, 1], getpos('.')[1:2])
375  norm %
376  call assert_equal([11, 1], getpos('.')[1:2])
377  norm %
378  call assert_equal([13, 1], getpos('.')[1:2])
379  norm %
380  call assert_equal([4, 1], getpos('.')[1:2])
381  /#  if BAR
382  norm $%
383  call assert_equal([8, 1], getpos('.')[1:2])
384  norm $%
385  call assert_equal([6, 1], getpos('.')[1:2])
386
387  " Test for [# and ]# command
388  call cursor(5, 1)
389  normal [#
390  call assert_equal([4, 1], getpos('.')[1:2])
391  call cursor(5, 1)
392  normal ]#
393  call assert_equal([9, 1], getpos('.')[1:2])
394  call cursor(10, 1)
395  normal [#
396  call assert_equal([9, 1], getpos('.')[1:2])
397  call cursor(10, 1)
398  normal ]#
399  call assert_equal([11, 1], getpos('.')[1:2])
400
401  " Finding a match before the first line or after the last line should fail
402  normal gg
403  call assert_beeps('normal [#')
404  normal G
405  call assert_beeps('normal ]#')
406
407  " Finding a match for a macro definition (#define) should fail
408  normal G
409  call assert_beeps('normal %')
410
411  bw!
412endfunc
413
414func Test_motion_c_comment()
415  new
416  a
417/*
418 * Test pressing % on beginning/end
419 * of C comments.
420 */
421/* Another comment */
422.
423  norm gg0%
424  call assert_equal([4, 3], getpos('.')[1:2])
425  norm %
426  call assert_equal([1, 1], getpos('.')[1:2])
427  norm gg0l%
428  call assert_equal([4, 3], getpos('.')[1:2])
429  norm h%
430  call assert_equal([1, 1], getpos('.')[1:2])
431
432  norm G^
433  norm %
434  call assert_equal([5, 21], getpos('.')[1:2])
435  norm %
436  call assert_equal([5, 1], getpos('.')[1:2])
437
438  bw!
439endfunc
440
441" vim: shiftwidth=2 sts=2 expandtab
442