Bram Moolenaar | 4a137b4 | 2017-08-04 22:37:11 +0200 | [diff] [blame] | 1 | " Tests for the :let command. |
| 2 | |
| 3 | func Test_let() |
| 4 | " Test to not autoload when assigning. It causes internal error. |
| 5 | set runtimepath+=./sautest |
| 6 | let Test104#numvar = function('tr') |
| 7 | call assert_equal("function('tr')", string(Test104#numvar)) |
| 8 | |
| 9 | let a = 1 |
| 10 | let b = 2 |
| 11 | |
| 12 | let out = execute('let a b') |
| 13 | let s = "\na #1\nb #2" |
| 14 | call assert_equal(s, out) |
| 15 | |
| 16 | let out = execute('let {0 == 1 ? "a" : "b"}') |
| 17 | let s = "\nb #2" |
| 18 | call assert_equal(s, out) |
| 19 | |
| 20 | let out = execute('let {0 == 1 ? "a" : "b"} a') |
| 21 | let s = "\nb #2\na #1" |
| 22 | call assert_equal(s, out) |
| 23 | |
| 24 | let out = execute('let a {0 == 1 ? "a" : "b"}') |
| 25 | let s = "\na #1\nb #2" |
| 26 | call assert_equal(s, out) |
Bram Moolenaar | 8f76e6b | 2019-11-26 16:50:30 +0100 | [diff] [blame] | 27 | |
| 28 | let x = 0 |
| 29 | if 0 | let x = 1 | endif |
| 30 | call assert_equal(0, x) |
Bram Moolenaar | 4a137b4 | 2017-08-04 22:37:11 +0200 | [diff] [blame] | 31 | endfunc |
Bram Moolenaar | 31b8160 | 2019-02-10 22:14:27 +0100 | [diff] [blame] | 32 | |
| 33 | func s:set_arg1(a) abort |
| 34 | let a:a = 1 |
| 35 | endfunction |
| 36 | |
| 37 | func s:set_arg2(a) abort |
| 38 | let a:b = 1 |
| 39 | endfunction |
| 40 | |
| 41 | func s:set_arg3(a) abort |
| 42 | let b = a: |
| 43 | let b['a'] = 1 |
| 44 | endfunction |
| 45 | |
| 46 | func s:set_arg4(a) abort |
| 47 | let b = a: |
| 48 | let b['a'] = 1 |
| 49 | endfunction |
| 50 | |
| 51 | func s:set_arg5(a) abort |
| 52 | let b = a: |
| 53 | let b['a'][0] = 1 |
| 54 | endfunction |
| 55 | |
| 56 | func s:set_arg6(a) abort |
| 57 | let a:a[0] = 1 |
| 58 | endfunction |
| 59 | |
| 60 | func s:set_arg7(a) abort |
| 61 | call extend(a:, {'a': 1}) |
| 62 | endfunction |
| 63 | |
| 64 | func s:set_arg8(a) abort |
| 65 | call extend(a:, {'b': 1}) |
| 66 | endfunction |
| 67 | |
| 68 | func s:set_arg9(a) abort |
| 69 | let a:['b'] = 1 |
| 70 | endfunction |
| 71 | |
| 72 | func s:set_arg10(a) abort |
| 73 | let b = a: |
| 74 | call extend(b, {'a': 1}) |
| 75 | endfunction |
| 76 | |
| 77 | func s:set_arg11(a) abort |
| 78 | let b = a: |
| 79 | call extend(b, {'b': 1}) |
| 80 | endfunction |
| 81 | |
| 82 | func s:set_arg12(a) abort |
| 83 | let b = a: |
| 84 | let b['b'] = 1 |
| 85 | endfunction |
| 86 | |
| 87 | func Test_let_arg_fail() |
| 88 | call assert_fails('call s:set_arg1(1)', 'E46:') |
| 89 | call assert_fails('call s:set_arg2(1)', 'E461:') |
| 90 | call assert_fails('call s:set_arg3(1)', 'E46:') |
| 91 | call assert_fails('call s:set_arg4(1)', 'E46:') |
| 92 | call assert_fails('call s:set_arg5(1)', 'E46:') |
| 93 | call s:set_arg6([0]) |
| 94 | call assert_fails('call s:set_arg7(1)', 'E742:') |
| 95 | call assert_fails('call s:set_arg8(1)', 'E742:') |
| 96 | call assert_fails('call s:set_arg9(1)', 'E461:') |
| 97 | call assert_fails('call s:set_arg10(1)', 'E742:') |
| 98 | call assert_fails('call s:set_arg11(1)', 'E742:') |
| 99 | call assert_fails('call s:set_arg12(1)', 'E461:') |
| 100 | endfunction |
| 101 | |
| 102 | func s:set_varg1(...) abort |
| 103 | let a:000 = [] |
| 104 | endfunction |
| 105 | |
| 106 | func s:set_varg2(...) abort |
| 107 | let a:000[0] = 1 |
| 108 | endfunction |
| 109 | |
| 110 | func s:set_varg3(...) abort |
| 111 | let a:000 += [1] |
| 112 | endfunction |
| 113 | |
| 114 | func s:set_varg4(...) abort |
| 115 | call add(a:000, 1) |
| 116 | endfunction |
| 117 | |
| 118 | func s:set_varg5(...) abort |
| 119 | let a:000[0][0] = 1 |
| 120 | endfunction |
| 121 | |
| 122 | func s:set_varg6(...) abort |
| 123 | let b = a:000 |
| 124 | let b[0] = 1 |
| 125 | endfunction |
| 126 | |
| 127 | func s:set_varg7(...) abort |
| 128 | let b = a:000 |
| 129 | let b += [1] |
| 130 | endfunction |
| 131 | |
| 132 | func s:set_varg8(...) abort |
| 133 | let b = a:000 |
| 134 | call add(b, 1) |
| 135 | endfunction |
| 136 | |
| 137 | func s:set_varg9(...) abort |
| 138 | let b = a:000 |
| 139 | let b[0][0] = 1 |
| 140 | endfunction |
| 141 | |
| 142 | func Test_let_varg_fail() |
| 143 | call assert_fails('call s:set_varg1(1)', 'E46:') |
| 144 | call assert_fails('call s:set_varg2(1)', 'E742:') |
| 145 | call assert_fails('call s:set_varg3(1)', 'E46:') |
| 146 | call assert_fails('call s:set_varg4(1)', 'E742:') |
| 147 | call s:set_varg5([0]) |
| 148 | call assert_fails('call s:set_varg6(1)', 'E742:') |
Bram Moolenaar | 05c00c0 | 2019-02-11 22:00:11 +0100 | [diff] [blame] | 149 | call assert_fails('call s:set_varg7(1)', 'E742:') |
Bram Moolenaar | 31b8160 | 2019-02-10 22:14:27 +0100 | [diff] [blame] | 150 | call assert_fails('call s:set_varg8(1)', 'E742:') |
| 151 | call s:set_varg9([0]) |
| 152 | endfunction |
Bram Moolenaar | f0908e6 | 2019-03-30 20:11:50 +0100 | [diff] [blame] | 153 | |
| 154 | func Test_let_utf8_environment() |
| 155 | let $a = 'ĀĒĪŌŪあいうえお' |
| 156 | call assert_equal('ĀĒĪŌŪあいうえお', $a) |
| 157 | endfunc |
Bram Moolenaar | f5842c5 | 2019-05-19 18:41:26 +0200 | [diff] [blame] | 158 | |
Bram Moolenaar | fcf8a87 | 2019-11-06 15:22:00 +0100 | [diff] [blame] | 159 | func Test_let_no_type_checking() |
| 160 | let v = 1 |
| 161 | let v = [1,2,3] |
| 162 | let v = {'a': 1, 'b': 2} |
| 163 | let v = 3.4 |
| 164 | let v = 'hello' |
| 165 | endfunc |
| 166 | |
| 167 | func Test_let_termcap() |
| 168 | " Terminal code |
| 169 | let old_t_te = &t_te |
| 170 | let &t_te = "\<Esc>[yes;" |
| 171 | call assert_match('t_te.*^[[yes;', execute("set termcap")) |
| 172 | let &t_te = old_t_te |
| 173 | |
| 174 | if exists("+t_k1") |
| 175 | " Key code |
| 176 | let old_t_k1 = &t_k1 |
| 177 | let &t_k1 = "that" |
| 178 | call assert_match('t_k1.*that', execute("set termcap")) |
| 179 | let &t_k1 = old_t_k1 |
| 180 | endif |
| 181 | |
| 182 | call assert_fails('let x = &t_xx', 'E113') |
| 183 | let &t_xx = "yes" |
| 184 | call assert_equal("yes", &t_xx) |
| 185 | let &t_xx = "" |
| 186 | call assert_fails('let x = &t_xx', 'E113') |
| 187 | endfunc |
| 188 | |
| 189 | func Test_let_option_error() |
| 190 | let _w = &tw |
| 191 | let &tw = 80 |
| 192 | call assert_fails('let &tw .= 1', 'E734') |
| 193 | call assert_equal(80, &tw) |
| 194 | let &tw = _w |
| 195 | |
| 196 | let _w = &fillchars |
| 197 | let &fillchars = "vert:|" |
| 198 | call assert_fails('let &fillchars += "diff:-"', 'E734') |
| 199 | call assert_equal("vert:|", &fillchars) |
| 200 | let &fillchars = _w |
| 201 | endfunc |
| 202 | |
| 203 | func Test_let_errors() |
| 204 | let s = 'abcd' |
| 205 | call assert_fails('let s[1] = 5', 'E689:') |
| 206 | |
| 207 | let l = [1, 2, 3] |
| 208 | call assert_fails('let l[:] = 5', 'E709:') |
| 209 | endfunc |
| 210 | |
Bram Moolenaar | 8471e57 | 2019-05-19 21:37:18 +0200 | [diff] [blame] | 211 | func Test_let_heredoc_fails() |
| 212 | call assert_fails('let v =<< marker', 'E991:') |
| 213 | |
| 214 | let text =<< trim END |
| 215 | func WrongSyntax() |
| 216 | let v =<< that there |
| 217 | endfunc |
| 218 | END |
| 219 | call writefile(text, 'XheredocFail') |
| 220 | call assert_fails('source XheredocFail', 'E126:') |
| 221 | call delete('XheredocFail') |
| 222 | |
Bram Moolenaar | 2458200 | 2019-07-21 14:14:26 +0200 | [diff] [blame] | 223 | let text =<< trim CodeEnd |
Bram Moolenaar | 8471e57 | 2019-05-19 21:37:18 +0200 | [diff] [blame] | 224 | func MissingEnd() |
| 225 | let v =<< END |
| 226 | endfunc |
Bram Moolenaar | 2458200 | 2019-07-21 14:14:26 +0200 | [diff] [blame] | 227 | CodeEnd |
Bram Moolenaar | 8471e57 | 2019-05-19 21:37:18 +0200 | [diff] [blame] | 228 | call writefile(text, 'XheredocWrong') |
| 229 | call assert_fails('source XheredocWrong', 'E126:') |
| 230 | call delete('XheredocWrong') |
Bram Moolenaar | 2458200 | 2019-07-21 14:14:26 +0200 | [diff] [blame] | 231 | |
| 232 | let text =<< trim TEXTend |
| 233 | let v =<< " comment |
| 234 | TEXTend |
| 235 | call writefile(text, 'XheredocNoMarker') |
| 236 | call assert_fails('source XheredocNoMarker', 'E172:') |
| 237 | call delete('XheredocNoMarker') |
| 238 | |
| 239 | let text =<< trim TEXTend |
| 240 | let v =<< text |
| 241 | TEXTend |
| 242 | call writefile(text, 'XheredocBadMarker') |
| 243 | call assert_fails('source XheredocBadMarker', 'E221:') |
| 244 | call delete('XheredocBadMarker') |
Bram Moolenaar | 8471e57 | 2019-05-19 21:37:18 +0200 | [diff] [blame] | 245 | endfunc |
| 246 | |
Bram Moolenaar | ecaa75b | 2019-07-21 23:04:21 +0200 | [diff] [blame] | 247 | func Test_let_heredoc_trim_no_indent_marker() |
| 248 | let text =<< trim END |
| 249 | Text |
| 250 | with |
| 251 | indent |
| 252 | END |
| 253 | call assert_equal(['Text', 'with', 'indent'], text) |
| 254 | endfunc |
| 255 | |
Bram Moolenaar | f5842c5 | 2019-05-19 18:41:26 +0200 | [diff] [blame] | 256 | " Test for the setting a variable using the heredoc syntax |
| 257 | func Test_let_heredoc() |
| 258 | let var1 =<< END |
| 259 | Some sample text |
| 260 | Text with indent |
| 261 | !@#$%^&*()-+_={}|[]\~`:";'<>?,./ |
| 262 | END |
| 263 | |
| 264 | call assert_equal(["Some sample text", "\tText with indent", " !@#$%^&*()-+_={}|[]\\~`:\";'<>?,./"], var1) |
| 265 | |
Bram Moolenaar | 2458200 | 2019-07-21 14:14:26 +0200 | [diff] [blame] | 266 | let var2 =<< XXX |
Bram Moolenaar | f5842c5 | 2019-05-19 18:41:26 +0200 | [diff] [blame] | 267 | Editor |
Bram Moolenaar | 2458200 | 2019-07-21 14:14:26 +0200 | [diff] [blame] | 268 | XXX |
Bram Moolenaar | f5842c5 | 2019-05-19 18:41:26 +0200 | [diff] [blame] | 269 | call assert_equal(['Editor'], var2) |
| 270 | |
| 271 | let var3 =<<END |
| 272 | END |
| 273 | call assert_equal([], var3) |
| 274 | |
| 275 | let var3 =<<END |
| 276 | vim |
| 277 | |
| 278 | end |
| 279 | END |
| 280 | END |
| 281 | END |
| 282 | call assert_equal(['vim', '', 'end', ' END', 'END '], var3) |
| 283 | |
| 284 | let var1 =<< trim END |
| 285 | Line1 |
| 286 | Line2 |
| 287 | Line3 |
| 288 | END |
| 289 | END |
| 290 | call assert_equal(['Line1', ' Line2', "\tLine3", ' END'], var1) |
| 291 | |
Bram Moolenaar | e7eb927 | 2019-06-24 00:58:07 +0200 | [diff] [blame] | 292 | let var1 =<< trim !!! |
| 293 | Line1 |
| 294 | line2 |
| 295 | Line3 |
| 296 | !!! |
| 297 | !!! |
| 298 | call assert_equal(['Line1', ' line2', "\tLine3", '!!!',], var1) |
| 299 | |
Bram Moolenaar | 2458200 | 2019-07-21 14:14:26 +0200 | [diff] [blame] | 300 | let var1 =<< trim XX |
Bram Moolenaar | f5842c5 | 2019-05-19 18:41:26 +0200 | [diff] [blame] | 301 | Line1 |
Bram Moolenaar | 2458200 | 2019-07-21 14:14:26 +0200 | [diff] [blame] | 302 | XX |
Bram Moolenaar | e7eb927 | 2019-06-24 00:58:07 +0200 | [diff] [blame] | 303 | call assert_equal(['Line1'], var1) |
Bram Moolenaar | f5842c5 | 2019-05-19 18:41:26 +0200 | [diff] [blame] | 304 | |
Bram Moolenaar | 8471e57 | 2019-05-19 21:37:18 +0200 | [diff] [blame] | 305 | " ignore "endfunc" |
| 306 | let var1 =<< END |
| 307 | something |
Bram Moolenaar | f5842c5 | 2019-05-19 18:41:26 +0200 | [diff] [blame] | 308 | endfunc |
Bram Moolenaar | 8471e57 | 2019-05-19 21:37:18 +0200 | [diff] [blame] | 309 | END |
| 310 | call assert_equal(['something', 'endfunc'], var1) |
Bram Moolenaar | f5842c5 | 2019-05-19 18:41:26 +0200 | [diff] [blame] | 311 | |
Bram Moolenaar | 8471e57 | 2019-05-19 21:37:18 +0200 | [diff] [blame] | 312 | " ignore "endfunc" with trim |
| 313 | let var1 =<< trim END |
| 314 | something |
| 315 | endfunc |
| 316 | END |
| 317 | call assert_equal(['something', 'endfunc'], var1) |
Bram Moolenaar | f5842c5 | 2019-05-19 18:41:26 +0200 | [diff] [blame] | 318 | |
Bram Moolenaar | e96a249 | 2019-06-25 04:12:16 +0200 | [diff] [blame] | 319 | " not concatenate lines |
| 320 | let var1 =<< END |
| 321 | some |
| 322 | \thing |
| 323 | \ else |
| 324 | END |
| 325 | call assert_equal(['some', ' \thing', ' \ else'], var1) |
| 326 | |
Bram Moolenaar | 8471e57 | 2019-05-19 21:37:18 +0200 | [diff] [blame] | 327 | " ignore "python << xx" |
| 328 | let var1 =<<END |
| 329 | something |
| 330 | python << xx |
| 331 | END |
| 332 | call assert_equal(['something', 'python << xx'], var1) |
| 333 | |
| 334 | " ignore "python << xx" with trim |
| 335 | let var1 =<< trim END |
| 336 | something |
| 337 | python << xx |
| 338 | END |
| 339 | call assert_equal(['something', 'python << xx'], var1) |
| 340 | |
| 341 | " ignore "append" |
Bram Moolenaar | 2458200 | 2019-07-21 14:14:26 +0200 | [diff] [blame] | 342 | let var1 =<< E |
Bram Moolenaar | 8471e57 | 2019-05-19 21:37:18 +0200 | [diff] [blame] | 343 | something |
| 344 | app |
Bram Moolenaar | 2458200 | 2019-07-21 14:14:26 +0200 | [diff] [blame] | 345 | E |
Bram Moolenaar | 8471e57 | 2019-05-19 21:37:18 +0200 | [diff] [blame] | 346 | call assert_equal(['something', 'app'], var1) |
| 347 | |
| 348 | " ignore "append" with trim |
Bram Moolenaar | 2458200 | 2019-07-21 14:14:26 +0200 | [diff] [blame] | 349 | let var1 =<< trim END |
Bram Moolenaar | 8471e57 | 2019-05-19 21:37:18 +0200 | [diff] [blame] | 350 | something |
| 351 | app |
Bram Moolenaar | 2458200 | 2019-07-21 14:14:26 +0200 | [diff] [blame] | 352 | END |
Bram Moolenaar | 8471e57 | 2019-05-19 21:37:18 +0200 | [diff] [blame] | 353 | call assert_equal(['something', 'app'], var1) |
Bram Moolenaar | b1ba9ab | 2019-10-16 23:34:42 +0200 | [diff] [blame] | 354 | |
| 355 | let check = [] |
| 356 | if 0 |
| 357 | let check =<< trim END |
| 358 | from heredoc |
| 359 | END |
| 360 | endif |
| 361 | call assert_equal([], check) |
Bram Moolenaar | 1e673b9 | 2019-11-06 15:02:50 +0100 | [diff] [blame] | 362 | |
| 363 | " unpack assignment |
| 364 | let [a, b, c] =<< END |
| 365 | x |
| 366 | \y |
| 367 | z |
| 368 | END |
| 369 | call assert_equal([' x', ' \y', ' z'], [a, b, c]) |
Bram Moolenaar | f5842c5 | 2019-05-19 18:41:26 +0200 | [diff] [blame] | 370 | endfunc |