blob: 427ed7b4f0ae5f3d1a24cdea9eddc530c72bfc03 [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 Moolenaard61e8aa2017-01-17 17:44:46 +0100159 " Check that clearing "Aap" avoids it showing up before Boolean.
160 hi Aap ctermfg=blue
161 call feedkeys(":syn list \<C-A>\<C-B>\"\<CR>", 'tx')
162 call assert_match('^"syn list Aap Boolean Character ', @:)
163 hi clear Aap
164
Bram Moolenaar73b484c2016-12-11 15:11:17 +0100165 call feedkeys(":syn list \<C-A>\<C-B>\"\<CR>", 'tx')
166 call assert_match('^"syn list Boolean Character ', @:)
167
168 call feedkeys(":syn match \<C-A>\<C-B>\"\<CR>", 'tx')
169 call assert_match('^"syn match Boolean Character ', @:)
170endfunc
Bram Moolenaarde318c52017-01-17 16:27:10 +0100171
172func Test_syntax_arg_skipped()
173 syn clear
174 syntax case ignore
175 if 0
176 syntax case match
177 endif
178 call assert_match('case ignore', execute('syntax case'))
179
180 syn keyword Foo foo
181 call assert_match('Foo', execute('syntax'))
182 syn clear
183 call assert_match('case match', execute('syntax case'))
184 call assert_notmatch('Foo', execute('syntax'))
185
186 if has('conceal')
187 syn clear
188 syntax conceal on
189 if 0
190 syntax conceal off
191 endif
192 call assert_match('conceal on', execute('syntax conceal'))
193 syn clear
194 call assert_match('conceal off', execute('syntax conceal'))
Bram Moolenaar58f60ca2017-01-17 17:19:00 +0100195
196 syntax conceal on
197 syntax conceal off
198 call assert_match('conceal off', execute('syntax conceal'))
Bram Moolenaarde318c52017-01-17 16:27:10 +0100199 endif
200
Bram Moolenaard61e8aa2017-01-17 17:44:46 +0100201 syntax region Bar start=/</ end=/>/
Bram Moolenaarde318c52017-01-17 16:27:10 +0100202 if 0
203 syntax region NotTest start=/</ end=/>/ contains=@Spell
204 endif
Bram Moolenaard61e8aa2017-01-17 17:44:46 +0100205 call assert_match('Bar', execute('syntax'))
Bram Moolenaarde318c52017-01-17 16:27:10 +0100206 call assert_notmatch('NotTest', execute('syntax'))
207 call assert_notmatch('Spell', execute('syntax'))
208
209 hi Foo ctermfg=blue
210 let a = execute('hi Foo')
211 if 0
212 syntax rest
213 endif
214 call assert_equal(a, execute('hi Foo'))
Bram Moolenaard61e8aa2017-01-17 17:44:46 +0100215 hi clear Bar
216 hi clear Foo
Bram Moolenaarde318c52017-01-17 16:27:10 +0100217
218 set ft=tags
219 syn off
220 if 0
221 syntax enable
222 endif
223 call assert_match('No Syntax items defined', execute('syntax'))
224 syntax enable
225 call assert_match('tagComment', execute('syntax'))
226 set ft=
227
228 syn clear
229 if 0
230 syntax include @Spell nothing
231 endif
232 call assert_notmatch('Spell', execute('syntax'))
233
234 syn clear
235 syn iskeyword 48-57,$,_
236 call assert_match('48-57,$,_', execute('syntax iskeyword'))
237 if 0
238 syn clear
239 syn iskeyword clear
240 endif
241 call assert_match('48-57,$,_', execute('syntax iskeyword'))
242 syn iskeyword clear
243 call assert_match('not set', execute('syntax iskeyword'))
244 syn iskeyword 48-57,$,_
245 syn clear
246 call assert_match('not set', execute('syntax iskeyword'))
247
248 syn clear
249 syn keyword Foo foo
250 if 0
251 syn keyword NotAdded bar
252 endif
253 call assert_match('Foo', execute('syntax'))
254 call assert_notmatch('NotAdded', execute('highlight'))
255
256 syn clear
257 syn keyword Foo foo
258 call assert_match('Foo', execute('syntax'))
259 call assert_match('Foo', execute('syntax list'))
260 call assert_notmatch('Foo', execute('if 0 | syntax | endif'))
261 call assert_notmatch('Foo', execute('if 0 | syntax list | endif'))
262
263 syn clear
264 syn match Fopi /asdf/
265 if 0
266 syn match Fopx /asdf/
267 endif
268 call assert_match('Fopi', execute('syntax'))
269 call assert_notmatch('Fopx', execute('syntax'))
270
271 syn clear
272 syn spell toplevel
273 call assert_match('spell toplevel', execute('syntax spell'))
274 if 0
275 syn spell notoplevel
276 endif
277 call assert_match('spell toplevel', execute('syntax spell'))
278 syn spell notoplevel
279 call assert_match('spell notoplevel', execute('syntax spell'))
280 syn spell default
281 call assert_match('spell default', execute('syntax spell'))
282
283 syn clear
284 if 0
285 syntax cluster Spell
286 endif
287 call assert_notmatch('Spell', execute('syntax'))
288
289 syn clear
290 syn keyword Foo foo
291 syn sync ccomment
292 syn sync maxlines=5
293 if 0
294 syn sync maxlines=11
295 endif
296 call assert_match('on C-style comments', execute('syntax sync'))
297 call assert_match('maximal 5 lines', execute('syntax sync'))
Bram Moolenaar58f60ca2017-01-17 17:19:00 +0100298 syn sync clear
Bram Moolenaarde318c52017-01-17 16:27:10 +0100299 if 0
300 syn sync ccomment
301 endif
302 call assert_notmatch('on C-style comments', execute('syntax sync'))
303
304 syn clear
305endfunc
306
Bram Moolenaarea588152017-04-10 22:45:30 +0200307func Test_syntax_invalid_arg()
Bram Moolenaar58f60ca2017-01-17 17:19:00 +0100308 call assert_fails('syntax case asdf', 'E390:')
Bram Moolenaard61e8aa2017-01-17 17:44:46 +0100309 if has('conceal')
310 call assert_fails('syntax conceal asdf', 'E390:')
311 endif
Bram Moolenaar58f60ca2017-01-17 17:19:00 +0100312 call assert_fails('syntax spell asdf', 'E390:')
Bram Moolenaarea588152017-04-10 22:45:30 +0200313 call assert_fails('syntax clear @ABCD', 'E391:')
314 call assert_fails('syntax include @Xxx', 'E397:')
315 call assert_fails('syntax region X start="{"', 'E399:')
316 call assert_fails('syntax sync x', 'E404:')
317 call assert_fails('syntax keyword Abc a[', 'E789:')
318 call assert_fails('syntax keyword Abc a[bc]d', 'E890:')
Bram Moolenaar58f60ca2017-01-17 17:19:00 +0100319endfunc
320
321func Test_syn_sync()
322 syntax region HereGroup start=/this/ end=/that/
323 syntax sync match SyncHere grouphere HereGroup "pattern"
324 call assert_match('SyncHere', execute('syntax sync'))
325 syn sync clear
326 call assert_notmatch('SyncHere', execute('syntax sync'))
327 syn clear
328endfunc
329
330func Test_syn_clear()
331 syntax keyword Foo foo
Bram Moolenaard61e8aa2017-01-17 17:44:46 +0100332 syntax keyword Bar tar
Bram Moolenaar58f60ca2017-01-17 17:19:00 +0100333 call assert_match('Foo', execute('syntax'))
Bram Moolenaard61e8aa2017-01-17 17:44:46 +0100334 call assert_match('Bar', execute('syntax'))
Bram Moolenaarc96272e2017-03-26 13:50:09 +0200335 call assert_equal('Foo', synIDattr(hlID("Foo"), "name"))
Bram Moolenaar58f60ca2017-01-17 17:19:00 +0100336 syn clear Foo
337 call assert_notmatch('Foo', execute('syntax'))
Bram Moolenaard61e8aa2017-01-17 17:44:46 +0100338 call assert_match('Bar', execute('syntax'))
Bram Moolenaarc96272e2017-03-26 13:50:09 +0200339 call assert_equal('Foo', synIDattr(hlID("Foo"), "name"))
Bram Moolenaard61e8aa2017-01-17 17:44:46 +0100340 syn clear Foo Bar
Bram Moolenaar58f60ca2017-01-17 17:19:00 +0100341 call assert_notmatch('Foo', execute('syntax'))
Bram Moolenaard61e8aa2017-01-17 17:44:46 +0100342 call assert_notmatch('Bar', execute('syntax'))
343 hi clear Foo
Bram Moolenaarc96272e2017-03-26 13:50:09 +0200344 call assert_equal('Foo', synIDattr(hlID("Foo"), "name"))
Bram Moolenaard61e8aa2017-01-17 17:44:46 +0100345 hi clear Bar
Bram Moolenaar58f60ca2017-01-17 17:19:00 +0100346endfunc
Bram Moolenaar4007ed42017-01-17 18:14:54 +0100347
348func Test_invalid_name()
349 syn clear
350 syn keyword Nop yes
351 call assert_fails("syntax keyword Wr\x17ong bar", 'E669:')
352 syntax keyword @Wrong bar
353 call assert_match('W18:', execute('1messages'))
354 syn clear
355 hi clear Nop
356 hi clear @Wrong
357endfunc
Bram Moolenaarf8ec9982017-04-09 15:41:31 +0200358
359func Test_ownsyntax()
360 new Xfoo
361 call setline(1, '#define FOO')
362 syntax on
363 set filetype=c
364 ownsyntax perl
365 call assert_equal('perlComment', synIDattr(synID(line('.'), col('.'), 1), 'name'))
366 call assert_equal('c', b:current_syntax)
367 call assert_equal('perl', w:current_syntax)
368
369 " A new split window should have the original syntax.
370 split
371 call assert_equal('cDefine', synIDattr(synID(line('.'), col('.'), 1), 'name'))
372 call assert_equal('c', b:current_syntax)
373 call assert_equal(0, exists('w:current_syntax'))
374
375 wincmd x
376 call assert_equal('perlComment', synIDattr(synID(line("."), col("."), 1), "name"))
377
378 syntax off
379 set filetype&
380 %bw!
381endfunc
382
383func Test_ownsyntax_completion()
384 call feedkeys(":ownsyntax java\<C-A>\<C-B>\"\<CR>", 'tx')
385 call assert_equal('"ownsyntax java javacc javascript', @:)
386endfunc
Bram Moolenaarea588152017-04-10 22:45:30 +0200387
388func Test_highlight_invalid_arg()
389 if has('gui_running')
390 call assert_fails('hi XXX guifg=xxx', 'E254:')
391 endif
392 call assert_fails('hi DoesNotExist', 'E411:')
393 call assert_fails('hi link', 'E412:')
394 call assert_fails('hi link a', 'E412:')
395 call assert_fails('hi link a b c', 'E413:')
396 call assert_fails('hi XXX =', 'E415:')
397 call assert_fails('hi XXX cterm', 'E416:')
398 call assert_fails('hi XXX cterm=', 'E417:')
399 call assert_fails('hi XXX cterm=DoesNotExist', 'E418:')
400 call assert_fails('hi XXX ctermfg=DoesNotExist', 'E421:')
401 call assert_fails('hi XXX xxx=White', 'E423:')
402endfunc
403
Bram Moolenaar1615b362017-06-04 21:06:09 +0200404func Test_bg_detection()
405 if has('gui_running')
406 return
407 endif
408 " auto-detection of &bg, make sure sure it isn't set anywhere before
409 " this test
410 hi Normal ctermbg=0
411 call assert_equal('dark', &bg)
412 hi Normal ctermbg=4
413 call assert_equal('dark', &bg)
414 hi Normal ctermbg=12
415 call assert_equal('light', &bg)
416 hi Normal ctermbg=15
417 call assert_equal('light', &bg)
418
419 " manually-set &bg takes precendence over auto-detection
420 set bg=light
421 hi Normal ctermbg=4
422 call assert_equal('light', &bg)
423 set bg=dark
424 hi Normal ctermbg=12
425 call assert_equal('dark', &bg)
426endfunc
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200427
428func Test_syntax_hangs()
429 if !has('reltime') || !has('float') || !has('syntax')
430 return
431 endif
432
433 " This pattern takes a long time to match, it should timeout.
434 new
435 call setline(1, ['aaa', repeat('abc ', 1000), 'ccc'])
436 let start = reltime()
437 set nolazyredraw redrawtime=101
438 syn match Error /\%#=1a*.*X\@<=b*/
439 redraw
440 let elapsed = reltimefloat(reltime(start))
441 call assert_true(elapsed > 0.1)
442 call assert_true(elapsed < 1.0)
443
444 " second time syntax HL is disabled
445 let start = reltime()
446 redraw
447 let elapsed = reltimefloat(reltime(start))
448 call assert_true(elapsed < 0.1)
449
450 " after CTRL-L the timeout flag is reset
451 let start = reltime()
452 exe "normal \<C-L>"
453 redraw
454 let elapsed = reltimefloat(reltime(start))
455 call assert_true(elapsed > 0.1)
456 call assert_true(elapsed < 1.0)
457
458 set redrawtime&
459 bwipe!
460endfunc