blob: 3c27f4f70099bb3f239a08a8f4fe86b7cad9a4a1 [file] [log] [blame]
Bram Moolenaarded27822017-01-02 14:27:34 +01001" Test for folding
2
Bram Moolenaar1159b162017-02-28 21:53:56 +01003func! Test_address_fold()
Bram Moolenaarded27822017-01-02 14:27:34 +01004 new
5 call setline(1, ['int FuncName() {/*{{{*/', 1, 2, 3, 4, 5, '}/*}}}*/',
6 \ 'after fold 1', 'after fold 2', 'after fold 3'])
7 setl fen fdm=marker
8 " The next ccommands should all copy the same part of the buffer,
9 " regardless of the adressing type, since the part to be copied
10 " is folded away
11 :1y
12 call assert_equal(['int FuncName() {/*{{{*/', '1', '2', '3', '4', '5', '}/*}}}*/'], getreg(0,1,1))
13 :.y
14 call assert_equal(['int FuncName() {/*{{{*/', '1', '2', '3', '4', '5', '}/*}}}*/'], getreg(0,1,1))
15 :.+y
16 call assert_equal(['int FuncName() {/*{{{*/', '1', '2', '3', '4', '5', '}/*}}}*/'], getreg(0,1,1))
17 :.,.y
18 call assert_equal(['int FuncName() {/*{{{*/', '1', '2', '3', '4', '5', '}/*}}}*/'], getreg(0,1,1))
19 :sil .1,.y
20 call assert_equal(['int FuncName() {/*{{{*/', '1', '2', '3', '4', '5', '}/*}}}*/'], getreg(0,1,1))
21 " use silent to make E493 go away
22 :sil .+,.y
23 call assert_equal(['int FuncName() {/*{{{*/', '1', '2', '3', '4', '5', '}/*}}}*/'], getreg(0,1,1))
24 :,y
25 call assert_equal(['int FuncName() {/*{{{*/', '1', '2', '3', '4', '5', '}/*}}}*/'], getreg(0,1,1))
26 :,+y
27 call assert_equal(['int FuncName() {/*{{{*/', '1', '2', '3', '4', '5', '}/*}}}*/','after fold 1'], getreg(0,1,1))
28 " using .+3 as second address should copy the whole folded line + the next 3
29 " lines
30 :.,+3y
31 call assert_equal(['int FuncName() {/*{{{*/', '1', '2', '3', '4', '5', '}/*}}}*/',
32 \ 'after fold 1', 'after fold 2', 'after fold 3'], getreg(0,1,1))
33 :sil .,-2y
34 call assert_equal(['int FuncName() {/*{{{*/', '1', '2', '3', '4', '5', '}/*}}}*/'], getreg(0,1,1))
35
36 " now test again with folding disabled
37 set nofoldenable
38 :1y
39 call assert_equal(['int FuncName() {/*{{{*/'], getreg(0,1,1))
40 :.y
41 call assert_equal(['int FuncName() {/*{{{*/'], getreg(0,1,1))
42 :.+y
43 call assert_equal(['1'], getreg(0,1,1))
44 :.,.y
45 call assert_equal(['int FuncName() {/*{{{*/'], getreg(0,1,1))
46 " use silent to make E493 go away
47 :sil .1,.y
48 call assert_equal(['int FuncName() {/*{{{*/', '1'], getreg(0,1,1))
49 " use silent to make E493 go away
50 :sil .+,.y
51 call assert_equal(['int FuncName() {/*{{{*/', '1'], getreg(0,1,1))
52 :,y
53 call assert_equal(['int FuncName() {/*{{{*/'], getreg(0,1,1))
54 :,+y
55 call assert_equal(['int FuncName() {/*{{{*/', '1'], getreg(0,1,1))
56 " using .+3 as second address should copy the whole folded line + the next 3
57 " lines
58 :.,+3y
59 call assert_equal(['int FuncName() {/*{{{*/', '1', '2', '3'], getreg(0,1,1))
60 :7
61 :sil .,-2y
62 call assert_equal(['4', '5', '}/*}}}*/'], getreg(0,1,1))
63
64 quit!
Bram Moolenaar1159b162017-02-28 21:53:56 +010065endfunc
Bram Moolenaar54b2bfa2017-01-02 14:57:08 +010066
Bram Moolenaar1159b162017-02-28 21:53:56 +010067func! Test_indent_fold()
Bram Moolenaar54b2bfa2017-01-02 14:57:08 +010068 new
69 call setline(1, ['', 'a', ' b', ' c'])
70 setl fen fdm=indent
71 2
72 norm! >>
73 let a=map(range(1,4), 'foldclosed(v:val)')
74 call assert_equal([-1,-1,-1,-1], a)
Bram Moolenaar1159b162017-02-28 21:53:56 +010075endfunc
Bram Moolenaar54b2bfa2017-01-02 14:57:08 +010076
Bram Moolenaar1159b162017-02-28 21:53:56 +010077func! Test_indent_fold()
Bram Moolenaar54b2bfa2017-01-02 14:57:08 +010078 new
79 call setline(1, ['', 'a', ' b', ' c'])
80 setl fen fdm=indent
81 2
82 norm! >>
83 let a=map(range(1,4), 'foldclosed(v:val)')
84 call assert_equal([-1,-1,-1,-1], a)
85 bw!
Bram Moolenaar1159b162017-02-28 21:53:56 +010086endfunc
Bram Moolenaar54b2bfa2017-01-02 14:57:08 +010087
Bram Moolenaar1159b162017-02-28 21:53:56 +010088func! Test_indent_fold2()
Bram Moolenaar54b2bfa2017-01-02 14:57:08 +010089 new
90 call setline(1, ['', '{{{', '}}}', '{{{', '}}}'])
91 setl fen fdm=marker
92 2
93 norm! >>
94 let a=map(range(1,5), 'foldclosed(v:val)')
95 call assert_equal([-1,-1,-1,4,4], a)
96 bw!
Bram Moolenaar1159b162017-02-28 21:53:56 +010097endfunc
98
99func Test_manual_fold_with_filter()
100 if !executable('cat')
101 return
102 endif
Bram Moolenaar3f3897e2017-03-04 15:28:53 +0100103 for type in ['manual', 'marker']
104 exe 'set foldmethod=' . type
105 new
106 call setline(1, range(1, 20))
107 4,$fold
108 %foldopen
109 10,$fold
110 %foldopen
111 " This filter command should not have an effect
112 1,8! cat
113 call feedkeys('5ggzdzMGdd', 'xt')
114 call assert_equal(['1', '2', '3', '4', '5', '6', '7', '8', '9'], getline(1, '$'))
115
116 bwipe!
117 set foldmethod&
118 endfor
Bram Moolenaar1159b162017-02-28 21:53:56 +0100119endfunc
Bram Moolenaareadbc2b2017-03-04 18:42:39 +0100120
121func! Test_indent_fold_with_read()
122 new
123 set foldmethod=indent
124 call setline(1, repeat(["\<Tab>a"], 4))
125 for n in range(1, 4)
126 call assert_equal(1, foldlevel(n))
127 endfor
128
129 call writefile(["a", "", "\<Tab>a"], 'Xfile')
130 foldopen
131 2read Xfile
132 %foldclose
133 call assert_equal(1, foldlevel(1))
134 call assert_equal(2, foldclosedend(1))
135 call assert_equal(0, foldlevel(3))
136 call assert_equal(0, foldlevel(4))
137 call assert_equal(1, foldlevel(5))
138 call assert_equal(7, foldclosedend(5))
139
140 bwipe!
141 set foldmethod&
142 call delete('Xfile')
143endfunc
144
145func Test_combining_folds_indent()
146 new
147 let one = "\<Tab>a"
148 let zero = 'a'
149 call setline(1, [one, one, zero, zero, zero, one, one, one])
150 set foldmethod=indent
151 3,5d
152 %foldclose
153 call assert_equal(5, foldclosedend(1))
154
155 set foldmethod&
156 bwipe!
157endfunc
158
159func Test_combining_folds_marker()
160 new
161 call setline(1, ['{{{', '}}}', '', '', '', '{{{', '', '}}}'])
162 set foldmethod=marker
163 3,5d
164 %foldclose
165 call assert_equal(2, foldclosedend(1))
166
167 set foldmethod&
168 bwipe!
169endfunc
170
171func s:TestFoldExpr(lnum)
172 let thisline = getline(a:lnum)
173 if thisline == 'a'
174 return 1
175 elseif thisline == 'b'
176 return 0
177 elseif thisline == 'c'
178 return '<1'
179 elseif thisline == 'd'
180 return '>1'
181 endif
182 return 0
183endfunction
184
185func Test_update_folds_expr_read()
186 new
187 call setline(1, ['a', 'a', 'a', 'a', 'a', 'a'])
188 set foldmethod=expr
189 set foldexpr=s:TestFoldExpr(v:lnum)
190 2
191 foldopen
192 call writefile(['b', 'b', 'a', 'a', 'd', 'a', 'a', 'c'], 'Xfile')
193 read Xfile
194 %foldclose
195 call assert_equal(2, foldclosedend(1))
196 call assert_equal(0, foldlevel(3))
197 call assert_equal(0, foldlevel(4))
198 call assert_equal(6, foldclosedend(5))
199 call assert_equal(10, foldclosedend(7))
200 call assert_equal(14, foldclosedend(11))
201
202 call delete('Xfile')
203 bwipe!
204 set foldmethod& foldexpr&
205endfunc