test_goto.vim 7.6 KB

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