blob: 107e2503e0920889c6adeb36e3af7a0e10ab0321 [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:')
Bram Moolenaar24157272020-08-16 22:50:01 +020044 call assert_fails('call filter(l, "v:val % 2 == 0")', 'E741:')
Bram Moolenaar9937a052019-06-15 15:45:06 +020045 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
Bram Moolenaarb6e3b882019-06-15 17:50:57 +020062 if has('channel')
63 unlet j
64 unlet c
65 endif
Bram Moolenaar9937a052019-06-15 15:45:06 +020066 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
Bram Moolenaare7eb9272019-06-24 00:58:07 +020088 hello
Bram Moolenaar9937a052019-06-15 15:45:06 +020089 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
Bram Moolenaarb6e3b882019-06-15 17:50:57 +0200113 if has('channel')
114 unlet l:j
115 unlet l:c
116 endif
Bram Moolenaar9937a052019-06-15 15:45:06 +0200117 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
Bram Moolenaar1bc353b2019-09-01 14:45:28 +0200129func Test_destructuring_with_lock()
Bram Moolenaar9937a052019-06-15 15:45:06 +0200130 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
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +0100201func Test_const_with_condition()
202 const x = 0
203 if 0 | const x = 1 | endif
204 call assert_equal(0, x)
205endfunc
206
Bram Moolenaarcc4423a2019-11-26 17:05:00 +0100207func 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'
218endfunc
219
220
Bram Moolenaar9937a052019-06-15 15:45:06 +0200221func Test_const_with_index_access()
222 let l = [1, 2, 3]
223 call assert_fails('const l[0] = 4', 'E996:')
224 call assert_fails('const l[0:1] = [1, 2]', 'E996:')
225
226 let d = {'aaa': 0}
227 call assert_fails("const d['aaa'] = 4", 'E996:')
228 call assert_fails("const d.aaa = 4", 'E996:')
229endfunc
230
231func Test_const_with_compound_assign()
232 let i = 0
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 call assert_fails('const i %= 4', 'E995:')
238
239 let s = 'a'
240 call assert_fails('const s .= "b"', 'E995:')
241
242 let [a, b, c] = [1, 2, 3]
243 call assert_fails('const [a, b, c] += [4, 5, 6]', 'E995:')
244
245 let [d; e] = [1, 2, 3]
246 call assert_fails('const [d; e] += [4, 5, 6]', 'E995:')
247endfunc
248
249func Test_const_with_special_variables()
250 call assert_fails('const $FOO = "hello"', 'E996:')
251 call assert_fails('const @a = "hello"', 'E996:')
252 call assert_fails('const &filetype = "vim"', 'E996:')
253 call assert_fails('const &l:filetype = "vim"', 'E996:')
254 call assert_fails('const &g:encoding = "utf-8"', 'E996:')
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100255
256 call assert_fails('const [a, $CONST_FOO] = [369, "abc"]', 'E996:')
257 call assert_equal(369, a)
258 call assert_equal(v:null, getenv("CONST_FOO"))
259
260 call assert_fails('const [b; $CONST_FOO] = [246, 2, "abc"]', 'E996:')
261 call assert_equal(246, b)
262 call assert_equal(v:null, getenv("CONST_FOO"))
Bram Moolenaar9937a052019-06-15 15:45:06 +0200263endfunc
264
Bram Moolenaar1c196e72019-06-16 15:41:58 +0200265func Test_const_with_eval_name()
266 let s = 'foo'
267
268 " eval name with :const should work
269 const abc_{s} = 1
270 const {s}{s} = 1
271
272 let s2 = 'abc_foo'
273 call assert_fails('const {s2} = "bar"', 'E995:')
274endfunc
275
Bram Moolenaar021bda52020-08-17 21:07:22 +0200276func Test_lock_depth_is_2()
277 " Modify list - error when changing item or adding/removing items
278 const l = [1, 2, [3, 4]]
279 call assert_fails('let l[0] = 42', 'E741:')
280 call assert_fails('let l[2][0] = 42', 'E741:')
Bram Moolenaar24157272020-08-16 22:50:01 +0200281 call assert_fails('call add(l, 4)', 'E741:')
282 call assert_fails('unlet l[1]', 'E741:')
Bram Moolenaar9937a052019-06-15 15:45:06 +0200283
Bram Moolenaar021bda52020-08-17 21:07:22 +0200284 " Modify blob - error when changing
285 const b = 0z001122
286 call assert_fails('let b[0] = 42', 'E741:')
287
288 " Modify dict - error when changing item or adding/removing items
289 const d = {'foo': 10}
290 call assert_fails("let d['foo'] = 'hello'", 'E741:')
291 call assert_fails("let d.foo = 'hello'", 'E741:')
Bram Moolenaar24157272020-08-16 22:50:01 +0200292 call assert_fails("let d['bar'] = 'hello'", 'E741:')
293 call assert_fails("unlet d['foo']", 'E741:')
Bram Moolenaar021bda52020-08-17 21:07:22 +0200294
295 " Modifying list or dict item contents is OK.
296 let lvar = ['a', 'b']
297 let bvar = 0z1122
298 const l2 = [0, lvar, bvar]
299 let l2[1][0] = 'c'
300 let l2[2][1] = 0x33
301 call assert_equal([0, ['c', 'b'], 0z1133], l2)
302
303 const d2 = #{a: 0, b: lvar, c: 4}
304 let d2.b[1] = 'd'
Bram Moolenaar9937a052019-06-15 15:45:06 +0200305endfunc
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100306
307" vim: shiftwidth=2 sts=2 expandtab