Bram Moolenaar | 9937a05 | 2019-06-15 15:45:06 +0200 | [diff] [blame] | 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 | const bl = 0zC0FFEE |
| 20 | const here =<< trim EOS |
| 21 | hello |
| 22 | EOS |
| 23 | |
| 24 | call assert_fails('let i = 1', 'E741:') |
| 25 | call assert_fails('let f = 1.1', 'E741:') |
| 26 | call assert_fails('let s = "vim"', 'E741:') |
| 27 | call assert_fails('let F = funcref("s:noop")', 'E741:') |
| 28 | call assert_fails('let l = [1, 2, 3]', 'E741:') |
| 29 | call assert_fails('let d = {"foo": 10}', 'E741:') |
| 30 | if has('channel') |
| 31 | call assert_fails('let j = test_null_job()', 'E741:') |
| 32 | call assert_fails('let c = test_null_channel()', 'E741:') |
| 33 | endif |
| 34 | call assert_fails('let b = v:true', 'E741:') |
| 35 | call assert_fails('let n = v:null', 'E741:') |
| 36 | call assert_fails('let bl = 0zC0FFEE', 'E741:') |
| 37 | call assert_fails('let here = "xxx"', 'E741:') |
| 38 | |
| 39 | " Unlet |
| 40 | unlet i |
| 41 | unlet f |
| 42 | unlet s |
| 43 | unlet F |
| 44 | unlet l |
| 45 | unlet d |
| 46 | unlet j |
| 47 | unlet c |
| 48 | unlet b |
| 49 | unlet n |
| 50 | unlet bl |
| 51 | unlet here |
| 52 | endfunc |
| 53 | |
| 54 | func Test_define_l_var_with_lock() |
| 55 | " With l: prefix |
| 56 | const l:i = 1 |
| 57 | const l:f = 1.1 |
| 58 | const l:s = 'vim' |
| 59 | const l:F = funcref('s:noop') |
| 60 | const l:l = [1, 2, 3] |
| 61 | const l:d = {'foo': 10} |
| 62 | if has('channel') |
| 63 | const l:j = test_null_job() |
| 64 | const l:c = test_null_channel() |
| 65 | endif |
| 66 | const l:b = v:true |
| 67 | const l:n = v:null |
| 68 | const l:bl = 0zC0FFEE |
| 69 | const l:here =<< trim EOS |
| 70 | hello |
| 71 | EOS |
| 72 | |
| 73 | call assert_fails('let l:i = 1', 'E741:') |
| 74 | call assert_fails('let l:f = 1.1', 'E741:') |
| 75 | call assert_fails('let l:s = "vim"', 'E741:') |
| 76 | call assert_fails('let l:F = funcref("s:noop")', 'E741:') |
| 77 | call assert_fails('let l:l = [1, 2, 3]', 'E741:') |
| 78 | call assert_fails('let l:d = {"foo": 10}', 'E741:') |
| 79 | if has('channel') |
| 80 | call assert_fails('let l:j = test_null_job()', 'E741:') |
| 81 | call assert_fails('let l:c = test_null_channel()', 'E741:') |
| 82 | endif |
| 83 | call assert_fails('let l:b = v:true', 'E741:') |
| 84 | call assert_fails('let l:n = v:null', 'E741:') |
| 85 | call assert_fails('let l:bl = 0zC0FFEE', 'E741:') |
| 86 | call assert_fails('let l:here = "xxx"', 'E741:') |
| 87 | |
| 88 | " Unlet |
| 89 | unlet l:i |
| 90 | unlet l:f |
| 91 | unlet l:s |
| 92 | unlet l:F |
| 93 | unlet l:l |
| 94 | unlet l:d |
| 95 | unlet l:j |
| 96 | unlet l:c |
| 97 | unlet l:b |
| 98 | unlet l:n |
| 99 | unlet l:bl |
| 100 | unlet l:here |
| 101 | endfunc |
| 102 | |
| 103 | func Test_define_script_var_with_lock() |
| 104 | const s:x = 0 |
| 105 | call assert_fails('let s:x = 1', 'E741:') |
| 106 | unlet s:x |
| 107 | endfunc |
| 108 | |
| 109 | func Test_descructuring_with_lock() |
| 110 | const [a, b, c] = [1, 1.1, 'vim'] |
| 111 | |
| 112 | call assert_fails('let a = 1', 'E741:') |
| 113 | call assert_fails('let b = 1.1', 'E741:') |
| 114 | call assert_fails('let c = "vim"', 'E741:') |
| 115 | |
| 116 | const [d; e] = [1, 1.1, 'vim'] |
| 117 | call assert_fails('let d = 1', 'E741:') |
| 118 | call assert_fails('let e = [2.2, "a"]', 'E741:') |
| 119 | endfunc |
| 120 | |
| 121 | func Test_cannot_modify_existing_variable() |
| 122 | let i = 1 |
| 123 | let f = 1.1 |
| 124 | let s = 'vim' |
| 125 | let F = funcref('s:noop') |
| 126 | let l = [1, 2, 3] |
| 127 | let d = {'foo': 10} |
| 128 | if has('channel') |
| 129 | let j = test_null_job() |
| 130 | let c = test_null_channel() |
| 131 | endif |
| 132 | let b = v:true |
| 133 | let n = v:null |
| 134 | let bl = 0zC0FFEE |
| 135 | |
| 136 | call assert_fails('const i = 1', 'E995:') |
| 137 | call assert_fails('const f = 1.1', 'E995:') |
| 138 | call assert_fails('const s = "vim"', 'E995:') |
| 139 | call assert_fails('const F = funcref("s:noop")', 'E995:') |
| 140 | call assert_fails('const l = [1, 2, 3]', 'E995:') |
| 141 | call assert_fails('const d = {"foo": 10}', 'E995:') |
| 142 | if has('channel') |
| 143 | call assert_fails('const j = test_null_job()', 'E995:') |
| 144 | call assert_fails('const c = test_null_channel()', 'E995:') |
| 145 | endif |
| 146 | call assert_fails('const b = v:true', 'E995:') |
| 147 | call assert_fails('const n = v:null', 'E995:') |
| 148 | call assert_fails('const bl = 0zC0FFEE', '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 | const bl2 = 0zC0FFEE |
| 164 | |
| 165 | call assert_fails('const i2 = 1', 'E995:') |
| 166 | call assert_fails('const f2 = 1.1', 'E995:') |
| 167 | call assert_fails('const s2 = "vim"', 'E995:') |
| 168 | call assert_fails('const F2 = funcref("s:noop")', 'E995:') |
| 169 | call assert_fails('const l2 = [1, 2, 3]', 'E995:') |
| 170 | call assert_fails('const d2 = {"foo": 10}', 'E995:') |
| 171 | if has('channel') |
| 172 | call assert_fails('const j2 = test_null_job()', 'E995:') |
| 173 | call assert_fails('const c2 = test_null_channel()', 'E995:') |
| 174 | endif |
| 175 | call assert_fails('const b2 = v:true', 'E995:') |
| 176 | call assert_fails('const n2 = v:null', 'E995:') |
| 177 | call assert_fails('const bl2 = 0zC0FFEE', 'E995:') |
| 178 | call assert_fails('const [i2, f2, s2] = [1, 1.1, "vim"]', 'E995:') |
| 179 | endfunc |
| 180 | |
| 181 | func Test_const_with_index_access() |
| 182 | let l = [1, 2, 3] |
| 183 | call assert_fails('const l[0] = 4', 'E996:') |
| 184 | call assert_fails('const l[0:1] = [1, 2]', 'E996:') |
| 185 | |
| 186 | let d = {'aaa': 0} |
| 187 | call assert_fails("const d['aaa'] = 4", 'E996:') |
| 188 | call assert_fails("const d.aaa = 4", 'E996:') |
| 189 | endfunc |
| 190 | |
| 191 | func Test_const_with_compound_assign() |
| 192 | let i = 0 |
| 193 | call assert_fails('const i += 4', 'E995:') |
| 194 | call assert_fails('const i -= 4', 'E995:') |
| 195 | call assert_fails('const i *= 4', 'E995:') |
| 196 | call assert_fails('const i /= 4', 'E995:') |
| 197 | call assert_fails('const i %= 4', 'E995:') |
| 198 | |
| 199 | let s = 'a' |
| 200 | call assert_fails('const s .= "b"', 'E995:') |
| 201 | |
| 202 | let [a, b, c] = [1, 2, 3] |
| 203 | call assert_fails('const [a, b, c] += [4, 5, 6]', 'E995:') |
| 204 | |
| 205 | let [d; e] = [1, 2, 3] |
| 206 | call assert_fails('const [d; e] += [4, 5, 6]', 'E995:') |
| 207 | endfunc |
| 208 | |
| 209 | func Test_const_with_special_variables() |
| 210 | call assert_fails('const $FOO = "hello"', 'E996:') |
| 211 | call assert_fails('const @a = "hello"', 'E996:') |
| 212 | call assert_fails('const &filetype = "vim"', 'E996:') |
| 213 | call assert_fails('const &l:filetype = "vim"', 'E996:') |
| 214 | call assert_fails('const &g:encoding = "utf-8"', 'E996:') |
| 215 | endfunc |
| 216 | |
| 217 | func Test_lock_depth_is_1() |
| 218 | const l = [1, 2, 3] |
| 219 | const d = {'foo': 10} |
| 220 | |
| 221 | " Modify list |
| 222 | call add(l, 4) |
| 223 | let l[0] = 42 |
| 224 | |
| 225 | " Modify dict |
| 226 | let d['bar'] = 'hello' |
| 227 | let d.foo = 44 |
| 228 | endfunc |