Bram Moolenaar | 4a137b4 | 2017-08-04 22:37:11 +0200 | [diff] [blame] | 1 | " Tests for the :let command. |
| 2 | |
Yegappan Lakshmanan | efbfa86 | 2022-04-17 12:47:40 +0100 | [diff] [blame] | 3 | import './vim9.vim' as v9 |
| 4 | |
Bram Moolenaar | 4a137b4 | 2017-08-04 22:37:11 +0200 | [diff] [blame] | 5 | func Test_let() |
| 6 | " Test to not autoload when assigning. It causes internal error. |
| 7 | set runtimepath+=./sautest |
| 8 | let Test104#numvar = function('tr') |
| 9 | call assert_equal("function('tr')", string(Test104#numvar)) |
| 10 | |
| 11 | let a = 1 |
| 12 | let b = 2 |
| 13 | |
| 14 | let out = execute('let a b') |
| 15 | let s = "\na #1\nb #2" |
| 16 | call assert_equal(s, out) |
| 17 | |
| 18 | let out = execute('let {0 == 1 ? "a" : "b"}') |
| 19 | let s = "\nb #2" |
| 20 | call assert_equal(s, out) |
| 21 | |
| 22 | let out = execute('let {0 == 1 ? "a" : "b"} a') |
| 23 | let s = "\nb #2\na #1" |
| 24 | call assert_equal(s, out) |
| 25 | |
| 26 | let out = execute('let a {0 == 1 ? "a" : "b"}') |
| 27 | let s = "\na #1\nb #2" |
| 28 | call assert_equal(s, out) |
Bram Moolenaar | 8f76e6b | 2019-11-26 16:50:30 +0100 | [diff] [blame] | 29 | |
Bram Moolenaar | 8dfcce3 | 2020-03-18 19:32:26 +0100 | [diff] [blame] | 30 | " Test for displaying a string variable |
| 31 | let s = 'vim' |
| 32 | let out = execute('let s') |
| 33 | let s = "\ns vim" |
| 34 | call assert_equal(s, out) |
| 35 | |
| 36 | " Test for displaying a list variable |
| 37 | let l = [1, 2] |
| 38 | let out = execute('let l') |
| 39 | let s = "\nl [1, 2]" |
| 40 | call assert_equal(s, out) |
| 41 | |
| 42 | " Test for displaying a dict variable |
| 43 | let d = {'k' : 'v'} |
| 44 | let out = execute('let d') |
| 45 | let s = "\nd {'k': 'v'}" |
| 46 | call assert_equal(s, out) |
| 47 | |
| 48 | " Test for displaying a function reference variable |
| 49 | let F = function('min') |
| 50 | let out = execute('let F') |
| 51 | let s = "\nF *min()" |
| 52 | call assert_equal(s, out) |
| 53 | |
Bram Moolenaar | 8f76e6b | 2019-11-26 16:50:30 +0100 | [diff] [blame] | 54 | let x = 0 |
| 55 | if 0 | let x = 1 | endif |
| 56 | call assert_equal(0, x) |
Bram Moolenaar | 8dfcce3 | 2020-03-18 19:32:26 +0100 | [diff] [blame] | 57 | |
| 58 | " Display a list item using an out of range index |
| 59 | let l = [10] |
| 60 | call assert_fails('let l[1]', 'E684:') |
| 61 | |
| 62 | " List special variable dictionaries |
| 63 | let g:Test_Global_Var = 5 |
| 64 | call assert_match("\nTest_Global_Var #5", execute('let g:')) |
| 65 | unlet g:Test_Global_Var |
| 66 | |
| 67 | let b:Test_Buf_Var = 8 |
| 68 | call assert_match("\nb:Test_Buf_Var #8", execute('let b:')) |
| 69 | unlet b:Test_Buf_Var |
| 70 | |
| 71 | let w:Test_Win_Var = 'foo' |
| 72 | call assert_equal("\nw:Test_Win_Var foo", execute('let w:')) |
| 73 | unlet w:Test_Win_Var |
| 74 | |
| 75 | let t:Test_Tab_Var = 'bar' |
| 76 | call assert_equal("\nt:Test_Tab_Var bar", execute('let t:')) |
| 77 | unlet t:Test_Tab_Var |
| 78 | |
| 79 | let s:Test_Script_Var = [7] |
| 80 | call assert_match("\ns:Test_Script_Var \\[7]", execute('let s:')) |
| 81 | unlet s:Test_Script_Var |
| 82 | |
| 83 | let l:Test_Local_Var = {'k' : 5} |
| 84 | call assert_match("\nl:Test_Local_Var {'k': 5}", execute('let l:')) |
| 85 | call assert_match("v:errors []", execute('let v:')) |
Bram Moolenaar | 9d8d0b5 | 2020-04-24 22:47:31 +0200 | [diff] [blame] | 86 | |
| 87 | " Test for assigning multiple list items |
| 88 | let l = [1, 2, 3] |
| 89 | let [l[0], l[1]] = [10, 20] |
| 90 | call assert_equal([10, 20, 3], l) |
| 91 | |
| 92 | " Test for errors in conditional expression |
| 93 | call assert_fails('let val = [] ? 1 : 2', 'E745:') |
| 94 | call assert_fails('let val = 1 ? 5+ : 6', 'E121:') |
| 95 | call assert_fails('let val = 1 ? 0 : 5+', 'E15:') |
| 96 | call assert_false(exists('val')) |
| 97 | |
| 98 | " Test for errors in logical operators |
| 99 | let @a = 'if [] || 0 | let val = 2 | endif' |
| 100 | call assert_fails('exe @a', 'E745:') |
| 101 | call assert_fails('call feedkeys(":let val = 0 || []\<cr>", "xt")', 'E745:') |
| 102 | call assert_fails('exe "let val = [] && 5"', 'E745:') |
| 103 | call assert_fails('exe "let val = 6 && []"', 'E745:') |
Bram Moolenaar | 4a137b4 | 2017-08-04 22:37:11 +0200 | [diff] [blame] | 104 | endfunc |
Bram Moolenaar | 31b8160 | 2019-02-10 22:14:27 +0100 | [diff] [blame] | 105 | |
| 106 | func s:set_arg1(a) abort |
| 107 | let a:a = 1 |
| 108 | endfunction |
| 109 | |
| 110 | func s:set_arg2(a) abort |
| 111 | let a:b = 1 |
| 112 | endfunction |
| 113 | |
| 114 | func s:set_arg3(a) abort |
| 115 | let b = a: |
| 116 | let b['a'] = 1 |
| 117 | endfunction |
| 118 | |
| 119 | func s:set_arg4(a) abort |
| 120 | let b = a: |
| 121 | let b['a'] = 1 |
| 122 | endfunction |
| 123 | |
| 124 | func s:set_arg5(a) abort |
| 125 | let b = a: |
| 126 | let b['a'][0] = 1 |
| 127 | endfunction |
| 128 | |
| 129 | func s:set_arg6(a) abort |
| 130 | let a:a[0] = 1 |
| 131 | endfunction |
| 132 | |
| 133 | func s:set_arg7(a) abort |
| 134 | call extend(a:, {'a': 1}) |
| 135 | endfunction |
| 136 | |
| 137 | func s:set_arg8(a) abort |
| 138 | call extend(a:, {'b': 1}) |
| 139 | endfunction |
| 140 | |
| 141 | func s:set_arg9(a) abort |
| 142 | let a:['b'] = 1 |
| 143 | endfunction |
| 144 | |
| 145 | func s:set_arg10(a) abort |
| 146 | let b = a: |
| 147 | call extend(b, {'a': 1}) |
| 148 | endfunction |
| 149 | |
| 150 | func s:set_arg11(a) abort |
| 151 | let b = a: |
| 152 | call extend(b, {'b': 1}) |
| 153 | endfunction |
| 154 | |
| 155 | func s:set_arg12(a) abort |
| 156 | let b = a: |
| 157 | let b['b'] = 1 |
| 158 | endfunction |
| 159 | |
| 160 | func Test_let_arg_fail() |
| 161 | call assert_fails('call s:set_arg1(1)', 'E46:') |
| 162 | call assert_fails('call s:set_arg2(1)', 'E461:') |
| 163 | call assert_fails('call s:set_arg3(1)', 'E46:') |
| 164 | call assert_fails('call s:set_arg4(1)', 'E46:') |
| 165 | call assert_fails('call s:set_arg5(1)', 'E46:') |
| 166 | call s:set_arg6([0]) |
| 167 | call assert_fails('call s:set_arg7(1)', 'E742:') |
| 168 | call assert_fails('call s:set_arg8(1)', 'E742:') |
| 169 | call assert_fails('call s:set_arg9(1)', 'E461:') |
| 170 | call assert_fails('call s:set_arg10(1)', 'E742:') |
| 171 | call assert_fails('call s:set_arg11(1)', 'E742:') |
| 172 | call assert_fails('call s:set_arg12(1)', 'E461:') |
| 173 | endfunction |
| 174 | |
| 175 | func s:set_varg1(...) abort |
| 176 | let a:000 = [] |
| 177 | endfunction |
| 178 | |
| 179 | func s:set_varg2(...) abort |
| 180 | let a:000[0] = 1 |
| 181 | endfunction |
| 182 | |
| 183 | func s:set_varg3(...) abort |
| 184 | let a:000 += [1] |
| 185 | endfunction |
| 186 | |
| 187 | func s:set_varg4(...) abort |
| 188 | call add(a:000, 1) |
| 189 | endfunction |
| 190 | |
| 191 | func s:set_varg5(...) abort |
| 192 | let a:000[0][0] = 1 |
| 193 | endfunction |
| 194 | |
| 195 | func s:set_varg6(...) abort |
| 196 | let b = a:000 |
| 197 | let b[0] = 1 |
| 198 | endfunction |
| 199 | |
| 200 | func s:set_varg7(...) abort |
| 201 | let b = a:000 |
| 202 | let b += [1] |
| 203 | endfunction |
| 204 | |
| 205 | func s:set_varg8(...) abort |
| 206 | let b = a:000 |
| 207 | call add(b, 1) |
| 208 | endfunction |
| 209 | |
| 210 | func s:set_varg9(...) abort |
| 211 | let b = a:000 |
| 212 | let b[0][0] = 1 |
| 213 | endfunction |
| 214 | |
| 215 | func Test_let_varg_fail() |
| 216 | call assert_fails('call s:set_varg1(1)', 'E46:') |
| 217 | call assert_fails('call s:set_varg2(1)', 'E742:') |
| 218 | call assert_fails('call s:set_varg3(1)', 'E46:') |
| 219 | call assert_fails('call s:set_varg4(1)', 'E742:') |
| 220 | call s:set_varg5([0]) |
| 221 | call assert_fails('call s:set_varg6(1)', 'E742:') |
Bram Moolenaar | 05c00c0 | 2019-02-11 22:00:11 +0100 | [diff] [blame] | 222 | call assert_fails('call s:set_varg7(1)', 'E742:') |
Bram Moolenaar | 31b8160 | 2019-02-10 22:14:27 +0100 | [diff] [blame] | 223 | call assert_fails('call s:set_varg8(1)', 'E742:') |
| 224 | call s:set_varg9([0]) |
| 225 | endfunction |
Bram Moolenaar | f0908e6 | 2019-03-30 20:11:50 +0100 | [diff] [blame] | 226 | |
| 227 | func Test_let_utf8_environment() |
| 228 | let $a = 'ĀĒĪŌŪあいうえお' |
| 229 | call assert_equal('ĀĒĪŌŪあいうえお', $a) |
| 230 | endfunc |
Bram Moolenaar | f5842c5 | 2019-05-19 18:41:26 +0200 | [diff] [blame] | 231 | |
Bram Moolenaar | fcf8a87 | 2019-11-06 15:22:00 +0100 | [diff] [blame] | 232 | func Test_let_no_type_checking() |
| 233 | let v = 1 |
| 234 | let v = [1,2,3] |
| 235 | let v = {'a': 1, 'b': 2} |
| 236 | let v = 3.4 |
| 237 | let v = 'hello' |
| 238 | endfunc |
| 239 | |
| 240 | func Test_let_termcap() |
| 241 | " Terminal code |
| 242 | let old_t_te = &t_te |
| 243 | let &t_te = "\<Esc>[yes;" |
| 244 | call assert_match('t_te.*^[[yes;', execute("set termcap")) |
| 245 | let &t_te = old_t_te |
| 246 | |
| 247 | if exists("+t_k1") |
| 248 | " Key code |
| 249 | let old_t_k1 = &t_k1 |
| 250 | let &t_k1 = "that" |
| 251 | call assert_match('t_k1.*that', execute("set termcap")) |
| 252 | let &t_k1 = old_t_k1 |
| 253 | endif |
| 254 | |
Bram Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 255 | call assert_fails('let x = &t_xx', 'E113:') |
Bram Moolenaar | fcf8a87 | 2019-11-06 15:22:00 +0100 | [diff] [blame] | 256 | let &t_xx = "yes" |
| 257 | call assert_equal("yes", &t_xx) |
| 258 | let &t_xx = "" |
Bram Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 259 | call assert_fails('let x = &t_xx', 'E113:') |
Bram Moolenaar | fcf8a87 | 2019-11-06 15:22:00 +0100 | [diff] [blame] | 260 | endfunc |
| 261 | |
| 262 | func Test_let_option_error() |
| 263 | let _w = &tw |
| 264 | let &tw = 80 |
Bram Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 265 | call assert_fails('let &tw .= 1', 'E734:') |
Bram Moolenaar | fcf8a87 | 2019-11-06 15:22:00 +0100 | [diff] [blame] | 266 | call assert_equal(80, &tw) |
| 267 | let &tw = _w |
| 268 | |
| 269 | let _w = &fillchars |
| 270 | let &fillchars = "vert:|" |
Bram Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 271 | call assert_fails('let &fillchars += "diff:-"', 'E734:') |
Bram Moolenaar | fcf8a87 | 2019-11-06 15:22:00 +0100 | [diff] [blame] | 272 | call assert_equal("vert:|", &fillchars) |
| 273 | let &fillchars = _w |
| 274 | endfunc |
| 275 | |
Bram Moolenaar | 8dfcce3 | 2020-03-18 19:32:26 +0100 | [diff] [blame] | 276 | " Errors with the :let statement |
Bram Moolenaar | fcf8a87 | 2019-11-06 15:22:00 +0100 | [diff] [blame] | 277 | func Test_let_errors() |
| 278 | let s = 'abcd' |
| 279 | call assert_fails('let s[1] = 5', 'E689:') |
| 280 | |
| 281 | let l = [1, 2, 3] |
| 282 | call assert_fails('let l[:] = 5', 'E709:') |
Bram Moolenaar | 8dfcce3 | 2020-03-18 19:32:26 +0100 | [diff] [blame] | 283 | |
Bram Moolenaar | b521766 | 2021-08-14 14:27:30 +0200 | [diff] [blame] | 284 | call assert_fails('let x:lnum=5', ['E121:', 'E121:']) |
Bram Moolenaar | 8dfcce3 | 2020-03-18 19:32:26 +0100 | [diff] [blame] | 285 | call assert_fails('let v:=5', 'E461:') |
| 286 | call assert_fails('let [a]', 'E474:') |
| 287 | call assert_fails('let [a, b] = [', 'E697:') |
| 288 | call assert_fails('let [a, b] = [10, 20', 'E696:') |
| 289 | call assert_fails('let [a, b] = 10', 'E714:') |
| 290 | call assert_fails('let [a, , b] = [10, 20]', 'E475:') |
| 291 | call assert_fails('let [a, b&] = [10, 20]', 'E475:') |
| 292 | call assert_fails('let $ = 10', 'E475:') |
| 293 | call assert_fails('let $FOO[1] = "abc"', 'E18:') |
| 294 | call assert_fails('let &buftype[1] = "nofile"', 'E18:') |
| 295 | let s = "var" |
| 296 | let var = 1 |
Bram Moolenaar | ea04a6e | 2020-04-23 13:38:02 +0200 | [diff] [blame] | 297 | call assert_fails('let var += [1,2]', 'E734:') |
Bram Moolenaar | 3a3b10e | 2021-06-26 15:00:59 +0200 | [diff] [blame] | 298 | call assert_fails('let {s}.1 = 2', 'E1203:') |
Bram Moolenaar | 8b63313 | 2020-03-20 18:20:51 +0100 | [diff] [blame] | 299 | call assert_fails('let a[1] = 5', 'E121:') |
| 300 | let l = [[1,2]] |
| 301 | call assert_fails('let l[:][0] = [5]', 'E708:') |
| 302 | let d = {'k' : 4} |
Bram Moolenaar | 63be3d4 | 2020-07-23 13:11:37 +0200 | [diff] [blame] | 303 | call assert_fails('let d.# = 5', 'E488:') |
Bram Moolenaar | 58fb7c3 | 2021-04-05 20:59:41 +0200 | [diff] [blame] | 304 | call assert_fails('let d.m += 5', 'E716:') |
Bram Moolenaar | 9d8d0b5 | 2020-04-24 22:47:31 +0200 | [diff] [blame] | 305 | call assert_fails('let m = d[{]', 'E15:') |
Bram Moolenaar | 8b63313 | 2020-03-20 18:20:51 +0100 | [diff] [blame] | 306 | let l = [1, 2] |
| 307 | call assert_fails('let l[2] = 0', 'E684:') |
| 308 | call assert_fails('let l[0:1] = [1, 2, 3]', 'E710:') |
| 309 | call assert_fails('let l[-2:-3] = [3, 4]', 'E684:') |
| 310 | call assert_fails('let l[0:4] = [5, 6]', 'E711:') |
Bram Moolenaar | 9d8d0b5 | 2020-04-24 22:47:31 +0200 | [diff] [blame] | 311 | call assert_fails('let l -= 2', 'E734:') |
| 312 | call assert_fails('let l += 2', 'E734:') |
Bram Moolenaar | ea04a6e | 2020-04-23 13:38:02 +0200 | [diff] [blame] | 313 | call assert_fails('let g:["a;b"] = 10', 'E461:') |
| 314 | call assert_fails('let g:.min = function("max")', 'E704:') |
Bram Moolenaar | 9d8d0b5 | 2020-04-24 22:47:31 +0200 | [diff] [blame] | 315 | if has('channel') |
| 316 | let ch = test_null_channel() |
| 317 | call assert_fails('let ch += 1', 'E734:') |
| 318 | endif |
Bram Moolenaar | fae55a9 | 2021-06-17 22:08:30 +0200 | [diff] [blame] | 319 | call assert_fails('let name = "a" .. "b",', 'E488: Trailing characters: ,') |
Bram Moolenaar | 8dfcce3 | 2020-03-18 19:32:26 +0100 | [diff] [blame] | 320 | |
| 321 | " This test works only when the language is English |
| 322 | if v:lang == "C" || v:lang =~ '^[Ee]n' |
| 323 | call assert_fails('let [a ; b;] = [10, 20]', |
| 324 | \ 'Double ; in list of variables') |
| 325 | endif |
Bram Moolenaar | fcf8a87 | 2019-11-06 15:22:00 +0100 | [diff] [blame] | 326 | endfunc |
| 327 | |
Bram Moolenaar | 8471e57 | 2019-05-19 21:37:18 +0200 | [diff] [blame] | 328 | func Test_let_heredoc_fails() |
| 329 | call assert_fails('let v =<< marker', 'E991:') |
Bram Moolenaar | 8dfcce3 | 2020-03-18 19:32:26 +0100 | [diff] [blame] | 330 | try |
| 331 | exe "let v =<< TEXT | abc | TEXT" |
| 332 | call assert_report('No exception thrown') |
| 333 | catch /E488:/ |
| 334 | catch |
| 335 | call assert_report("Caught exception: " .. v:exception) |
| 336 | endtry |
Bram Moolenaar | 8471e57 | 2019-05-19 21:37:18 +0200 | [diff] [blame] | 337 | |
| 338 | let text =<< trim END |
| 339 | func WrongSyntax() |
| 340 | let v =<< that there |
| 341 | endfunc |
| 342 | END |
| 343 | call writefile(text, 'XheredocFail') |
Bram Moolenaar | b5b9480 | 2020-12-13 17:50:20 +0100 | [diff] [blame] | 344 | call assert_fails('source XheredocFail', 'E1145:') |
Bram Moolenaar | 8471e57 | 2019-05-19 21:37:18 +0200 | [diff] [blame] | 345 | call delete('XheredocFail') |
| 346 | |
Bram Moolenaar | 2458200 | 2019-07-21 14:14:26 +0200 | [diff] [blame] | 347 | let text =<< trim CodeEnd |
Bram Moolenaar | 8471e57 | 2019-05-19 21:37:18 +0200 | [diff] [blame] | 348 | func MissingEnd() |
| 349 | let v =<< END |
| 350 | endfunc |
Bram Moolenaar | 2458200 | 2019-07-21 14:14:26 +0200 | [diff] [blame] | 351 | CodeEnd |
Bram Moolenaar | 8471e57 | 2019-05-19 21:37:18 +0200 | [diff] [blame] | 352 | call writefile(text, 'XheredocWrong') |
Bram Moolenaar | b5b9480 | 2020-12-13 17:50:20 +0100 | [diff] [blame] | 353 | call assert_fails('source XheredocWrong', 'E1145:') |
Bram Moolenaar | 8471e57 | 2019-05-19 21:37:18 +0200 | [diff] [blame] | 354 | call delete('XheredocWrong') |
Bram Moolenaar | 2458200 | 2019-07-21 14:14:26 +0200 | [diff] [blame] | 355 | |
| 356 | let text =<< trim TEXTend |
| 357 | let v =<< " comment |
| 358 | TEXTend |
| 359 | call writefile(text, 'XheredocNoMarker') |
| 360 | call assert_fails('source XheredocNoMarker', 'E172:') |
| 361 | call delete('XheredocNoMarker') |
| 362 | |
| 363 | let text =<< trim TEXTend |
| 364 | let v =<< text |
| 365 | TEXTend |
| 366 | call writefile(text, 'XheredocBadMarker') |
| 367 | call assert_fails('source XheredocBadMarker', 'E221:') |
| 368 | call delete('XheredocBadMarker') |
Bram Moolenaar | 8dfcce3 | 2020-03-18 19:32:26 +0100 | [diff] [blame] | 369 | |
| 370 | call writefile(['let v =<< TEXT', 'abc'], 'XheredocMissingMarker') |
| 371 | call assert_fails('source XheredocMissingMarker', 'E990:') |
| 372 | call delete('XheredocMissingMarker') |
Bram Moolenaar | 8471e57 | 2019-05-19 21:37:18 +0200 | [diff] [blame] | 373 | endfunc |
| 374 | |
Bram Moolenaar | ecaa75b | 2019-07-21 23:04:21 +0200 | [diff] [blame] | 375 | func Test_let_heredoc_trim_no_indent_marker() |
| 376 | let text =<< trim END |
| 377 | Text |
| 378 | with |
| 379 | indent |
| 380 | END |
| 381 | call assert_equal(['Text', 'with', 'indent'], text) |
| 382 | endfunc |
| 383 | |
Yegappan Lakshmanan | efbfa86 | 2022-04-17 12:47:40 +0100 | [diff] [blame] | 384 | " Test for the setting a variable using the heredoc syntax. |
| 385 | " Keep near the end, this messes up highlighting. |
Bram Moolenaar | f5842c5 | 2019-05-19 18:41:26 +0200 | [diff] [blame] | 386 | func Test_let_heredoc() |
| 387 | let var1 =<< END |
| 388 | Some sample text |
| 389 | Text with indent |
| 390 | !@#$%^&*()-+_={}|[]\~`:";'<>?,./ |
| 391 | END |
| 392 | |
| 393 | call assert_equal(["Some sample text", "\tText with indent", " !@#$%^&*()-+_={}|[]\\~`:\";'<>?,./"], var1) |
| 394 | |
Bram Moolenaar | 2458200 | 2019-07-21 14:14:26 +0200 | [diff] [blame] | 395 | let var2 =<< XXX |
Bram Moolenaar | f5842c5 | 2019-05-19 18:41:26 +0200 | [diff] [blame] | 396 | Editor |
Bram Moolenaar | 2458200 | 2019-07-21 14:14:26 +0200 | [diff] [blame] | 397 | XXX |
Bram Moolenaar | f5842c5 | 2019-05-19 18:41:26 +0200 | [diff] [blame] | 398 | call assert_equal(['Editor'], var2) |
| 399 | |
| 400 | let var3 =<<END |
| 401 | END |
| 402 | call assert_equal([], var3) |
| 403 | |
| 404 | let var3 =<<END |
| 405 | vim |
| 406 | |
| 407 | end |
| 408 | END |
| 409 | END |
| 410 | END |
| 411 | call assert_equal(['vim', '', 'end', ' END', 'END '], var3) |
| 412 | |
| 413 | let var1 =<< trim END |
| 414 | Line1 |
| 415 | Line2 |
| 416 | Line3 |
| 417 | END |
| 418 | END |
| 419 | call assert_equal(['Line1', ' Line2', "\tLine3", ' END'], var1) |
| 420 | |
Bram Moolenaar | e7eb927 | 2019-06-24 00:58:07 +0200 | [diff] [blame] | 421 | let var1 =<< trim !!! |
| 422 | Line1 |
| 423 | line2 |
| 424 | Line3 |
| 425 | !!! |
| 426 | !!! |
| 427 | call assert_equal(['Line1', ' line2', "\tLine3", '!!!',], var1) |
| 428 | |
Bram Moolenaar | 2458200 | 2019-07-21 14:14:26 +0200 | [diff] [blame] | 429 | let var1 =<< trim XX |
Bram Moolenaar | f5842c5 | 2019-05-19 18:41:26 +0200 | [diff] [blame] | 430 | Line1 |
Bram Moolenaar | 2458200 | 2019-07-21 14:14:26 +0200 | [diff] [blame] | 431 | XX |
Bram Moolenaar | e7eb927 | 2019-06-24 00:58:07 +0200 | [diff] [blame] | 432 | call assert_equal(['Line1'], var1) |
Bram Moolenaar | f5842c5 | 2019-05-19 18:41:26 +0200 | [diff] [blame] | 433 | |
Bram Moolenaar | 8471e57 | 2019-05-19 21:37:18 +0200 | [diff] [blame] | 434 | " ignore "endfunc" |
| 435 | let var1 =<< END |
| 436 | something |
Bram Moolenaar | f5842c5 | 2019-05-19 18:41:26 +0200 | [diff] [blame] | 437 | endfunc |
Bram Moolenaar | 8471e57 | 2019-05-19 21:37:18 +0200 | [diff] [blame] | 438 | END |
| 439 | call assert_equal(['something', 'endfunc'], var1) |
Bram Moolenaar | f5842c5 | 2019-05-19 18:41:26 +0200 | [diff] [blame] | 440 | |
Bram Moolenaar | 8471e57 | 2019-05-19 21:37:18 +0200 | [diff] [blame] | 441 | " ignore "endfunc" with trim |
| 442 | let var1 =<< trim END |
| 443 | something |
| 444 | endfunc |
| 445 | END |
| 446 | call assert_equal(['something', 'endfunc'], var1) |
Bram Moolenaar | f5842c5 | 2019-05-19 18:41:26 +0200 | [diff] [blame] | 447 | |
Bram Moolenaar | e96a249 | 2019-06-25 04:12:16 +0200 | [diff] [blame] | 448 | " not concatenate lines |
| 449 | let var1 =<< END |
| 450 | some |
| 451 | \thing |
| 452 | \ else |
| 453 | END |
| 454 | call assert_equal(['some', ' \thing', ' \ else'], var1) |
| 455 | |
Bram Moolenaar | 8471e57 | 2019-05-19 21:37:18 +0200 | [diff] [blame] | 456 | " ignore "python << xx" |
| 457 | let var1 =<<END |
| 458 | something |
| 459 | python << xx |
| 460 | END |
| 461 | call assert_equal(['something', 'python << xx'], var1) |
| 462 | |
| 463 | " ignore "python << xx" with trim |
| 464 | let var1 =<< trim END |
| 465 | something |
| 466 | python << xx |
| 467 | END |
| 468 | call assert_equal(['something', 'python << xx'], var1) |
| 469 | |
| 470 | " ignore "append" |
Bram Moolenaar | 2458200 | 2019-07-21 14:14:26 +0200 | [diff] [blame] | 471 | let var1 =<< E |
Bram Moolenaar | 8471e57 | 2019-05-19 21:37:18 +0200 | [diff] [blame] | 472 | something |
| 473 | app |
Bram Moolenaar | 2458200 | 2019-07-21 14:14:26 +0200 | [diff] [blame] | 474 | E |
Bram Moolenaar | 8471e57 | 2019-05-19 21:37:18 +0200 | [diff] [blame] | 475 | call assert_equal(['something', 'app'], var1) |
| 476 | |
| 477 | " ignore "append" with trim |
Bram Moolenaar | 2458200 | 2019-07-21 14:14:26 +0200 | [diff] [blame] | 478 | let var1 =<< trim END |
Bram Moolenaar | 8471e57 | 2019-05-19 21:37:18 +0200 | [diff] [blame] | 479 | something |
| 480 | app |
Bram Moolenaar | 2458200 | 2019-07-21 14:14:26 +0200 | [diff] [blame] | 481 | END |
Bram Moolenaar | 8471e57 | 2019-05-19 21:37:18 +0200 | [diff] [blame] | 482 | call assert_equal(['something', 'app'], var1) |
Bram Moolenaar | b1ba9ab | 2019-10-16 23:34:42 +0200 | [diff] [blame] | 483 | |
| 484 | let check = [] |
| 485 | if 0 |
| 486 | let check =<< trim END |
| 487 | from heredoc |
| 488 | END |
| 489 | endif |
| 490 | call assert_equal([], check) |
Bram Moolenaar | 1e673b9 | 2019-11-06 15:02:50 +0100 | [diff] [blame] | 491 | |
| 492 | " unpack assignment |
| 493 | let [a, b, c] =<< END |
| 494 | x |
| 495 | \y |
| 496 | z |
| 497 | END |
| 498 | call assert_equal([' x', ' \y', ' z'], [a, b, c]) |
Bram Moolenaar | f5842c5 | 2019-05-19 18:41:26 +0200 | [diff] [blame] | 499 | endfunc |
Bram Moolenaar | 8dfcce3 | 2020-03-18 19:32:26 +0100 | [diff] [blame] | 500 | |
Yegappan Lakshmanan | efbfa86 | 2022-04-17 12:47:40 +0100 | [diff] [blame] | 501 | " Test for evaluating Vim expressions in a heredoc using `=expr` |
| 502 | " Keep near the end, this messes up highlighting. |
| 503 | func Test_let_heredoc_eval() |
| 504 | let str = '' |
| 505 | let code =<< trim eval END |
| 506 | let a = `=5 + 10` |
| 507 | let b = `=min([10, 6])` + `=max([4, 6])` |
| 508 | `=str` |
| 509 | let c = "abc`=str`d" |
| 510 | END |
| 511 | call assert_equal(['let a = 15', 'let b = 6 + 6', '', 'let c = "abcd"'], code) |
Bram Moolenaar | 05c7f5d | 2022-04-28 16:51:41 +0100 | [diff] [blame] | 512 | |
Yegappan Lakshmanan | efbfa86 | 2022-04-17 12:47:40 +0100 | [diff] [blame] | 513 | let $TESTVAR = "Hello" |
| 514 | let code =<< eval trim END |
| 515 | let s = "`=$TESTVAR`" |
| 516 | END |
| 517 | call assert_equal(['let s = "Hello"'], code) |
Bram Moolenaar | 05c7f5d | 2022-04-28 16:51:41 +0100 | [diff] [blame] | 518 | |
Yegappan Lakshmanan | efbfa86 | 2022-04-17 12:47:40 +0100 | [diff] [blame] | 519 | let code =<< eval END |
| 520 | let s = "`=$TESTVAR`" |
| 521 | END |
| 522 | call assert_equal([' let s = "Hello"'], code) |
Bram Moolenaar | 05c7f5d | 2022-04-28 16:51:41 +0100 | [diff] [blame] | 523 | |
Yegappan Lakshmanan | efbfa86 | 2022-04-17 12:47:40 +0100 | [diff] [blame] | 524 | let a = 10 |
| 525 | let data =<< eval END |
| 526 | `=a` |
| 527 | END |
| 528 | call assert_equal(['10'], data) |
Bram Moolenaar | 05c7f5d | 2022-04-28 16:51:41 +0100 | [diff] [blame] | 529 | |
Yegappan Lakshmanan | efbfa86 | 2022-04-17 12:47:40 +0100 | [diff] [blame] | 530 | let x = 'X' |
| 531 | let code =<< eval trim END |
| 532 | let a = `abc` |
| 533 | let b = `=x` |
| 534 | let c = ` |
| 535 | END |
| 536 | call assert_equal(['let a = `abc`', 'let b = X', 'let c = `'], code) |
Bram Moolenaar | 05c7f5d | 2022-04-28 16:51:41 +0100 | [diff] [blame] | 537 | |
Yegappan Lakshmanan | efbfa86 | 2022-04-17 12:47:40 +0100 | [diff] [blame] | 538 | let code = 'xxx' |
| 539 | let code =<< eval trim END |
| 540 | let n = `=5 + |
| 541 | 6` |
| 542 | END |
| 543 | call assert_equal('xxx', code) |
Bram Moolenaar | 05c7f5d | 2022-04-28 16:51:41 +0100 | [diff] [blame] | 544 | |
Yegappan Lakshmanan | efbfa86 | 2022-04-17 12:47:40 +0100 | [diff] [blame] | 545 | let code =<< eval trim END |
| 546 | let n = `=min([1, 2]` + `=max([3, 4])` |
| 547 | END |
| 548 | call assert_equal('xxx', code) |
| 549 | |
| 550 | let lines =<< trim LINES |
| 551 | let text =<< eval trim END |
| 552 | let b = `= |
| 553 | END |
| 554 | LINES |
| 555 | call v9.CheckScriptFailure(lines, 'E1083:') |
| 556 | |
| 557 | let lines =<< trim LINES |
| 558 | let text =<< eval trim END |
| 559 | let b = `=abc |
| 560 | END |
| 561 | LINES |
| 562 | call v9.CheckScriptFailure(lines, 'E1083:') |
| 563 | |
| 564 | let lines =<< trim LINES |
| 565 | let text =<< eval trim END |
| 566 | let b = `=` |
| 567 | END |
| 568 | LINES |
| 569 | call v9.CheckScriptFailure(lines, 'E15:') |
| 570 | |
Bram Moolenaar | 05c7f5d | 2022-04-28 16:51:41 +0100 | [diff] [blame] | 571 | " skipped heredoc |
| 572 | if 0 |
| 573 | let msg =<< trim eval END |
| 574 | n is: `=n` |
| 575 | END |
| 576 | endif |
| 577 | |
Yegappan Lakshmanan | efbfa86 | 2022-04-17 12:47:40 +0100 | [diff] [blame] | 578 | " Test for sourcing a script containing a heredoc with invalid expression. |
| 579 | " Variable assignment should fail, if expression evaluation fails |
| 580 | new |
| 581 | let g:Xvar = 'test' |
| 582 | let g:b = 10 |
| 583 | let lines =<< trim END |
| 584 | let Xvar =<< eval CODE |
| 585 | let a = 1 |
| 586 | let b = `=5+` |
| 587 | let c = 2 |
| 588 | CODE |
| 589 | let g:Count += 1 |
| 590 | END |
| 591 | call setline(1, lines) |
| 592 | let g:Count = 0 |
| 593 | call assert_fails('source', 'E15:') |
| 594 | call assert_equal(1, g:Count) |
| 595 | call setline(3, 'let b = `=abc`') |
| 596 | call assert_fails('source', 'E121:') |
| 597 | call assert_equal(2, g:Count) |
| 598 | call setline(3, 'let b = `=abc` + `=min([9, 4])` + 2') |
| 599 | call assert_fails('source', 'E121:') |
| 600 | call assert_equal(3, g:Count) |
| 601 | call assert_equal('test', g:Xvar) |
| 602 | call assert_equal(10, g:b) |
| 603 | bw! |
| 604 | endfunc |
| 605 | |
Bram Moolenaar | 8dfcce3 | 2020-03-18 19:32:26 +0100 | [diff] [blame] | 606 | " vim: shiftwidth=2 sts=2 expandtab |