Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1 | " Tests for Vim9 script expressions |
| 2 | |
| 3 | source check.vim |
| 4 | |
| 5 | " Check that "line" inside ":def" results in an "error" message. |
| 6 | func CheckDefFailure(line, error) |
| 7 | call writefile(['def! Func()', a:line, 'enddef'], 'Xdef') |
| 8 | call assert_fails('so Xdef', a:error, a:line) |
| 9 | call delete('Xdef') |
| 10 | endfunc |
| 11 | |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 12 | func CheckDefFailureMult(lines, error) |
| 13 | call writefile(['def! Func()'] + a:lines + ['enddef'], 'Xdef') |
| 14 | call assert_fails('so Xdef', a:error, join(a:lines, ' | ')) |
| 15 | call delete('Xdef') |
| 16 | endfunc |
| 17 | |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 18 | " Check that "line" inside ":def" results in an "error" message when executed. |
| 19 | func CheckDefExecFailure(line, error) |
| 20 | call writefile(['def! Func()', a:line, 'enddef'], 'Xdef') |
| 21 | so Xdef |
| 22 | call assert_fails('call Func()', a:error, a:line) |
| 23 | call delete('Xdef') |
| 24 | endfunc |
| 25 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 26 | func CheckDefFailureList(lines, error) |
| 27 | call writefile(['def! Func()'] + a:lines + ['enddef'], 'Xdef') |
| 28 | call assert_fails('so Xdef', a:error, string(a:lines)) |
| 29 | call delete('Xdef') |
| 30 | endfunc |
| 31 | |
| 32 | " test cond ? expr : expr |
| 33 | def Test_expr1() |
| 34 | assert_equal('one', true ? 'one' : 'two') |
| 35 | assert_equal('one', 1 ? 'one' : 'two') |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 36 | if has('float') |
| 37 | assert_equal('one', 0.1 ? 'one' : 'two') |
| 38 | endif |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 39 | assert_equal('one', 'x' ? 'one' : 'two') |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 40 | assert_equal('one', 0z1234 ? 'one' : 'two') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 41 | assert_equal('one', [0] ? 'one' : 'two') |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 42 | assert_equal('one', #{x: 0} ? 'one' : 'two') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 43 | let var = 1 |
| 44 | assert_equal('one', var ? 'one' : 'two') |
| 45 | |
| 46 | assert_equal('two', false ? 'one' : 'two') |
| 47 | assert_equal('two', 0 ? 'one' : 'two') |
Bram Moolenaar | 7f829ca | 2020-01-31 22:12:41 +0100 | [diff] [blame] | 48 | if has('float') |
| 49 | assert_equal('two', 0.0 ? 'one' : 'two') |
| 50 | endif |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 51 | assert_equal('two', '' ? 'one' : 'two') |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 52 | assert_equal('two', 0z ? 'one' : 'two') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 53 | assert_equal('two', [] ? 'one' : 'two') |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 54 | assert_equal('two', {} ? 'one' : 'two') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 55 | var = 0 |
| 56 | assert_equal('two', var ? 'one' : 'two') |
| 57 | enddef |
| 58 | |
| 59 | func Test_expr1_fails() |
| 60 | call CheckDefFailure("let x = 1 ? 'one'", "Missing ':' after '?'") |
Bram Moolenaar | 9be61bb | 2020-03-30 22:51:24 +0200 | [diff] [blame] | 61 | call CheckDefFailure("let x = 1 ? 'one' : xxx", "E1001:") |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 62 | |
| 63 | let msg = "white space required before and after '?'" |
| 64 | call CheckDefFailure("let x = 1? 'one' : 'two'", msg) |
| 65 | call CheckDefFailure("let x = 1 ?'one' : 'two'", msg) |
| 66 | call CheckDefFailure("let x = 1?'one' : 'two'", msg) |
| 67 | |
| 68 | let msg = "white space required before and after ':'" |
| 69 | call CheckDefFailure("let x = 1 ? 'one': 'two'", msg) |
| 70 | call CheckDefFailure("let x = 1 ? 'one' :'two'", msg) |
| 71 | call CheckDefFailure("let x = 1 ? 'one':'two'", msg) |
| 72 | endfunc |
| 73 | |
| 74 | " TODO: define inside test function |
| 75 | def Record(val: any): any |
| 76 | g:vals->add(val) |
| 77 | return val |
| 78 | enddef |
| 79 | |
| 80 | " test || |
| 81 | def Test_expr2() |
| 82 | assert_equal(2, 2 || 0) |
| 83 | assert_equal(7, 0 || 0 || 7) |
| 84 | assert_equal(0, 0 || 0) |
| 85 | assert_equal('', 0 || '') |
| 86 | |
| 87 | g:vals = [] |
| 88 | assert_equal(3, Record(3) || Record(1)) |
| 89 | assert_equal([3], g:vals) |
| 90 | |
| 91 | g:vals = [] |
| 92 | assert_equal(5, Record(0) || Record(5)) |
| 93 | assert_equal([0, 5], g:vals) |
| 94 | |
| 95 | g:vals = [] |
| 96 | assert_equal(4, Record(0) || Record(4) || Record(0)) |
| 97 | assert_equal([0, 4], g:vals) |
| 98 | |
| 99 | g:vals = [] |
| 100 | assert_equal(0, Record([]) || Record('') || Record(0)) |
| 101 | assert_equal([[], '', 0], g:vals) |
| 102 | enddef |
| 103 | |
| 104 | func Test_expr2_fails() |
| 105 | let msg = "white space required before and after '||'" |
| 106 | call CheckDefFailure("let x = 1||2", msg) |
| 107 | call CheckDefFailure("let x = 1 ||2", msg) |
| 108 | call CheckDefFailure("let x = 1|| 2", msg) |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 109 | |
| 110 | call CheckDefFailure("let x = 1 || xxx", 'E1001:') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 111 | endfunc |
| 112 | |
| 113 | " test && |
| 114 | def Test_expr3() |
| 115 | assert_equal(0, 2 && 0) |
| 116 | assert_equal(0, 0 && 0 && 7) |
| 117 | assert_equal(7, 2 && 3 && 7) |
| 118 | assert_equal(0, 0 && 0) |
| 119 | assert_equal(0, 0 && '') |
| 120 | assert_equal('', 8 && '') |
| 121 | |
| 122 | g:vals = [] |
| 123 | assert_equal(1, Record(3) && Record(1)) |
| 124 | assert_equal([3, 1], g:vals) |
| 125 | |
| 126 | g:vals = [] |
| 127 | assert_equal(0, Record(0) && Record(5)) |
| 128 | assert_equal([0], g:vals) |
| 129 | |
| 130 | g:vals = [] |
| 131 | assert_equal(0, Record(0) && Record(4) && Record(0)) |
| 132 | assert_equal([0], g:vals) |
| 133 | |
| 134 | g:vals = [] |
| 135 | assert_equal(0, Record(8) && Record(4) && Record(0)) |
| 136 | assert_equal([8, 4, 0], g:vals) |
| 137 | |
| 138 | g:vals = [] |
| 139 | assert_equal(0, Record([1]) && Record('z') && Record(0)) |
| 140 | assert_equal([[1], 'z', 0], g:vals) |
| 141 | enddef |
| 142 | |
| 143 | func Test_expr3_fails() |
| 144 | let msg = "white space required before and after '&&'" |
| 145 | call CheckDefFailure("let x = 1&&2", msg) |
| 146 | call CheckDefFailure("let x = 1 &&2", msg) |
| 147 | call CheckDefFailure("let x = 1&& 2", msg) |
| 148 | endfunc |
| 149 | |
| 150 | let atrue = v:true |
| 151 | let afalse = v:false |
| 152 | let anone = v:none |
| 153 | let anull = v:null |
| 154 | let anint = 10 |
| 155 | let alsoint = 4 |
| 156 | if has('float') |
| 157 | let afloat = 0.1 |
| 158 | endif |
| 159 | let astring = 'asdf' |
| 160 | let ablob = 0z01ab |
| 161 | let alist = [2, 3, 4] |
| 162 | let adict = #{aaa: 2, bbb: 8} |
| 163 | |
| 164 | " test == comperator |
| 165 | def Test_expr4_equal() |
| 166 | assert_equal(true, true == true) |
| 167 | assert_equal(false, true == false) |
| 168 | assert_equal(true, true == g:atrue) |
| 169 | assert_equal(false, g:atrue == false) |
| 170 | |
| 171 | assert_equal(true, v:none == v:none) |
| 172 | assert_equal(false, v:none == v:null) |
| 173 | assert_equal(true, g:anone == v:none) |
| 174 | assert_equal(false, v:none == g:anull) |
| 175 | |
| 176 | assert_equal(false, 2 == 0) |
| 177 | assert_equal(true, 61 == 61) |
| 178 | assert_equal(true, g:anint == 10) |
| 179 | assert_equal(false, 61 == g:anint) |
| 180 | |
| 181 | if has('float') |
| 182 | assert_equal(true, 0.3 == 0.3) |
| 183 | assert_equal(false, 0.4 == 0.3) |
| 184 | assert_equal(true, 0.1 == g:afloat) |
| 185 | assert_equal(false, g:afloat == 0.3) |
| 186 | |
| 187 | assert_equal(true, 3.0 == 3) |
| 188 | assert_equal(true, 3 == 3.0) |
| 189 | assert_equal(false, 3.1 == 3) |
| 190 | assert_equal(false, 3 == 3.1) |
| 191 | endif |
| 192 | |
| 193 | assert_equal(true, 'abc' == 'abc') |
| 194 | assert_equal(false, 'xyz' == 'abc') |
| 195 | assert_equal(true, g:astring == 'asdf') |
| 196 | assert_equal(false, 'xyz' == g:astring) |
| 197 | |
Bram Moolenaar | 9be61bb | 2020-03-30 22:51:24 +0200 | [diff] [blame] | 198 | assert_equal(false, 'abc' == 'aBc') |
| 199 | assert_equal(false, 'abc' ==# 'aBc') |
| 200 | assert_equal(true, 'abc' ==? 'aBc') |
| 201 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 202 | assert_equal(false, 'abc' == 'ABC') |
| 203 | set ignorecase |
| 204 | assert_equal(false, 'abc' == 'ABC') |
Bram Moolenaar | 9be61bb | 2020-03-30 22:51:24 +0200 | [diff] [blame] | 205 | assert_equal(false, 'abc' ==# 'ABC') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 206 | set noignorecase |
| 207 | |
Bram Moolenaar | 9be61bb | 2020-03-30 22:51:24 +0200 | [diff] [blame] | 208 | call CheckDefFailure("let x = 'a' == xxx", 'E1001:') |
| 209 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 210 | assert_equal(true, 0z3f == 0z3f) |
| 211 | assert_equal(false, 0z3f == 0z4f) |
| 212 | assert_equal(true, g:ablob == 0z01ab) |
| 213 | assert_equal(false, 0z3f == g:ablob) |
| 214 | |
| 215 | assert_equal(true, [1, 2, 3] == [1, 2, 3]) |
| 216 | assert_equal(false, [1, 2, 3] == [2, 3, 1]) |
| 217 | assert_equal(true, [2, 3, 4] == g:alist) |
| 218 | assert_equal(false, g:alist == [2, 3, 1]) |
| 219 | assert_equal(false, [1, 2, 3] == []) |
| 220 | assert_equal(false, [1, 2, 3] == ['1', '2', '3']) |
| 221 | |
| 222 | assert_equal(true, #{one: 1, two: 2} == #{one: 1, two: 2}) |
| 223 | assert_equal(false, #{one: 1, two: 2} == #{one: 2, two: 2}) |
| 224 | assert_equal(false, #{one: 1, two: 2} == #{two: 2}) |
| 225 | assert_equal(false, #{one: 1, two: 2} == #{}) |
| 226 | assert_equal(true, g:adict == #{bbb: 8, aaa: 2}) |
| 227 | assert_equal(false, #{ccc: 9, aaa: 2} == g:adict) |
| 228 | |
| 229 | assert_equal(true, function('Test_expr4_equal') == function('Test_expr4_equal')) |
| 230 | assert_equal(false, function('Test_expr4_equal') == function('Test_expr4_is')) |
| 231 | |
| 232 | assert_equal(true, function('Test_expr4_equal', [123]) == function('Test_expr4_equal', [123])) |
| 233 | assert_equal(false, function('Test_expr4_equal', [123]) == function('Test_expr4_is', [123])) |
| 234 | assert_equal(false, function('Test_expr4_equal', [123]) == function('Test_expr4_equal', [999])) |
| 235 | enddef |
| 236 | |
| 237 | " test != comperator |
| 238 | def Test_expr4_notequal() |
| 239 | assert_equal(false, true != true) |
| 240 | assert_equal(true, true != false) |
| 241 | assert_equal(false, true != g:atrue) |
| 242 | assert_equal(true, g:atrue != false) |
| 243 | |
| 244 | assert_equal(false, v:none != v:none) |
| 245 | assert_equal(true, v:none != v:null) |
| 246 | assert_equal(false, g:anone != v:none) |
| 247 | assert_equal(true, v:none != g:anull) |
| 248 | |
| 249 | assert_equal(true, 2 != 0) |
| 250 | assert_equal(false, 55 != 55) |
| 251 | assert_equal(false, g:anint != 10) |
| 252 | assert_equal(true, 61 != g:anint) |
| 253 | |
| 254 | if has('float') |
| 255 | assert_equal(false, 0.3 != 0.3) |
| 256 | assert_equal(true, 0.4 != 0.3) |
| 257 | assert_equal(false, 0.1 != g:afloat) |
| 258 | assert_equal(true, g:afloat != 0.3) |
| 259 | |
| 260 | assert_equal(false, 3.0 != 3) |
| 261 | assert_equal(false, 3 != 3.0) |
| 262 | assert_equal(true, 3.1 != 3) |
| 263 | assert_equal(true, 3 != 3.1) |
| 264 | endif |
| 265 | |
| 266 | assert_equal(false, 'abc' != 'abc') |
| 267 | assert_equal(true, 'xyz' != 'abc') |
| 268 | assert_equal(false, g:astring != 'asdf') |
| 269 | assert_equal(true, 'xyz' != g:astring) |
| 270 | |
| 271 | assert_equal(true, 'abc' != 'ABC') |
| 272 | set ignorecase |
| 273 | assert_equal(true, 'abc' != 'ABC') |
| 274 | set noignorecase |
| 275 | |
| 276 | assert_equal(false, 0z3f != 0z3f) |
| 277 | assert_equal(true, 0z3f != 0z4f) |
| 278 | assert_equal(false, g:ablob != 0z01ab) |
| 279 | assert_equal(true, 0z3f != g:ablob) |
| 280 | |
| 281 | assert_equal(false, [1, 2, 3] != [1, 2, 3]) |
| 282 | assert_equal(true, [1, 2, 3] != [2, 3, 1]) |
| 283 | assert_equal(false, [2, 3, 4] != g:alist) |
| 284 | assert_equal(true, g:alist != [2, 3, 1]) |
| 285 | assert_equal(true, [1, 2, 3] != []) |
| 286 | assert_equal(true, [1, 2, 3] != ['1', '2', '3']) |
| 287 | |
| 288 | assert_equal(false, #{one: 1, two: 2} != #{one: 1, two: 2}) |
| 289 | assert_equal(true, #{one: 1, two: 2} != #{one: 2, two: 2}) |
| 290 | assert_equal(true, #{one: 1, two: 2} != #{two: 2}) |
| 291 | assert_equal(true, #{one: 1, two: 2} != #{}) |
| 292 | assert_equal(false, g:adict != #{bbb: 8, aaa: 2}) |
| 293 | assert_equal(true, #{ccc: 9, aaa: 2} != g:adict) |
| 294 | |
| 295 | assert_equal(false, function('Test_expr4_equal') != function('Test_expr4_equal')) |
| 296 | assert_equal(true, function('Test_expr4_equal') != function('Test_expr4_is')) |
| 297 | |
| 298 | assert_equal(false, function('Test_expr4_equal', [123]) != function('Test_expr4_equal', [123])) |
| 299 | assert_equal(true, function('Test_expr4_equal', [123]) != function('Test_expr4_is', [123])) |
| 300 | assert_equal(true, function('Test_expr4_equal', [123]) != function('Test_expr4_equal', [999])) |
| 301 | enddef |
| 302 | |
| 303 | " test > comperator |
| 304 | def Test_expr4_greater() |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 305 | assert_true(2 > 0) |
| 306 | assert_true(2 > 1) |
| 307 | assert_false(2 > 2) |
| 308 | assert_false(2 > 3) |
| 309 | if has('float') |
| 310 | assert_true(2.0 > 0.0) |
| 311 | assert_true(2.0 > 1.0) |
| 312 | assert_false(2.0 > 2.0) |
| 313 | assert_false(2.0 > 3.0) |
| 314 | endif |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 315 | enddef |
| 316 | |
| 317 | " test >= comperator |
| 318 | def Test_expr4_greaterequal() |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 319 | assert_true(2 >= 0) |
| 320 | assert_true(2 >= 2) |
| 321 | assert_false(2 >= 3) |
| 322 | if has('float') |
| 323 | assert_true(2.0 >= 0.0) |
| 324 | assert_true(2.0 >= 2.0) |
| 325 | assert_false(2.0 >= 3.0) |
| 326 | endif |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 327 | enddef |
| 328 | |
| 329 | " test < comperator |
| 330 | def Test_expr4_smaller() |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 331 | assert_false(2 < 0) |
| 332 | assert_false(2 < 2) |
| 333 | assert_true(2 < 3) |
| 334 | if has('float') |
| 335 | assert_false(2.0 < 0.0) |
| 336 | assert_false(2.0 < 2.0) |
| 337 | assert_true(2.0 < 3.0) |
| 338 | endif |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 339 | enddef |
| 340 | |
| 341 | " test <= comperator |
| 342 | def Test_expr4_smallerequal() |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 343 | assert_false(2 <= 0) |
| 344 | assert_false(2 <= 1) |
| 345 | assert_true(2 <= 2) |
| 346 | assert_true(2 <= 3) |
| 347 | if has('float') |
| 348 | assert_false(2.0 <= 0.0) |
| 349 | assert_false(2.0 <= 1.0) |
| 350 | assert_true(2.0 <= 2.0) |
| 351 | assert_true(2.0 <= 3.0) |
| 352 | endif |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 353 | enddef |
| 354 | |
| 355 | " test =~ comperator |
| 356 | def Test_expr4_match() |
| 357 | assert_equal(false, '2' =~ '0') |
| 358 | assert_equal(true, '2' =~ '[0-9]') |
| 359 | enddef |
| 360 | |
| 361 | " test !~ comperator |
| 362 | def Test_expr4_nomatch() |
| 363 | assert_equal(true, '2' !~ '0') |
| 364 | assert_equal(false, '2' !~ '[0-9]') |
| 365 | enddef |
| 366 | |
| 367 | " test is comperator |
| 368 | def Test_expr4_is() |
| 369 | let mylist = [2] |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 370 | assert_false(mylist is [2]) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 371 | let other = mylist |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 372 | assert_true(mylist is other) |
| 373 | |
| 374 | let myblob = 0z1234 |
| 375 | assert_false(myblob is 0z1234) |
| 376 | let otherblob = myblob |
| 377 | assert_true(myblob is otherblob) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 378 | enddef |
| 379 | |
| 380 | " test isnot comperator |
| 381 | def Test_expr4_isnot() |
| 382 | let mylist = [2] |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 383 | assert_true('2' isnot '0') |
| 384 | assert_true(mylist isnot [2]) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 385 | let other = mylist |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 386 | assert_false(mylist isnot other) |
| 387 | |
| 388 | let myblob = 0z1234 |
| 389 | assert_true(myblob isnot 0z1234) |
| 390 | let otherblob = myblob |
| 391 | assert_false(myblob isnot otherblob) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 392 | enddef |
| 393 | |
| 394 | def RetVoid() |
| 395 | let x = 1 |
| 396 | enddef |
| 397 | |
| 398 | func Test_expr4_fails() |
| 399 | let msg = "white space required before and after '>'" |
| 400 | call CheckDefFailure("let x = 1>2", msg) |
| 401 | call CheckDefFailure("let x = 1 >2", msg) |
| 402 | call CheckDefFailure("let x = 1> 2", msg) |
| 403 | |
| 404 | let msg = "white space required before and after '=='" |
| 405 | call CheckDefFailure("let x = 1==2", msg) |
| 406 | call CheckDefFailure("let x = 1 ==2", msg) |
| 407 | call CheckDefFailure("let x = 1== 2", msg) |
| 408 | |
| 409 | let msg = "white space required before and after 'is'" |
| 410 | call CheckDefFailure("let x = '1'is'2'", msg) |
| 411 | call CheckDefFailure("let x = '1' is'2'", msg) |
| 412 | call CheckDefFailure("let x = '1'is '2'", msg) |
| 413 | |
| 414 | let msg = "white space required before and after 'isnot'" |
| 415 | call CheckDefFailure("let x = '1'isnot'2'", msg) |
| 416 | call CheckDefFailure("let x = '1' isnot'2'", msg) |
| 417 | call CheckDefFailure("let x = '1'isnot '2'", msg) |
| 418 | |
| 419 | call CheckDefFailure("let x = 1 is# 2", 'E15:') |
| 420 | call CheckDefFailure("let x = 1 is? 2", 'E15:') |
| 421 | call CheckDefFailure("let x = 1 isnot# 2", 'E15:') |
| 422 | call CheckDefFailure("let x = 1 isnot? 2", 'E15:') |
| 423 | |
| 424 | call CheckDefFailure("let x = 1 == '2'", 'Cannot compare number with string') |
| 425 | call CheckDefFailure("let x = '1' == 2", 'Cannot compare string with number') |
| 426 | call CheckDefFailure("let x = 1 == RetVoid()", 'Cannot use void value') |
| 427 | call CheckDefFailure("let x = RetVoid() == 1", 'Cannot compare void with number') |
| 428 | |
| 429 | call CheckDefFailure("let x = true > false", 'Cannot compare bool with bool') |
| 430 | call CheckDefFailure("let x = true >= false", 'Cannot compare bool with bool') |
| 431 | call CheckDefFailure("let x = true < false", 'Cannot compare bool with bool') |
| 432 | call CheckDefFailure("let x = true <= false", 'Cannot compare bool with bool') |
| 433 | call CheckDefFailure("let x = true =~ false", 'Cannot compare bool with bool') |
| 434 | call CheckDefFailure("let x = true !~ false", 'Cannot compare bool with bool') |
| 435 | call CheckDefFailure("let x = true is false", 'Cannot use "is" with bool') |
| 436 | call CheckDefFailure("let x = true isnot false", 'Cannot use "isnot" with bool') |
| 437 | |
| 438 | call CheckDefFailure("let x = v:none is v:null", 'Cannot use "is" with special') |
| 439 | call CheckDefFailure("let x = v:none isnot v:null", 'Cannot use "isnot" with special') |
| 440 | call CheckDefFailure("let x = 123 is 123", 'Cannot use "is" with number') |
| 441 | call CheckDefFailure("let x = 123 isnot 123", 'Cannot use "isnot" with number') |
| 442 | if has('float') |
| 443 | call CheckDefFailure("let x = 1.3 is 1.3", 'Cannot use "is" with float') |
| 444 | call CheckDefFailure("let x = 1.3 isnot 1.3", 'Cannot use "isnot" with float') |
| 445 | endif |
| 446 | |
| 447 | call CheckDefFailure("let x = 0za1 > 0z34", 'Cannot compare blob with blob') |
| 448 | call CheckDefFailure("let x = 0za1 >= 0z34", 'Cannot compare blob with blob') |
| 449 | call CheckDefFailure("let x = 0za1 < 0z34", 'Cannot compare blob with blob') |
| 450 | call CheckDefFailure("let x = 0za1 <= 0z34", 'Cannot compare blob with blob') |
| 451 | call CheckDefFailure("let x = 0za1 =~ 0z34", 'Cannot compare blob with blob') |
| 452 | call CheckDefFailure("let x = 0za1 !~ 0z34", 'Cannot compare blob with blob') |
| 453 | |
| 454 | call CheckDefFailure("let x = [13] > [88]", 'Cannot compare list with list') |
| 455 | call CheckDefFailure("let x = [13] >= [88]", 'Cannot compare list with list') |
| 456 | call CheckDefFailure("let x = [13] < [88]", 'Cannot compare list with list') |
| 457 | call CheckDefFailure("let x = [13] <= [88]", 'Cannot compare list with list') |
| 458 | call CheckDefFailure("let x = [13] =~ [88]", 'Cannot compare list with list') |
| 459 | call CheckDefFailure("let x = [13] !~ [88]", 'Cannot compare list with list') |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 460 | |
| 461 | call CheckDefFailureMult(['let j: job', 'let chan: channel', 'let r = j == chan'], 'Cannot compare job with channel') |
| 462 | call CheckDefFailureMult(['let j: job', 'let x: list<any>', 'let r = j == x'], 'Cannot compare job with list') |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 463 | call CheckDefFailureMult(['let j: job', 'let Xx: func', 'let r = j == Xx'], 'Cannot compare job with func') |
| 464 | call CheckDefFailureMult(['let j: job', 'let Xx: func', 'let r = j == Xx'], 'Cannot compare job with func') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 465 | endfunc |
| 466 | |
| 467 | " test addition, subtraction, concatenation |
| 468 | def Test_expr5() |
| 469 | assert_equal(66, 60 + 6) |
| 470 | assert_equal(70, 60 + g:anint) |
| 471 | assert_equal(9, g:alsoint + 5) |
| 472 | assert_equal(14, g:alsoint + g:anint) |
| 473 | |
| 474 | assert_equal(54, 60 - 6) |
| 475 | assert_equal(50, 60 - g:anint) |
| 476 | assert_equal(-1, g:alsoint - 5) |
| 477 | assert_equal(-6, g:alsoint - g:anint) |
| 478 | |
| 479 | assert_equal('hello', 'hel' .. 'lo') |
| 480 | assert_equal('hello 123', 'hello ' .. 123) |
| 481 | assert_equal('123 hello', 123 .. ' hello') |
| 482 | assert_equal('123456', 123 .. 456) |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 483 | |
| 484 | assert_equal([1, 2, 3, 4], [1, 2] + [3, 4]) |
| 485 | assert_equal(0z11223344, 0z1122 + 0z3344) |
| 486 | assert_equal(0z112201ab, 0z1122 + g:ablob) |
| 487 | assert_equal(0z01ab3344, g:ablob + 0z3344) |
| 488 | assert_equal(0z01ab01ab, g:ablob + g:ablob) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 489 | enddef |
| 490 | |
| 491 | def Test_expr5_float() |
Bram Moolenaar | 7f829ca | 2020-01-31 22:12:41 +0100 | [diff] [blame] | 492 | if !has('float') |
| 493 | MissingFeature 'float' |
| 494 | else |
| 495 | assert_equal(66.0, 60.0 + 6.0) |
| 496 | assert_equal(66.0, 60.0 + 6) |
| 497 | assert_equal(66.0, 60 + 6.0) |
| 498 | assert_equal(5.1, g:afloat + 5) |
| 499 | assert_equal(8.1, 8 + g:afloat) |
| 500 | assert_equal(10.1, g:anint + g:afloat) |
| 501 | assert_equal(10.1, g:afloat + g:anint) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 502 | |
Bram Moolenaar | 7f829ca | 2020-01-31 22:12:41 +0100 | [diff] [blame] | 503 | assert_equal(54.0, 60.0 - 6.0) |
| 504 | assert_equal(54.0, 60.0 - 6) |
| 505 | assert_equal(54.0, 60 - 6.0) |
| 506 | assert_equal(-4.9, g:afloat - 5) |
| 507 | assert_equal(7.9, 8 - g:afloat) |
| 508 | assert_equal(9.9, g:anint - g:afloat) |
| 509 | assert_equal(-9.9, g:afloat - g:anint) |
| 510 | endif |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 511 | enddef |
| 512 | |
| 513 | func Test_expr5_fails() |
| 514 | let msg = "white space required before and after '+'" |
| 515 | call CheckDefFailure("let x = 1+2", msg) |
| 516 | call CheckDefFailure("let x = 1 +2", msg) |
| 517 | call CheckDefFailure("let x = 1+ 2", msg) |
| 518 | |
| 519 | let msg = "white space required before and after '-'" |
| 520 | call CheckDefFailure("let x = 1-2", msg) |
| 521 | call CheckDefFailure("let x = 1 -2", msg) |
| 522 | call CheckDefFailure("let x = 1- 2", msg) |
| 523 | |
| 524 | let msg = "white space required before and after '..'" |
| 525 | call CheckDefFailure("let x = '1'..'2'", msg) |
| 526 | call CheckDefFailure("let x = '1' ..'2'", msg) |
| 527 | call CheckDefFailure("let x = '1'.. '2'", msg) |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 528 | |
| 529 | call CheckDefFailure("let x = 0z1122 + 33", 'E1035') |
| 530 | call CheckDefFailure("let x = 0z1122 + [3]", 'E1035') |
| 531 | call CheckDefFailure("let x = 0z1122 + 'asd'", 'E1035') |
| 532 | call CheckDefFailure("let x = 33 + 0z1122", 'E1035') |
| 533 | call CheckDefFailure("let x = [3] + 0z1122", 'E1035') |
| 534 | call CheckDefFailure("let x = 'asdf' + 0z1122", 'E1035') |
Bram Moolenaar | 92dba36 | 2020-03-30 21:22:56 +0200 | [diff] [blame] | 535 | call CheckDefFailure("let x = 6 + xxx", 'E1001') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 536 | endfunc |
| 537 | |
| 538 | " test multiply, divide, modulo |
| 539 | def Test_expr6() |
| 540 | assert_equal(36, 6 * 6) |
| 541 | assert_equal(24, 6 * g:alsoint) |
| 542 | assert_equal(24, g:alsoint * 6) |
| 543 | assert_equal(40, g:anint * g:alsoint) |
| 544 | |
| 545 | assert_equal(10, 60 / 6) |
| 546 | assert_equal(6, 60 / g:anint) |
| 547 | assert_equal(1, g:anint / 6) |
| 548 | assert_equal(2, g:anint / g:alsoint) |
| 549 | |
| 550 | assert_equal(5, 11 % 6) |
| 551 | assert_equal(4, g:anint % 6) |
| 552 | assert_equal(3, 13 % g:anint) |
| 553 | assert_equal(2, g:anint % g:alsoint) |
| 554 | |
| 555 | assert_equal(4, 6 * 4 / 6) |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 556 | |
| 557 | let x = [2] |
| 558 | let y = [3] |
| 559 | assert_equal(5, x[0] + y[0]) |
| 560 | assert_equal(6, x[0] * y[0]) |
| 561 | if has('float') |
| 562 | let xf = [2.0] |
| 563 | let yf = [3.0] |
| 564 | assert_equal(5.0, xf[0] + yf[0]) |
| 565 | assert_equal(6.0, xf[0] * yf[0]) |
| 566 | endif |
Bram Moolenaar | 92dba36 | 2020-03-30 21:22:56 +0200 | [diff] [blame] | 567 | |
| 568 | call CheckDefFailure("let x = 6 * xxx", 'E1001') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 569 | enddef |
| 570 | |
| 571 | def Test_expr6_float() |
Bram Moolenaar | 7f829ca | 2020-01-31 22:12:41 +0100 | [diff] [blame] | 572 | if !has('float') |
| 573 | MissingFeature 'float' |
| 574 | else |
| 575 | assert_equal(36.0, 6.0 * 6) |
| 576 | assert_equal(36.0, 6 * 6.0) |
| 577 | assert_equal(36.0, 6.0 * 6.0) |
| 578 | assert_equal(1.0, g:afloat * g:anint) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 579 | |
Bram Moolenaar | 7f829ca | 2020-01-31 22:12:41 +0100 | [diff] [blame] | 580 | assert_equal(10.0, 60 / 6.0) |
| 581 | assert_equal(10.0, 60.0 / 6) |
| 582 | assert_equal(10.0, 60.0 / 6.0) |
| 583 | assert_equal(0.01, g:afloat / g:anint) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 584 | |
Bram Moolenaar | 7f829ca | 2020-01-31 22:12:41 +0100 | [diff] [blame] | 585 | assert_equal(4.0, 6.0 * 4 / 6) |
| 586 | assert_equal(4.0, 6 * 4.0 / 6) |
| 587 | assert_equal(4.0, 6 * 4 / 6.0) |
| 588 | assert_equal(4.0, 6.0 * 4.0 / 6) |
| 589 | assert_equal(4.0, 6 * 4.0 / 6.0) |
| 590 | assert_equal(4.0, 6.0 * 4 / 6.0) |
| 591 | assert_equal(4.0, 6.0 * 4.0 / 6.0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 592 | |
Bram Moolenaar | 7f829ca | 2020-01-31 22:12:41 +0100 | [diff] [blame] | 593 | assert_equal(4.0, 6.0 * 4.0 / 6.0) |
| 594 | endif |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 595 | enddef |
| 596 | |
| 597 | func Test_expr6_fails() |
| 598 | let msg = "white space required before and after '*'" |
| 599 | call CheckDefFailure("let x = 1*2", msg) |
| 600 | call CheckDefFailure("let x = 1 *2", msg) |
| 601 | call CheckDefFailure("let x = 1* 2", msg) |
| 602 | |
| 603 | let msg = "white space required before and after '/'" |
| 604 | call CheckDefFailure("let x = 1/2", msg) |
| 605 | call CheckDefFailure("let x = 1 /2", msg) |
| 606 | call CheckDefFailure("let x = 1/ 2", msg) |
| 607 | |
| 608 | let msg = "white space required before and after '%'" |
| 609 | call CheckDefFailure("let x = 1%2", msg) |
| 610 | call CheckDefFailure("let x = 1 %2", msg) |
| 611 | call CheckDefFailure("let x = 1% 2", msg) |
| 612 | |
| 613 | call CheckDefFailure("let x = '1' * '2'", 'E1036:') |
| 614 | call CheckDefFailure("let x = '1' / '2'", 'E1036:') |
| 615 | call CheckDefFailure("let x = '1' % '2'", 'E1035:') |
| 616 | |
| 617 | call CheckDefFailure("let x = 0z01 * 0z12", 'E1036:') |
| 618 | call CheckDefFailure("let x = 0z01 / 0z12", 'E1036:') |
| 619 | call CheckDefFailure("let x = 0z01 % 0z12", 'E1035:') |
| 620 | |
| 621 | call CheckDefFailure("let x = [1] * [2]", 'E1036:') |
| 622 | call CheckDefFailure("let x = [1] / [2]", 'E1036:') |
| 623 | call CheckDefFailure("let x = [1] % [2]", 'E1035:') |
| 624 | |
| 625 | call CheckDefFailure("let x = #{one: 1} * #{two: 2}", 'E1036:') |
| 626 | call CheckDefFailure("let x = #{one: 1} / #{two: 2}", 'E1036:') |
| 627 | call CheckDefFailure("let x = #{one: 1} % #{two: 2}", 'E1035:') |
| 628 | |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 629 | call CheckDefFailure("let x = 0xff[1]", 'E714:') |
| 630 | if has('float') |
| 631 | call CheckDefFailure("let x = 0.7[1]", 'E714:') |
| 632 | endif |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 633 | endfunc |
| 634 | |
| 635 | func Test_expr6_float_fails() |
| 636 | CheckFeature float |
| 637 | call CheckDefFailure("let x = 1.0 % 2", 'E1035:') |
| 638 | endfunc |
| 639 | |
| 640 | " define here to use old style parsing |
| 641 | if has('float') |
| 642 | let g:float_zero = 0.0 |
| 643 | let g:float_neg = -9.8 |
| 644 | let g:float_big = 9.9e99 |
| 645 | endif |
| 646 | let g:blob_empty = 0z |
| 647 | let g:blob_one = 0z01 |
| 648 | let g:blob_long = 0z0102.0304 |
| 649 | |
| 650 | let g:string_empty = '' |
| 651 | let g:string_short = 'x' |
| 652 | let g:string_long = 'abcdefghijklm' |
| 653 | let g:string_special = "ab\ncd\ref\ekk" |
| 654 | |
| 655 | let g:special_true = v:true |
| 656 | let g:special_false = v:false |
| 657 | let g:special_null = v:null |
| 658 | let g:special_none = v:none |
| 659 | |
| 660 | let g:list_empty = [] |
| 661 | let g:list_mixed = [1, 'b', v:false] |
| 662 | |
| 663 | let g:dict_empty = {} |
| 664 | let g:dict_one = #{one: 1} |
| 665 | |
| 666 | let $TESTVAR = 'testvar' |
| 667 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 668 | " test low level expression |
| 669 | def Test_expr7_number() |
| 670 | " number constant |
| 671 | assert_equal(0, 0) |
| 672 | assert_equal(654, 0654) |
| 673 | |
| 674 | assert_equal(6, 0x6) |
| 675 | assert_equal(15, 0xf) |
| 676 | assert_equal(255, 0xff) |
| 677 | enddef |
| 678 | |
| 679 | def Test_expr7_float() |
| 680 | " float constant |
Bram Moolenaar | 7f829ca | 2020-01-31 22:12:41 +0100 | [diff] [blame] | 681 | if !has('float') |
| 682 | MissingFeature 'float' |
| 683 | else |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 684 | assert_equal(g:float_zero, .0) |
| 685 | assert_equal(g:float_zero, 0.0) |
| 686 | assert_equal(g:float_neg, -9.8) |
| 687 | assert_equal(g:float_big, 9.9e99) |
| 688 | endif |
| 689 | enddef |
| 690 | |
| 691 | def Test_expr7_blob() |
| 692 | " blob constant |
| 693 | assert_equal(g:blob_empty, 0z) |
| 694 | assert_equal(g:blob_one, 0z01) |
| 695 | assert_equal(g:blob_long, 0z0102.0304) |
Bram Moolenaar | 92dba36 | 2020-03-30 21:22:56 +0200 | [diff] [blame] | 696 | |
| 697 | call CheckDefFailure("let x = 0z123", 'E973:') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 698 | enddef |
| 699 | |
| 700 | def Test_expr7_string() |
| 701 | " string constant |
| 702 | assert_equal(g:string_empty, '') |
| 703 | assert_equal(g:string_empty, "") |
| 704 | assert_equal(g:string_short, 'x') |
| 705 | assert_equal(g:string_short, "x") |
| 706 | assert_equal(g:string_long, 'abcdefghijklm') |
| 707 | assert_equal(g:string_long, "abcdefghijklm") |
| 708 | assert_equal(g:string_special, "ab\ncd\ref\ekk") |
Bram Moolenaar | 92dba36 | 2020-03-30 21:22:56 +0200 | [diff] [blame] | 709 | |
| 710 | call CheckDefFailure('let x = "abc', 'E114:') |
| 711 | call CheckDefFailure("let x = 'abc", 'E115:') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 712 | enddef |
| 713 | |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 714 | def Test_expr7_vimvar() |
| 715 | let old: list<string> = v:oldfiles |
| 716 | let compl: dict<any> = v:completed_item |
| 717 | |
| 718 | call CheckDefFailure("let old: list<number> = v:oldfiles", 'E1013: type mismatch, expected list<number> but got list<string>') |
| 719 | call CheckDefFailure("let old: dict<number> = v:completed_item", 'E1013: type mismatch, expected dict<number> but got dict<any>') |
| 720 | enddef |
| 721 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 722 | def Test_expr7_special() |
| 723 | " special constant |
| 724 | assert_equal(g:special_true, true) |
| 725 | assert_equal(g:special_false, false) |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 726 | assert_equal(g:special_true, v:true) |
| 727 | assert_equal(g:special_false, v:false) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 728 | assert_equal(g:special_null, v:null) |
| 729 | assert_equal(g:special_none, v:none) |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 730 | |
| 731 | call CheckDefFailure('v:true = true', 'E46:') |
| 732 | call CheckDefFailure('v:true = false', 'E46:') |
| 733 | call CheckDefFailure('v:false = true', 'E46:') |
| 734 | call CheckDefFailure('v:null = 11', 'E46:') |
| 735 | call CheckDefFailure('v:none = 22', 'E46:') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 736 | enddef |
| 737 | |
| 738 | def Test_expr7_list() |
| 739 | " list |
| 740 | assert_equal(g:list_empty, []) |
| 741 | assert_equal(g:list_empty, [ ]) |
| 742 | assert_equal(g:list_mixed, [1, 'b', false]) |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 743 | assert_equal('b', g:list_mixed[1]) |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 744 | |
| 745 | call CheckDefExecFailure("let x = g:anint[3]", 'E714:') |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 746 | call CheckDefFailure("let x = g:list_mixed[xxx]", 'E1001:') |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 747 | call CheckDefExecFailure("let x = g:list_mixed['xx']", 'E39:') |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 748 | call CheckDefFailure("let x = g:list_mixed[0", 'E111:') |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 749 | call CheckDefExecFailure("let x = g:list_empty[3]", 'E684:') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 750 | enddef |
| 751 | |
| 752 | def Test_expr7_lambda() |
| 753 | " lambda |
| 754 | let La = { -> 'result'} |
| 755 | assert_equal('result', La()) |
| 756 | assert_equal([1, 3, 5], [1, 2, 3]->map({key, val -> key + val})) |
| 757 | enddef |
| 758 | |
| 759 | def Test_expr7_dict() |
| 760 | " dictionary |
| 761 | assert_equal(g:dict_empty, {}) |
| 762 | assert_equal(g:dict_empty, { }) |
| 763 | assert_equal(g:dict_one, {'one': 1}) |
| 764 | let key = 'one' |
| 765 | let val = 1 |
| 766 | assert_equal(g:dict_one, {key: val}) |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 767 | |
Bram Moolenaar | c58164c | 2020-03-29 18:40:30 +0200 | [diff] [blame] | 768 | call CheckDefFailure("let x = #{8: 8}", 'E1014:') |
| 769 | call CheckDefFailure("let x = #{xxx}", 'E720:') |
Bram Moolenaar | 82de464 | 2020-04-12 18:02:06 +0200 | [diff] [blame] | 770 | call CheckDefFailureMult(["let x = #{xxx: 1", "let y = 2"], 'E722:') |
Bram Moolenaar | c58164c | 2020-03-29 18:40:30 +0200 | [diff] [blame] | 771 | call CheckDefFailure("let x = #{xxx: 1,", 'E723:') |
| 772 | call CheckDefFailure("let x = {'a': xxx}", 'E1001:') |
| 773 | call CheckDefFailure("let x = {xxx: 8}", 'E1001:') |
| 774 | call CheckDefFailure("let x = #{a: 1, a: 2}", 'E721:') |
Bram Moolenaar | 92dba36 | 2020-03-30 21:22:56 +0200 | [diff] [blame] | 775 | call CheckDefFailure("let x = #", 'E1015:') |
Bram Moolenaar | d25ec2c | 2020-03-30 21:05:45 +0200 | [diff] [blame] | 776 | call CheckDefFailure("let x += 1", 'E1020:') |
| 777 | call CheckDefFailure("let x = x + 1", 'E1001:') |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 778 | call CheckDefExecFailure("let x = g:anint.member", 'E715:') |
| 779 | call CheckDefExecFailure("let x = g:dict_empty.member", 'E716:') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 780 | enddef |
| 781 | |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 782 | def Test_expr_member() |
| 783 | assert_equal(1, g:dict_one.one) |
| 784 | |
| 785 | call CheckDefFailure("let x = g:dict_one.#$!", 'E1002:') |
| 786 | enddef |
| 787 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 788 | def Test_expr7_option() |
| 789 | " option |
| 790 | set ts=11 |
| 791 | assert_equal(11, &ts) |
Bram Moolenaar | 401d9ff | 2020-02-19 18:14:44 +0100 | [diff] [blame] | 792 | &ts = 9 |
| 793 | assert_equal(9, &ts) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 794 | set ts=8 |
| 795 | set grepprg=some\ text |
| 796 | assert_equal('some text', &grepprg) |
Bram Moolenaar | 97a2af3 | 2020-01-28 22:52:48 +0100 | [diff] [blame] | 797 | &grepprg = test_null_string() |
| 798 | assert_equal('', &grepprg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 799 | set grepprg& |
| 800 | enddef |
| 801 | |
| 802 | def Test_expr7_environment() |
| 803 | " environment variable |
| 804 | assert_equal('testvar', $TESTVAR) |
| 805 | assert_equal('', $ASDF_ASD_XXX) |
Bram Moolenaar | c58164c | 2020-03-29 18:40:30 +0200 | [diff] [blame] | 806 | |
| 807 | call CheckDefFailure("let x = $$$", 'E1002:') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 808 | enddef |
| 809 | |
| 810 | def Test_expr7_register() |
Bram Moolenaar | 401d9ff | 2020-02-19 18:14:44 +0100 | [diff] [blame] | 811 | @a = 'register a' |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 812 | assert_equal('register a', @a) |
| 813 | enddef |
| 814 | |
| 815 | def Test_expr7_parens() |
| 816 | " (expr) |
| 817 | assert_equal(4, (6 * 4) / 6) |
| 818 | assert_equal(0, 6 * ( 4 / 6 )) |
| 819 | |
| 820 | assert_equal(6, +6) |
| 821 | assert_equal(-6, -6) |
| 822 | assert_equal(6, --6) |
| 823 | assert_equal(6, -+-6) |
| 824 | assert_equal(-6, ---6) |
| 825 | enddef |
| 826 | |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 827 | def Test_expr7_negate() |
| 828 | assert_equal(-99, -99) |
| 829 | assert_equal(99, --99) |
| 830 | let nr = 88 |
| 831 | assert_equal(-88, -nr) |
| 832 | assert_equal(88, --nr) |
| 833 | enddef |
| 834 | |
| 835 | def Echo(arg): string |
| 836 | return arg |
| 837 | enddef |
| 838 | |
| 839 | def s:EchoArg(arg): string |
| 840 | return arg |
| 841 | enddef |
| 842 | |
| 843 | def Test_expr7_call() |
| 844 | assert_equal('yes', 'yes'->Echo()) |
| 845 | assert_equal('yes', 'yes'->s:EchoArg()) |
| 846 | |
| 847 | call CheckDefFailure("let x = 'yes'->Echo", 'E107:') |
| 848 | enddef |
| 849 | |
| 850 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 851 | def Test_expr7_not() |
| 852 | assert_equal(true, !'') |
| 853 | assert_equal(true, ![]) |
| 854 | assert_equal(false, !'asdf') |
| 855 | assert_equal(false, ![2]) |
| 856 | assert_equal(true, !!'asdf') |
| 857 | assert_equal(true, !![2]) |
Bram Moolenaar | 8ed0458 | 2020-02-22 19:07:28 +0100 | [diff] [blame] | 858 | |
| 859 | assert_equal(true, !test_null_partial()) |
| 860 | assert_equal(false, !{-> 'yes'}) |
| 861 | |
| 862 | assert_equal(true, !test_null_dict()) |
| 863 | assert_equal(true, !{}) |
| 864 | assert_equal(false, !{'yes': 'no'}) |
| 865 | |
Bram Moolenaar | b4d2cb1 | 2020-02-22 20:33:08 +0100 | [diff] [blame] | 866 | if has('channel') |
| 867 | assert_equal(true, !test_null_job()) |
| 868 | assert_equal(true, !test_null_channel()) |
| 869 | endif |
Bram Moolenaar | 8ed0458 | 2020-02-22 19:07:28 +0100 | [diff] [blame] | 870 | |
| 871 | assert_equal(true, !test_null_blob()) |
| 872 | assert_equal(true, !0z) |
| 873 | assert_equal(false, !0z01) |
| 874 | |
| 875 | assert_equal(true, !test_void()) |
| 876 | assert_equal(true, !test_unknown()) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 877 | enddef |
| 878 | |
| 879 | func Test_expr7_fails() |
| 880 | call CheckDefFailure("let x = (12", "E110:") |
| 881 | |
| 882 | call CheckDefFailure("let x = -'xx'", "E1030:") |
| 883 | call CheckDefFailure("let x = +'xx'", "E1030:") |
Bram Moolenaar | c58164c | 2020-03-29 18:40:30 +0200 | [diff] [blame] | 884 | call CheckDefFailure("let x = -0z12", "E974:") |
| 885 | call CheckDefExecFailure("let x = -[8]", "E39:") |
| 886 | call CheckDefExecFailure("let x = -{'a': 1}", "E39:") |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 887 | |
| 888 | call CheckDefFailure("let x = @", "E1002:") |
| 889 | call CheckDefFailure("let x = @<", "E354:") |
Bram Moolenaar | 58ceca5 | 2020-01-28 22:46:22 +0100 | [diff] [blame] | 890 | |
Bram Moolenaar | ee619e5 | 2020-03-28 21:38:06 +0100 | [diff] [blame] | 891 | call CheckDefFailure("let x = [1, 2", "E697:") |
| 892 | call CheckDefFailure("let x = [notfound]", "E1001:") |
| 893 | |
| 894 | call CheckDefFailure("let x = { -> 123) }", "E451:") |
| 895 | call CheckDefFailure("let x = 123->{x -> x + 5) }", "E451:") |
| 896 | |
Bram Moolenaar | 401d9ff | 2020-02-19 18:14:44 +0100 | [diff] [blame] | 897 | call CheckDefFailure("let x = ¬exist", 'E113:') |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 898 | call CheckDefFailure("&grepprg = [343]", 'E1013:') |
Bram Moolenaar | fd1823e | 2020-02-19 20:23:11 +0100 | [diff] [blame] | 899 | |
| 900 | call CheckDefExecFailure("echo s:doesnt_exist", 'E121:') |
| 901 | call CheckDefExecFailure("echo g:doesnt_exist", 'E121:') |
Bram Moolenaar | 09f28f4 | 2020-02-20 23:08:34 +0100 | [diff] [blame] | 902 | |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 903 | call CheckDefFailure("echo a:somevar", 'E1075:') |
| 904 | call CheckDefFailure("echo l:somevar", 'E1075:') |
| 905 | call CheckDefFailure("echo x:somevar", 'E1075:') |
| 906 | |
| 907 | " TODO |
| 908 | call CheckDefFailure("echo b:somevar", 'not supported yet') |
| 909 | call CheckDefFailure("echo w:somevar", 'not supported yet') |
| 910 | call CheckDefFailure("echo t:somevar", 'not supported yet') |
| 911 | |
Bram Moolenaar | 09f28f4 | 2020-02-20 23:08:34 +0100 | [diff] [blame] | 912 | call CheckDefExecFailure("let x = +g:astring", 'E1030:') |
| 913 | call CheckDefExecFailure("let x = +g:ablob", 'E974:') |
| 914 | call CheckDefExecFailure("let x = +g:alist", 'E745:') |
| 915 | call CheckDefExecFailure("let x = +g:adict", 'E728:') |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 916 | |
| 917 | call CheckDefFailureMult(["let x = ''", "let y = x.memb"], 'E715:') |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 918 | |
| 919 | call CheckDefExecFailure("[1, 2->len()", 'E492:') |
| 920 | call CheckDefExecFailure("#{a: 1->len()", 'E488:') |
| 921 | call CheckDefExecFailure("{'a': 1->len()", 'E492:') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 922 | endfunc |
| 923 | |
| 924 | let g:Funcrefs = [function('add')] |
| 925 | |
| 926 | func CallMe(arg) |
| 927 | return a:arg |
| 928 | endfunc |
| 929 | |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 930 | func CallMe2(one, two) |
| 931 | return a:one .. a:two |
| 932 | endfunc |
| 933 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 934 | def Test_expr7_trailing() |
| 935 | " user function call |
| 936 | assert_equal(123, CallMe(123)) |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 937 | assert_equal(123, CallMe( 123)) |
| 938 | assert_equal(123, CallMe(123 )) |
| 939 | assert_equal('yesno', CallMe2('yes', 'no')) |
| 940 | assert_equal('yesno', CallMe2( 'yes', 'no' )) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 941 | assert_equal('nothing', CallMe('nothing')) |
| 942 | |
| 943 | " partial call |
| 944 | let Part = function('CallMe') |
| 945 | assert_equal('yes', Part('yes')) |
| 946 | |
| 947 | " funcref call, using list index |
| 948 | let l = [] |
| 949 | g:Funcrefs[0](l, 2) |
| 950 | assert_equal([2], l) |
| 951 | |
| 952 | " method call |
| 953 | l = [2, 5, 6] |
| 954 | l->map({k, v -> k + v}) |
| 955 | assert_equal([2, 6, 8], l) |
| 956 | |
| 957 | " lambda method call |
| 958 | l = [2, 5] |
| 959 | l->{l -> add(l, 8)}() |
| 960 | assert_equal([2, 5, 8], l) |
| 961 | |
| 962 | " dict member |
| 963 | let d = #{key: 123} |
| 964 | assert_equal(123, d.key) |
| 965 | enddef |
| 966 | |
| 967 | func Test_expr7_trailing_fails() |
| 968 | call CheckDefFailureList(['let l = [2]', 'l->{l -> add(l, 8)}'], 'E107') |
Bram Moolenaar | ee619e5 | 2020-03-28 21:38:06 +0100 | [diff] [blame] | 969 | call CheckDefFailureList(['let l = [2]', 'l->{l -> add(l, 8)} ()'], 'E274') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 970 | endfunc |
| 971 | |
| 972 | func Test_expr_fails() |
| 973 | call CheckDefFailure("let x = '1'is2", 'E488:') |
| 974 | call CheckDefFailure("let x = '1'isnot2", 'E488:') |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 975 | |
| 976 | call CheckDefExecFailure("CallMe ('yes')", 'E492:') |
| 977 | call CheckDefFailure("CallMe2('yes','no')", 'E1069:') |
| 978 | call CheckDefFailure("CallMe2('yes' , 'no')", 'E1068:') |
Bram Moolenaar | 0c2ca58 | 2020-02-25 22:58:29 +0100 | [diff] [blame] | 979 | |
| 980 | call CheckDefFailure("v:nosuch += 3", 'E1001:') |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 981 | call CheckDefFailure("let v:statusmsg = ''", 'E1064:') |
Bram Moolenaar | 0c2ca58 | 2020-02-25 22:58:29 +0100 | [diff] [blame] | 982 | call CheckDefFailure("let asdf = v:nosuch", 'E1001:') |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 983 | |
| 984 | call CheckDefFailure("echo len('asdf'", 'E110:') |
| 985 | call CheckDefFailure("echo Func0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789()", 'E1011:') |
| 986 | call CheckDefFailure("echo doesnotexist()", 'E117:') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 987 | endfunc |