045_folding_spec.lua 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. -- Tests for folding.
  2. local Screen = require('test.functional.ui.screen')
  3. local helpers = require('test.functional.helpers')(after_each)
  4. local feed, insert, feed_command, expect_any =
  5. helpers.feed, helpers.insert, helpers.feed_command, helpers.expect_any
  6. describe('folding', function()
  7. local screen
  8. before_each(function()
  9. helpers.clear()
  10. screen = Screen.new(20, 8)
  11. screen:attach()
  12. end)
  13. it('creation, opening, moving (to the end) and closing', function()
  14. insert([[
  15. 1 aa
  16. 2 bb
  17. 3 cc
  18. last
  19. ]])
  20. -- Basic test if a fold can be created, opened, moving to the end and
  21. -- closed.
  22. feed_command('1')
  23. feed('zf2j')
  24. feed_command('call append("$", "manual " . getline(foldclosed(".")))')
  25. feed('zo')
  26. feed_command('call append("$", foldclosed("."))')
  27. feed(']z')
  28. feed_command('call append("$", getline("."))')
  29. feed('zc')
  30. feed_command('call append("$", getline(foldclosed(".")))')
  31. expect_any([[
  32. manual 1 aa
  33. -1
  34. 3 cc
  35. 1 aa]])
  36. end)
  37. it("foldmethod=marker", function()
  38. screen:try_resize(20, 10)
  39. insert([[
  40. dd {{{
  41. ee {{{ }}}
  42. ff }}}
  43. ]])
  44. feed_command('set fdm=marker fdl=1')
  45. feed_command('2')
  46. feed_command('call append("$", "line 2 foldlevel=" . foldlevel("."))')
  47. feed('[z')
  48. feed_command('call append("$", foldlevel("."))')
  49. feed('jo{{ <esc>r{jj') -- writes '{{{' and moves 2 lines bot
  50. feed_command('call append("$", foldlevel("."))')
  51. feed('kYpj')
  52. feed_command('call append("$", foldlevel("."))')
  53. helpers.poke_eventloop()
  54. screen:expect([[
  55. dd {{{ |
  56. ee {{{ }}} |
  57. {{{ |
  58. ff }}} |
  59. ff }}} |
  60. ^ |
  61. line 2 foldlevel=2 |
  62. 1 |
  63. 1 |
  64. |
  65. ]])
  66. end)
  67. it("foldmethod=indent", function()
  68. screen:try_resize(20, 8)
  69. feed_command('set fdm=indent sw=2')
  70. insert([[
  71. aa
  72. bb
  73. cc
  74. last
  75. ]])
  76. feed_command('call append("$", "foldlevel line3=" . foldlevel(3))')
  77. feed_command('call append("$", foldlevel(2))')
  78. feed('zR')
  79. helpers.poke_eventloop()
  80. screen:expect([[
  81. aa |
  82. bb |
  83. cc |
  84. last |
  85. ^ |
  86. foldlevel line3=2 |
  87. 1 |
  88. |
  89. ]])
  90. end)
  91. it("foldmethod=syntax", function()
  92. screen:try_resize(35, 15)
  93. insert([[
  94. 1 aa
  95. 2 bb
  96. 3 cc
  97. 4 dd {{{
  98. 5 ee {{{ }}}
  99. 6 ff }}}
  100. 7 gg
  101. 8 hh
  102. 9 ii
  103. a jj
  104. b kk
  105. last]])
  106. feed_command('set fdm=syntax fdl=0')
  107. feed_command('syn region Hup start="dd" end="ii" fold contains=Fd1,Fd2,Fd3')
  108. feed_command('syn region Fd1 start="ee" end="ff" fold contained')
  109. feed_command('syn region Fd2 start="gg" end="hh" fold contained')
  110. feed_command('syn region Fd3 start="commentstart" end="commentend" fold contained')
  111. feed('Gzk')
  112. feed_command('call append("$", "folding " . getline("."))')
  113. feed('k')
  114. feed_command('call append("$", getline("."))')
  115. feed('jAcommentstart <esc>Acommentend<esc>')
  116. feed_command('set fdl=1')
  117. feed('3j')
  118. feed_command('call append("$", getline("."))')
  119. feed_command('set fdl=0')
  120. feed('zO<C-L>j') -- <C-L> redraws screen
  121. feed_command('call append("$", getline("."))')
  122. feed_command('set fdl=0')
  123. expect_any([[
  124. folding 9 ii
  125. 3 cc
  126. 9 ii
  127. a jj]])
  128. end)
  129. it("foldmethod=expression", function()
  130. insert([[
  131. 1 aa
  132. 2 bb
  133. 3 cc
  134. 4 dd {{{
  135. 5 ee {{{ }}}
  136. 6 ff }}}
  137. 7 gg
  138. 8 hh
  139. 9 ii
  140. a jj
  141. b kk
  142. last ]])
  143. feed_command([[
  144. fun Flvl()
  145. let l = getline(v:lnum)
  146. if l =~ "bb$"
  147. return 2
  148. elseif l =~ "gg$"
  149. return "s1"
  150. elseif l =~ "ii$"
  151. return ">2"
  152. elseif l =~ "kk$"
  153. return "0"
  154. endif
  155. return "="
  156. endfun
  157. ]])
  158. feed_command('set fdm=expr fde=Flvl()')
  159. feed_command('/bb$')
  160. feed_command('call append("$", "expr " . foldlevel("."))')
  161. feed_command('/hh$')
  162. feed_command('call append("$", foldlevel("."))')
  163. feed_command('/ii$')
  164. feed_command('call append("$", foldlevel("."))')
  165. feed_command('/kk$')
  166. feed_command('call append("$", foldlevel("."))')
  167. expect_any([[
  168. expr 2
  169. 1
  170. 2
  171. 0]])
  172. end)
  173. it('can be opened after :move', function()
  174. -- luacheck: ignore
  175. screen:try_resize(35, 8)
  176. insert([[
  177. Test fdm=indent and :move bug END
  178. line2
  179. Test fdm=indent START
  180. line3
  181. line4]])
  182. feed_command('set noai nosta ')
  183. feed_command('set fdm=indent')
  184. feed_command('1m1')
  185. feed('2jzc')
  186. feed_command('m0')
  187. feed('zR')
  188. expect_any([[
  189. Test fdm=indent START
  190. line3
  191. line4
  192. Test fdm=indent and :move bug END
  193. line2]])
  194. end)
  195. end)