blob: 44e54bb9addea213474b8118272410202b2e2a45 [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
8func Test_proptype_global()
9 call prop_type_add('comment', {'highlight': 'Directory', 'priority': 123, 'start_incl': 1, 'end_incl': 1})
10 let proptypes = prop_type_list()
11 call assert_equal(1, len(proptypes))
12 call assert_equal('comment', proptypes[0])
13
14 let proptype = prop_type_get('comment')
15 call assert_equal('Directory', proptype['highlight'])
16 call assert_equal(123, proptype['priority'])
17 call assert_equal(1, proptype['start_incl'])
18 call assert_equal(1, proptype['end_incl'])
19
20 call prop_type_delete('comment')
21 call assert_equal(0, len(prop_type_list()))
22
23 call prop_type_add('one', {})
24 call assert_equal(1, len(prop_type_list()))
25 let proptype = prop_type_get('one')
26 call assert_false(has_key(proptype, 'highlight'))
27 call assert_equal(0, proptype['priority'])
28 call assert_equal(0, proptype['start_incl'])
29 call assert_equal(0, proptype['end_incl'])
30
31 call prop_type_add('two', {})
32 call assert_equal(2, len(prop_type_list()))
33 call prop_type_delete('one')
34 call assert_equal(1, len(prop_type_list()))
35 call prop_type_delete('two')
36 call assert_equal(0, len(prop_type_list()))
37endfunc
38
39func Test_proptype_buf()
40 let bufnr = bufnr('')
41 call prop_type_add('comment', {'bufnr': bufnr, 'highlight': 'Directory', 'priority': 123, 'start_incl': 1, 'end_incl': 1})
42 let proptypes = prop_type_list({'bufnr': bufnr})
43 call assert_equal(1, len(proptypes))
44 call assert_equal('comment', proptypes[0])
45
46 let proptype = prop_type_get('comment', {'bufnr': bufnr})
47 call assert_equal('Directory', proptype['highlight'])
48 call assert_equal(123, proptype['priority'])
49 call assert_equal(1, proptype['start_incl'])
50 call assert_equal(1, proptype['end_incl'])
51
52 call prop_type_delete('comment', {'bufnr': bufnr})
53 call assert_equal(0, len(prop_type_list({'bufnr': bufnr})))
54
55 call prop_type_add('one', {'bufnr': bufnr})
56 let proptype = prop_type_get('one', {'bufnr': bufnr})
57 call assert_false(has_key(proptype, 'highlight'))
58 call assert_equal(0, proptype['priority'])
59 call assert_equal(0, proptype['start_incl'])
60 call assert_equal(0, proptype['end_incl'])
61
62 call prop_type_add('two', {'bufnr': bufnr})
63 call assert_equal(2, len(prop_type_list({'bufnr': bufnr})))
64 call prop_type_delete('one', {'bufnr': bufnr})
65 call assert_equal(1, len(prop_type_list({'bufnr': bufnr})))
66 call prop_type_delete('two', {'bufnr': bufnr})
67 call assert_equal(0, len(prop_type_list({'bufnr': bufnr})))
68endfunc
69
70func AddPropTypes()
71 call prop_type_add('one', {})
72 call prop_type_add('two', {})
73 call prop_type_add('three', {})
74 call prop_type_add('whole', {})
75endfunc
76
77func DeletePropTypes()
78 call prop_type_delete('one')
79 call prop_type_delete('two')
80 call prop_type_delete('three')
81 call prop_type_delete('whole')
82endfunc
83
84func SetupPropsInFirstLine()
85 call setline(1, 'one two three')
86 call prop_add(1, 1, {'length': 3, 'id': 11, 'type': 'one'})
87 call prop_add(1, 5, {'length': 3, 'id': 12, 'type': 'two'})
88 call prop_add(1, 8, {'length': 5, 'id': 13, 'type': 'three'})
89 call prop_add(1, 1, {'length': 13, 'id': 14, 'type': 'whole'})
90endfunc
91
92let s:expected_props = [{'col': 1, 'length': 13, 'id': 14, 'type': 'whole', 'start': 1, 'end': 1},
93 \ {'col': 1, 'length': 3, 'id': 11, 'type': 'one', 'start': 1, 'end': 1},
94 \ {'col': 5, 'length': 3, 'id': 12, 'type': 'two', 'start': 1, 'end': 1},
95 \ {'col': 8, 'length': 5, 'id': 13, 'type': 'three', 'start': 1, 'end': 1},
96 \ ]
97
98func Test_prop_add()
99 new
100 call AddPropTypes()
101 call SetupPropsInFirstLine()
102 call assert_equal(s:expected_props, prop_list(1))
103 call assert_fails("call prop_add(10, 1, {'length': 1, 'id': 14, 'type': 'whole'})", 'E966:')
104 call assert_fails("call prop_add(1, 22, {'length': 1, 'id': 14, 'type': 'whole'})", 'E964:')
105
106 " Insert a line above, text props must still be there.
107 call append(0, 'empty')
108 call assert_equal(s:expected_props, prop_list(2))
109 " Delete a line above, text props must still be there.
110 1del
111 call assert_equal(s:expected_props, prop_list(1))
112
113 call DeletePropTypes()
114 bwipe!
115endfunc
116
117func Test_prop_remove()
118 new
119 call AddPropTypes()
120 call SetupPropsInFirstLine()
121 let props = deepcopy(s:expected_props)
122 call assert_equal(props, prop_list(1))
123
124 " remove by id
125 call prop_remove({'id': 12}, 1)
126 unlet props[2]
127 call assert_equal(props, prop_list(1))
128
129 " remove by type
130 call prop_remove({'type': 'one'}, 1)
131 unlet props[1]
132 call assert_equal(props, prop_list(1))
133
134 call DeletePropTypes()
135 bwipe!
136endfunc
137
138func Test_prop_add_remove_buf()
139 new
140 let bufnr = bufnr('')
141 call AddPropTypes()
142 call setline(1, 'one two three')
143 wincmd w
144 call prop_add(1, 1, {'length': 3, 'id': 11, 'type': 'one', 'bufnr': bufnr})
145 call prop_add(1, 5, {'length': 3, 'id': 12, 'type': 'two', 'bufnr': bufnr})
146 call prop_add(1, 11, {'length': 3, 'id': 13, 'type': 'three', 'bufnr': bufnr})
147
148 let props = [
149 \ {'col': 1, 'length': 3, 'id': 11, 'type': 'one', 'start': 1, 'end': 1},
150 \ {'col': 5, 'length': 3, 'id': 12, 'type': 'two', 'start': 1, 'end': 1},
151 \ {'col': 11, 'length': 3, 'id': 13, 'type': 'three', 'start': 1, 'end': 1},
152 \]
153 call assert_equal(props, prop_list(1, {'bufnr': bufnr}))
154
155 " remove by id
156 call prop_remove({'id': 12, 'bufnr': bufnr}, 1)
157 unlet props[1]
158 call assert_equal(props, prop_list(1, {'bufnr': bufnr}))
159
160 " remove by type
161 call prop_remove({'type': 'one', 'bufnr': bufnr}, 1)
162 unlet props[0]
163 call assert_equal(props, prop_list(1, {'bufnr': bufnr}))
164
165 call DeletePropTypes()
166 wincmd w
167 bwipe!
168endfunc
169
170
171func Test_prop_clear()
172 new
173 call AddPropTypes()
174 call SetupPropsInFirstLine()
175 call assert_equal(s:expected_props, prop_list(1))
176
177 call prop_clear(1)
178 call assert_equal([], prop_list(1))
179
180 call DeletePropTypes()
181 bwipe!
182endfunc
183
184func Test_prop_clear_buf()
185 new
186 call AddPropTypes()
187 call SetupPropsInFirstLine()
188 let bufnr = bufnr('')
189 wincmd w
190 call assert_equal(s:expected_props, prop_list(1, {'bufnr': bufnr}))
191
192 call prop_clear(1, 1, {'bufnr': bufnr})
193 call assert_equal([], prop_list(1, {'bufnr': bufnr}))
194
195 wincmd w
196 call DeletePropTypes()
197 bwipe!
198endfunc
199
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100200" Setup a three line prop in lines 2 - 4.
201" Add short props in line 1 and 5.
202func Setup_three_line_prop()
203 new
204 call setline(1, ['one', 'twotwo', 'three', 'fourfour', 'five'])
205 call prop_add(1, 2, {'length': 1, 'type': 'comment'})
206 call prop_add(2, 4, {'end_lnum': 4, 'end_col': 5, 'type': 'comment'})
207 call prop_add(5, 2, {'length': 1, 'type': 'comment'})
208endfunc
209
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100210func Test_prop_multiline()
211 call prop_type_add('comment', {'highlight': 'Directory'})
212 new
213 call setline(1, ['xxxxxxx', 'yyyyyyyyy', 'zzzzzzzz'])
214
215 " start halfway line 1, end halfway line 3
216 call prop_add(1, 3, {'end_lnum': 3, 'end_col': 5, 'type': 'comment'})
217 let expect1 = {'col': 3, 'length': 6, 'type': 'comment', 'start': 1, 'end': 0, 'id': 0}
218 call assert_equal([expect1], prop_list(1))
219 let expect2 = {'col': 1, 'length': 10, 'type': 'comment', 'start': 0, 'end': 0, 'id': 0}
220 call assert_equal([expect2], prop_list(2))
221 let expect3 = {'col': 1, 'length': 5, 'type': 'comment', 'start': 0, 'end': 1, 'id': 0}
222 call assert_equal([expect3], prop_list(3))
223 call prop_clear(1, 3)
224
225 " include all three lines
226 call prop_add(1, 1, {'end_lnum': 3, 'end_col': 999, 'type': 'comment'})
227 let expect1.col = 1
228 let expect1.length = 8
229 call assert_equal([expect1], prop_list(1))
230 call assert_equal([expect2], prop_list(2))
231 let expect3.length = 9
232 call assert_equal([expect3], prop_list(3))
233 call prop_clear(1, 3)
234
235 bwipe!
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100236
237 " Test deleting the first line with a prop.
238 call Setup_three_line_prop()
239 let expect2 = {'col': 4, 'length': 4, 'type': 'comment', 'start': 1, 'end': 0, 'id': 0}
240 call assert_equal([expect2], prop_list(2))
241 2del
242 let expect_short = {'col': 2, 'length': 1, 'type': 'comment', 'start': 1, 'end': 1, 'id': 0}
243 call assert_equal([expect_short], prop_list(1))
244 let expect2 = {'col': 1, 'length': 6, 'type': 'comment', 'start': 1, 'end': 0, 'id': 0}
245 call assert_equal([expect2], prop_list(2))
246 bwipe!
247
248 " Test deleting the last line with a prop.
249 call Setup_three_line_prop()
250 let expect3 = {'col': 1, 'length': 6, 'type': 'comment', 'start': 0, 'end': 0, 'id': 0}
251 call assert_equal([expect3], prop_list(3))
252 let expect4 = {'col': 1, 'length': 5, 'type': 'comment', 'start': 0, 'end': 1, 'id': 0}
253 call assert_equal([expect4], prop_list(4))
254 4del
255 let expect3 = {'col': 1, 'length': 6, 'type': 'comment', 'start': 0, 'end': 1, 'id': 0}
256 call assert_equal([expect3], prop_list(3))
257 call assert_equal([expect_short], prop_list(4))
258 bwipe!
259
Bram Moolenaarb56ac042018-12-28 23:22:40 +0100260 " Test appending a line below the text prop start.
261 call Setup_three_line_prop()
262 let expect2 = {'col': 4, 'length': 4, 'type': 'comment', 'start': 1, 'end': 0, 'id': 0}
263 call assert_equal([expect2], prop_list(2))
264 call append(2, "new line")
265 call assert_equal([expect2], prop_list(2))
266 let expect3 = {'col': 1, 'length': 9, 'type': 'comment', 'start': 0, 'end': 0, 'id': 0}
267 call assert_equal([expect3], prop_list(3))
268 bwipe!
269
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100270 call prop_type_delete('comment')
271endfunc
272
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100273func Test_prop_byteoff()
274 call prop_type_add('comment', {'highlight': 'Directory'})
275 new
Bram Moolenaar00b1e042018-12-26 23:42:10 +0100276 call setline(1, ['line1', 'second line', ''])
Bram Moolenaar8cf734e2018-12-26 01:09:00 +0100277 set ff=unix
Bram Moolenaar00b1e042018-12-26 23:42:10 +0100278 call assert_equal(19, line2byte(3))
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100279 call prop_add(1, 1, {'end_col': 3, 'type': 'comment'})
Bram Moolenaar00b1e042018-12-26 23:42:10 +0100280 call assert_equal(19, line2byte(3))
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100281
282 bwipe!
283 call prop_type_delete('comment')
284endfunc
285
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100286
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100287" TODO: screenshot test with highlighting