blob: 737ff209ee09a9fd95e461a55bbb1ea47f3e42fe [file] [log] [blame]
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001" Tests for defining text property types and adding text properties to the
2" buffer.
3
4if !has('textprop')
5 finish
6endif
7
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01008source screendump.vim
9
Bram Moolenaarb9c67a52019-01-01 19:49:20 +010010" test length zero
11
Bram Moolenaar98aefe72018-12-13 22:20:09 +010012func Test_proptype_global()
13 call prop_type_add('comment', {'highlight': 'Directory', 'priority': 123, 'start_incl': 1, 'end_incl': 1})
14 let proptypes = prop_type_list()
15 call assert_equal(1, len(proptypes))
16 call assert_equal('comment', proptypes[0])
17
18 let proptype = prop_type_get('comment')
19 call assert_equal('Directory', proptype['highlight'])
20 call assert_equal(123, proptype['priority'])
21 call assert_equal(1, proptype['start_incl'])
22 call assert_equal(1, proptype['end_incl'])
23
24 call prop_type_delete('comment')
25 call assert_equal(0, len(prop_type_list()))
26
27 call prop_type_add('one', {})
28 call assert_equal(1, len(prop_type_list()))
29 let proptype = prop_type_get('one')
30 call assert_false(has_key(proptype, 'highlight'))
31 call assert_equal(0, proptype['priority'])
32 call assert_equal(0, proptype['start_incl'])
33 call assert_equal(0, proptype['end_incl'])
34
35 call prop_type_add('two', {})
36 call assert_equal(2, len(prop_type_list()))
37 call prop_type_delete('one')
38 call assert_equal(1, len(prop_type_list()))
39 call prop_type_delete('two')
40 call assert_equal(0, len(prop_type_list()))
41endfunc
42
43func Test_proptype_buf()
44 let bufnr = bufnr('')
45 call prop_type_add('comment', {'bufnr': bufnr, 'highlight': 'Directory', 'priority': 123, 'start_incl': 1, 'end_incl': 1})
46 let proptypes = prop_type_list({'bufnr': bufnr})
47 call assert_equal(1, len(proptypes))
48 call assert_equal('comment', proptypes[0])
49
50 let proptype = prop_type_get('comment', {'bufnr': bufnr})
51 call assert_equal('Directory', proptype['highlight'])
52 call assert_equal(123, proptype['priority'])
53 call assert_equal(1, proptype['start_incl'])
54 call assert_equal(1, proptype['end_incl'])
55
56 call prop_type_delete('comment', {'bufnr': bufnr})
57 call assert_equal(0, len(prop_type_list({'bufnr': bufnr})))
58
59 call prop_type_add('one', {'bufnr': bufnr})
60 let proptype = prop_type_get('one', {'bufnr': bufnr})
61 call assert_false(has_key(proptype, 'highlight'))
62 call assert_equal(0, proptype['priority'])
63 call assert_equal(0, proptype['start_incl'])
64 call assert_equal(0, proptype['end_incl'])
65
66 call prop_type_add('two', {'bufnr': bufnr})
67 call assert_equal(2, len(prop_type_list({'bufnr': bufnr})))
68 call prop_type_delete('one', {'bufnr': bufnr})
69 call assert_equal(1, len(prop_type_list({'bufnr': bufnr})))
70 call prop_type_delete('two', {'bufnr': bufnr})
71 call assert_equal(0, len(prop_type_list({'bufnr': bufnr})))
72endfunc
73
74func AddPropTypes()
75 call prop_type_add('one', {})
76 call prop_type_add('two', {})
77 call prop_type_add('three', {})
78 call prop_type_add('whole', {})
79endfunc
80
81func DeletePropTypes()
82 call prop_type_delete('one')
83 call prop_type_delete('two')
84 call prop_type_delete('three')
85 call prop_type_delete('whole')
86endfunc
87
88func SetupPropsInFirstLine()
89 call setline(1, 'one two three')
90 call prop_add(1, 1, {'length': 3, 'id': 11, 'type': 'one'})
91 call prop_add(1, 5, {'length': 3, 'id': 12, 'type': 'two'})
92 call prop_add(1, 8, {'length': 5, 'id': 13, 'type': 'three'})
93 call prop_add(1, 1, {'length': 13, 'id': 14, 'type': 'whole'})
94endfunc
95
96let s:expected_props = [{'col': 1, 'length': 13, 'id': 14, 'type': 'whole', 'start': 1, 'end': 1},
97 \ {'col': 1, 'length': 3, 'id': 11, 'type': 'one', 'start': 1, 'end': 1},
98 \ {'col': 5, 'length': 3, 'id': 12, 'type': 'two', 'start': 1, 'end': 1},
99 \ {'col': 8, 'length': 5, 'id': 13, 'type': 'three', 'start': 1, 'end': 1},
100 \ ]
101
102func Test_prop_add()
103 new
104 call AddPropTypes()
105 call SetupPropsInFirstLine()
106 call assert_equal(s:expected_props, prop_list(1))
107 call assert_fails("call prop_add(10, 1, {'length': 1, 'id': 14, 'type': 'whole'})", 'E966:')
108 call assert_fails("call prop_add(1, 22, {'length': 1, 'id': 14, 'type': 'whole'})", 'E964:')
109
110 " Insert a line above, text props must still be there.
111 call append(0, 'empty')
112 call assert_equal(s:expected_props, prop_list(2))
113 " Delete a line above, text props must still be there.
114 1del
115 call assert_equal(s:expected_props, prop_list(1))
116
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100117 " Prop without length or end column is zero length
118 call prop_clear(1)
119 call prop_add(1, 5, {'type': 'two'})
120 let expected = [{'col': 5, 'length': 0, 'type': 'two', 'id': 0, 'start': 1, 'end': 1}]
121 call assert_equal(expected, prop_list(1))
122
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100123 call DeletePropTypes()
124 bwipe!
125endfunc
126
127func Test_prop_remove()
128 new
129 call AddPropTypes()
130 call SetupPropsInFirstLine()
131 let props = deepcopy(s:expected_props)
132 call assert_equal(props, prop_list(1))
133
134 " remove by id
135 call prop_remove({'id': 12}, 1)
136 unlet props[2]
137 call assert_equal(props, prop_list(1))
138
139 " remove by type
140 call prop_remove({'type': 'one'}, 1)
141 unlet props[1]
142 call assert_equal(props, prop_list(1))
143
144 call DeletePropTypes()
145 bwipe!
146endfunc
147
148func Test_prop_add_remove_buf()
149 new
150 let bufnr = bufnr('')
151 call AddPropTypes()
152 call setline(1, 'one two three')
153 wincmd w
154 call prop_add(1, 1, {'length': 3, 'id': 11, 'type': 'one', 'bufnr': bufnr})
155 call prop_add(1, 5, {'length': 3, 'id': 12, 'type': 'two', 'bufnr': bufnr})
156 call prop_add(1, 11, {'length': 3, 'id': 13, 'type': 'three', 'bufnr': bufnr})
157
158 let props = [
159 \ {'col': 1, 'length': 3, 'id': 11, 'type': 'one', 'start': 1, 'end': 1},
160 \ {'col': 5, 'length': 3, 'id': 12, 'type': 'two', 'start': 1, 'end': 1},
161 \ {'col': 11, 'length': 3, 'id': 13, 'type': 'three', 'start': 1, 'end': 1},
162 \]
163 call assert_equal(props, prop_list(1, {'bufnr': bufnr}))
164
165 " remove by id
166 call prop_remove({'id': 12, 'bufnr': bufnr}, 1)
167 unlet props[1]
168 call assert_equal(props, prop_list(1, {'bufnr': bufnr}))
169
170 " remove by type
171 call prop_remove({'type': 'one', 'bufnr': bufnr}, 1)
172 unlet props[0]
173 call assert_equal(props, prop_list(1, {'bufnr': bufnr}))
174
175 call DeletePropTypes()
176 wincmd w
177 bwipe!
178endfunc
179
Bram Moolenaar33c8ca92019-01-02 18:00:27 +0100180func Test_prop_backspace()
181 new
182 set bs=2
183 call setline(1, 'xonex xtwoxx')
184 call AddPropTypes()
185 call prop_add(1, 2, {'length': 3, 'id': 11, 'type': 'one'})
186 call prop_add(1, 8, {'length': 3, 'id': 12, 'type': 'two'})
187 let expected = [
188 \ {'col': 2, 'length': 3, 'id': 11, 'type': 'one', 'start': 1, 'end': 1},
189 \ {'col': 8, 'length': 3, 'id': 12, 'type': 'two', 'start': 1, 'end': 1},
190 \]
191 call assert_equal(expected, prop_list(1))
192
193 exe "normal 0li\<BS>\<Esc>fxli\<BS>\<Esc>"
194 call assert_equal('one xtwoxx', getline(1))
195 let expected[0].col = 1
196 let expected[1].col = 6
197 call assert_equal(expected, prop_list(1))
198
199 call DeletePropTypes()
200 bwipe!
201 set bs&
202endfunc
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100203
204func Test_prop_clear()
205 new
206 call AddPropTypes()
207 call SetupPropsInFirstLine()
208 call assert_equal(s:expected_props, prop_list(1))
209
210 call prop_clear(1)
211 call assert_equal([], prop_list(1))
212
213 call DeletePropTypes()
214 bwipe!
215endfunc
216
217func Test_prop_clear_buf()
218 new
219 call AddPropTypes()
220 call SetupPropsInFirstLine()
221 let bufnr = bufnr('')
222 wincmd w
223 call assert_equal(s:expected_props, prop_list(1, {'bufnr': bufnr}))
224
225 call prop_clear(1, 1, {'bufnr': bufnr})
226 call assert_equal([], prop_list(1, {'bufnr': bufnr}))
227
228 wincmd w
229 call DeletePropTypes()
230 bwipe!
231endfunc
232
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100233" Setup a three line prop in lines 2 - 4.
234" Add short props in line 1 and 5.
235func Setup_three_line_prop()
236 new
237 call setline(1, ['one', 'twotwo', 'three', 'fourfour', 'five'])
238 call prop_add(1, 2, {'length': 1, 'type': 'comment'})
239 call prop_add(2, 4, {'end_lnum': 4, 'end_col': 5, 'type': 'comment'})
240 call prop_add(5, 2, {'length': 1, 'type': 'comment'})
241endfunc
242
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100243func Test_prop_multiline()
244 call prop_type_add('comment', {'highlight': 'Directory'})
245 new
246 call setline(1, ['xxxxxxx', 'yyyyyyyyy', 'zzzzzzzz'])
247
248 " start halfway line 1, end halfway line 3
249 call prop_add(1, 3, {'end_lnum': 3, 'end_col': 5, 'type': 'comment'})
250 let expect1 = {'col': 3, 'length': 6, 'type': 'comment', 'start': 1, 'end': 0, 'id': 0}
251 call assert_equal([expect1], prop_list(1))
252 let expect2 = {'col': 1, 'length': 10, 'type': 'comment', 'start': 0, 'end': 0, 'id': 0}
253 call assert_equal([expect2], prop_list(2))
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100254 let expect3 = {'col': 1, 'length': 4, 'type': 'comment', 'start': 0, 'end': 1, 'id': 0}
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100255 call assert_equal([expect3], prop_list(3))
256 call prop_clear(1, 3)
257
258 " include all three lines
259 call prop_add(1, 1, {'end_lnum': 3, 'end_col': 999, 'type': 'comment'})
260 let expect1.col = 1
261 let expect1.length = 8
262 call assert_equal([expect1], prop_list(1))
263 call assert_equal([expect2], prop_list(2))
264 let expect3.length = 9
265 call assert_equal([expect3], prop_list(3))
266 call prop_clear(1, 3)
267
268 bwipe!
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100269
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100270 " Test deleting the first line of a multi-line prop.
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100271 call Setup_three_line_prop()
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100272 let expect_short = {'col': 2, 'length': 1, 'type': 'comment', 'start': 1, 'end': 1, 'id': 0}
273 call assert_equal([expect_short], prop_list(1))
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100274 let expect2 = {'col': 4, 'length': 4, 'type': 'comment', 'start': 1, 'end': 0, 'id': 0}
275 call assert_equal([expect2], prop_list(2))
276 2del
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100277 call assert_equal([expect_short], prop_list(1))
278 let expect2 = {'col': 1, 'length': 6, 'type': 'comment', 'start': 1, 'end': 0, 'id': 0}
279 call assert_equal([expect2], prop_list(2))
280 bwipe!
281
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100282 " Test deleting the last line of a multi-line prop.
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100283 call Setup_three_line_prop()
284 let expect3 = {'col': 1, 'length': 6, 'type': 'comment', 'start': 0, 'end': 0, 'id': 0}
285 call assert_equal([expect3], prop_list(3))
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100286 let expect4 = {'col': 1, 'length': 4, 'type': 'comment', 'start': 0, 'end': 1, 'id': 0}
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100287 call assert_equal([expect4], prop_list(4))
288 4del
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100289 let expect3.end = 1
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100290 call assert_equal([expect3], prop_list(3))
291 call assert_equal([expect_short], prop_list(4))
292 bwipe!
293
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100294 " Test appending a line below the multi-line text prop start.
Bram Moolenaarb56ac042018-12-28 23:22:40 +0100295 call Setup_three_line_prop()
296 let expect2 = {'col': 4, 'length': 4, 'type': 'comment', 'start': 1, 'end': 0, 'id': 0}
297 call assert_equal([expect2], prop_list(2))
298 call append(2, "new line")
299 call assert_equal([expect2], prop_list(2))
300 let expect3 = {'col': 1, 'length': 9, 'type': 'comment', 'start': 0, 'end': 0, 'id': 0}
301 call assert_equal([expect3], prop_list(3))
302 bwipe!
303
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100304 call prop_type_delete('comment')
305endfunc
306
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100307func Test_prop_byteoff()
308 call prop_type_add('comment', {'highlight': 'Directory'})
309 new
Bram Moolenaar00b1e042018-12-26 23:42:10 +0100310 call setline(1, ['line1', 'second line', ''])
Bram Moolenaar8cf734e2018-12-26 01:09:00 +0100311 set ff=unix
Bram Moolenaar00b1e042018-12-26 23:42:10 +0100312 call assert_equal(19, line2byte(3))
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100313 call prop_add(1, 1, {'end_col': 3, 'type': 'comment'})
Bram Moolenaar00b1e042018-12-26 23:42:10 +0100314 call assert_equal(19, line2byte(3))
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100315
316 bwipe!
317 call prop_type_delete('comment')
318endfunc
319
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100320" screenshot test with textprop highlighting
321funct Test_textprop_screenshots()
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100322 if !CanRunVimInTerminal() || &encoding != 'utf-8'
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100323 return
324 endif
325 call writefile([
Bram Moolenaar44746aa2019-01-02 00:02:11 +0100326 \ "call setline(1, ['One two', 'Numbér 123 änd thœn 4¾7.', '--aa--bb--cc--dd--'])",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100327 \ "hi NumberProp ctermfg=blue",
328 \ "hi LongProp ctermbg=yellow",
329 \ "call prop_type_add('number', {'highlight': 'NumberProp'})",
330 \ "call prop_type_add('long', {'highlight': 'LongProp'})",
Bram Moolenaar44746aa2019-01-02 00:02:11 +0100331 \ "call prop_type_add('start', {'highlight': 'NumberProp', 'start_incl': 1})",
332 \ "call prop_type_add('end', {'highlight': 'NumberProp', 'end_incl': 1})",
333 \ "call prop_type_add('both', {'highlight': 'NumberProp', 'start_incl': 1, 'end_incl': 1})",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100334 \ "call prop_add(1, 4, {'end_lnum': 3, 'end_col': 3, 'type': 'long'})",
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100335 \ "call prop_add(2, 9, {'length': 3, 'type': 'number'})",
336 \ "call prop_add(2, 24, {'length': 4, 'type': 'number'})",
Bram Moolenaar44746aa2019-01-02 00:02:11 +0100337 \ "call prop_add(3, 3, {'length': 2, 'type': 'number'})",
338 \ "call prop_add(3, 7, {'length': 2, 'type': 'start'})",
339 \ "call prop_add(3, 11, {'length': 2, 'type': 'end'})",
340 \ "call prop_add(3, 15, {'length': 2, 'type': 'both'})",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100341 \ "set number",
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100342 \ "hi clear SpellBad",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100343 \ "set spell",
Bram Moolenaar44746aa2019-01-02 00:02:11 +0100344 \ "normal 3G0llix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>",
Bram Moolenaar33c8ca92019-01-02 18:00:27 +0100345 \ "normal 3G0lli\<BS>\<Esc>",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100346 \], 'XtestProp')
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100347 let buf = RunVimInTerminal('-S XtestProp', {'rows': 6})
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100348 call VerifyScreenDump(buf, 'Test_textprop_01', {})
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100349
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100350 " clean up
351 call StopVimInTerminal(buf)
Bram Moolenaar2f21fa82018-12-31 20:05:56 +0100352 call delete('XtestProp')
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100353endfunc