xref: /vim-8.2.3635/src/testdir/test_const.vim (revision 1d59aa1f)
1" Test for :const
2
3func s:noop()
4endfunc
5
6func Test_define_var_with_lock()
7    const i = 1
8    const f = 1.1
9    const s = 'vim'
10    const F = funcref('s:noop')
11    const l = [1, 2, 3]
12    const d = {'foo': 10}
13    if has('channel')
14      const j = test_null_job()
15      const c = test_null_channel()
16    endif
17    const b = v:true
18    const n = v:null
19    const bl = 0zC0FFEE
20    const here =<< trim EOS
21      hello
22    EOS
23
24    call assert_true(exists('i'))
25    call assert_true(exists('f'))
26    call assert_true(exists('s'))
27    call assert_true(exists('F'))
28    call assert_true(exists('l'))
29    call assert_true(exists('d'))
30    if has('channel')
31        call assert_true(exists('j'))
32        call assert_true(exists('c'))
33    endif
34    call assert_true(exists('b'))
35    call assert_true(exists('n'))
36    call assert_true(exists('bl'))
37    call assert_true(exists('here'))
38
39    call assert_fails('let i = 1', 'E741:')
40    call assert_fails('let f = 1.1', 'E741:')
41    call assert_fails('let s = "vim"', 'E741:')
42    call assert_fails('let F = funcref("s:noop")', 'E741:')
43    call assert_fails('let l = [1, 2, 3]', 'E741:')
44    call assert_fails('call filter(l, "v:val % 2 == 0")', 'E741:')
45    call assert_fails('let d = {"foo": 10}', 'E741:')
46    if has('channel')
47      call assert_fails('let j = test_null_job()', 'E741:')
48      call assert_fails('let c = test_null_channel()', 'E741:')
49    endif
50    call assert_fails('let b = v:true', 'E741:')
51    call assert_fails('let n = v:null', 'E741:')
52    call assert_fails('let bl = 0zC0FFEE', 'E741:')
53    call assert_fails('let here = "xxx"', 'E741:')
54
55    " Unlet
56    unlet i
57    unlet f
58    unlet s
59    unlet F
60    unlet l
61    unlet d
62    if has('channel')
63      unlet j
64      unlet c
65    endif
66    unlet b
67    unlet n
68    unlet bl
69    unlet here
70endfunc
71
72func Test_define_l_var_with_lock()
73    " With l: prefix
74    const l:i = 1
75    const l:f = 1.1
76    const l:s = 'vim'
77    const l:F = funcref('s:noop')
78    const l:l = [1, 2, 3]
79    const l:d = {'foo': 10}
80    if has('channel')
81      const l:j = test_null_job()
82      const l:c = test_null_channel()
83    endif
84    const l:b = v:true
85    const l:n = v:null
86    const l:bl = 0zC0FFEE
87    const l:here =<< trim EOS
88      hello
89    EOS
90
91    call assert_fails('let l:i = 1', 'E741:')
92    call assert_fails('let l:f = 1.1', 'E741:')
93    call assert_fails('let l:s = "vim"', 'E741:')
94    call assert_fails('let l:F = funcref("s:noop")', 'E741:')
95    call assert_fails('let l:l = [1, 2, 3]', 'E741:')
96    call assert_fails('let l:d = {"foo": 10}', 'E741:')
97    if has('channel')
98      call assert_fails('let l:j = test_null_job()', 'E741:')
99      call assert_fails('let l:c = test_null_channel()', 'E741:')
100    endif
101    call assert_fails('let l:b = v:true', 'E741:')
102    call assert_fails('let l:n = v:null', 'E741:')
103    call assert_fails('let l:bl = 0zC0FFEE', 'E741:')
104    call assert_fails('let l:here = "xxx"', 'E741:')
105
106    " Unlet
107    unlet l:i
108    unlet l:f
109    unlet l:s
110    unlet l:F
111    unlet l:l
112    unlet l:d
113    if has('channel')
114      unlet l:j
115      unlet l:c
116    endif
117    unlet l:b
118    unlet l:n
119    unlet l:bl
120    unlet l:here
121endfunc
122
123func Test_define_script_var_with_lock()
124    const s:x = 0
125    call assert_fails('let s:x = 1', 'E741:')
126    unlet s:x
127endfunc
128
129func Test_destructuring_with_lock()
130    const [a, b, c] = [1, 1.1, 'vim']
131
132    call assert_fails('let a = 1', 'E741:')
133    call assert_fails('let b = 1.1', 'E741:')
134    call assert_fails('let c = "vim"', 'E741:')
135
136    const [d; e] = [1, 1.1, 'vim']
137    call assert_fails('let d = 1', 'E741:')
138    call assert_fails('let e = [2.2, "a"]', 'E741:')
139endfunc
140
141func Test_cannot_modify_existing_variable()
142    let i = 1
143    let f = 1.1
144    let s = 'vim'
145    let F = funcref('s:noop')
146    let l = [1, 2, 3]
147    let d = {'foo': 10}
148    if has('channel')
149      let j = test_null_job()
150      let c = test_null_channel()
151    endif
152    let b = v:true
153    let n = v:null
154    let bl = 0zC0FFEE
155
156    call assert_fails('const i = 1', 'E995:')
157    call assert_fails('const f = 1.1', 'E995:')
158    call assert_fails('const s = "vim"', 'E995:')
159    call assert_fails('const F = funcref("s:noop")', 'E995:')
160    call assert_fails('const l = [1, 2, 3]', 'E995:')
161    call assert_fails('const d = {"foo": 10}', 'E995:')
162    if has('channel')
163      call assert_fails('const j = test_null_job()', 'E995:')
164      call assert_fails('const c = test_null_channel()', 'E995:')
165    endif
166    call assert_fails('const b = v:true', 'E995:')
167    call assert_fails('const n = v:null', 'E995:')
168    call assert_fails('const bl = 0zC0FFEE', 'E995:')
169    call assert_fails('const [i, f, s] = [1, 1.1, "vim"]', 'E995:')
170
171    const i2 = 1
172    const f2 = 1.1
173    const s2 = 'vim'
174    const F2 = funcref('s:noop')
175    const l2 = [1, 2, 3]
176    const d2 = {'foo': 10}
177    if has('channel')
178      const j2 = test_null_job()
179      const c2 = test_null_channel()
180    endif
181    const b2 = v:true
182    const n2 = v:null
183    const bl2 = 0zC0FFEE
184
185    call assert_fails('const i2 = 1', 'E995:')
186    call assert_fails('const f2 = 1.1', 'E995:')
187    call assert_fails('const s2 = "vim"', 'E995:')
188    call assert_fails('const F2 = funcref("s:noop")', 'E995:')
189    call assert_fails('const l2 = [1, 2, 3]', 'E995:')
190    call assert_fails('const d2 = {"foo": 10}', 'E995:')
191    if has('channel')
192      call assert_fails('const j2 = test_null_job()', 'E995:')
193      call assert_fails('const c2 = test_null_channel()', 'E995:')
194    endif
195    call assert_fails('const b2 = v:true', 'E995:')
196    call assert_fails('const n2 = v:null', 'E995:')
197    call assert_fails('const bl2 = 0zC0FFEE', 'E995:')
198    call assert_fails('const [i2, f2, s2] = [1, 1.1, "vim"]', 'E995:')
199endfunc
200
201func Test_const_with_condition()
202  const x = 0
203  if 0 | const x = 1 | endif
204  call assert_equal(0, x)
205endfunc
206
207func Test_lockvar()
208  let x = 'hello'
209  lockvar x
210  call assert_fails('let x = "there"', 'E741:')
211  if 0 | unlockvar x | endif
212  call assert_fails('let x = "there"', 'E741:')
213  unlockvar x
214  let x = 'there'
215
216  if 0 | lockvar x | endif
217  let x = 'again'
218
219  let val = [1, 2, 3]
220  lockvar 0 val
221  let val[0] = 9
222  call assert_equal([9, 2, 3], val)
223  call add(val, 4)
224  call assert_equal([9, 2, 3, 4], val)
225  call assert_fails('let val = [4, 5, 6]', 'E1122:')
226endfunc
227
228
229func Test_const_with_index_access()
230    let l = [1, 2, 3]
231    call assert_fails('const l[0] = 4', 'E996:')
232    call assert_fails('const l[0:1] = [1, 2]', 'E996:')
233
234    let d = {'aaa': 0}
235    call assert_fails("const d['aaa'] = 4", 'E996:')
236    call assert_fails("const d.aaa = 4", 'E996:')
237endfunc
238
239func Test_const_with_compound_assign()
240    let i = 0
241    call assert_fails('const i += 4', 'E995:')
242    call assert_fails('const i -= 4', 'E995:')
243    call assert_fails('const i *= 4', 'E995:')
244    call assert_fails('const i /= 4', 'E995:')
245    call assert_fails('const i %= 4', 'E995:')
246
247    let s = 'a'
248    call assert_fails('const s .= "b"', 'E995:')
249
250    let [a, b, c] = [1, 2, 3]
251    call assert_fails('const [a, b, c] += [4, 5, 6]', 'E995:')
252
253    let [d; e] = [1, 2, 3]
254    call assert_fails('const [d; e] += [4, 5, 6]', 'E995:')
255endfunc
256
257func Test_const_with_special_variables()
258    call assert_fails('const $FOO = "hello"', 'E996:')
259    call assert_fails('const @a = "hello"', 'E996:')
260    call assert_fails('const &filetype = "vim"', 'E996:')
261    call assert_fails('const &l:filetype = "vim"', 'E996:')
262    call assert_fails('const &g:encoding = "utf-8"', 'E996:')
263
264    call assert_fails('const [a, $CONST_FOO] = [369, "abc"]', 'E996:')
265    call assert_equal(369, a)
266    call assert_equal(v:null, getenv("CONST_FOO"))
267
268    call assert_fails('const [b; $CONST_FOO] = [246, 2, "abc"]', 'E996:')
269    call assert_equal(246, b)
270    call assert_equal(v:null, getenv("CONST_FOO"))
271endfunc
272
273func Test_const_with_eval_name()
274    let s = 'foo'
275
276    " eval name with :const should work
277    const abc_{s} = 1
278    const {s}{s} = 1
279
280    let s2 = 'abc_foo'
281    call assert_fails('const {s2} = "bar"', 'E995:')
282endfunc
283
284func Test_lock_depth_is_2()
285    " Modify list - error when changing item or adding/removing items
286    const l = [1, 2, [3, 4]]
287    call assert_fails('let l[0] = 42', 'E741:')
288    call assert_fails('let l[2][0] = 42', 'E741:')
289    call assert_fails('call add(l, 4)', 'E741:')
290    call assert_fails('unlet l[1]', 'E741:')
291
292    " Modify blob - error when changing
293    const b = 0z001122
294    call assert_fails('let b[0] = 42', 'E741:')
295
296    " Modify dict - error when changing item or adding/removing items
297    const d = {'foo': 10}
298    call assert_fails("let d['foo'] = 'hello'", 'E741:')
299    call assert_fails("let d.foo = 'hello'", 'E741:')
300    call assert_fails("let d['bar'] = 'hello'", 'E741:')
301    call assert_fails("unlet d['foo']", 'E741:')
302
303    " Modifying list or dict item contents is OK.
304    let lvar = ['a', 'b']
305    let bvar = 0z1122
306    const l2 = [0, lvar, bvar]
307    let l2[1][0] = 'c'
308    let l2[2][1] = 0x33
309    call assert_equal([0, ['c', 'b'], 0z1133], l2)
310
311    const d2 = #{a: 0, b: lvar, c: 4}
312    let d2.b[1] = 'd'
313endfunc
314
315" vim: shiftwidth=2 sts=2 expandtab
316