robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
22.3 KiB859 linesraw
1-- Tests for the exists() and has() functions.
2
3local t = require('test.testutil')
4local n = require('test.functional.testnvim')()
5
6local describe, it, setup, teardown = t.describe, t.it, t.setup, t.teardown
7local source = n.source
8local clear, expect = n.clear, n.expect
9local write_file = t.write_file
10
11describe('exists() and has() functions', function()
12  setup(function()
13    clear()
14    -- Create a temporary script needed for the test.
15    write_file(
16      'test60.vim',
17      [[
18      " Vim script for exists() function test
19      " Script-local variables are checked here
20
21      " Existing script-local variable
22      let s:script_var = 1
23      echo 's:script_var: 1'
24      if exists('s:script_var')
25          echo "OK"
26      else
27          echo "FAILED"
28      endif
29
30      " Non-existing script-local variable
31      unlet s:script_var
32      echo 's:script_var: 0'
33      if !exists('s:script_var')
34          echo "OK"
35      else
36          echo "FAILED"
37      endif
38
39      " Existing script-local list
40      let s:script_list = ["blue", "orange"]
41      echo 's:script_list: 1'
42      if exists('s:script_list')
43          echo "OK"
44      else
45          echo "FAILED"
46      endif
47
48      " Non-existing script-local list
49      unlet s:script_list
50      echo 's:script_list: 0'
51      if !exists('s:script_list')
52          echo "OK"
53      else
54          echo "FAILED"
55      endif
56
57      " Existing script-local dictionary
58      let s:script_dict = {"xcord":100, "ycord":2}
59      echo 's:script_dict: 1'
60      if exists('s:script_dict')
61          echo "OK"
62      else
63          echo "FAILED"
64      endif
65
66      " Non-existing script-local dictionary
67      unlet s:script_dict
68      echo 's:script_dict: 0'
69      if !exists('s:script_dict')
70          echo "OK"
71      else
72          echo "FAILED"
73      endif
74
75      " Existing script curly-brace variable
76      let str = "script"
77      let s:curly_{str}_var = 1
78      echo 's:curly_' . str . '_var: 1'
79      if exists('s:curly_{str}_var')
80          echo "OK"
81      else
82          echo "FAILED"
83      endif
84
85      " Non-existing script-local curly-brace variable
86      unlet s:curly_{str}_var
87      echo 's:curly_' . str . '_var: 0'
88      if !exists('s:curly_{str}_var')
89          echo "OK"
90      else
91          echo "FAILED"
92      endif
93
94      " Existing script-local function
95      function! s:my_script_func()
96      endfunction
97
98      echo '*s:my_script_func: 1'
99      if exists('*s:my_script_func')
100          echo "OK"
101      else
102          echo "FAILED"
103      endif
104
105      " Non-existing script-local function
106      delfunction s:my_script_func
107
108      echo '*s:my_script_func: 0'
109      if !exists('*s:my_script_func')
110          echo "OK"
111      else
112          echo "FAILED"
113      endif
114      unlet str
115      ]]
116    )
117  end)
118  teardown(function()
119    os.remove('test.out')
120    os.remove('test60.vim')
121  end)
122
123  it('is working', function()
124    source([=[
125      " Add the special directory with test scripts to &rtp
126      set rtp+=test/functional/fixtures
127      set wildchar=^E
128      function! RunTest(str, result)
129          if exists(a:str) == a:result
130              echo "OK"
131          else
132              echo "FAILED: Checking for " . a:str
133          endif
134      endfunction
135      function! TestExists()
136          augroup myagroup
137          autocmd! BufEnter       *.my     echo "myfile edited"
138          autocmd! FuncUndefined  UndefFun exec "fu UndefFun()\nendfu"
139          augroup END
140          set rtp+=./sautest
141          let test_cases = []
142          " Valid autocmd group.
143          let test_cases += [['#myagroup', 1]]
144          " Valid autocmd group with garbage.
145          let test_cases += [['#myagroup+b', 0]]
146          " Valid autocmd group and event.
147          let test_cases += [['#myagroup#BufEnter', 1]]
148          " Valid autocmd group, event and pattern.
149          let test_cases += [['#myagroup#BufEnter#*.my', 1]]
150          " Valid autocmd event.
151          let test_cases += [['#BufEnter', 1]]
152          " Valid autocmd event and pattern.
153          let test_cases += [['#BufEnter#*.my', 1]]
154          " Non-existing autocmd group or event.
155          let test_cases += [['#xyzagroup', 0]]
156          " Non-existing autocmd group and valid autocmd event.
157          let test_cases += [['#xyzagroup#BufEnter', 0]]
158          " Valid autocmd group and event with no matching pattern.
159          let test_cases += [['#myagroup#CmdwinEnter', 0]]
160          " Valid autocmd group and non-existing autocmd event.
161          let test_cases += [['#myagroup#xyzacmd', 0]]
162          " Valid autocmd group and event and non-matching pattern.
163          let test_cases += [['#myagroup#BufEnter#xyzpat', 0]]
164          " Valid autocmd event and non-matching pattern.
165          let test_cases += [['#BufEnter#xyzpat', 0]]
166          " Empty autocmd group, event and pattern.
167          let test_cases += [['###', 0]]
168          " Empty autocmd group and event or empty event and pattern.
169          let test_cases += [['##', 0]]
170          " Valid autocmd event.
171          let test_cases += [['##FileReadCmd', 1]]
172          " Non-existing autocmd event.
173          let test_cases += [['##MySpecialCmd', 0]]
174          " Existing and working option (long form).
175          let test_cases += [['&textwidth', 1]]
176          " Existing and working option (short form).
177          let test_cases += [['&tw', 1]]
178          " Existing and working option with garbage.
179          let test_cases += [['&tw-', 0]]
180          " Global option.
181          let test_cases += [['&g:errorformat', 1]]
182          " Local option.
183          let test_cases += [['&l:errorformat', 1]]
184          " Negative form of existing and working option (long form).
185          let test_cases += [['&nojoinspaces', 0]]
186          " Negative form of existing and working option (short form).
187          let test_cases += [['&nojs', 0]]
188          " Non-existing option.
189          let test_cases += [['&myxyzoption', 0]]
190          " Existing and working option (long form).
191          let test_cases += [['+incsearch', 1]]
192          " Existing and working option with garbage.
193          let test_cases += [['+incsearch!1', 0]]
194          " Existing and working option (short form).
195          let test_cases += [['+is', 1]]
196          " Existing option that is hidden.
197          let test_cases += [['+mouseshape', 0]]
198          " Existing environment variable.
199          let $EDITOR_NAME = 'Vim Editor'
200          let test_cases += [['$EDITOR_NAME', 1]]
201          " Non-existing environment variable.
202          let test_cases += [['$NON_ENV_VAR', 0]]
203          " Valid internal function.
204          let test_cases += [['*bufnr', 1]]
205          " Valid internal function with ().
206          let test_cases += [['*bufnr()', 1]]
207          " Non-existing internal function.
208          let test_cases += [['*myxyzfunc', 0]]
209          " Valid internal function with garbage.
210          let test_cases += [['*bufnr&6', 0]]
211          " Valid user defined function.
212          let test_cases += [['*TestExists', 1]]
213          " Non-existing user defined function.
214          let test_cases += [['*MyxyzFunc', 0]]
215          " Function that may be created by FuncUndefined event.
216          let test_cases += [['*UndefFun', 0]]
217          " Function that may be created by script autoloading.
218          let test_cases += [['*footest#F', 0]]
219          redir! > test.out
220          for [test_case, result] in test_cases
221              echo test_case . ": " . result
222              call RunTest(test_case, result)
223          endfor
224          " Valid internal command (full match).
225          echo ':edit: 2'
226          if exists(':edit') == 2
227            echo "OK"
228          else
229            echo "FAILED"
230          endif
231          " Valid internal command (full match) with garbage.
232          echo ':edit/a: 0'
233          if exists(':edit/a') == 0
234            echo "OK"
235          else
236            echo "FAILED"
237          endif
238          " Valid internal command (partial match).
239          echo ':q: 1'
240          if exists(':q') == 1
241            echo "OK"
242          else
243            echo "FAILED"
244          endif
245          " Non-existing internal command.
246          echo ':invalidcmd: 0'
247          if !exists(':invalidcmd')
248            echo "OK"
249          else
250            echo "FAILED"
251          endif
252          " User defined command (full match).
253          command! MyCmd :echo 'My command'
254          echo ':MyCmd: 2'
255          if exists(':MyCmd') == 2
256            echo "OK"
257          else
258            echo "FAILED"
259          endif
260          " User defined command (partial match).
261          command! MyOtherCmd :echo 'Another command'
262          echo ':My: 3'
263          if exists(':My') == 3
264            echo "OK"
265          else
266            echo "FAILED"
267          endif
268          " Command modifier.
269          echo ':rightbelow: 2'
270          if exists(':rightbelow') == 2
271            echo "OK"
272          else
273            echo "FAILED"
274          endif
275          " Non-existing user defined command (full match).
276          delcommand MyCmd
277          echo ':MyCmd: 0'
278          if !exists(':MyCmd')
279            echo "OK"
280          else
281            echo "FAILED"
282          endif
283          " Non-existing user defined command (partial match).
284          delcommand MyOtherCmd
285          echo ':My: 0'
286          if !exists(':My')
287            echo "OK"
288          else
289            echo "FAILED"
290          endif
291          " Valid local variable.
292          let local_var = 1
293          echo 'local_var: 1'
294          if exists('local_var')
295            echo "OK"
296          else
297            echo "FAILED"
298          endif
299          " Valid local variable with garbage.
300          let local_var = 1
301          echo 'local_var%n: 0'
302          if !exists('local_var%n')
303            echo "OK"
304          else
305            echo "FAILED"
306          endif
307          " Non-existing local variable.
308          unlet local_var
309          echo 'local_var: 0'
310          if !exists('local_var')
311            echo "OK"
312          else
313            echo "FAILED"
314          endif
315          " Non-existing autoload variable that may be autoloaded.
316          echo 'footest#x: 0'
317          if !exists('footest#x')
318            echo "OK"
319          else
320            echo "FAILED"
321          endif
322          " Valid local list.
323          let local_list = ["blue", "orange"]
324          echo 'local_list: 1'
325          if exists('local_list')
326            echo "OK"
327          else
328            echo "FAILED"
329          endif
330          " Valid local list item.
331          echo 'local_list[1]: 1'
332          if exists('local_list[1]')
333            echo "OK"
334          else
335            echo "FAILED"
336          endif
337          " Valid local list item with garbage.
338          echo 'local_list[1]+5: 0'
339          if !exists('local_list[1]+5')
340            echo "OK"
341          else
342            echo "FAILED"
343          endif
344          " Invalid local list item.
345          echo 'local_list[2]: 0'
346          if !exists('local_list[2]')
347            echo "OK"
348          else
349            echo "FAILED"
350          endif
351          " Non-existing local list.
352          unlet local_list
353          echo 'local_list: 0'
354          if !exists('local_list')
355            echo "OK"
356          else
357            echo "FAILED"
358          endif
359          " Valid local dictionary.
360          let local_dict = {"xcord":100, "ycord":2}
361          echo 'local_dict: 1'
362          if exists('local_dict')
363            echo "OK"
364          else
365            echo "FAILED"
366          endif
367          " Non-existing local dictionary.
368          unlet local_dict
369          echo 'local_dict: 0'
370          if !exists('local_dict')
371            echo "OK"
372          else
373            echo "FAILED"
374          endif
375          " Existing local curly-brace variable.
376          let str = "local"
377          let curly_{str}_var = 1
378          echo 'curly_' . str . '_var: 1'
379          if exists('curly_{str}_var')
380            echo "OK"
381          else
382            echo "FAILED"
383          endif
384          " Non-existing local curly-brace variable.
385          unlet curly_{str}_var
386          echo 'curly_' . str . '_var: 0'
387          if !exists('curly_{str}_var')
388            echo "OK"
389          else
390            echo "FAILED"
391          endif
392          " Existing global variable.
393          let g:global_var = 1
394          echo 'g:global_var: 1'
395          if exists('g:global_var')
396            echo "OK"
397          else
398            echo "FAILED"
399          endif
400          " Existing global variable with garbage.
401          echo 'g:global_var-n: 1'
402          if !exists('g:global_var-n')
403            echo "OK"
404          else
405            echo "FAILED"
406          endif
407          " Non-existing global variable.
408          unlet g:global_var
409          echo 'g:global_var: 0'
410          if !exists('g:global_var')
411            echo "OK"
412          else
413            echo "FAILED"
414          endif
415          " Existing global list.
416          let g:global_list = ["blue", "orange"]
417          echo 'g:global_list: 1'
418          if exists('g:global_list')
419            echo "OK"
420          else
421            echo "FAILED"
422          endif
423          " Non-existing global list.
424          unlet g:global_list
425          echo 'g:global_list: 0'
426          if !exists('g:global_list')
427            echo "OK"
428          else
429            echo "FAILED"
430          endif
431          " Existing global dictionary.
432          let g:global_dict = {"xcord":100, "ycord":2}
433          echo 'g:global_dict: 1'
434          if exists('g:global_dict')
435            echo "OK"
436          else
437            echo "FAILED"
438          endif
439          " Non-existing global dictionary.
440          unlet g:global_dict
441          echo 'g:global_dict: 0'
442          if !exists('g:global_dict')
443            echo "OK"
444          else
445            echo "FAILED"
446          endif
447          " Existing global curly-brace variable.
448          let str = "global"
449          let g:curly_{str}_var = 1
450          echo 'g:curly_' . str . '_var: 1'
451          if exists('g:curly_{str}_var')
452            echo "OK"
453          else
454            echo "FAILED"
455          endif
456          " Non-existing global curly-brace variable.
457          unlet g:curly_{str}_var
458          echo 'g:curly_' . str . '_var: 0'
459          if !exists('g:curly_{str}_var')
460            echo "OK"
461          else
462            echo "FAILED"
463          endif
464          " Existing window variable.
465          echo 'w:window_var: 1'
466          let w:window_var = 1
467          if exists('w:window_var')
468            echo "OK"
469          else
470            echo "FAILED"
471          endif
472          " Non-existing window variable.
473          unlet w:window_var
474          echo 'w:window_var: 0'
475          if !exists('w:window_var')
476            echo "OK"
477          else
478            echo "FAILED"
479          endif
480          " Existing window list.
481          let w:window_list = ["blue", "orange"]
482          echo 'w:window_list: 1'
483          if exists('w:window_list')
484            echo "OK"
485          else
486            echo "FAILED"
487          endif
488          " Non-existing window list.
489          unlet w:window_list
490          echo 'w:window_list: 0'
491          if !exists('w:window_list')
492            echo "OK"
493          else
494            echo "FAILED"
495          endif
496          " Existing window dictionary.
497          let w:window_dict = {"xcord":100, "ycord":2}
498          echo 'w:window_dict: 1'
499          if exists('w:window_dict')
500            echo "OK"
501          else
502            echo "FAILED"
503          endif
504          " Non-existing window dictionary.
505          unlet w:window_dict
506          echo 'w:window_dict: 0'
507          if !exists('w:window_dict')
508            echo "OK"
509          else
510            echo "FAILED"
511          endif
512          " Existing window curly-brace variable.
513          let str = "window"
514          let w:curly_{str}_var = 1
515          echo 'w:curly_' . str . '_var: 1'
516          if exists('w:curly_{str}_var')
517            echo "OK"
518          else
519            echo "FAILED"
520          endif
521          " Non-existing window curly-brace variable.
522          unlet w:curly_{str}_var
523          echo 'w:curly_' . str . '_var: 0'
524          if !exists('w:curly_{str}_var')
525            echo "OK"
526          else
527            echo "FAILED"
528          endif
529          " Existing buffer variable.
530          echo 'b:buffer_var: 1'
531          let b:buffer_var = 1
532          if exists('b:buffer_var')
533            echo "OK"
534          else
535            echo "FAILED"
536          endif
537          " Non-existing buffer variable.
538          unlet b:buffer_var
539          echo 'b:buffer_var: 0'
540          if !exists('b:buffer_var')
541            echo "OK"
542          else
543            echo "FAILED"
544          endif
545          " Existing buffer list.
546          let b:buffer_list = ["blue", "orange"]
547          echo 'b:buffer_list: 1'
548          if exists('b:buffer_list')
549            echo "OK"
550          else
551            echo "FAILED"
552          endif
553          " Non-existing buffer list.
554          unlet b:buffer_list
555          echo 'b:buffer_list: 0'
556          if !exists('b:buffer_list')
557            echo "OK"
558          else
559            echo "FAILED"
560          endif
561          " Existing buffer dictionary.
562          let b:buffer_dict = {"xcord":100, "ycord":2}
563          echo 'b:buffer_dict: 1'
564          if exists('b:buffer_dict')
565            echo "OK"
566          else
567            echo "FAILED"
568          endif
569          " Non-existing buffer dictionary.
570          unlet b:buffer_dict
571          echo 'b:buffer_dict: 0'
572          if !exists('b:buffer_dict')
573            echo "OK"
574          else
575            echo "FAILED"
576          endif
577          " Existing buffer curly-brace variable.
578          let str = "buffer"
579          let b:curly_{str}_var = 1
580          echo 'b:curly_' . str . '_var: 1'
581          if exists('b:curly_{str}_var')
582            echo "OK"
583          else
584            echo "FAILED"
585          endif
586          " Non-existing buffer curly-brace variable.
587          unlet b:curly_{str}_var
588          echo 'b:curly_' . str . '_var: 0'
589          if !exists('b:curly_{str}_var')
590            echo "OK"
591          else
592            echo "FAILED"
593          endif
594          " Script-local tests.
595          source test60.vim
596          " Existing Vim internal variable.
597          echo 'v:version: 1'
598          if exists('v:version')
599            echo "OK"
600          else
601            echo "FAILED"
602          endif
603          " Non-existing Vim internal variable.
604          echo 'v:non_exists_var: 0'
605          if !exists('v:non_exists_var')
606            echo "OK"
607          else
608            echo "FAILED"
609          endif
610          " Function arguments.
611          function TestFuncArg(func_arg, ...)
612              echo 'a:func_arg: 1'
613              if exists('a:func_arg')
614                  echo "OK"
615              else
616                  echo "FAILED"
617              endif
618              echo 'a:non_exists_arg: 0'
619              if !exists('a:non_exists_arg')
620                  echo "OK"
621              else
622                  echo "FAILED"
623              endif
624              echo 'a:1: 1'
625              if exists('a:1')
626                  echo "OK"
627              else
628                  echo "FAILED"
629              endif
630              echo 'a:2: 0'
631              if !exists('a:2')
632                  echo "OK"
633              else
634                  echo "FAILED"
635              endif
636          endfunction
637          call TestFuncArg("arg1", "arg2")
638          echo ' g:footest#x =' g:footest#x
639          echo '   footest#F()' footest#F()
640          echo 'UndefFun()' UndefFun()
641          redir END
642      endfunction
643
644      call TestExists()
645
646      edit! test.out
647      set ff=unix
648    ]=])
649
650    -- Assert buffer contents.
651    expect([[
652
653      #myagroup: 1
654      OK
655      #myagroup+b: 0
656      OK
657      #myagroup#BufEnter: 1
658      OK
659      #myagroup#BufEnter#*.my: 1
660      OK
661      #BufEnter: 1
662      OK
663      #BufEnter#*.my: 1
664      OK
665      #xyzagroup: 0
666      OK
667      #xyzagroup#BufEnter: 0
668      OK
669      #myagroup#CmdwinEnter: 0
670      OK
671      #myagroup#xyzacmd: 0
672      OK
673      #myagroup#BufEnter#xyzpat: 0
674      OK
675      #BufEnter#xyzpat: 0
676      OK
677      ###: 0
678      OK
679      ##: 0
680      OK
681      ##FileReadCmd: 1
682      OK
683      ##MySpecialCmd: 0
684      OK
685      &textwidth: 1
686      OK
687      &tw: 1
688      OK
689      &tw-: 0
690      OK
691      &g:errorformat: 1
692      OK
693      &l:errorformat: 1
694      OK
695      &nojoinspaces: 0
696      OK
697      &nojs: 0
698      OK
699      &myxyzoption: 0
700      OK
701      +incsearch: 1
702      OK
703      +incsearch!1: 0
704      OK
705      +is: 1
706      OK
707      +mouseshape: 0
708      OK
709      $EDITOR_NAME: 1
710      OK
711      $NON_ENV_VAR: 0
712      OK
713      *bufnr: 1
714      OK
715      *bufnr(): 1
716      OK
717      *myxyzfunc: 0
718      OK
719      *bufnr&6: 0
720      OK
721      *TestExists: 1
722      OK
723      *MyxyzFunc: 0
724      OK
725      *UndefFun: 0
726      OK
727      *footest#F: 0
728      OK
729      :edit: 2
730      OK
731      :edit/a: 0
732      OK
733      :q: 1
734      OK
735      :invalidcmd: 0
736      OK
737      :MyCmd: 2
738      OK
739      :My: 3
740      OK
741      :rightbelow: 2
742      OK
743      :MyCmd: 0
744      OK
745      :My: 0
746      OK
747      local_var: 1
748      OK
749      local_var%n: 0
750      OK
751      local_var: 0
752      OK
753      footest#x: 0
754      OK
755      local_list: 1
756      OK
757      local_list[1]: 1
758      OK
759      local_list[1]+5: 0
760      OK
761      local_list[2]: 0
762      OK
763      local_list: 0
764      OK
765      local_dict: 1
766      OK
767      local_dict: 0
768      OK
769      curly_local_var: 1
770      OK
771      curly_local_var: 0
772      OK
773      g:global_var: 1
774      OK
775      g:global_var-n: 1
776      OK
777      g:global_var: 0
778      OK
779      g:global_list: 1
780      OK
781      g:global_list: 0
782      OK
783      g:global_dict: 1
784      OK
785      g:global_dict: 0
786      OK
787      g:curly_global_var: 1
788      OK
789      g:curly_global_var: 0
790      OK
791      w:window_var: 1
792      OK
793      w:window_var: 0
794      OK
795      w:window_list: 1
796      OK
797      w:window_list: 0
798      OK
799      w:window_dict: 1
800      OK
801      w:window_dict: 0
802      OK
803      w:curly_window_var: 1
804      OK
805      w:curly_window_var: 0
806      OK
807      b:buffer_var: 1
808      OK
809      b:buffer_var: 0
810      OK
811      b:buffer_list: 1
812      OK
813      b:buffer_list: 0
814      OK
815      b:buffer_dict: 1
816      OK
817      b:buffer_dict: 0
818      OK
819      b:curly_buffer_var: 1
820      OK
821      b:curly_buffer_var: 0
822      OK
823      s:script_var: 1
824      OK
825      s:script_var: 0
826      OK
827      s:script_list: 1
828      OK
829      s:script_list: 0
830      OK
831      s:script_dict: 1
832      OK
833      s:script_dict: 0
834      OK
835      s:curly_script_var: 1
836      OK
837      s:curly_script_var: 0
838      OK
839      *s:my_script_func: 1
840      OK
841      *s:my_script_func: 0
842      OK
843      v:version: 1
844      OK
845      v:non_exists_var: 0
846      OK
847      a:func_arg: 1
848      OK
849      a:non_exists_arg: 0
850      OK
851      a:1: 1
852      OK
853      a:2: 0
854      OK
855       g:footest#x = 1
856         footest#F() 0
857      UndefFun() 0]])
858  end)
859end)