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