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