test_jumplist.vim 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. " Tests for the jumplist functionality
  2. " Tests for the getjumplist() function
  3. func Test_getjumplist()
  4. if !has("jumplist")
  5. return
  6. endif
  7. %bwipe
  8. clearjumps
  9. call assert_equal([[], 0], getjumplist())
  10. call assert_equal([[], 0], getjumplist(1))
  11. call assert_equal([[], 0], getjumplist(1, 1))
  12. call assert_equal([], getjumplist(100))
  13. call assert_equal([], getjumplist(1, 100))
  14. let lines = []
  15. for i in range(1, 100)
  16. call add(lines, "Line " . i)
  17. endfor
  18. call writefile(lines, "Xtest", 'D')
  19. " Jump around and create a jump list
  20. edit Xtest
  21. let bnr = bufnr('%')
  22. normal 50%
  23. normal G
  24. normal gg
  25. let expected = [[
  26. \ {'lnum': 1, 'bufnr': bnr, 'col': 0, 'coladd': 0},
  27. \ {'lnum': 50, 'bufnr': bnr, 'col': 0, 'coladd': 0},
  28. \ {'lnum': 100, 'bufnr': bnr, 'col': 0, 'coladd': 0}], 3]
  29. call assert_equal(expected, getjumplist())
  30. " jumplist doesn't change in between calls
  31. call assert_equal(expected, getjumplist())
  32. " Traverse the jump list and verify the results
  33. 5
  34. exe "normal \<C-O>"
  35. call assert_equal(2, 1->getjumplist()[1])
  36. exe "normal 2\<C-O>"
  37. call assert_equal(0, getjumplist(1, 1)[1])
  38. exe "normal 3\<C-I>"
  39. call assert_equal(3, getjumplist()[1])
  40. exe "normal \<C-O>"
  41. normal 20%
  42. let expected = [[
  43. \ {'lnum': 1, 'bufnr': bnr, 'col': 0, 'coladd': 0},
  44. \ {'lnum': 50, 'bufnr': bnr, 'col': 0, 'coladd': 0},
  45. \ {'lnum': 5, 'bufnr': bnr, 'col': 0, 'coladd': 0},
  46. \ {'lnum': 100, 'bufnr': bnr, 'col': 0, 'coladd': 0}], 4]
  47. call assert_equal(expected, getjumplist())
  48. " jumplist doesn't change in between calls
  49. call assert_equal(expected, getjumplist())
  50. let l = getjumplist()
  51. call test_garbagecollect_now()
  52. call assert_equal(4, l[1])
  53. clearjumps
  54. call test_garbagecollect_now()
  55. call assert_equal(4, l[1])
  56. endfunc
  57. func Test_jumplist_wipe_buf()
  58. new
  59. clearjumps
  60. " Put some random text and fill the jump list.
  61. call setline(1, ['foo', 'bar', 'baz'])
  62. normal G
  63. normal gg
  64. setl nomodified bufhidden=wipe
  65. e XXJumpListBuffer
  66. " The jump list is empty as the buffer was wiped out.
  67. call assert_equal([[], 0], getjumplist())
  68. let jumps = execute(':jumps')
  69. call assert_equal('>', jumps[-1:])
  70. " Put some random text and fill the jump list.
  71. call setline(1, ['foo', 'bar', 'baz'])
  72. setl bufhidden=hide
  73. " References to wiped buffer are deleted with multiple tabpages.
  74. let [w1, t1] = [win_getid(), tabpagenr()]
  75. clearjumps
  76. normal G
  77. normal gg
  78. enew
  79. split XXJumpListBuffer
  80. let [w2, t2] = [win_getid(), tabpagenr()]
  81. clearjumps
  82. normal G
  83. normal gg
  84. enew
  85. tabnew XXJumpListBuffer
  86. let [w3, t3] = [win_getid(), tabpagenr()]
  87. clearjumps
  88. normal G
  89. normal gg
  90. enew
  91. split XXJumpListBuffer
  92. let [w4, t4] = [win_getid(), tabpagenr()]
  93. clearjumps
  94. normal G
  95. normal gg
  96. enew
  97. for [w, t] in [[w1, t1], [w2, t2], [w3, t3], [w4, t4]]
  98. call assert_equal(2, len(getjumplist(w, t)[0]))
  99. endfor
  100. bwipe! XXJumpListBuffer
  101. for [w, t] in [[w1, t1], [w2, t2], [w3, t3], [w4, t4]]
  102. call assert_equal(0, len(getjumplist(w, t)[0]))
  103. endfor
  104. %bwipe!
  105. endfunc
  106. " Test for '' mark in an empty buffer
  107. func Test_empty_buffer()
  108. new
  109. insert
  110. a
  111. b
  112. c
  113. d
  114. .
  115. call assert_equal(1, line("''"))
  116. bwipe!
  117. endfunc
  118. " Test for 'jumpoptions'
  119. func Test_jumpoptions()
  120. new
  121. call setline(1, range(1, 200))
  122. clearjumps
  123. set jumpoptions=stack
  124. " Jump around to add some locations to the jump list.
  125. normal 10G
  126. normal 20G
  127. normal 30G
  128. normal 40G
  129. normal 50G
  130. let bnr = bufnr()
  131. " discards the tail when navigating from the middle
  132. exe "normal \<C-O>\<C-O>"
  133. call assert_equal([
  134. \ [{'lnum': 1, 'bufnr': bnr, 'col': 0, 'coladd': 0},
  135. \ {'lnum': 10, 'bufnr': bnr, 'col': 0, 'coladd': 0},
  136. \ {'lnum': 20, 'bufnr': bnr, 'col': 0, 'coladd': 0},
  137. \ {'lnum': 30, 'bufnr': bnr, 'col': 0, 'coladd': 0},
  138. \ {'lnum': 40, 'bufnr': bnr, 'col': 0, 'coladd': 0},
  139. \ {'lnum': 50, 'bufnr': bnr, 'col': 0, 'coladd': 0}
  140. \ ], 3], getjumplist())
  141. " new jump location is added immediately after the last one
  142. normal 90G
  143. call assert_equal([
  144. \ [{'lnum': 1, 'bufnr': bnr, 'col': 0, 'coladd': 0},
  145. \ {'lnum': 10, 'bufnr': bnr, 'col': 0, 'coladd': 0},
  146. \ {'lnum': 20, 'bufnr': bnr, 'col': 0, 'coladd': 0},
  147. \ {'lnum': 30, 'bufnr': bnr, 'col': 0, 'coladd': 0},
  148. \ ], 4], getjumplist())
  149. " does not add the same location twice adjacently
  150. normal 60G
  151. normal 60G
  152. call assert_equal([
  153. \ [{'lnum': 1, 'bufnr': bnr, 'col': 0, 'coladd': 0},
  154. \ {'lnum': 10, 'bufnr': bnr, 'col': 0, 'coladd': 0},
  155. \ {'lnum': 20, 'bufnr': bnr, 'col': 0, 'coladd': 0},
  156. \ {'lnum': 30, 'bufnr': bnr, 'col': 0, 'coladd': 0},
  157. \ {'lnum': 90, 'bufnr': bnr, 'col': 0, 'coladd': 0},
  158. "\ Nvim: avoids useless/phantom jumps
  159. "\ {'lnum': 60, 'bufnr': bnr, 'col': 0, 'coladd': 0},
  160. "\ ], 6], getjumplist())
  161. \ ], 5], getjumplist())
  162. " does add the same location twice non adjacently
  163. normal 10G
  164. normal 20G
  165. call assert_equal([
  166. \ [{'lnum': 1, 'bufnr': bnr, 'col': 0, 'coladd': 0},
  167. \ {'lnum': 10, 'bufnr': bnr, 'col': 0, 'coladd': 0},
  168. \ {'lnum': 20, 'bufnr': bnr, 'col': 0, 'coladd': 0},
  169. \ {'lnum': 30, 'bufnr': bnr, 'col': 0, 'coladd': 0},
  170. \ {'lnum': 90, 'bufnr': bnr, 'col': 0, 'coladd': 0},
  171. \ {'lnum': 60, 'bufnr': bnr, 'col': 0, 'coladd': 0},
  172. \ {'lnum': 10, 'bufnr': bnr, 'col': 0, 'coladd': 0},
  173. \ ], 7], getjumplist())
  174. set jumpoptions&
  175. %bw!
  176. endfunc
  177. " vim: shiftwidth=2 sts=2 expandtab