summaryrefslogtreecommitdiffstats
path: root/src/testdir/test_exists.vim
blob: 9cdb3fb5c0b330b8c3019aab3ff69a25920aeb44 (plain)
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
" Tests for the exists() function

import './vim9.vim' as v9

func Test_exists()
  augroup myagroup
      autocmd! BufEnter       *.my     echo "myfile edited"
      autocmd! FuncUndefined  UndefFun exec "fu UndefFun()\nendfu"
  augroup END
  set rtp+=./sautest

  " valid autocmd group
  call assert_equal(1, exists('#myagroup'))
  " valid autocmd group with garbage
  call assert_equal(0, exists('#myagroup+b'))
  " Valid autocmd group and event
  call assert_equal(1, exists('#myagroup#BufEnter'))
  " Valid autocmd group, event and pattern
  call assert_equal(1, exists('#myagroup#BufEnter#*.my'))
  " Valid autocmd event
  call assert_equal(1, exists('#BufEnter'))
  " Valid autocmd event and pattern
  call assert_equal(1, exists('#BufEnter#*.my'))
  " Non-existing autocmd group or event
  call assert_equal(0, exists('#xyzagroup'))
  " Non-existing autocmd group and valid autocmd event
  call assert_equal(0, exists('#xyzagroup#BufEnter'))
  " Valid autocmd group and event with no matching pattern
  call assert_equal(0, exists('#myagroup#CmdwinEnter'))
  " Valid autocmd group and non-existing autocmd event
  call assert_equal(0, exists('#myagroup#xyzacmd'))
  " Valid autocmd group and event and non-matching pattern
  call assert_equal(0, exists('#myagroup#BufEnter#xyzpat'))
  " Valid autocmd event and non-matching pattern
  call assert_equal(0, exists('#BufEnter#xyzpat'))
  " Empty autocmd group, event and pattern
  call assert_equal(0, exists('###'))
  " Empty autocmd group and event or empty event and pattern
  call assert_equal(0, exists('##'))
  " Valid autocmd event
  call assert_equal(1, exists('##FileReadCmd'))
  " Non-existing autocmd event
  call assert_equal(0, exists('##MySpecialCmd'))

  " Existing and working option (long form)
  call assert_equal(1, exists('&textwidth'))
  " Existing and working option (short form)
  call assert_equal(1, exists('&tw'))
  " Existing and working option with garbage
  call assert_equal(0, exists('&tw-'))
  " Global option
  call assert_equal(1, exists('&g:errorformat'))
  " Local option
  call assert_equal(1, exists('&l:errorformat'))
  " Negative form of existing and working option (long form)
  call assert_equal(0, exists('&nojoinspaces'))
  " Negative form of existing and working option (short form)
  call assert_equal(0, exists('&nojs'))
  " Non-existing option
  call assert_equal(0, exists('&myxyzoption'))

  " Existing and working option (long form)
  call assert_equal(1, exists('+incsearch'))
  " Existing and working option with garbage
  call assert_equal(0, exists('+incsearch!1'))
  " Existing and working option (short form)
  call assert_equal(1, exists('+is'))
  " Existing option that is hidden.
  call assert_equal(0, exists('+autoprint'))

  " Existing environment variable
  let $EDITOR_NAME = 'Vim Editor'
  call assert_equal(1, exists('$EDITOR_NAME'))
  if has('unix')
    " ${name} environment variables are supported only on Unix-like systems
    call assert_equal(1, exists('${VIM}'))
  endif
  " Non-existing environment variable
  call assert_equal(0, exists('$NON_ENV_VAR'))

  " Valid internal function
  call assert_equal(1, exists('*bufnr'))
  " Valid internal function with ()
  call assert_equal(1, exists('*bufnr()'))
  " Non-existing internal function
  call assert_equal(0, exists('*myxyzfunc'))
  " Valid internal function with garbage
  call assert_equal(0, exists('*bufnr&6'))
  " Valid user defined function
  call assert_equal(1, exists('*Test_exists'))
  " Non-existing user defined function
  call assert_equal(0, exists('*MyxyzFunc'))
  " Function that may be created by FuncUndefined event
  call assert_equal(0, exists('*UndefFun'))
  " Function that may be created by script autoloading
  call assert_equal(0, exists('*footest#F'))

  call assert_equal(has('float'), exists('*acos'))
  call assert_equal(1, exists('?acos'))
  call assert_equal(has('win32'), exists('*debugbreak'))
  call assert_equal(1, exists('?debugbreak'))

  " Valid internal command (full match)
  call assert_equal(2, exists(':edit'))
  " Valid internal command (full match) with garbage
  call assert_equal(0, exists(':edit/a'))
  " Valid internal command (partial match)
  call assert_equal(1, exists(':q'))
  " Valid internal command with a digit
  call assert_equal(2, exists(':2match'))
  " Non-existing internal command
  call assert_equal(0, exists(':invalidcmd'))
  " Internal command with a count
  call assert_equal(0, exists(':3buffer'))

  " User defined command (full match)
  command! MyCmd :echo 'My command'
  call assert_equal(2, exists(':MyCmd'))
  " User defined command (partial match)
  command! MyOtherCmd :echo 'Another command'
  call assert_equal(3, exists(':My'))

  " Command modifier
  call assert_equal(2, exists(':rightbelow'))

  " Non-existing user defined command (full match)
  delcommand MyCmd
  call assert_equal(0, exists(':MyCmd'))

  " Non-existing user defined command (partial match)
  delcommand MyOtherCmd
  call assert_equal(0, exists(':My'))

  " Valid local variable
  let local_var = 1
  call assert_equal(1, exists('local_var'))
  " Valid local variable with garbage
  call assert_equal(0, exists('local_var%n'))
  " Non-existing local variable
  unlet local_var
  call assert_equal(0, exists('local_var'))

  " Non-existing autoload variable that may be autoloaded
  call assert_equal(0, exists('footest#x'))

  " Valid local list
  let local_list = ["blue", "orange"]
  call assert_equal(1, exists('local_list'))
  " Valid local list item
  call assert_equal(1, exists('local_list[1]'))
  " Valid local list item with garbage
  call assert_equal(0, exists('local_list[1]+5'))
  " Invalid local list item
  call assert_equal(0, exists('local_list[2]'))
  " Non-existing local list
  unlet local_list
  call assert_equal(0, exists('local_list'))
  " Valid local dictionary
  let local_dict = {"xcord":100, "ycord":2}
  call assert_equal(1, exists('local_dict'))
  " Non-existing local dictionary
  unlet local_dict
  call assert_equal(0, exists('local_dict'))
  " Existing local curly-brace variable
  let str = "local"
  let curly_{str}_var = 1
  call assert_equal(1, exists('curly_{str}_var'))
  " Non-existing local curly-brace variable
  unlet curly_{str}_var
  call assert_equal(0, exists('curly_{str}_var'))

  " Existing global variable
  let g:global_var = 1
  call assert_equal(1, exists('g:global_var'))
  " Existing global variable with garbage
  call assert_equal(0, exists('g:global_var-n'))
  " Non-existing global variable
  unlet g:global_var
  call assert_equal(0, exists('g:global_var'))
  " Existing global list
  let g:global_list = ["blue", "orange"]
  call assert_equal(1, exists('g:global_list'))
  " Non-existing global list
  unlet g:global_list
  call assert_equal(0, exists('g:global_list'))
  " Existing global dictionary
  let g:global_dict = {"xcord":100, "ycord":2}
  call assert_equal(1, exists('g:global_dict'))
  " Non-existing global dictionary
  unlet g:global_dict
  call assert_equal(0, exists('g:global_dict'))
  " Existing global curly-brace variable
  let str = "global"
  let g:curly_{str}_var = 1
  call assert_equal(1, exists('g:curly_{str}_var'))
  " Non-existing global curly-brace variable
  unlet g:curly_{str}_var
  call assert_equal(0, exists('g:curly_{str}_var'))

  " Existing window variable
  let w:window_var = 1
  call assert_equal(1, exists('w:window_var'))
  " Non-existing window variable
  unlet w:window_var
  call assert_equal(0, exists('w:window_var'))
  " Existing window list
  let w:window_list = ["blue", "orange"]
  call assert_equal(1, exists('w:window_list'))
  " Non-existing window list
  unlet w:window_list
  call assert_equal(0, exists('w:window_list'))
  " Existing window dictionary
  let w:window_dict = {"xcord":100, "ycord":2}
  call assert_equal(1, exists('w:window_dict'))
  " Non-existing window dictionary
  unlet w:window_dict
  call assert_equal(0, exists('w:window_dict'))
  " Existing window curly-brace variable
  let str = "window"
  let w:curly_{str}_var = 1
  call assert_equal(1, exists('w:curly_{str}_var'))
  " Non-existing window curly-brace variable
  unlet w:curly_{str}_var
  call assert_equal(0, exists('w:curly_{str}_var'))

  " Existing tab variable
  let t:tab_var = 1
  call assert_equal(1, exists('t:tab_var'))
  " Non-existing tab variable
  unlet t:tab_var
  call assert_equal(0, exists('t:tab_var'))
  " Existing tab list
  let t:tab_list = ["blue", "orange"]
  call assert_equal(1, exists('t:tab_list'))
  " Non-existing tab list
  unlet t:tab_list
  call assert_equal(0, exists('t:tab_list'))
  " Existing tab dictionary
  let t:tab_dict = {"xcord":100, "ycord":2}
  call assert_equal(1, exists('t:tab_dict'))
  " Non-existing tab dictionary
  unlet t:tab_dict
  call assert_equal(0, exists('t:tab_dict'))
  " Existing tab curly-brace variable
  let str = "tab"
  let t:curly_{str}_var = 1
  call assert_equal(1, exists('t:curly_{str}_var'))
  " Non-existing tab curly-brace variable
  unlet t:curly_{str}_var
  call assert_equal(0, exists('t:curly_{str}_var'))

  " Existing buffer variable
  let b:buffer_var = 1
  call assert_equal(1, exists('b:buffer_var'))
  " Non-existing buffer variable
  unlet b:buffer_var
  call assert_equal(0, exists('b:buffer_var'))
  " Existing buffer list
  let b:buffer_list = ["blue", "orange"]
  call assert_equal(1, exists('b:buffer_list'))
  " Non-existing buffer list
  unlet b:buffer_list
  call assert_equal(0, exists('b:buffer_list'))
  " Existing buffer dictionary
  let b:buffer_dict = {"xcord":100, "ycord":2}
  call assert_equal(1, exists('b:buffer_dict'))
  " Non-existing buffer dictionary
  unlet b:buffer_dict
  call assert_equal(0, exists('b:buffer_dict'))
  " Existing buffer curly-brace variable
  let str = "buffer"
  let b:curly_{str}_var = 1
  call assert_equal(1, exists('b:curly_{str}_var'))
  " Non-existing buffer curly-brace variable
  unlet b:curly_{str}_var
  call assert_equal(0, exists('b:curly_{str}_var'))

  " Existing Vim internal variable
  call assert_equal(1, exists('v:version'))
  " Non-existing Vim internal variable
  call assert_equal(0, exists('v:non_exists_var'))

  " Existing script-local variable
  let s:script_var = 1
  call assert_equal(1, exists('s:script_var'))
  " Non-existing script-local variable
  unlet s:script_var
  call assert_equal(0, exists('s:script_var'))
  " Existing script-local list
  let s:script_list = ["blue", "orange"]
  call assert_equal(1, exists('s:script_list'))
  " Non-existing script-local list
  unlet s:script_list
  call assert_equal(0, exists('s:script_list'))
  " Existing script-local dictionary
  let s:script_dict = {"xcord":100, "ycord":2}
  call assert_equal(1, exists('s:script_dict'))
  " Non-existing script-local dictionary
  unlet s:script_dict
  call assert_equal(0, exists('s:script_dict'))
  " Existing script curly-brace variable
  let str = "script"
  let s:curly_{str}_var = 1
  call assert_equal(1, exists('s:curly_{str}_var'))
  " Non-existing script-local curly-brace variable
  unlet s:curly_{str}_var
  call assert_equal(0, exists('s:curly_{str}_var'))

  " Existing script-local function
  function! s:my_script_func()
  endfunction

  echo '*s:my_script_func: 1'
  call assert_equal(1, exists('*s:my_script_func'))

  " Non-existing script-local function
  delfunction s:my_script_func

  call assert_equal(0, exists('*s:my_script_func'))
  unlet str

  call assert_equal(1, g:footest#x)
  call assert_equal(0, footest#F())
  call assert_equal(0, UndefFun())
endfunc

" exists() test for Function arguments
func FuncArg_Tests(func_arg, ...)
  call assert_equal(1, exists('a:func_arg'))
  call assert_equal(0, exists('a:non_exists_arg'))
  call assert_equal(1, exists('a:1'))
  call assert_equal(0, exists('a:2'))
endfunc

func Test_exists_funcarg()
  call FuncArg_Tests("arg1", "arg2")
endfunc

" Test for using exists() with class and object variables and methods.
func Test_exists_class_object()
  let lines =<< trim END
    vim9script
    class A
      var var1: number = 10
      static var var2: number = 10
      static def Foo()
      enddef
      def Bar()
      enddef
    endclass

    assert_equal(1, exists("A"))
    var a = A.new()
    assert_equal(1, exists("a"))

    assert_equal(1, exists("a.var1"))
    assert_fails('exists("a.var2")', 'E1375: Class variable "var2" accessible only using class "A"')
    assert_fails('exists("a.var3")', 'E1326: Variable "var3" not found in object "A"')
    assert_equal(1, exists("A.var2"))
    assert_fails('exists("A.var1")', 'E1376: Object variable "var1" accessible only using class "A" object')
    assert_fails('exists("A.var3")', 'E1337: Class variable "var3" not found in class "A"')

    assert_equal(1, exists("a.Bar"))
    assert_fails('exists("a.Barz")', 'E1326: Variable "Barz" not found in object "A"')
    assert_fails('exists("a.Foo")', 'E1326: Variable "Foo" not found in object "A"')
    assert_equal(1, exists("A.Foo"))
    assert_fails('exists("A.Bar")', 'E1337: Class variable "Bar" not found in class "A"')
    assert_fails('exists("A.Barz")', 'E1337: Class variable "Barz" not found in class "A"')

    def Baz()
      assert_equal(1, exists("A"))
      var aa = A.new()
      assert_equal(1, exists("A.var2"))
      assert_fails('exists("A.var1")', 'E1376: Object variable "var1" accessible only using class "A" object')
      assert_fails('exists("A.var3")', 'E1337: Class variable "var3" not found in class "A"')

      assert_equal(1, exists("A.Foo"))
      assert_fails('exists("A.Bar")', 'E1337: Class variable "Bar" not found in class "A"')
      assert_fails('exists("A.Barz")', 'E1337: Class variable "Barz" not found in class "A"')
    enddef
    Baz()
  END
  call v9.CheckSourceSuccess(lines)
endfunc

" vim: shiftwidth=2 sts=2 expandtab