blob: c93f8b76d55e5f5b908d429a0f5066213f028b37 [file] [log] [blame]
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01001" Test for syntax and syntax iskeyword option
2
3if !has("syntax")
4 finish
5endif
6
7func GetSyntaxItem(pat)
8 let c = ''
9 let a = ['a', getreg('a'), getregtype('a')]
10 0
11 redraw!
12 call search(a:pat, 'W')
13 let synid = synID(line('.'), col('.'), 1)
14 while synid == synID(line('.'), col('.'), 1)
15 norm! v"ay
16 " stop at whitespace
17 if @a =~# '\s'
18 break
19 endif
20 let c .= @a
21 norm! l
22 endw
23 call call('setreg', a)
24 0
25 return c
26endfunc
27
28func Test_syn_iskeyword()
29 new
30 call setline(1, [
31 \ 'CREATE TABLE FOOBAR(',
32 \ ' DLTD_BY VARCHAR2(100)',
33 \ ');',
34 \ ''])
35
36 syntax on
37 set ft=sql
38 syn match SYN /C\k\+\>/
39 hi link SYN ErrorMsg
40 call assert_equal('DLTD_BY', GetSyntaxItem('DLTD'))
41 /\<D\k\+\>/:norm! ygn
42 call assert_equal('DLTD_BY', @0)
43 redir @c
44 syn iskeyword
45 redir END
46 call assert_equal("\nsyntax iskeyword not set", @c)
47
48 syn iskeyword @,48-57,_,192-255
49 redir @c
50 syn iskeyword
51 redir END
52 call assert_equal("\nsyntax iskeyword @,48-57,_,192-255", @c)
53
54 setlocal isk-=_
55 call assert_equal('DLTD_BY', GetSyntaxItem('DLTD'))
56 /\<D\k\+\>/:norm! ygn
Bram Moolenaar73b484c2016-12-11 15:11:17 +010057 let b2 = @0
Bram Moolenaarb8060fe2016-01-19 22:29:28 +010058 call assert_equal('DLTD', @0)
59
60 syn iskeyword clear
61 redir @c
62 syn iskeyword
63 redir END
64 call assert_equal("\nsyntax iskeyword not set", @c)
65
66 quit!
67endfunc
Bram Moolenaarc3691332016-04-20 12:49:49 +020068
69func Test_syntax_after_reload()
70 split Xsomefile
71 call setline(1, ['hello', 'there'])
72 w!
73 only!
74 setl filetype=hello
75 au FileType hello let g:gotit = 1
76 call assert_false(exists('g:gotit'))
77 edit other
78 buf Xsomefile
79 call assert_equal('hello', &filetype)
80 call assert_true(exists('g:gotit'))
81 call delete('Xsomefile')
82endfunc
Bram Moolenaar73b484c2016-12-11 15:11:17 +010083
84func Test_syntime()
85 if !has('profile')
Bram Moolenaar4c8980b2016-12-11 15:24:48 +010086 return
Bram Moolenaar73b484c2016-12-11 15:11:17 +010087 endif
88
89 syntax on
90 syntime on
91 let a = execute('syntime report')
92 call assert_equal("\nNo Syntax items defined for this buffer", a)
93
94 view ../memfile_test.c
95 setfiletype cpp
96 redraw
97 let a = execute('syntime report')
98 call assert_match('^ TOTAL *COUNT *MATCH *SLOWEST *AVERAGE *NAME *PATTERN', a)
99 call assert_match(' \d*\.\d* \+[^0]\d* .* cppRawString ', a)
100 call assert_match(' \d*\.\d* \+[^0]\d* .* cppNumber ', a)
101
102 syntime off
103 syntime clear
104 let a = execute('syntime report')
105 call assert_match('^ TOTAL *COUNT *MATCH *SLOWEST *AVERAGE *NAME *PATTERN', a)
106 call assert_notmatch('.* cppRawString *', a)
107 call assert_notmatch('.* cppNumber*', a)
108 call assert_notmatch('[1-9]', a)
109
110 call assert_fails('syntime abc', 'E475')
111
112 syntax clear
113 let a = execute('syntime report')
114 call assert_equal("\nNo Syntax items defined for this buffer", a)
115
116 bd
117endfunc
118
119func Test_syntax_list()
120 syntax on
121 let a = execute('syntax list')
122 call assert_equal("\nNo Syntax items defined for this buffer", a)
123
124 view ../memfile_test.c
125 setfiletype c
126
127 let a = execute('syntax list')
128 call assert_match('cInclude*', a)
129 call assert_match('cDefine', a)
130
131 let a = execute('syntax list cDefine')
132 call assert_notmatch('cInclude*', a)
133 call assert_match('cDefine', a)
134 call assert_match(' links to Macro$', a)
135
136 call assert_fails('syntax list ABCD', 'E28:')
137 call assert_fails('syntax list @ABCD', 'E392:')
138
139 syntax clear
140 let a = execute('syntax list')
141 call assert_equal("\nNo Syntax items defined for this buffer", a)
142
143 bd
144endfunc
145
146func Test_syntax_completion()
147 call feedkeys(":syn \<C-A>\<C-B>\"\<CR>", 'tx')
148 call assert_equal('"syn case clear cluster conceal enable include iskeyword keyword list manual match off on region reset spell sync', @:)
149
150 call feedkeys(":syn case \<C-A>\<C-B>\"\<CR>", 'tx')
151 call assert_equal('"syn case ignore match', @:)
152
Bram Moolenaar2d028392017-01-08 18:28:22 +0100153 call feedkeys(":syn spell \<C-A>\<C-B>\"\<CR>", 'tx')
154 call assert_equal('"syn spell default notoplevel toplevel', @:)
155
156 call feedkeys(":syn sync \<C-A>\<C-B>\"\<CR>", 'tx')
157 call assert_equal('"syn sync ccomment clear fromstart linebreaks= linecont lines= match maxlines= minlines= region', @:)
158
Bram Moolenaar73b484c2016-12-11 15:11:17 +0100159 call feedkeys(":syn list \<C-A>\<C-B>\"\<CR>", 'tx')
160 call assert_match('^"syn list Boolean Character ', @:)
161
162 call feedkeys(":syn match \<C-A>\<C-B>\"\<CR>", 'tx')
163 call assert_match('^"syn match Boolean Character ', @:)
164endfunc
Bram Moolenaarde318c52017-01-17 16:27:10 +0100165
166func Test_syntax_arg_skipped()
167 syn clear
168 syntax case ignore
169 if 0
170 syntax case match
171 endif
172 call assert_match('case ignore', execute('syntax case'))
173
174 syn keyword Foo foo
175 call assert_match('Foo', execute('syntax'))
176 syn clear
177 call assert_match('case match', execute('syntax case'))
178 call assert_notmatch('Foo', execute('syntax'))
179
180 if has('conceal')
181 syn clear
182 syntax conceal on
183 if 0
184 syntax conceal off
185 endif
186 call assert_match('conceal on', execute('syntax conceal'))
187 syn clear
188 call assert_match('conceal off', execute('syntax conceal'))
Bram Moolenaar58f60ca2017-01-17 17:19:00 +0100189
190 syntax conceal on
191 syntax conceal off
192 call assert_match('conceal off', execute('syntax conceal'))
Bram Moolenaarde318c52017-01-17 16:27:10 +0100193 endif
194
195 syntax region Tar start=/</ end=/>/
196 if 0
197 syntax region NotTest start=/</ end=/>/ contains=@Spell
198 endif
199 call assert_match('Tar', execute('syntax'))
200 call assert_notmatch('NotTest', execute('syntax'))
201 call assert_notmatch('Spell', execute('syntax'))
202
203 hi Foo ctermfg=blue
204 let a = execute('hi Foo')
205 if 0
206 syntax rest
207 endif
208 call assert_equal(a, execute('hi Foo'))
209
210 set ft=tags
211 syn off
212 if 0
213 syntax enable
214 endif
215 call assert_match('No Syntax items defined', execute('syntax'))
216 syntax enable
217 call assert_match('tagComment', execute('syntax'))
218 set ft=
219
220 syn clear
221 if 0
222 syntax include @Spell nothing
223 endif
224 call assert_notmatch('Spell', execute('syntax'))
225
226 syn clear
227 syn iskeyword 48-57,$,_
228 call assert_match('48-57,$,_', execute('syntax iskeyword'))
229 if 0
230 syn clear
231 syn iskeyword clear
232 endif
233 call assert_match('48-57,$,_', execute('syntax iskeyword'))
234 syn iskeyword clear
235 call assert_match('not set', execute('syntax iskeyword'))
236 syn iskeyword 48-57,$,_
237 syn clear
238 call assert_match('not set', execute('syntax iskeyword'))
239
240 syn clear
241 syn keyword Foo foo
242 if 0
243 syn keyword NotAdded bar
244 endif
245 call assert_match('Foo', execute('syntax'))
246 call assert_notmatch('NotAdded', execute('highlight'))
247
248 syn clear
249 syn keyword Foo foo
250 call assert_match('Foo', execute('syntax'))
251 call assert_match('Foo', execute('syntax list'))
252 call assert_notmatch('Foo', execute('if 0 | syntax | endif'))
253 call assert_notmatch('Foo', execute('if 0 | syntax list | endif'))
254
255 syn clear
256 syn match Fopi /asdf/
257 if 0
258 syn match Fopx /asdf/
259 endif
260 call assert_match('Fopi', execute('syntax'))
261 call assert_notmatch('Fopx', execute('syntax'))
262
263 syn clear
264 syn spell toplevel
265 call assert_match('spell toplevel', execute('syntax spell'))
266 if 0
267 syn spell notoplevel
268 endif
269 call assert_match('spell toplevel', execute('syntax spell'))
270 syn spell notoplevel
271 call assert_match('spell notoplevel', execute('syntax spell'))
272 syn spell default
273 call assert_match('spell default', execute('syntax spell'))
274
275 syn clear
276 if 0
277 syntax cluster Spell
278 endif
279 call assert_notmatch('Spell', execute('syntax'))
280
281 syn clear
282 syn keyword Foo foo
283 syn sync ccomment
284 syn sync maxlines=5
285 if 0
286 syn sync maxlines=11
287 endif
288 call assert_match('on C-style comments', execute('syntax sync'))
289 call assert_match('maximal 5 lines', execute('syntax sync'))
Bram Moolenaar58f60ca2017-01-17 17:19:00 +0100290 syn sync clear
Bram Moolenaarde318c52017-01-17 16:27:10 +0100291 if 0
292 syn sync ccomment
293 endif
294 call assert_notmatch('on C-style comments', execute('syntax sync'))
295
296 syn clear
297endfunc
298
Bram Moolenaar58f60ca2017-01-17 17:19:00 +0100299func Test_invalid_arg()
300 call assert_fails('syntax case asdf', 'E390:')
301 call assert_fails('syntax conceal asdf', 'E390:')
302 call assert_fails('syntax spell asdf', 'E390:')
303endfunc
304
305func Test_syn_sync()
306 syntax region HereGroup start=/this/ end=/that/
307 syntax sync match SyncHere grouphere HereGroup "pattern"
308 call assert_match('SyncHere', execute('syntax sync'))
309 syn sync clear
310 call assert_notmatch('SyncHere', execute('syntax sync'))
311 syn clear
312endfunc
313
314func Test_syn_clear()
315 syntax keyword Foo foo
316 syntax keyword Tar tar
317 call assert_match('Foo', execute('syntax'))
318 call assert_match('Tar', execute('syntax'))
319 syn clear Foo
320 call assert_notmatch('Foo', execute('syntax'))
321 call assert_match('Tar', execute('syntax'))
322 syn clear Foo Tar
323 call assert_notmatch('Foo', execute('syntax'))
324 call assert_notmatch('Tar', execute('syntax'))
325endfunc