neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

test_const.vim (8466B)


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