1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
|
Tests for the exists() and has() functions. vim: set ft=vim ts=8 sw=2 :
STARTTEST
:so small.vim
:function! RunTest(str, result)
if exists(a:str) == a:result
echo "OK"
else
echo "FAILED: Checking for " . a:str
endif
endfunction
:function! TestExists()
augroup myagroup
autocmd! BufEnter *.my echo "myfile edited"
autocmd! FuncUndefined UndefFun exec "fu UndefFun()\nendfu"
augroup END
set rtp+=./sautest
let test_cases = []
" valid autocmd group
let test_cases += [['#myagroup', 1]]
" valid autocmd group with garbage
let test_cases += [['#myagroup+b', 0]]
" Valid autocmd group and event
let test_cases += [['#myagroup#BufEnter', 1]]
" Valid autocmd group, event and pattern
let test_cases += [['#myagroup#BufEnter#*.my', 1]]
" Valid autocmd event
let test_cases += [['#BufEnter', 1]]
" Valid autocmd event and pattern
let test_cases += [['#BufEnter#*.my', 1]]
" Non-existing autocmd group or event
let test_cases += [['#xyzagroup', 0]]
" Non-existing autocmd group and valid autocmd event
let test_cases += [['#xyzagroup#BufEnter', 0]]
" Valid autocmd group and event with no matching pattern
let test_cases += [['#myagroup#CmdwinEnter', 0]]
" Valid autocmd group and non-existing autocmd event
let test_cases += [['#myagroup#xyzacmd', 0]]
" Valid autocmd group and event and non-matching pattern
let test_cases += [['#myagroup#BufEnter#xyzpat', 0]]
" Valid autocmd event and non-matching pattern
let test_cases += [['#BufEnter#xyzpat', 0]]
" Empty autocmd group, event and pattern
let test_cases += [['###', 0]]
" Empty autocmd group and event or empty event and pattern
let test_cases += [['##', 0]]
" Valid autocmd event
let test_cases += [['##FileReadCmd', 1]]
" Non-existing autocmd event
let test_cases += [['##MySpecialCmd', 0]]
" Existing and working option (long form)
let test_cases += [['&textwidth', 1]]
" Existing and working option (short form)
let test_cases += [['&tw', 1]]
" Existing and working option with garbage
let test_cases += [['&tw-', 0]]
" Global option
let test_cases += [['&g:errorformat', 1]]
" Local option
let test_cases += [['&l:errorformat', 1]]
" Negative form of existing and working option (long form)
let test_cases += [['&nojoinspaces', 0]]
" Negative form of existing and working option (short form)
let test_cases += [['&nojs', 0]]
" Non-existing option
let test_cases += [['&myxyzoption', 0]]
" Existing and working option (long form)
let test_cases += [['+incsearch', 1]]
" Existing and working option with garbage
let test_cases += [['+incsearch!1', 0]]
" Existing and working option (short form)
let test_cases += [['+is', 1]]
" Existing option that is hidden.
let test_cases += [['+autoprint', 0]]
" Existing environment variable
let $EDITOR_NAME = 'Vim Editor'
let test_cases += [['$EDITOR_NAME', 1]]
" Non-existing environment variable
let test_cases += [['$NON_ENV_VAR', 0]]
" Valid internal function
let test_cases += [['*bufnr', 1]]
" Valid internal function with ()
let test_cases += [['*bufnr()', 1]]
" Non-existing internal function
let test_cases += [['*myxyzfunc', 0]]
" Valid internal function with garbage
let test_cases += [['*bufnr&6', 0]]
" Valid user defined function
let test_cases += [['*TestExists', 1]]
" Non-existing user defined function
let test_cases += [['*MyxyzFunc', 0]]
" Function that may be created by FuncUndefined event
let test_cases += [['*UndefFun', 0]]
" Function that may be created by script autoloading
let test_cases += [['*footest#F', 0]]
redir! > test.out
for [test_case, result] in test_cases
echo test_case . ": " . result
call RunTest(test_case, result)
endfor
" Valid internal command (full match)
echo ':edit: 2'
if exists(':edit') == 2
echo "OK"
else
echo "FAILED"
endif
" Valid internal command (full match) with garbage
echo ':edit/a: 0'
if exists(':edit/a') == 0
echo "OK"
else
echo "FAILED"
endif
" Valid internal command (partial match)
echo ':q: 1'
if exists(':q') == 1
echo "OK"
else
echo "FAILED"
endif
" Non-existing internal command
echo ':invalidcmd: 0'
if !exists(':invalidcmd')
echo "OK"
else
echo "FAILED"
endif
" User defined command (full match)
command! MyCmd :echo 'My command'
echo ':MyCmd: 2'
if exists(':MyCmd') == 2
echo "OK"
else
echo "FAILED"
endif
" User defined command (partial match)
command! MyOtherCmd :echo 'Another command'
echo ':My: 3'
if exists(':My') == 3
echo "OK"
else
echo "FAILED"
endif
" Command modifier
echo ':rightbelow: 2'
if exists(':rightbelow') == 2
echo "OK"
else
echo "FAILED"
endif
" Non-existing user defined command (full match)
delcommand MyCmd
echo ':MyCmd: 0'
if !exists(':MyCmd')
echo "OK"
else
echo "FAILED"
endif
" Non-existing user defined command (partial match)
delcommand MyOtherCmd
echo ':My: 0'
if !exists(':My')
echo "OK"
else
echo "FAILED"
endif
" Valid local variable
let local_var = 1
echo 'local_var: 1'
if exists('local_var')
echo "OK"
else
echo "FAILED"
endif
" Valid local variable with garbage
let local_var = 1
echo 'local_var%n: 0'
if !exists('local_var%n')
echo "OK"
else
echo "FAILED"
endif
" Non-existing local variable
unlet local_var
echo 'local_var: 0'
if !exists('local_var')
echo "OK"
else
echo "FAILED"
endif
" Non-existing autoload variable that may be autoloaded
echo 'footest#x: 0'
if !exists('footest#x')
echo "OK"
else
echo "FAILED"
endif
" Valid local list
let local_list = ["blue", "orange"]
echo 'local_list: 1'
if exists('local_list')
echo "OK"
else
echo "FAILED"
endif
" Valid local list item
echo 'local_list[1]: 1'
if exists('local_list[1]')
echo "OK"
else
echo "FAILED"
endif
" Valid local list item with garbage
echo 'local_list[1]+5: 0'
if !exists('local_list[1]+5')
echo "OK"
else
echo "FAILED"
endif
" Invalid local list item
echo 'local_list[2]: 0'
if !exists('local_list[2]')
echo "OK"
else
echo "FAILED"
endif
" Non-existing local list
unlet local_list
echo 'local_list: 0'
if !exists('local_list')
echo "OK"
else
echo "FAILED"
endif
" Valid local dictionary
let local_dict = {"xcord":100, "ycord":2}
echo 'local_dict: 1'
if exists('local_dict')
echo "OK"
else
echo "FAILED"
endif
" Non-existing local dictionary
unlet local_dict
echo 'local_dict: 0'
if !exists('local_dict')
echo "OK"
else
echo "FAILED"
endif
" Existing local curly-brace variable
let str = "local"
let curly_{str}_var = 1
echo 'curly_' . str . '_var: 1'
if exists('curly_{str}_var')
echo "OK"
else
echo "FAILED"
endif
" Non-existing local curly-brace variable
unlet curly_{str}_var
echo 'curly_' . str . '_var: 0'
if !exists('curly_{str}_var')
echo "OK"
else
echo "FAILED"
endif
" Existing global variable
let g:global_var = 1
echo 'g:global_var: 1'
if exists('g:global_var')
echo "OK"
else
echo "FAILED"
endif
" Existing global variable with garbage
echo 'g:global_var-n: 1'
if !exists('g:global_var-n')
echo "OK"
else
echo "FAILED"
endif
" Non-existing global variable
unlet g:global_var
echo 'g:global_var: 0'
if !exists('g:global_var')
echo "OK"
else
echo "FAILED"
endif
" Existing global list
let g:global_list = ["blue", "orange"]
echo 'g:global_list: 1'
if exists('g:global_list')
echo "OK"
else
echo "FAILED"
endif
" Non-existing global list
unlet g:global_list
echo 'g:global_list: 0'
if !exists('g:global_list')
echo "OK"
else
echo "FAILED"
endif
" Existing global dictionary
let g:global_dict = {"xcord":100, "ycord":2}
echo 'g:global_dict: 1'
if exists('g:global_dict')
echo "OK"
else
echo "FAILED"
endif
" Non-existing global dictionary
unlet g:global_dict
echo 'g:global_dict: 0'
if !exists('g:global_dict')
echo "OK"
else
echo "FAILED"
endif
" Existing global curly-brace variable
let str = "global"
let g:curly_{str}_var = 1
echo 'g:curly_' . str . '_var: 1'
if exists('g:curly_{str}_var')
echo "OK"
else
echo "FAILED"
endif
" Non-existing global curly-brace variable
unlet g:curly_{str}_var
echo 'g:curly_' . str . '_var: 0'
if !exists('g:curly_{str}_var')
echo "OK"
else
echo "FAILED"
endif
" Existing window variable
echo 'w:window_var: 1'
let w:window_var = 1
if exists('w:window_var')
echo "OK"
else
echo "FAILED"
endif
" Non-existing window variable
unlet w:window_var
echo 'w:window_var: 0'
if !exists('w:window_var')
echo "OK"
else
echo "FAILED"
endif
" Existing window list
let w:window_list = ["blue", "orange"]
echo 'w:window_list: 1'
if exists('w:window_list')
echo "OK"
else
echo "FAILED"
endif
" Non-existing window list
unlet w:window_list
echo 'w:window_list: 0'
if !exists('w:window_list')
echo "OK"
else
echo "FAILED"
endif
" Existing window dictionary
let w:window_dict = {"xcord":100, "ycord":2}
echo 'w:window_dict: 1'
if exists('w:window_dict')
echo "OK"
else
echo "FAILED"
endif
" Non-existing window dictionary
unlet w:window_dict
echo 'w:window_dict: 0'
if !exists('w:window_dict')
echo "OK"
else
echo "FAILED"
endif
" Existing window curly-brace variable
let str = "window"
let w:curly_{str}_var = 1
echo 'w:curly_' . str . '_var: 1'
if exists('w:curly_{str}_var')
echo "OK"
else
echo "FAILED"
endif
" Non-existing window curly-brace variable
unlet w:curly_{str}_var
echo 'w:curly_' . str . '_var: 0'
if !exists('w:curly_{str}_var')
echo "OK"
else
echo "FAILED"
endif
" Existing buffer variable
echo 'b:buffer_var: 1'
let b:buffer_var = 1
if exists('b:buffer_var')
echo "OK"
else
echo "FAILED"
endif
" Non-existing buffer variable
unlet b:buffer_var
echo 'b:buffer_var: 0'
if !exists('b:buffer_var')
echo "OK"
else
echo "FAILED"
endif
" Existing buffer list
let b:buffer_list = ["blue", "orange"]
echo 'b:buffer_list: 1'
if exists('b:buffer_list')
echo "OK"
else
echo "FAILED"
endif
" Non-existing buffer list
unlet b:buffer_list
echo 'b:buffer_list: 0'
if !exists('b:buffer_list')
echo "OK"
else
echo "FAILED"
endif
" Existing buffer dictionary
let b:buffer_dict = {"xcord":100, "ycord":2}
echo 'b:buffer_dict: 1'
if exists('b:buffer_dict')
echo "OK"
else
echo "FAILED"
endif
" Non-existing buffer dictionary
unlet b:buffer_dict
echo 'b:buffer_dict: 0'
if !exists('b:buffer_dict')
echo "OK"
else
echo "FAILED"
endif
" Existing buffer curly-brace variable
let str = "buffer"
let b:curly_{str}_var = 1
echo 'b:curly_' . str . '_var: 1'
if exists('b:curly_{str}_var')
echo "OK"
else
echo "FAILED"
endif
" Non-existing buffer curly-brace variable
unlet b:curly_{str}_var
echo 'b:curly_' . str . '_var: 0'
if !exists('b:curly_{str}_var')
echo "OK"
else
echo "FAILED"
endif
" Script-local tests
source test60.vim
" Existing Vim internal variable
echo 'v:version: 1'
if exists('v:version')
echo "OK"
else
echo "FAILED"
endif
" Non-existing Vim internal variable
echo 'v:non_exists_var: 0'
if !exists('v:non_exists_var')
echo "OK"
else
echo "FAILED"
endif
" Function arguments
function TestFuncArg(func_arg, ...)
echo 'a:func_arg: 1'
if exists('a:func_arg')
echo "OK"
else
echo "FAILED"
endif
echo 'a:non_exists_arg: 0'
if !exists('a:non_exists_arg')
echo "OK"
else
echo "FAILED"
endif
echo 'a:1: 1'
if exists('a:1')
echo "OK"
else
echo "FAILED"
endif
echo 'a:2: 0'
if !exists('a:2')
echo "OK"
else
echo "FAILED"
endif
endfunction
call TestFuncArg("arg1", "arg2")
echo ' g:footest#x =' g:footest#x
echo ' footest#F()' footest#F()
echo 'UndefFun()' UndefFun()
redir END
endfunction
:call TestExists()
:"
:delfunc TestExists
:delfunc RunTest
:delfunc TestFuncArg
:edit! test.out
:set ff=unix
:w
:qa!
:while getchar(1) | call getchar() | endwhile
ENDTEST
|