summaryrefslogtreecommitdiffstats
path: root/src/testdir/test_const.vim
blob: 2b79f42feb28ad266709b5d962300a3812529b3f (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
" Test for :const

func s:noop()
endfunc

func Test_define_var_with_lock()
    const i = 1
    const f = 1.1
    const s = 'vim'
    const F = funcref('s:noop')
    const l = [1, 2, 3]
    const d = {'foo': 10}
    if has('channel')
      const j = test_null_job()
      const c = test_null_channel()
    endif
    const b = v:true
    const n = v:null
    const bl = 0zC0FFEE
    const here =<< trim EOS
      hello
    EOS

    call assert_true(exists('i'))
    call assert_true(exists('f'))
    call assert_true(exists('s'))
    call assert_true(exists('F'))
    call assert_true(exists('l'))
    call assert_true(exists('d'))
    if has('channel')
        call assert_true(exists('j'))
        call assert_true(exists('c'))
    endif
    call assert_true(exists('b'))
    call assert_true(exists('n'))
    call assert_true(exists('bl'))
    call assert_true(exists('here'))

    call assert_fails('let i = 1', 'E741:')
    call assert_fails('let f = 1.1', 'E741:')
    call assert_fails('let s = "vim"', 'E741:')
    call assert_fails('let F = funcref("s:noop")', 'E741:')
    call assert_fails('let l = [1, 2, 3]', 'E741:')
    call assert_fails('call filter(l, "v:val % 2 == 0")', 'E741:')
    call assert_fails('let d = {"foo": 10}', 'E741:')
    if has('channel')
      call assert_fails('let j = test_null_job()', 'E741:')
      call assert_fails('let c = test_null_channel()', 'E741:')
    endif
    call assert_fails('let b = v:true', 'E741:')
    call assert_fails('let n = v:null', 'E741:')
    call assert_fails('let bl = 0zC0FFEE', 'E741:')
    call assert_fails('let here = "xxx"', 'E741:')

    " Unlet
    unlet i
    unlet f
    unlet s
    unlet F
    unlet l
    unlet d
    if has('channel')
      unlet j
      unlet c
    endif
    unlet b
    unlet n
    unlet bl
    unlet here
endfunc

func Test_define_l_var_with_lock()
    " With l: prefix
    const l:i = 1
    const l:f = 1.1
    const l:s = 'vim'
    const l:F = funcref('s:noop')
    const l:l = [1, 2, 3]
    const l:d = {'foo': 10}
    if has('channel')
      const l:j = test_null_job()
      const l:c = test_null_channel()
    endif
    const l:b = v:true
    const l:n = v:null
    const l:bl = 0zC0FFEE
    const l:here =<< trim EOS
      hello
    EOS

    call assert_fails('let l:i = 1', 'E741:')
    call assert_fails('let l:f = 1.1', 'E741:')
    call assert_fails('let l:s = "vim"', 'E741:')
    call assert_fails('let l:F = funcref("s:noop")', 'E741:')
    call assert_fails('let l:l = [1, 2, 3]', 'E741:')
    call assert_fails('let l:d = {"foo": 10}', 'E741:')
    if has('channel')
      call assert_fails('let l:j = test_null_job()', 'E741:')
      call assert_fails('let l:c = test_null_channel()', 'E741:')
    endif
    call assert_fails('let l:b = v:true', 'E741:')
    call assert_fails('let l:n = v:null', 'E741:')
    call assert_fails('let l:bl = 0zC0FFEE', 'E741:')
    call assert_fails('let l:here = "xxx"', 'E741:')

    " Unlet
    unlet l:i
    unlet l:f
    unlet l:s
    unlet l:F
    unlet l:l
    unlet l:d
    if has('channel')
      unlet l:j
      unlet l:c
    endif
    unlet l:b
    unlet l:n
    unlet l:bl
    unlet l:here
endfunc

func Test_define_script_var_with_lock()
    const s:x = 0
    call assert_fails('let s:x = 1', 'E741:')
    unlet s:x
endfunc

func Test_destructuring_with_lock()
    const [a, b, c] = [1, 1.1, 'vim']

    call assert_fails('let a = 1', 'E741:')
    call assert_fails('let b = 1.1', 'E741:')
    call assert_fails('let c = "vim"', 'E741:')

    const [d; e] = [1, 1.1, 'vim']
    call assert_fails('let d = 1', 'E741:')
    call assert_fails('let e = [2.2, "a"]', 'E741:')
endfunc

func Test_cannot_modify_existing_variable()
    let i = 1
    let f = 1.1
    let s = 'vim'
    let F = funcref('s:noop')
    let l = [1, 2, 3]
    let d = {'foo': 10}
    if has('channel')
      let j = test_null_job()
      let c = test_null_channel()
    endif
    let b = v:true
    let n = v:null
    let bl = 0zC0FFEE

    call assert_fails('const i = 1', 'E995:')
    call assert_fails('const f = 1.1', 'E995:')
    call assert_fails('const s = "vim"', 'E995:')
    call assert_fails('const F = funcref("s:noop")', 'E995:')
    call assert_fails('const l = [1, 2, 3]', 'E995:')
    call assert_fails('const d = {"foo": 10}', 'E995:')
    if has('channel')
      call assert_fails('const j = test_null_job()', 'E995:')
      call assert_fails('const c = test_null_channel()', 'E995:')
    endif
    call assert_fails('const b = v:true', 'E995:')
    call assert_fails('const n = v:null', 'E995:')
    call assert_fails('const bl = 0zC0FFEE', 'E995:')
    call assert_fails('const [i, f, s] = [1, 1.1, "vim"]', 'E995:')

    const i2 = 1
    const f2 = 1.1
    const s2 = 'vim'
    const F2 = funcref('s:noop')
    const l2 = [1, 2, 3]
    const d2 = {'foo': 10}
    if has('channel')
      const j2 = test_null_job()
      const c2 = test_null_channel()
    endif
    const b2 = v:true
    const n2 = v:null
    const bl2 = 0zC0FFEE

    call assert_fails('const i2 = 1', 'E995:')
    call assert_fails('const f2 = 1.1', 'E995:')
    call assert_fails('const s2 = "vim"', 'E995:')
    call assert_fails('const F2 = funcref("s:noop")', 'E995:')
    call assert_fails('const l2 = [1, 2, 3]', 'E995:')
    call assert_fails('const d2 = {"foo": 10}', 'E995:')
    if has('channel')
      call assert_fails('const j2 = test_null_job()', 'E995:')
      call assert_fails('const c2 = test_null_channel()', 'E995:')
    endif
    call assert_fails('const b2 = v:true', 'E995:')
    call assert_fails('const n2 = v:null', 'E995:')
    call assert_fails('const bl2 = 0zC0FFEE', 'E995:')
    call assert_fails('const [i2, f2, s2] = [1, 1.1, "vim"]', 'E995:')
endfunc

func Test_const_with_condition()
  const x = 0
  if 0 | const x = 1 | endif
  call assert_equal(0, x)
endfunc

func Test_lockvar()
  let x = 'hello'
  lockvar x
  call assert_fails('let x = "there"', 'E741:')
  if 0 | unlockvar x | endif
  call assert_fails('let x = "there"', 'E741:')
  unlockvar x
  let x = 'there'

  if 0 | lockvar x | endif
  let x = 'again'

  let val = [1, 2, 3]
  lockvar 0 val
  let val[0] = 9
  call assert_equal([9, 2, 3], val)
  call add(val, 4)
  call assert_equal([9, 2, 3, 4], val)
  call assert_fails('let val = [4, 5, 6]', 'E1122:')

  let l =<< trim END
    let d = {}
    lockvar d
    func d.fn()
      return 1
    endfunc
  END
  let @a = l->join("\n")
  call assert_fails('exe @a', 'E741:')

  let l =<< trim END
    let d = {}
    let d.fn = function("min")
    lockvar d.fn
    func! d.fn()
      return 1
    endfunc
  END
  let @a = l->join("\n")
  call assert_fails('exe @a', 'E741:')
endfunc

func Test_const_with_index_access()
    let l = [1, 2, 3]
    call assert_fails('const l[0] = 4', 'E996:')
    call assert_fails('const l[0:1] = [1, 2]', 'E996:')

    let d = {'aaa': 0}
    call assert_fails("const d['aaa'] = 4", 'E996:')
    call assert_fails("const d.aaa = 4", 'E996:')
endfunc

func Test_const_with_compound_assign()
    let i = 0
    call assert_fails('const i += 4', 'E995:')
    call assert_fails('const i -= 4', 'E995:')
    call assert_fails('const i *= 4', 'E995:')
    call assert_fails('const i /= 4', 'E995:')
    call assert_fails('const i %= 4', 'E995:')

    let s = 'a'
    call assert_fails('const s .= "b"', 'E995:')

    let [a, b, c] = [1, 2, 3]
    call assert_fails('const [a, b, c] += [4, 5, 6]', 'E995:')

    let [d; e] = [1, 2, 3]
    call assert_fails('const [d; e] += [4, 5, 6]', 'E995:')
endfunc

func Test_const_with_special_variables()
    call assert_fails('const $FOO = "hello"', 'E996:')
    call assert_fails('const @a = "hello"', 'E996:')
    call assert_fails('const &filetype = "vim"', 'E996:')
    call assert_fails('const &l:filetype = "vim"', 'E996:')
    call assert_fails('const &g:encoding = "utf-8"', 'E996:')

    call assert_fails('const [a, $CONST_FOO] = [369, "abc"]', 'E996:')
    call assert_equal(369, a)
    call assert_equal(v:null, getenv("CONST_FOO"))

    call assert_fails('const [b; $CONST_FOO] = [246, 2, "abc"]', 'E996:')
    call assert_equal(246, b)
    call assert_equal(v:null, getenv("CONST_FOO"))
endfunc

func Test_const_with_eval_name()
    let s = 'foo'

    " eval name with :const should work
    const abc_{s} = 1
    const {s}{s} = 1

    let s2 = 'abc_foo'
    call assert_fails('const {s2} = "bar"', 'E995:')
endfunc

func Test_lock_depth_is_2()
    " Modify list - error when changing item or adding/removing items
    const l = [1, 2, [3, 4]]
    call assert_fails('let l[0] = 42', 'E741:')
    call assert_fails('let l[2][0] = 42', 'E741:')
    call assert_fails('call add(l, 4)', 'E741:')
    call assert_fails('unlet l[1]', 'E741:')

    " Modify blob - error when changing
    const b = 0z001122
    call assert_fails('let b[0] = 42', 'E741:')

    " Modify dict - error when changing item or adding/removing items
    const d = {'foo': 10}
    call assert_fails("let d['foo'] = 'hello'", 'E741:')
    call assert_fails("let d.foo = 'hello'", 'E741:')
    call assert_fails("let d['bar'] = 'hello'", 'E741:')
    call assert_fails("unlet d['foo']", 'E741:')

    " Modifying list or dict item contents is OK.
    let lvar = ['a', 'b']
    let bvar = 0z1122
    const l2 = [0, lvar, bvar]
    let l2[1][0] = 'c'
    let l2[2][1] = 0x33
    call assert_equal([0, ['c', 'b'], 0z1133], l2)

    const d2 = #{a: 0, b: lvar, c: 4}
    let d2.b[1] = 'd'
endfunc

" vim: shiftwidth=2 sts=2 expandtab