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) |
| 27 | endfunc |
Bram Moolenaar | 31b8160 | 2019-02-10 22:14:27 +0100 | [diff] [blame] | 28 | |
| 29 | func s:set_arg1(a) abort |
| 30 | let a:a = 1 |
| 31 | endfunction |
| 32 | |
| 33 | func s:set_arg2(a) abort |
| 34 | let a:b = 1 |
| 35 | endfunction |
| 36 | |
| 37 | func s:set_arg3(a) abort |
| 38 | let b = a: |
| 39 | let b['a'] = 1 |
| 40 | endfunction |
| 41 | |
| 42 | func s:set_arg4(a) abort |
| 43 | let b = a: |
| 44 | let b['a'] = 1 |
| 45 | endfunction |
| 46 | |
| 47 | func s:set_arg5(a) abort |
| 48 | let b = a: |
| 49 | let b['a'][0] = 1 |
| 50 | endfunction |
| 51 | |
| 52 | func s:set_arg6(a) abort |
| 53 | let a:a[0] = 1 |
| 54 | endfunction |
| 55 | |
| 56 | func s:set_arg7(a) abort |
| 57 | call extend(a:, {'a': 1}) |
| 58 | endfunction |
| 59 | |
| 60 | func s:set_arg8(a) abort |
| 61 | call extend(a:, {'b': 1}) |
| 62 | endfunction |
| 63 | |
| 64 | func s:set_arg9(a) abort |
| 65 | let a:['b'] = 1 |
| 66 | endfunction |
| 67 | |
| 68 | func s:set_arg10(a) abort |
| 69 | let b = a: |
| 70 | call extend(b, {'a': 1}) |
| 71 | endfunction |
| 72 | |
| 73 | func s:set_arg11(a) abort |
| 74 | let b = a: |
| 75 | call extend(b, {'b': 1}) |
| 76 | endfunction |
| 77 | |
| 78 | func s:set_arg12(a) abort |
| 79 | let b = a: |
| 80 | let b['b'] = 1 |
| 81 | endfunction |
| 82 | |
| 83 | func Test_let_arg_fail() |
| 84 | call assert_fails('call s:set_arg1(1)', 'E46:') |
| 85 | call assert_fails('call s:set_arg2(1)', 'E461:') |
| 86 | call assert_fails('call s:set_arg3(1)', 'E46:') |
| 87 | call assert_fails('call s:set_arg4(1)', 'E46:') |
| 88 | call assert_fails('call s:set_arg5(1)', 'E46:') |
| 89 | call s:set_arg6([0]) |
| 90 | call assert_fails('call s:set_arg7(1)', 'E742:') |
| 91 | call assert_fails('call s:set_arg8(1)', 'E742:') |
| 92 | call assert_fails('call s:set_arg9(1)', 'E461:') |
| 93 | call assert_fails('call s:set_arg10(1)', 'E742:') |
| 94 | call assert_fails('call s:set_arg11(1)', 'E742:') |
| 95 | call assert_fails('call s:set_arg12(1)', 'E461:') |
| 96 | endfunction |
| 97 | |
| 98 | func s:set_varg1(...) abort |
| 99 | let a:000 = [] |
| 100 | endfunction |
| 101 | |
| 102 | func s:set_varg2(...) abort |
| 103 | let a:000[0] = 1 |
| 104 | endfunction |
| 105 | |
| 106 | func s:set_varg3(...) abort |
| 107 | let a:000 += [1] |
| 108 | endfunction |
| 109 | |
| 110 | func s:set_varg4(...) abort |
| 111 | call add(a:000, 1) |
| 112 | endfunction |
| 113 | |
| 114 | func s:set_varg5(...) abort |
| 115 | let a:000[0][0] = 1 |
| 116 | endfunction |
| 117 | |
| 118 | func s:set_varg6(...) abort |
| 119 | let b = a:000 |
| 120 | let b[0] = 1 |
| 121 | endfunction |
| 122 | |
| 123 | func s:set_varg7(...) abort |
| 124 | let b = a:000 |
| 125 | let b += [1] |
| 126 | endfunction |
| 127 | |
| 128 | func s:set_varg8(...) abort |
| 129 | let b = a:000 |
| 130 | call add(b, 1) |
| 131 | endfunction |
| 132 | |
| 133 | func s:set_varg9(...) abort |
| 134 | let b = a:000 |
| 135 | let b[0][0] = 1 |
| 136 | endfunction |
| 137 | |
| 138 | func Test_let_varg_fail() |
| 139 | call assert_fails('call s:set_varg1(1)', 'E46:') |
| 140 | call assert_fails('call s:set_varg2(1)', 'E742:') |
| 141 | call assert_fails('call s:set_varg3(1)', 'E46:') |
| 142 | call assert_fails('call s:set_varg4(1)', 'E742:') |
| 143 | call s:set_varg5([0]) |
| 144 | call assert_fails('call s:set_varg6(1)', 'E742:') |
Bram Moolenaar | 05c00c0 | 2019-02-11 22:00:11 +0100 | [diff] [blame] | 145 | call assert_fails('call s:set_varg7(1)', 'E742:') |
Bram Moolenaar | 31b8160 | 2019-02-10 22:14:27 +0100 | [diff] [blame] | 146 | call assert_fails('call s:set_varg8(1)', 'E742:') |
| 147 | call s:set_varg9([0]) |
| 148 | endfunction |
Bram Moolenaar | f0908e6 | 2019-03-30 20:11:50 +0100 | [diff] [blame] | 149 | |
| 150 | func Test_let_utf8_environment() |
| 151 | let $a = 'ĀĒĪŌŪあいうえお' |
| 152 | call assert_equal('ĀĒĪŌŪあいうえお', $a) |
| 153 | endfunc |
Bram Moolenaar | f5842c5 | 2019-05-19 18:41:26 +0200 | [diff] [blame] | 154 | |
Bram Moolenaar | 8471e57 | 2019-05-19 21:37:18 +0200 | [diff] [blame] | 155 | func Test_let_heredoc_fails() |
| 156 | call assert_fails('let v =<< marker', 'E991:') |
| 157 | |
| 158 | let text =<< trim END |
| 159 | func WrongSyntax() |
| 160 | let v =<< that there |
| 161 | endfunc |
| 162 | END |
| 163 | call writefile(text, 'XheredocFail') |
| 164 | call assert_fails('source XheredocFail', 'E126:') |
| 165 | call delete('XheredocFail') |
| 166 | |
Bram Moolenaar | 2458200 | 2019-07-21 14:14:26 +0200 | [diff] [blame] | 167 | let text =<< trim CodeEnd |
Bram Moolenaar | 8471e57 | 2019-05-19 21:37:18 +0200 | [diff] [blame] | 168 | func MissingEnd() |
| 169 | let v =<< END |
| 170 | endfunc |
Bram Moolenaar | 2458200 | 2019-07-21 14:14:26 +0200 | [diff] [blame] | 171 | CodeEnd |
Bram Moolenaar | 8471e57 | 2019-05-19 21:37:18 +0200 | [diff] [blame] | 172 | call writefile(text, 'XheredocWrong') |
| 173 | call assert_fails('source XheredocWrong', 'E126:') |
| 174 | call delete('XheredocWrong') |
Bram Moolenaar | 2458200 | 2019-07-21 14:14:26 +0200 | [diff] [blame] | 175 | |
| 176 | let text =<< trim TEXTend |
| 177 | let v =<< " comment |
| 178 | TEXTend |
| 179 | call writefile(text, 'XheredocNoMarker') |
| 180 | call assert_fails('source XheredocNoMarker', 'E172:') |
| 181 | call delete('XheredocNoMarker') |
| 182 | |
| 183 | let text =<< trim TEXTend |
| 184 | let v =<< text |
| 185 | TEXTend |
| 186 | call writefile(text, 'XheredocBadMarker') |
| 187 | call assert_fails('source XheredocBadMarker', 'E221:') |
| 188 | call delete('XheredocBadMarker') |
Bram Moolenaar | 8471e57 | 2019-05-19 21:37:18 +0200 | [diff] [blame] | 189 | endfunc |
| 190 | |
Bram Moolenaar | ecaa75b | 2019-07-21 23:04:21 +0200 | [diff] [blame] | 191 | func Test_let_heredoc_trim_no_indent_marker() |
| 192 | let text =<< trim END |
| 193 | Text |
| 194 | with |
| 195 | indent |
| 196 | END |
| 197 | call assert_equal(['Text', 'with', 'indent'], text) |
| 198 | endfunc |
| 199 | |
Bram Moolenaar | f5842c5 | 2019-05-19 18:41:26 +0200 | [diff] [blame] | 200 | " Test for the setting a variable using the heredoc syntax |
| 201 | func Test_let_heredoc() |
| 202 | let var1 =<< END |
| 203 | Some sample text |
| 204 | Text with indent |
| 205 | !@#$%^&*()-+_={}|[]\~`:";'<>?,./ |
| 206 | END |
| 207 | |
| 208 | call assert_equal(["Some sample text", "\tText with indent", " !@#$%^&*()-+_={}|[]\\~`:\";'<>?,./"], var1) |
| 209 | |
Bram Moolenaar | 2458200 | 2019-07-21 14:14:26 +0200 | [diff] [blame] | 210 | let var2 =<< XXX |
Bram Moolenaar | f5842c5 | 2019-05-19 18:41:26 +0200 | [diff] [blame] | 211 | Editor |
Bram Moolenaar | 2458200 | 2019-07-21 14:14:26 +0200 | [diff] [blame] | 212 | XXX |
Bram Moolenaar | f5842c5 | 2019-05-19 18:41:26 +0200 | [diff] [blame] | 213 | call assert_equal(['Editor'], var2) |
| 214 | |
| 215 | let var3 =<<END |
| 216 | END |
| 217 | call assert_equal([], var3) |
| 218 | |
| 219 | let var3 =<<END |
| 220 | vim |
| 221 | |
| 222 | end |
| 223 | END |
| 224 | END |
| 225 | END |
| 226 | call assert_equal(['vim', '', 'end', ' END', 'END '], var3) |
| 227 | |
| 228 | let var1 =<< trim END |
| 229 | Line1 |
| 230 | Line2 |
| 231 | Line3 |
| 232 | END |
| 233 | END |
| 234 | call assert_equal(['Line1', ' Line2', "\tLine3", ' END'], var1) |
| 235 | |
Bram Moolenaar | e7eb927 | 2019-06-24 00:58:07 +0200 | [diff] [blame] | 236 | let var1 =<< trim !!! |
| 237 | Line1 |
| 238 | line2 |
| 239 | Line3 |
| 240 | !!! |
| 241 | !!! |
| 242 | call assert_equal(['Line1', ' line2', "\tLine3", '!!!',], var1) |
| 243 | |
Bram Moolenaar | 2458200 | 2019-07-21 14:14:26 +0200 | [diff] [blame] | 244 | let var1 =<< trim XX |
Bram Moolenaar | f5842c5 | 2019-05-19 18:41:26 +0200 | [diff] [blame] | 245 | Line1 |
Bram Moolenaar | 2458200 | 2019-07-21 14:14:26 +0200 | [diff] [blame] | 246 | XX |
Bram Moolenaar | e7eb927 | 2019-06-24 00:58:07 +0200 | [diff] [blame] | 247 | call assert_equal(['Line1'], var1) |
Bram Moolenaar | f5842c5 | 2019-05-19 18:41:26 +0200 | [diff] [blame] | 248 | |
Bram Moolenaar | 8471e57 | 2019-05-19 21:37:18 +0200 | [diff] [blame] | 249 | " ignore "endfunc" |
| 250 | let var1 =<< END |
| 251 | something |
Bram Moolenaar | f5842c5 | 2019-05-19 18:41:26 +0200 | [diff] [blame] | 252 | endfunc |
Bram Moolenaar | 8471e57 | 2019-05-19 21:37:18 +0200 | [diff] [blame] | 253 | END |
| 254 | call assert_equal(['something', 'endfunc'], var1) |
Bram Moolenaar | f5842c5 | 2019-05-19 18:41:26 +0200 | [diff] [blame] | 255 | |
Bram Moolenaar | 8471e57 | 2019-05-19 21:37:18 +0200 | [diff] [blame] | 256 | " ignore "endfunc" with trim |
| 257 | let var1 =<< trim END |
| 258 | something |
| 259 | endfunc |
| 260 | END |
| 261 | call assert_equal(['something', 'endfunc'], var1) |
Bram Moolenaar | f5842c5 | 2019-05-19 18:41:26 +0200 | [diff] [blame] | 262 | |
Bram Moolenaar | e96a249 | 2019-06-25 04:12:16 +0200 | [diff] [blame] | 263 | " not concatenate lines |
| 264 | let var1 =<< END |
| 265 | some |
| 266 | \thing |
| 267 | \ else |
| 268 | END |
| 269 | call assert_equal(['some', ' \thing', ' \ else'], var1) |
| 270 | |
Bram Moolenaar | 8471e57 | 2019-05-19 21:37:18 +0200 | [diff] [blame] | 271 | " ignore "python << xx" |
| 272 | let var1 =<<END |
| 273 | something |
| 274 | python << xx |
| 275 | END |
| 276 | call assert_equal(['something', 'python << xx'], var1) |
| 277 | |
| 278 | " ignore "python << xx" with trim |
| 279 | let var1 =<< trim END |
| 280 | something |
| 281 | python << xx |
| 282 | END |
| 283 | call assert_equal(['something', 'python << xx'], var1) |
| 284 | |
| 285 | " ignore "append" |
Bram Moolenaar | 2458200 | 2019-07-21 14:14:26 +0200 | [diff] [blame] | 286 | let var1 =<< E |
Bram Moolenaar | 8471e57 | 2019-05-19 21:37:18 +0200 | [diff] [blame] | 287 | something |
| 288 | app |
Bram Moolenaar | 2458200 | 2019-07-21 14:14:26 +0200 | [diff] [blame] | 289 | E |
Bram Moolenaar | 8471e57 | 2019-05-19 21:37:18 +0200 | [diff] [blame] | 290 | call assert_equal(['something', 'app'], var1) |
| 291 | |
| 292 | " ignore "append" with trim |
Bram Moolenaar | 2458200 | 2019-07-21 14:14:26 +0200 | [diff] [blame] | 293 | let var1 =<< trim END |
Bram Moolenaar | 8471e57 | 2019-05-19 21:37:18 +0200 | [diff] [blame] | 294 | something |
| 295 | app |
Bram Moolenaar | 2458200 | 2019-07-21 14:14:26 +0200 | [diff] [blame] | 296 | END |
Bram Moolenaar | 8471e57 | 2019-05-19 21:37:18 +0200 | [diff] [blame] | 297 | call assert_equal(['something', 'app'], var1) |
Bram Moolenaar | b1ba9ab | 2019-10-16 23:34:42 +0200 | [diff] [blame] | 298 | |
| 299 | let check = [] |
| 300 | if 0 |
| 301 | let check =<< trim END |
| 302 | from heredoc |
| 303 | END |
| 304 | endif |
| 305 | call assert_equal([], check) |
Bram Moolenaar | f5842c5 | 2019-05-19 18:41:26 +0200 | [diff] [blame] | 306 | endfunc |