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