test_goto.vim (7799B)
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. 6 func 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! 15 endfunc 16 17 func 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) 28 endfunc 29 30 func 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) 41 endfunc 42 43 func 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) 55 endfunc 56 57 func 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) 69 endfunc 70 71 func 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) 83 endfunc 84 85 func 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) 96 endfunc 97 98 func 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) 110 endfunc 111 112 func 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) 123 endfunc 124 125 " Using gd to jump to a declaration in a fold 126 func 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! 141 endfunc 142 143 func 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) 157 endfunc 158 159 func 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) 169 endfunc 170 171 func 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) 185 endfunc 186 187 func 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) 198 endfunc 199 200 func 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) 211 endfunc 212 213 func 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& 226 endfunc 227 228 func 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) 237 endfunc 238 239 func 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) 248 endfunc 249 250 func 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) 265 endfunc 266 267 func 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) 276 endfunc 277 278 func 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) 287 endfunc 288 289 func 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 300 call XTest_goto_decl('gd', lines, 4, 7) 301 endfunc 302 303 func Test_gd_string_only() 304 let lines =<< trim [CODE] 305 int func(void) 306 { 307 char *s = "x"; 308 309 return x; 310 } 311 [CODE] 312 313 call XTest_goto_decl('gd', lines, 5, 10) 314 endfunc 315 316 " Check that setting some options does not change curswant 317 func Test_set_options_keep_col() 318 new 319 call setline(1, ['long long long line', 'short line']) 320 normal ggfi 321 let pos = getcurpos() 322 normal j 323 set invhlsearch spell spelllang=en,cjk spelloptions=camel textwidth=80 324 set cursorline cursorcolumn cursorlineopt=line colorcolumn=+1 winfixbuf 325 set comments=:# commentstring=#%s define=function 326 set background=dark 327 set background=light 328 normal k 329 call assert_equal(pos, getcurpos()) 330 bwipe! 331 set hlsearch& spell& spelllang& spelloptions& textwidth& 332 set cursorline& cursorcolumn& cursorlineopt& colorcolumn& winfixbuf& 333 set comments& commentstring& define& 334 set background& 335 endfunc 336 337 func Test_gd_local_block() 338 let lines =<< trim [CODE] 339 int main() 340 { 341 char *a = "NOT NULL"; 342 if(a) 343 { 344 char *b = a; 345 printf("%s\n", b); 346 } 347 else 348 { 349 char *b = "NULL"; 350 return b; 351 } 352 353 return 0; 354 } 355 [CODE] 356 357 call XTest_goto_decl('1gd', lines, 11, 11) 358 endfunc 359 360 func Test_motion_if_elif_else_endif() 361 new 362 let lines =<< trim END 363 /* Test pressing % on #if, #else #elsif and #endif, 364 * with nested #if 365 */ 366 #if FOO 367 /* ... */ 368 # if BAR 369 /* ... */ 370 # endif 371 #elif BAR 372 /* ... */ 373 #else 374 /* ... */ 375 #endif 376 377 #define FOO 1 378 END 379 call setline(1, lines) 380 /#if FOO 381 norm % 382 call assert_equal([9, 1], getpos('.')[1:2]) 383 norm % 384 call assert_equal([11, 1], getpos('.')[1:2]) 385 norm % 386 call assert_equal([13, 1], getpos('.')[1:2]) 387 norm % 388 call assert_equal([4, 1], getpos('.')[1:2]) 389 /# if BAR 390 norm $% 391 call assert_equal([8, 1], getpos('.')[1:2]) 392 norm $% 393 call assert_equal([6, 1], getpos('.')[1:2]) 394 395 " Test for [# and ]# command 396 call cursor(5, 1) 397 normal [# 398 call assert_equal([4, 1], getpos('.')[1:2]) 399 call cursor(5, 1) 400 normal ]# 401 call assert_equal([9, 1], getpos('.')[1:2]) 402 call cursor(10, 1) 403 normal [# 404 call assert_equal([9, 1], getpos('.')[1:2]) 405 call cursor(10, 1) 406 normal ]# 407 call assert_equal([11, 1], getpos('.')[1:2]) 408 409 " Finding a match before the first line or after the last line should fail 410 normal gg 411 call assert_beeps('normal [#') 412 normal G 413 call assert_beeps('normal ]#') 414 415 " Finding a match for a macro definition (#define) should fail 416 normal G 417 call assert_beeps('normal %') 418 419 bw! 420 endfunc 421 422 func Test_motion_c_comment() 423 new 424 a 425 /* 426 * Test pressing % on beginning/end 427 * of C comments. 428 */ 429 /* Another comment */ 430 . 431 norm gg0% 432 call assert_equal([4, 3], getpos('.')[1:2]) 433 norm % 434 call assert_equal([1, 1], getpos('.')[1:2]) 435 norm gg0l% 436 call assert_equal([4, 3], getpos('.')[1:2]) 437 norm h% 438 call assert_equal([1, 1], getpos('.')[1:2]) 439 440 norm G^ 441 norm % 442 call assert_equal([5, 21], getpos('.')[1:2]) 443 norm % 444 call assert_equal([5, 1], getpos('.')[1:2]) 445 446 bw! 447 endfunc 448 449 " vim: shiftwidth=2 sts=2 expandtab