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 | |
| 573 | def Test_expr5_float() |
Bram Moolenaar | 7f829ca | 2020-01-31 22:12:41 +0100 | [diff] [blame] | 574 | if !has('float') |
| 575 | MissingFeature 'float' |
| 576 | else |
| 577 | assert_equal(66.0, 60.0 + 6.0) |
| 578 | assert_equal(66.0, 60.0 + 6) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 579 | assert_equal(66.0, 60 + |
| 580 | 6.0) |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 581 | assert_equal(5.1, g:afloat |
| 582 | + 5) |
Bram Moolenaar | 7f829ca | 2020-01-31 22:12:41 +0100 | [diff] [blame] | 583 | assert_equal(8.1, 8 + g:afloat) |
| 584 | assert_equal(10.1, g:anint + g:afloat) |
| 585 | assert_equal(10.1, g:afloat + g:anint) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 586 | |
Bram Moolenaar | 7f829ca | 2020-01-31 22:12:41 +0100 | [diff] [blame] | 587 | assert_equal(54.0, 60.0 - 6.0) |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 588 | assert_equal(54.0, 60.0 |
| 589 | - 6) |
Bram Moolenaar | 7f829ca | 2020-01-31 22:12:41 +0100 | [diff] [blame] | 590 | assert_equal(54.0, 60 - 6.0) |
| 591 | assert_equal(-4.9, g:afloat - 5) |
| 592 | assert_equal(7.9, 8 - g:afloat) |
| 593 | assert_equal(9.9, g:anint - g:afloat) |
| 594 | assert_equal(-9.9, g:afloat - g:anint) |
| 595 | endif |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 596 | enddef |
| 597 | |
| 598 | func Test_expr5_fails() |
| 599 | let msg = "white space required before and after '+'" |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 600 | call CheckDefFailure(["let x = 1+2"], msg) |
| 601 | call CheckDefFailure(["let x = 1 +2"], msg) |
| 602 | call CheckDefFailure(["let x = 1+ 2"], msg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 603 | |
| 604 | let msg = "white space required before and after '-'" |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 605 | call CheckDefFailure(["let x = 1-2"], msg) |
| 606 | call CheckDefFailure(["let x = 1 -2"], msg) |
| 607 | call CheckDefFailure(["let x = 1- 2"], msg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 608 | |
| 609 | let msg = "white space required before and after '..'" |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 610 | call CheckDefFailure(["let x = '1'..'2'"], msg) |
| 611 | call CheckDefFailure(["let x = '1' ..'2'"], msg) |
| 612 | call CheckDefFailure(["let x = '1'.. '2'"], msg) |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 613 | |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 614 | call CheckDefFailure(["let x = 0z1122 + 33"], 'E1051') |
| 615 | call CheckDefFailure(["let x = 0z1122 + [3]"], 'E1051') |
| 616 | call CheckDefFailure(["let x = 0z1122 + 'asd'"], 'E1051') |
| 617 | call CheckDefFailure(["let x = 33 + 0z1122"], 'E1051') |
| 618 | call CheckDefFailure(["let x = [3] + 0z1122"], 'E1051') |
| 619 | call CheckDefFailure(["let x = 'asdf' + 0z1122"], 'E1051') |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 620 | call CheckDefFailure(["let x = 6 + xxx"], 'E1001') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 621 | endfunc |
| 622 | |
| 623 | " test multiply, divide, modulo |
| 624 | def Test_expr6() |
| 625 | assert_equal(36, 6 * 6) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 626 | assert_equal(24, 6 * |
| 627 | g:alsoint) |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 628 | assert_equal(24, g:alsoint |
| 629 | * 6) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 630 | assert_equal(40, g:anint * g:alsoint) |
| 631 | |
| 632 | assert_equal(10, 60 / 6) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 633 | assert_equal(6, 60 / |
| 634 | g:anint) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 635 | assert_equal(1, g:anint / 6) |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 636 | assert_equal(2, g:anint |
| 637 | / g:alsoint) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 638 | |
| 639 | assert_equal(5, 11 % 6) |
| 640 | assert_equal(4, g:anint % 6) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 641 | assert_equal(3, 13 % |
| 642 | g:anint) |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 643 | assert_equal(2, g:anint |
| 644 | % g:alsoint) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 645 | |
| 646 | assert_equal(4, 6 * 4 / 6) |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 647 | |
| 648 | let x = [2] |
| 649 | let y = [3] |
| 650 | assert_equal(5, x[0] + y[0]) |
| 651 | assert_equal(6, x[0] * y[0]) |
| 652 | if has('float') |
| 653 | let xf = [2.0] |
| 654 | let yf = [3.0] |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 655 | assert_equal(5.0, xf[0] |
| 656 | + yf[0]) |
| 657 | assert_equal(6.0, xf[0] |
| 658 | * yf[0]) |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 659 | endif |
Bram Moolenaar | 92dba36 | 2020-03-30 21:22:56 +0200 | [diff] [blame] | 660 | |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 661 | call CheckDefFailure(["let x = 6 * xxx"], 'E1001') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 662 | enddef |
| 663 | |
| 664 | def Test_expr6_float() |
Bram Moolenaar | 7f829ca | 2020-01-31 22:12:41 +0100 | [diff] [blame] | 665 | if !has('float') |
| 666 | MissingFeature 'float' |
| 667 | else |
| 668 | assert_equal(36.0, 6.0 * 6) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 669 | assert_equal(36.0, 6 * |
| 670 | 6.0) |
Bram Moolenaar | 7f829ca | 2020-01-31 22:12:41 +0100 | [diff] [blame] | 671 | assert_equal(36.0, 6.0 * 6.0) |
| 672 | assert_equal(1.0, g:afloat * g:anint) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 673 | |
Bram Moolenaar | 7f829ca | 2020-01-31 22:12:41 +0100 | [diff] [blame] | 674 | assert_equal(10.0, 60 / 6.0) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 675 | assert_equal(10.0, 60.0 / |
| 676 | 6) |
Bram Moolenaar | 7f829ca | 2020-01-31 22:12:41 +0100 | [diff] [blame] | 677 | assert_equal(10.0, 60.0 / 6.0) |
| 678 | assert_equal(0.01, g:afloat / g:anint) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 679 | |
Bram Moolenaar | 7f829ca | 2020-01-31 22:12:41 +0100 | [diff] [blame] | 680 | assert_equal(4.0, 6.0 * 4 / 6) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 681 | assert_equal(4.0, 6 * |
| 682 | 4.0 / |
| 683 | 6) |
Bram Moolenaar | 7f829ca | 2020-01-31 22:12:41 +0100 | [diff] [blame] | 684 | assert_equal(4.0, 6 * 4 / 6.0) |
| 685 | assert_equal(4.0, 6.0 * 4.0 / 6) |
| 686 | assert_equal(4.0, 6 * 4.0 / 6.0) |
| 687 | assert_equal(4.0, 6.0 * 4 / 6.0) |
| 688 | assert_equal(4.0, 6.0 * 4.0 / 6.0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 689 | |
Bram Moolenaar | 7f829ca | 2020-01-31 22:12:41 +0100 | [diff] [blame] | 690 | assert_equal(4.0, 6.0 * 4.0 / 6.0) |
| 691 | endif |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 692 | enddef |
| 693 | |
| 694 | func Test_expr6_fails() |
| 695 | let msg = "white space required before and after '*'" |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 696 | call CheckDefFailure(["let x = 1*2"], msg) |
| 697 | call CheckDefFailure(["let x = 1 *2"], msg) |
| 698 | call CheckDefFailure(["let x = 1* 2"], msg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 699 | |
| 700 | let msg = "white space required before and after '/'" |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 701 | call CheckDefFailure(["let x = 1/2"], msg) |
| 702 | call CheckDefFailure(["let x = 1 /2"], msg) |
| 703 | call CheckDefFailure(["let x = 1/ 2"], msg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 704 | |
| 705 | let msg = "white space required before and after '%'" |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 706 | call CheckDefFailure(["let x = 1%2"], msg) |
| 707 | call CheckDefFailure(["let x = 1 %2"], msg) |
| 708 | call CheckDefFailure(["let x = 1% 2"], msg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 709 | |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 710 | call CheckDefFailure(["let x = '1' * '2'"], 'E1036:') |
| 711 | call CheckDefFailure(["let x = '1' / '2'"], 'E1036:') |
| 712 | call CheckDefFailure(["let x = '1' % '2'"], 'E1035:') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 713 | |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 714 | call CheckDefFailure(["let x = 0z01 * 0z12"], 'E1036:') |
| 715 | call CheckDefFailure(["let x = 0z01 / 0z12"], 'E1036:') |
| 716 | call CheckDefFailure(["let x = 0z01 % 0z12"], 'E1035:') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 717 | |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 718 | call CheckDefFailure(["let x = [1] * [2]"], 'E1036:') |
| 719 | call CheckDefFailure(["let x = [1] / [2]"], 'E1036:') |
| 720 | call CheckDefFailure(["let x = [1] % [2]"], 'E1035:') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 721 | |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 722 | call CheckDefFailure(["let x = #{one: 1} * #{two: 2}"], 'E1036:') |
| 723 | call CheckDefFailure(["let x = #{one: 1} / #{two: 2}"], 'E1036:') |
| 724 | call CheckDefFailure(["let x = #{one: 1} % #{two: 2}"], 'E1035:') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 725 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 726 | call CheckDefFailure(["let x = 0xff[1]"], 'E1090:') |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 727 | if has('float') |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 728 | call CheckDefFailure(["let x = 0.7[1]"], 'E1090:') |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 729 | endif |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 730 | endfunc |
| 731 | |
| 732 | func Test_expr6_float_fails() |
| 733 | CheckFeature float |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 734 | call CheckDefFailure(["let x = 1.0 % 2"], 'E1035:') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 735 | endfunc |
| 736 | |
| 737 | " define here to use old style parsing |
| 738 | if has('float') |
| 739 | let g:float_zero = 0.0 |
| 740 | let g:float_neg = -9.8 |
| 741 | let g:float_big = 9.9e99 |
| 742 | endif |
| 743 | let g:blob_empty = 0z |
| 744 | let g:blob_one = 0z01 |
| 745 | let g:blob_long = 0z0102.0304 |
| 746 | |
| 747 | let g:string_empty = '' |
| 748 | let g:string_short = 'x' |
| 749 | let g:string_long = 'abcdefghijklm' |
| 750 | let g:string_special = "ab\ncd\ref\ekk" |
| 751 | |
| 752 | let g:special_true = v:true |
| 753 | let g:special_false = v:false |
| 754 | let g:special_null = v:null |
| 755 | let g:special_none = v:none |
| 756 | |
| 757 | let g:list_empty = [] |
| 758 | let g:list_mixed = [1, 'b', v:false] |
| 759 | |
| 760 | let g:dict_empty = {} |
| 761 | let g:dict_one = #{one: 1} |
| 762 | |
| 763 | let $TESTVAR = 'testvar' |
| 764 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 765 | " test low level expression |
| 766 | def Test_expr7_number() |
| 767 | " number constant |
| 768 | assert_equal(0, 0) |
| 769 | assert_equal(654, 0654) |
| 770 | |
| 771 | assert_equal(6, 0x6) |
| 772 | assert_equal(15, 0xf) |
| 773 | assert_equal(255, 0xff) |
| 774 | enddef |
| 775 | |
| 776 | def Test_expr7_float() |
| 777 | " float constant |
Bram Moolenaar | 7f829ca | 2020-01-31 22:12:41 +0100 | [diff] [blame] | 778 | if !has('float') |
| 779 | MissingFeature 'float' |
| 780 | else |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 781 | assert_equal(g:float_zero, .0) |
| 782 | assert_equal(g:float_zero, 0.0) |
| 783 | assert_equal(g:float_neg, -9.8) |
| 784 | assert_equal(g:float_big, 9.9e99) |
| 785 | endif |
| 786 | enddef |
| 787 | |
| 788 | def Test_expr7_blob() |
| 789 | " blob constant |
| 790 | assert_equal(g:blob_empty, 0z) |
| 791 | assert_equal(g:blob_one, 0z01) |
| 792 | assert_equal(g:blob_long, 0z0102.0304) |
Bram Moolenaar | 92dba36 | 2020-03-30 21:22:56 +0200 | [diff] [blame] | 793 | |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 794 | call CheckDefFailure(["let x = 0z123"], 'E973:') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 795 | enddef |
| 796 | |
| 797 | def Test_expr7_string() |
| 798 | " string constant |
| 799 | assert_equal(g:string_empty, '') |
| 800 | assert_equal(g:string_empty, "") |
| 801 | assert_equal(g:string_short, 'x') |
| 802 | assert_equal(g:string_short, "x") |
| 803 | assert_equal(g:string_long, 'abcdefghijklm') |
| 804 | assert_equal(g:string_long, "abcdefghijklm") |
| 805 | assert_equal(g:string_special, "ab\ncd\ref\ekk") |
Bram Moolenaar | 92dba36 | 2020-03-30 21:22:56 +0200 | [diff] [blame] | 806 | |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 807 | call CheckDefFailure(['let x = "abc'], 'E114:') |
| 808 | call CheckDefFailure(["let x = 'abc"], 'E115:') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 809 | enddef |
| 810 | |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 811 | def Test_expr7_vimvar() |
| 812 | let old: list<string> = v:oldfiles |
| 813 | let compl: dict<any> = v:completed_item |
| 814 | |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 815 | call CheckDefFailure(["let old: list<number> = v:oldfiles"], 'E1013: type mismatch, expected list<number> but got list<string>') |
| 816 | 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] | 817 | enddef |
| 818 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 819 | def Test_expr7_special() |
| 820 | " special constant |
| 821 | assert_equal(g:special_true, true) |
| 822 | assert_equal(g:special_false, false) |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 823 | assert_equal(g:special_true, v:true) |
| 824 | assert_equal(g:special_false, v:false) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 825 | assert_equal(g:special_null, v:null) |
| 826 | assert_equal(g:special_none, v:none) |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 827 | |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 828 | call CheckDefFailure(['v:true = true'], 'E46:') |
| 829 | call CheckDefFailure(['v:true = false'], 'E46:') |
| 830 | call CheckDefFailure(['v:false = true'], 'E46:') |
| 831 | call CheckDefFailure(['v:null = 11'], 'E46:') |
| 832 | call CheckDefFailure(['v:none = 22'], 'E46:') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 833 | enddef |
| 834 | |
| 835 | def Test_expr7_list() |
| 836 | " list |
| 837 | assert_equal(g:list_empty, []) |
| 838 | assert_equal(g:list_empty, [ ]) |
| 839 | assert_equal(g:list_mixed, [1, 'b', false]) |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 840 | assert_equal('b', g:list_mixed[1]) |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 841 | |
Bram Moolenaar | 015f426 | 2020-05-05 21:25:22 +0200 | [diff] [blame] | 842 | call CheckDefExecFailure(["let x = g:anint[3]"], 'E714:') |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 843 | call CheckDefFailure(["let x = g:list_mixed[xxx]"], 'E1001:') |
Bram Moolenaar | 015f426 | 2020-05-05 21:25:22 +0200 | [diff] [blame] | 844 | call CheckDefExecFailure(["let x = g:list_mixed['xx']"], 'E39:') |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 845 | call CheckDefFailure(["let x = g:list_mixed[0"], 'E111:') |
Bram Moolenaar | 015f426 | 2020-05-05 21:25:22 +0200 | [diff] [blame] | 846 | call CheckDefExecFailure(["let x = g:list_empty[3]"], 'E684:') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 847 | enddef |
| 848 | |
| 849 | def Test_expr7_lambda() |
| 850 | " lambda |
| 851 | let La = { -> 'result'} |
| 852 | assert_equal('result', La()) |
| 853 | assert_equal([1, 3, 5], [1, 2, 3]->map({key, val -> key + val})) |
| 854 | enddef |
| 855 | |
| 856 | def Test_expr7_dict() |
| 857 | " dictionary |
| 858 | assert_equal(g:dict_empty, {}) |
| 859 | assert_equal(g:dict_empty, { }) |
| 860 | assert_equal(g:dict_one, {'one': 1}) |
| 861 | let key = 'one' |
| 862 | let val = 1 |
| 863 | assert_equal(g:dict_one, {key: val}) |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 864 | |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 865 | call CheckDefFailure(["let x = #{8: 8}"], 'E1014:') |
| 866 | call CheckDefFailure(["let x = #{xxx}"], 'E720:') |
| 867 | call CheckDefFailure(["let x = #{xxx: 1", "let y = 2"], 'E722:') |
| 868 | call CheckDefFailure(["let x = #{xxx: 1,"], 'E723:') |
| 869 | call CheckDefFailure(["let x = {'a': xxx}"], 'E1001:') |
| 870 | call CheckDefFailure(["let x = {xxx: 8}"], 'E1001:') |
| 871 | call CheckDefFailure(["let x = #{a: 1, a: 2}"], 'E721:') |
| 872 | call CheckDefFailure(["let x = #"], 'E1015:') |
| 873 | call CheckDefFailure(["let x += 1"], 'E1020:') |
| 874 | call CheckDefFailure(["let x = x + 1"], 'E1001:') |
Bram Moolenaar | 015f426 | 2020-05-05 21:25:22 +0200 | [diff] [blame] | 875 | call CheckDefExecFailure(["let x = g:anint.member"], 'E715:') |
| 876 | call CheckDefExecFailure(["let x = g:dict_empty.member"], 'E716:') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 877 | enddef |
| 878 | |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 879 | def Test_expr_member() |
| 880 | assert_equal(1, g:dict_one.one) |
Bram Moolenaar | 4dac32c | 2020-05-15 21:44:19 +0200 | [diff] [blame] | 881 | let d: dict<number> = g:dict_one |
| 882 | assert_equal(1, d['one']) |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 883 | |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 884 | call CheckDefFailure(["let x = g:dict_one.#$!"], 'E1002:') |
Bram Moolenaar | 4dac32c | 2020-05-15 21:44:19 +0200 | [diff] [blame] | 885 | call CheckDefExecFailure(["let d: dict<any>", "echo d['a']"], 'E716:') |
| 886 | 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] | 887 | enddef |
| 888 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 889 | def Test_expr7_option() |
| 890 | " option |
| 891 | set ts=11 |
| 892 | assert_equal(11, &ts) |
Bram Moolenaar | 401d9ff | 2020-02-19 18:14:44 +0100 | [diff] [blame] | 893 | &ts = 9 |
| 894 | assert_equal(9, &ts) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 895 | set ts=8 |
| 896 | set grepprg=some\ text |
| 897 | assert_equal('some text', &grepprg) |
Bram Moolenaar | 97a2af3 | 2020-01-28 22:52:48 +0100 | [diff] [blame] | 898 | &grepprg = test_null_string() |
| 899 | assert_equal('', &grepprg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 900 | set grepprg& |
| 901 | enddef |
| 902 | |
| 903 | def Test_expr7_environment() |
| 904 | " environment variable |
| 905 | assert_equal('testvar', $TESTVAR) |
| 906 | assert_equal('', $ASDF_ASD_XXX) |
Bram Moolenaar | c58164c | 2020-03-29 18:40:30 +0200 | [diff] [blame] | 907 | |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 908 | call CheckDefFailure(["let x = $$$"], 'E1002:') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 909 | enddef |
| 910 | |
| 911 | def Test_expr7_register() |
Bram Moolenaar | 401d9ff | 2020-02-19 18:14:44 +0100 | [diff] [blame] | 912 | @a = 'register a' |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 913 | assert_equal('register a', @a) |
| 914 | enddef |
| 915 | |
| 916 | def Test_expr7_parens() |
| 917 | " (expr) |
| 918 | assert_equal(4, (6 * 4) / 6) |
| 919 | assert_equal(0, 6 * ( 4 / 6 )) |
| 920 | |
| 921 | assert_equal(6, +6) |
| 922 | assert_equal(-6, -6) |
| 923 | assert_equal(6, --6) |
| 924 | assert_equal(6, -+-6) |
| 925 | assert_equal(-6, ---6) |
| 926 | enddef |
| 927 | |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 928 | def Test_expr7_negate() |
| 929 | assert_equal(-99, -99) |
| 930 | assert_equal(99, --99) |
| 931 | let nr = 88 |
| 932 | assert_equal(-88, -nr) |
| 933 | assert_equal(88, --nr) |
| 934 | enddef |
| 935 | |
Bram Moolenaar | 6e94978 | 2020-04-13 17:21:00 +0200 | [diff] [blame] | 936 | def Echo(arg: any): string |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 937 | return arg |
| 938 | enddef |
| 939 | |
Bram Moolenaar | 6e94978 | 2020-04-13 17:21:00 +0200 | [diff] [blame] | 940 | def s:EchoArg(arg: any): string |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 941 | return arg |
| 942 | enddef |
| 943 | |
| 944 | def Test_expr7_call() |
| 945 | assert_equal('yes', 'yes'->Echo()) |
| 946 | assert_equal('yes', 'yes'->s:EchoArg()) |
| 947 | |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 948 | call CheckDefFailure(["let x = 'yes'->Echo"], 'E107:') |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 949 | enddef |
| 950 | |
| 951 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 952 | def Test_expr7_not() |
| 953 | assert_equal(true, !'') |
| 954 | assert_equal(true, ![]) |
| 955 | assert_equal(false, !'asdf') |
| 956 | assert_equal(false, ![2]) |
| 957 | assert_equal(true, !!'asdf') |
| 958 | assert_equal(true, !![2]) |
Bram Moolenaar | 8ed0458 | 2020-02-22 19:07:28 +0100 | [diff] [blame] | 959 | |
| 960 | assert_equal(true, !test_null_partial()) |
| 961 | assert_equal(false, !{-> 'yes'}) |
| 962 | |
| 963 | assert_equal(true, !test_null_dict()) |
| 964 | assert_equal(true, !{}) |
| 965 | assert_equal(false, !{'yes': 'no'}) |
| 966 | |
Bram Moolenaar | b4d2cb1 | 2020-02-22 20:33:08 +0100 | [diff] [blame] | 967 | if has('channel') |
| 968 | assert_equal(true, !test_null_job()) |
| 969 | assert_equal(true, !test_null_channel()) |
| 970 | endif |
Bram Moolenaar | 8ed0458 | 2020-02-22 19:07:28 +0100 | [diff] [blame] | 971 | |
| 972 | assert_equal(true, !test_null_blob()) |
| 973 | assert_equal(true, !0z) |
| 974 | assert_equal(false, !0z01) |
| 975 | |
| 976 | assert_equal(true, !test_void()) |
| 977 | assert_equal(true, !test_unknown()) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 978 | enddef |
| 979 | |
| 980 | func Test_expr7_fails() |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 981 | call CheckDefFailure(["let x = (12"], "E110:") |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 982 | |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 983 | call CheckDefFailure(["let x = -'xx'"], "E1030:") |
| 984 | call CheckDefFailure(["let x = +'xx'"], "E1030:") |
| 985 | call CheckDefFailure(["let x = -0z12"], "E974:") |
Bram Moolenaar | 015f426 | 2020-05-05 21:25:22 +0200 | [diff] [blame] | 986 | call CheckDefExecFailure(["let x = -[8]"], "E39:") |
| 987 | call CheckDefExecFailure(["let x = -{'a': 1}"], "E39:") |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 988 | |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 989 | call CheckDefFailure(["let x = @"], "E1002:") |
| 990 | call CheckDefFailure(["let x = @<"], "E354:") |
Bram Moolenaar | 58ceca5 | 2020-01-28 22:46:22 +0100 | [diff] [blame] | 991 | |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 992 | call CheckDefFailure(["let x = [1, 2"], "E697:") |
| 993 | call CheckDefFailure(["let x = [notfound]"], "E1001:") |
Bram Moolenaar | ee619e5 | 2020-03-28 21:38:06 +0100 | [diff] [blame] | 994 | |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 995 | call CheckDefFailure(["let x = { -> 123) }"], "E451:") |
| 996 | call CheckDefFailure(["let x = 123->{x -> x + 5) }"], "E451:") |
Bram Moolenaar | ee619e5 | 2020-03-28 21:38:06 +0100 | [diff] [blame] | 997 | |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 998 | call CheckDefFailure(["let x = ¬exist"], 'E113:') |
| 999 | call CheckDefFailure(["&grepprg = [343]"], 'E1013:') |
Bram Moolenaar | fd1823e | 2020-02-19 20:23:11 +0100 | [diff] [blame] | 1000 | |
Bram Moolenaar | 015f426 | 2020-05-05 21:25:22 +0200 | [diff] [blame] | 1001 | call CheckDefExecFailure(["echo s:doesnt_exist"], 'E121:') |
| 1002 | call CheckDefExecFailure(["echo g:doesnt_exist"], 'E121:') |
Bram Moolenaar | 09f28f4 | 2020-02-20 23:08:34 +0100 | [diff] [blame] | 1003 | |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 1004 | call CheckDefFailure(["echo a:somevar"], 'E1075:') |
| 1005 | call CheckDefFailure(["echo l:somevar"], 'E1075:') |
| 1006 | call CheckDefFailure(["echo x:somevar"], 'E1075:') |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 1007 | |
Bram Moolenaar | 015f426 | 2020-05-05 21:25:22 +0200 | [diff] [blame] | 1008 | call CheckDefExecFailure(["let x = +g:astring"], 'E1030:') |
| 1009 | call CheckDefExecFailure(["let x = +g:ablob"], 'E974:') |
| 1010 | call CheckDefExecFailure(["let x = +g:alist"], 'E745:') |
| 1011 | call CheckDefExecFailure(["let x = +g:adict"], 'E728:') |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1012 | |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 1013 | call CheckDefFailure(["let x = ''", "let y = x.memb"], 'E715:') |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 1014 | |
Bram Moolenaar | 015f426 | 2020-05-05 21:25:22 +0200 | [diff] [blame] | 1015 | call CheckDefExecFailure(["[1, 2->len()"], 'E492:') |
| 1016 | call CheckDefExecFailure(["#{a: 1->len()"], 'E488:') |
| 1017 | call CheckDefExecFailure(["{'a': 1->len()"], 'E492:') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1018 | endfunc |
| 1019 | |
| 1020 | let g:Funcrefs = [function('add')] |
| 1021 | |
| 1022 | func CallMe(arg) |
| 1023 | return a:arg |
| 1024 | endfunc |
| 1025 | |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 1026 | func CallMe2(one, two) |
| 1027 | return a:one .. a:two |
| 1028 | endfunc |
| 1029 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1030 | def Test_expr7_trailing() |
| 1031 | " user function call |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 1032 | assert_equal(123, g:CallMe(123)) |
| 1033 | assert_equal(123, g:CallMe( 123)) |
| 1034 | assert_equal(123, g:CallMe(123 )) |
| 1035 | assert_equal('yesno', g:CallMe2('yes', 'no')) |
| 1036 | assert_equal('yesno', g:CallMe2( 'yes', 'no' )) |
| 1037 | assert_equal('nothing', g:CallMe('nothing')) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1038 | |
| 1039 | " partial call |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 1040 | let Part = function('g:CallMe') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1041 | assert_equal('yes', Part('yes')) |
| 1042 | |
| 1043 | " funcref call, using list index |
| 1044 | let l = [] |
| 1045 | g:Funcrefs[0](l, 2) |
| 1046 | assert_equal([2], l) |
| 1047 | |
| 1048 | " method call |
| 1049 | l = [2, 5, 6] |
| 1050 | l->map({k, v -> k + v}) |
| 1051 | assert_equal([2, 6, 8], l) |
| 1052 | |
| 1053 | " lambda method call |
| 1054 | l = [2, 5] |
| 1055 | l->{l -> add(l, 8)}() |
| 1056 | assert_equal([2, 5, 8], l) |
| 1057 | |
| 1058 | " dict member |
| 1059 | let d = #{key: 123} |
| 1060 | assert_equal(123, d.key) |
| 1061 | enddef |
| 1062 | |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 1063 | def Test_expr7_subscript_linebreak() |
| 1064 | let range = range( |
| 1065 | 3) |
| 1066 | let l = range-> |
| 1067 | map('string(v:key)') |
| 1068 | assert_equal(['0', '1', '2'], l) |
| 1069 | |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1070 | l = range |
| 1071 | ->map('string(v:key)') |
| 1072 | assert_equal(['0', '1', '2'], l) |
| 1073 | |
| 1074 | l = range # comment |
| 1075 | ->map('string(v:key)') |
| 1076 | assert_equal(['0', '1', '2'], l) |
| 1077 | |
| 1078 | l = range |
| 1079 | |
| 1080 | ->map('string(v:key)') |
| 1081 | assert_equal(['0', '1', '2'], l) |
| 1082 | |
| 1083 | l = range |
| 1084 | # comment |
| 1085 | ->map('string(v:key)') |
| 1086 | assert_equal(['0', '1', '2'], l) |
| 1087 | |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 1088 | assert_equal('1', l[ |
| 1089 | 1]) |
| 1090 | |
| 1091 | let d = #{one: 33} |
| 1092 | assert_equal(33, d. |
| 1093 | one) |
| 1094 | enddef |
| 1095 | |
Bram Moolenaar | 6e94978 | 2020-04-13 17:21:00 +0200 | [diff] [blame] | 1096 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1097 | func Test_expr7_trailing_fails() |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 1098 | call CheckDefFailure(['let l = [2]', 'l->{l -> add(l, 8)}'], 'E107') |
| 1099 | call CheckDefFailure(['let l = [2]', 'l->{l -> add(l, 8)} ()'], 'E274') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1100 | endfunc |
| 1101 | |
| 1102 | func Test_expr_fails() |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 1103 | call CheckDefFailure(["let x = '1'is2"], 'E488:') |
| 1104 | call CheckDefFailure(["let x = '1'isnot2"], 'E488:') |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 1105 | |
Bram Moolenaar | 015f426 | 2020-05-05 21:25:22 +0200 | [diff] [blame] | 1106 | call CheckDefExecFailure(["CallMe ('yes')"], 'E492:') |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 1107 | call CheckDefFailure(["CallMe2('yes','no')"], 'E1069:') |
| 1108 | call CheckDefFailure(["CallMe2('yes' , 'no')"], 'E1068:') |
Bram Moolenaar | 0c2ca58 | 2020-02-25 22:58:29 +0100 | [diff] [blame] | 1109 | |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 1110 | call CheckDefFailure(["v:nosuch += 3"], 'E1001:') |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 1111 | call CheckDefFailure(["let v:statusmsg = ''"], 'E1016: Cannot declare a v: variable:') |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 1112 | call CheckDefFailure(["let asdf = v:nosuch"], 'E1001:') |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 1113 | |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 1114 | call CheckDefFailure(["echo len('asdf'"], 'E110:') |
| 1115 | call CheckDefFailure(["echo Func0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789()"], 'E1011:') |
| 1116 | call CheckDefFailure(["echo doesnotexist()"], 'E117:') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1117 | endfunc |