robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
9.7 KiB329 linesraw
1-- Tests for folding.
2
3local n = require('test.functional.testnvim')()
4local t = require('test.testutil')
5local Screen = require('test.functional.ui.screen')
6
7local describe, it, before_each = t.describe, t.it, t.before_each
8local feed, insert, feed_command, expect_any = n.feed, n.insert, n.feed_command, n.expect_any
9local command = n.command
10local exec = n.exec
11
12describe('folding', function()
13  local screen
14
15  before_each(function()
16    n.clear()
17
18    screen = Screen.new(45, 8)
19  end)
20
21  it('creation, opening, moving (to the end) and closing', function()
22    insert([[
23      1 aa
24      2 bb
25      3 cc
26      last
27      ]])
28
29    -- Basic test if a fold can be created, opened, moving to the end and
30    -- closed.
31    feed_command('1')
32    feed('zf2j')
33    feed_command('call append("$", "manual " . getline(foldclosed(".")))')
34    feed('zo')
35    feed_command('call append("$", foldclosed("."))')
36    feed(']z')
37    feed_command('call append("$", getline("."))')
38    feed('zc')
39    feed_command('call append("$", getline(foldclosed(".")))')
40
41    expect_any([[
42      manual 1 aa
43      -1
44      3 cc
45      1 aa]])
46  end)
47
48  it('foldmethod=marker', function()
49    screen:try_resize(20, 10)
50    insert([[
51      dd {{{
52      ee {{{ }}}
53      ff }}}
54    ]])
55    feed_command('set fdm=marker fdl=1')
56    feed_command('2')
57    feed_command('call append("$", "line 2 foldlevel=" . foldlevel("."))')
58    feed('[z')
59    feed_command('call append("$", foldlevel("."))')
60    feed('jo{{ <esc>r{jj') -- writes '{{{' and moves 2 lines bot
61    feed_command('call append("$", foldlevel("."))')
62    feed('kYpj')
63    feed_command('call append("$", foldlevel("."))')
64
65    n.poke_eventloop()
66    screen:expect([[
67      dd {{{              |
68      ee {{{ }}}          |
69      {{{                 |
70      ff }}}              |*2
71      ^                    |
72      line 2 foldlevel=2  |
73      1                   |*2
74                          |
75    ]])
76  end)
77
78  it('foldmethod=indent', function()
79    screen:try_resize(20, 8)
80    feed_command('set fdm=indent sw=2')
81    insert([[
82    aa
83      bb
84        cc
85    last
86    ]])
87    feed_command('call append("$", "foldlevel line3=" . foldlevel(3))')
88    feed_command('call append("$", foldlevel(2))')
89    feed('zR')
90
91    n.poke_eventloop()
92    screen:expect([[
93      aa                  |
94        bb                |
95          cc              |
96      last                |
97      ^                    |
98      foldlevel line3=2   |
99      1                   |
100                          |
101    ]])
102  end)
103
104  it('foldmethod=syntax', function()
105    screen:try_resize(35, 15)
106    insert([[
107      1 aa
108      2 bb
109      3 cc
110      4 dd {{{
111      5 ee {{{ }}}
112      6 ff }}}
113      7 gg
114      8 hh
115      9 ii
116      a jj
117      b kk
118      last]])
119    feed_command('set fdm=syntax fdl=0')
120    feed_command('syn region Hup start="dd" end="ii" fold contains=Fd1,Fd2,Fd3')
121    feed_command('syn region Fd1 start="ee" end="ff" fold contained')
122    feed_command('syn region Fd2 start="gg" end="hh" fold contained')
123    feed_command('syn region Fd3 start="commentstart" end="commentend" fold contained')
124    feed('Gzk')
125    feed_command('call append("$", "folding " . getline("."))')
126    feed('k')
127    feed_command('call append("$", getline("."))')
128    feed('jAcommentstart  <esc>Acommentend<esc>')
129    feed_command('set fdl=1')
130    feed('3j')
131    feed_command('call append("$", getline("."))')
132    feed_command('set fdl=0')
133    feed('zO<C-L>j') -- <C-L> redraws screen
134    feed_command('call append("$", getline("."))')
135    feed_command('set fdl=0')
136    expect_any([[
137      folding 9 ii
138      3 cc
139      9 ii
140      a jj]])
141  end)
142
143  it('foldmethod=expression', function()
144    insert([[
145      1 aa
146      2 bb
147      3 cc
148      4 dd {{{
149      5 ee {{{ }}}
150      6 ff }}}
151      7 gg
152      8 hh
153      9 ii
154      a jj
155      b kk
156      last ]])
157
158    feed_command([[
159    fun Flvl()
160     let l = getline(v:lnum)
161     if l =~ "bb$"
162       return 2
163     elseif l =~ "gg$"
164       return "s1"
165     elseif l =~ "ii$"
166       return ">2"
167     elseif l =~ "kk$"
168       return "0"
169     endif
170     return "="
171    endfun
172    ]])
173    feed_command('set fdm=expr fde=Flvl()')
174    feed_command('/bb$')
175    feed_command('call append("$", "expr " . foldlevel("."))')
176    feed_command('/hh$')
177    feed_command('call append("$", foldlevel("."))')
178    feed_command('/ii$')
179    feed_command('call append("$", foldlevel("."))')
180    feed_command('/kk$')
181    feed_command('call append("$", foldlevel("."))')
182
183    expect_any([[
184      expr 2
185      1
186      2
187      0]])
188  end)
189
190  it('can be opened after :move', function()
191    -- luacheck: ignore
192    screen:try_resize(35, 8)
193    insert([[
194      Test fdm=indent and :move bug END
195      line2
196      	Test fdm=indent START
197      	line3
198      	line4]])
199    feed_command('set noai nosta ')
200    feed_command('set fdm=indent')
201    feed_command('1m1')
202    feed('2jzc')
203    feed_command('m0')
204    feed('zR')
205
206    expect_any([[
207      	Test fdm=indent START
208      	line3
209      	line4
210      Test fdm=indent and :move bug END
211      line2]])
212  end)
213
214  -- oldtest: Test_folds_with_rnu()
215  it('with relative line numbers', function()
216    command('set fdm=marker rnu foldcolumn=2')
217    command('call setline(1, ["{{{1", "nline 1", "{{{1", "line 2"])')
218
219    screen:expect([[
220      {7:+ }{8:  0 }{13:^+--  2 lines: ·························}|
221      {7:+ }{8:  1 }{13:+--  2 lines: ·························}|
222      {1:~                                            }|*5
223                                                   |
224    ]])
225    feed('j')
226    screen:expect([[
227      {7:+ }{8:  1 }{13:+--  2 lines: ·························}|
228      {7:+ }{8:  0 }{13:^+--  2 lines: ·························}|
229      {1:~                                            }|*5
230                                                   |
231    ]])
232  end)
233
234  -- oldtest: Test_foldclose_opt()
235  it('foldclose=all', function()
236    exec([[
237      set foldmethod=manual foldclose=all foldopen=all
238      call setline(1, ['one', 'two', 'three', 'four'])
239      2,3fold
240    ]])
241
242    screen:expect([[
243      ^one                                          |
244      {13:+--  2 lines: two····························}|
245      four                                         |
246      {1:~                                            }|*4
247                                                   |
248    ]])
249    feed('2G')
250    screen:expect([[
251      one                                          |
252      ^two                                          |
253      three                                        |
254      four                                         |
255      {1:~                                            }|*3
256                                                   |
257    ]])
258    feed('4G')
259    screen:expect([[
260      one                                          |
261      {13:+--  2 lines: two····························}|
262      ^four                                         |
263      {1:~                                            }|*4
264                                                   |
265    ]])
266    feed('3G')
267    screen:expect([[
268      one                                          |
269      two                                          |
270      ^three                                        |
271      four                                         |
272      {1:~                                            }|*3
273                                                   |
274    ]])
275    feed('1G')
276    screen:expect([[
277      ^one                                          |
278      {13:+--  2 lines: two····························}|
279      four                                         |
280      {1:~                                            }|*4
281                                                   |
282    ]])
283    feed('2G')
284    screen:expect([[
285      one                                          |
286      ^two                                          |
287      three                                        |
288      four                                         |
289      {1:~                                            }|*3
290                                                   |
291    ]])
292    feed('k')
293    screen:expect([[
294      ^one                                          |
295      {13:+--  2 lines: two····························}|
296      four                                         |
297      {1:~                                            }|*4
298                                                   |
299    ]])
300  end)
301
302  -- oldtest: Test_foldtext_and_fillchars_rightleft()
303  it("fold text is displayed properly with 'rightleft'", function()
304    screen:try_resize(70, 5)
305    exec([[
306      let longtext = 'Lorem ipsum dolor sit amet, consectetur adipiscing'
307      let g:multibyte = 'Lorem ipsum dolor sit amet'
308
309      call setline(1, [longtext, longtext, longtext])
310      1,2fold
311
312      setlocal rightleft
313      set noshowmode noshowcmd
314    ]])
315    screen:expect([[
316      {13:······gnicsipida rutetcesnoc ,tema tis rolod muspi meroL :senil 2  --^+}|
317                          gnicsipida rutetcesnoc ,tema tis rolod muspi meroL|
318      {1:                                                                     ~}|*2
319                                                                            |
320    ]])
321    command('call setline(1, [g:multibyte, g:multibyte, g:multibyte])')
322    screen:expect([[
323      {13:········tema tis rolod muspi meroL :senil 2  -^-+}|
324                            tema tis rolod muspi meroL|
325      {1:                                                                     ~}|*2
326                                                                            |
327    ]])
328  end)
329end)