blob: a0f919e45e823c7b362f550a8e595bfed3fe9bb1 [file] [log] [blame]
Bram Moolenaar9937a052019-06-15 15:45:06 +02001" 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
Bram Moolenaare7eb9272019-06-24 00:58:07 +020021 hello
Bram Moolenaar9937a052019-06-15 15:45:06 +020022 EOS
23
Bram Moolenaar1c196e72019-06-16 15:41:58 +020024 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
Bram Moolenaar9937a052019-06-15 15:45:06 +020039 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('let d = {"foo": 10}', 'E741:')
45 if has('channel')
46 call assert_fails('let j = test_null_job()', 'E741:')
47 call assert_fails('let c = test_null_channel()', 'E741:')
48 endif
49 call assert_fails('let b = v:true', 'E741:')
50 call assert_fails('let n = v:null', 'E741:')
51 call assert_fails('let bl = 0zC0FFEE', 'E741:')
52 call assert_fails('let here = "xxx"', 'E741:')
53
54 " Unlet
55 unlet i
56 unlet f
57 unlet s
58 unlet F
59 unlet l
60 unlet d
Bram Moolenaarb6e3b882019-06-15 17:50:57 +020061 if has('channel')
62 unlet j
63 unlet c
64 endif
Bram Moolenaar9937a052019-06-15 15:45:06 +020065 unlet b
66 unlet n
67 unlet bl
68 unlet here
69endfunc
70
71func Test_define_l_var_with_lock()
72 " With l: prefix
73 const l:i = 1
74 const l:f = 1.1
75 const l:s = 'vim'
76 const l:F = funcref('s:noop')
77 const l:l = [1, 2, 3]
78 const l:d = {'foo': 10}
79 if has('channel')
80 const l:j = test_null_job()
81 const l:c = test_null_channel()
82 endif
83 const l:b = v:true
84 const l:n = v:null
85 const l:bl = 0zC0FFEE
86 const l:here =<< trim EOS
Bram Moolenaare7eb9272019-06-24 00:58:07 +020087 hello
Bram Moolenaar9937a052019-06-15 15:45:06 +020088 EOS
89
90 call assert_fails('let l:i = 1', 'E741:')
91 call assert_fails('let l:f = 1.1', 'E741:')
92 call assert_fails('let l:s = "vim"', 'E741:')
93 call assert_fails('let l:F = funcref("s:noop")', 'E741:')
94 call assert_fails('let l:l = [1, 2, 3]', 'E741:')
95 call assert_fails('let l:d = {"foo": 10}', 'E741:')
96 if has('channel')
97 call assert_fails('let l:j = test_null_job()', 'E741:')
98 call assert_fails('let l:c = test_null_channel()', 'E741:')
99 endif
100 call assert_fails('let l:b = v:true', 'E741:')
101 call assert_fails('let l:n = v:null', 'E741:')
102 call assert_fails('let l:bl = 0zC0FFEE', 'E741:')
103 call assert_fails('let l:here = "xxx"', 'E741:')
104
105 " Unlet
106 unlet l:i
107 unlet l:f
108 unlet l:s
109 unlet l:F
110 unlet l:l
111 unlet l:d
Bram Moolenaarb6e3b882019-06-15 17:50:57 +0200112 if has('channel')
113 unlet l:j
114 unlet l:c
115 endif
Bram Moolenaar9937a052019-06-15 15:45:06 +0200116 unlet l:b
117 unlet l:n
118 unlet l:bl
119 unlet l:here
120endfunc
121
122func Test_define_script_var_with_lock()
123 const s:x = 0
124 call assert_fails('let s:x = 1', 'E741:')
125 unlet s:x
126endfunc
127
Bram Moolenaar1bc353b2019-09-01 14:45:28 +0200128func Test_destructuring_with_lock()
Bram Moolenaar9937a052019-06-15 15:45:06 +0200129 const [a, b, c] = [1, 1.1, 'vim']
130
131 call assert_fails('let a = 1', 'E741:')
132 call assert_fails('let b = 1.1', 'E741:')
133 call assert_fails('let c = "vim"', 'E741:')
134
135 const [d; e] = [1, 1.1, 'vim']
136 call assert_fails('let d = 1', 'E741:')
137 call assert_fails('let e = [2.2, "a"]', 'E741:')
138endfunc
139
140func Test_cannot_modify_existing_variable()
141 let i = 1
142 let f = 1.1
143 let s = 'vim'
144 let F = funcref('s:noop')
145 let l = [1, 2, 3]
146 let d = {'foo': 10}
147 if has('channel')
148 let j = test_null_job()
149 let c = test_null_channel()
150 endif
151 let b = v:true
152 let n = v:null
153 let bl = 0zC0FFEE
154
155 call assert_fails('const i = 1', 'E995:')
156 call assert_fails('const f = 1.1', 'E995:')
157 call assert_fails('const s = "vim"', 'E995:')
158 call assert_fails('const F = funcref("s:noop")', 'E995:')
159 call assert_fails('const l = [1, 2, 3]', 'E995:')
160 call assert_fails('const d = {"foo": 10}', 'E995:')
161 if has('channel')
162 call assert_fails('const j = test_null_job()', 'E995:')
163 call assert_fails('const c = test_null_channel()', 'E995:')
164 endif
165 call assert_fails('const b = v:true', 'E995:')
166 call assert_fails('const n = v:null', 'E995:')
167 call assert_fails('const bl = 0zC0FFEE', 'E995:')
168 call assert_fails('const [i, f, s] = [1, 1.1, "vim"]', 'E995:')
169
170 const i2 = 1
171 const f2 = 1.1
172 const s2 = 'vim'
173 const F2 = funcref('s:noop')
174 const l2 = [1, 2, 3]
175 const d2 = {'foo': 10}
176 if has('channel')
177 const j2 = test_null_job()
178 const c2 = test_null_channel()
179 endif
180 const b2 = v:true
181 const n2 = v:null
182 const bl2 = 0zC0FFEE
183
184 call assert_fails('const i2 = 1', 'E995:')
185 call assert_fails('const f2 = 1.1', 'E995:')
186 call assert_fails('const s2 = "vim"', 'E995:')
187 call assert_fails('const F2 = funcref("s:noop")', 'E995:')
188 call assert_fails('const l2 = [1, 2, 3]', 'E995:')
189 call assert_fails('const d2 = {"foo": 10}', 'E995:')
190 if has('channel')
191 call assert_fails('const j2 = test_null_job()', 'E995:')
192 call assert_fails('const c2 = test_null_channel()', 'E995:')
193 endif
194 call assert_fails('const b2 = v:true', 'E995:')
195 call assert_fails('const n2 = v:null', 'E995:')
196 call assert_fails('const bl2 = 0zC0FFEE', 'E995:')
197 call assert_fails('const [i2, f2, s2] = [1, 1.1, "vim"]', 'E995:')
198endfunc
199
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +0100200func Test_const_with_condition()
201 const x = 0
202 if 0 | const x = 1 | endif
203 call assert_equal(0, x)
204endfunc
205
Bram Moolenaarcc4423a2019-11-26 17:05:00 +0100206func Test_lockvar()
207 let x = 'hello'
208 lockvar x
209 call assert_fails('let x = "there"', 'E741')
210 if 0 | unlockvar x | endif
211 call assert_fails('let x = "there"', 'E741')
212 unlockvar x
213 let x = 'there'
214
215 if 0 | lockvar x | endif
216 let x = 'again'
217endfunc
218
219
Bram Moolenaar9937a052019-06-15 15:45:06 +0200220func Test_const_with_index_access()
221 let l = [1, 2, 3]
222 call assert_fails('const l[0] = 4', 'E996:')
223 call assert_fails('const l[0:1] = [1, 2]', 'E996:')
224
225 let d = {'aaa': 0}
226 call assert_fails("const d['aaa'] = 4", 'E996:')
227 call assert_fails("const d.aaa = 4", 'E996:')
228endfunc
229
230func Test_const_with_compound_assign()
231 let i = 0
232 call assert_fails('const i += 4', 'E995:')
233 call assert_fails('const i -= 4', 'E995:')
234 call assert_fails('const i *= 4', 'E995:')
235 call assert_fails('const i /= 4', 'E995:')
236 call assert_fails('const i %= 4', 'E995:')
237
238 let s = 'a'
239 call assert_fails('const s .= "b"', 'E995:')
240
241 let [a, b, c] = [1, 2, 3]
242 call assert_fails('const [a, b, c] += [4, 5, 6]', 'E995:')
243
244 let [d; e] = [1, 2, 3]
245 call assert_fails('const [d; e] += [4, 5, 6]', 'E995:')
246endfunc
247
248func Test_const_with_special_variables()
249 call assert_fails('const $FOO = "hello"', 'E996:')
250 call assert_fails('const @a = "hello"', 'E996:')
251 call assert_fails('const &filetype = "vim"', 'E996:')
252 call assert_fails('const &l:filetype = "vim"', 'E996:')
253 call assert_fails('const &g:encoding = "utf-8"', 'E996:')
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100254
255 call assert_fails('const [a, $CONST_FOO] = [369, "abc"]', 'E996:')
256 call assert_equal(369, a)
257 call assert_equal(v:null, getenv("CONST_FOO"))
258
259 call assert_fails('const [b; $CONST_FOO] = [246, 2, "abc"]', 'E996:')
260 call assert_equal(246, b)
261 call assert_equal(v:null, getenv("CONST_FOO"))
Bram Moolenaar9937a052019-06-15 15:45:06 +0200262endfunc
263
Bram Moolenaar1c196e72019-06-16 15:41:58 +0200264func Test_const_with_eval_name()
265 let s = 'foo'
266
267 " eval name with :const should work
268 const abc_{s} = 1
269 const {s}{s} = 1
270
271 let s2 = 'abc_foo'
272 call assert_fails('const {s2} = "bar"', 'E995:')
273endfunc
274
Bram Moolenaar9937a052019-06-15 15:45:06 +0200275func Test_lock_depth_is_1()
276 const l = [1, 2, 3]
277 const d = {'foo': 10}
278
279 " Modify list
280 call add(l, 4)
281 let l[0] = 42
282
283 " Modify dict
284 let d['bar'] = 'hello'
285 let d.foo = 44
286endfunc
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100287
288" vim: shiftwidth=2 sts=2 expandtab