blob: ccf7d2846054712a0a99d3419915e6a13bea5137 [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 Moolenaare3d31b02018-12-24 23:07:04 +0100200func Test_prop_multiline()
201 call prop_type_add('comment', {'highlight': 'Directory'})
202 new
203 call setline(1, ['xxxxxxx', 'yyyyyyyyy', 'zzzzzzzz'])
204
205 " start halfway line 1, end halfway line 3
206 call prop_add(1, 3, {'end_lnum': 3, 'end_col': 5, 'type': 'comment'})
207 let expect1 = {'col': 3, 'length': 6, 'type': 'comment', 'start': 1, 'end': 0, 'id': 0}
208 call assert_equal([expect1], prop_list(1))
209 let expect2 = {'col': 1, 'length': 10, 'type': 'comment', 'start': 0, 'end': 0, 'id': 0}
210 call assert_equal([expect2], prop_list(2))
211 let expect3 = {'col': 1, 'length': 5, 'type': 'comment', 'start': 0, 'end': 1, 'id': 0}
212 call assert_equal([expect3], prop_list(3))
213 call prop_clear(1, 3)
214
215 " include all three lines
216 call prop_add(1, 1, {'end_lnum': 3, 'end_col': 999, 'type': 'comment'})
217 let expect1.col = 1
218 let expect1.length = 8
219 call assert_equal([expect1], prop_list(1))
220 call assert_equal([expect2], prop_list(2))
221 let expect3.length = 9
222 call assert_equal([expect3], prop_list(3))
223 call prop_clear(1, 3)
224
225 bwipe!
226 call prop_type_delete('comment')
227endfunc
228
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100229func Test_prop_byteoff()
230 call prop_type_add('comment', {'highlight': 'Directory'})
231 new
232 call setline(1, ['line1', 'line2', ''])
Bram Moolenaar8cf734e2018-12-26 01:09:00 +0100233 set ff=unix
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100234 call assert_equal(13, line2byte(3))
235 call prop_add(1, 1, {'end_col': 3, 'type': 'comment'})
236 call assert_equal(13, line2byte(3))
237
238 bwipe!
239 call prop_type_delete('comment')
240endfunc
241
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100242
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100243" TODO: screenshot test with highlighting