Bram Moolenaar | d58f03b | 2017-01-29 22:48:45 +0100 | [diff] [blame] | 1 | " Tests for Lua. |
Bram Moolenaar | d58f03b | 2017-01-29 22:48:45 +0100 | [diff] [blame] | 2 | |
| 3 | if !has('lua') |
| 4 | finish |
| 5 | endif |
| 6 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 7 | " Check that switching to another buffer does not trigger ml_get error. |
| 8 | func Test_command_new_no_ml_get_error() |
Bram Moolenaar | d58f03b | 2017-01-29 22:48:45 +0100 | [diff] [blame] | 9 | new |
| 10 | let wincount = winnr('$') |
| 11 | call setline(1, ['one', 'two', 'three']) |
| 12 | luado vim.command("new") |
| 13 | call assert_equal(wincount + 1, winnr('$')) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 14 | %bwipe! |
| 15 | endfunc |
| 16 | |
| 17 | " Test vim.command() |
| 18 | func Test_command() |
| 19 | new |
| 20 | call setline(1, ['one', 'two', 'three']) |
| 21 | luado vim.command("1,2d_") |
| 22 | call assert_equal(['three'], getline(1, '$')) |
Bram Moolenaar | d58f03b | 2017-01-29 22:48:45 +0100 | [diff] [blame] | 23 | bwipe! |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 24 | endfunc |
| 25 | |
| 26 | " Test vim.eval() |
| 27 | func Test_eval() |
| 28 | " lua.eval with a number |
| 29 | lua v = vim.eval('123') |
| 30 | call assert_equal('number', luaeval('vim.type(v)')) |
| 31 | call assert_equal(123.0, luaeval('v')) |
| 32 | |
| 33 | " lua.eval with a string |
| 34 | lua v = vim.eval('"abc"') |
| 35 | call assert_equal('string', luaeval('vim.type(v)')) |
| 36 | call assert_equal('abc', luaeval('v')) |
| 37 | |
| 38 | " lua.eval with a list |
| 39 | lua v = vim.eval("['a']") |
| 40 | call assert_equal('list', luaeval('vim.type(v)')) |
| 41 | call assert_equal(['a'], luaeval('v')) |
| 42 | |
| 43 | " lua.eval with a dict |
| 44 | lua v = vim.eval("{'a':'b'}") |
| 45 | call assert_equal('dict', luaeval('vim.type(v)')) |
| 46 | call assert_equal({'a':'b'}, luaeval('v')) |
| 47 | |
| 48 | call assert_fails('lua v = vim.eval(nil)', |
| 49 | \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got nil)") |
| 50 | call assert_fails('lua v = vim.eval(true)', |
| 51 | \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got boolean)") |
| 52 | call assert_fails('lua v = vim.eval({})', |
| 53 | \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got table)") |
| 54 | call assert_fails('lua v = vim.eval(print)', |
| 55 | \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got function)") |
| 56 | call assert_fails('lua v = vim.eval(vim.buffer())', |
| 57 | \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got userdata)") |
| 58 | |
| 59 | lua v = nil |
| 60 | endfunc |
| 61 | |
| 62 | " Test vim.window() |
| 63 | func Test_window() |
| 64 | e Xfoo2 |
| 65 | new Xfoo1 |
| 66 | |
| 67 | " Window 1 (top window) contains Xfoo1 |
| 68 | " Window 2 (bottom window) contains Xfoo2 |
| 69 | call assert_equal('Xfoo1', luaeval('vim.window(1):buffer().name')) |
| 70 | call assert_equal('Xfoo2', luaeval('vim.window(2):buffer().name')) |
| 71 | |
| 72 | " Window 3 does not exist so vim.window(3) should return nil |
| 73 | call assert_equal('nil', luaeval('tostring(vim.window(3))')) |
| 74 | |
| 75 | %bwipe! |
| 76 | endfunc |
| 77 | |
| 78 | " Test vim.window().height |
| 79 | func Test_window_height() |
| 80 | new |
| 81 | lua vim.window().height = 2 |
| 82 | call assert_equal(2, winheight(0)) |
| 83 | lua vim.window().height = vim.window().height + 1 |
| 84 | call assert_equal(3, winheight(0)) |
| 85 | bwipe! |
| 86 | endfunc |
| 87 | |
| 88 | " Test vim.window().width |
| 89 | func Test_window_width() |
| 90 | vert new |
| 91 | lua vim.window().width = 2 |
| 92 | call assert_equal(2, winwidth(0)) |
| 93 | lua vim.window().width = vim.window().width + 1 |
| 94 | call assert_equal(3, winwidth(0)) |
| 95 | bwipe! |
| 96 | endfunc |
| 97 | |
| 98 | " Test vim.window().line and vim.window.col |
| 99 | func Test_window_line_col() |
| 100 | new |
| 101 | call setline(1, ['line1', 'line2', 'line3']) |
| 102 | lua vim.window().line = 2 |
| 103 | lua vim.window().col = 4 |
| 104 | call assert_equal([0, 2, 4, 0], getpos('.')) |
| 105 | lua vim.window().line = vim.window().line + 1 |
| 106 | lua vim.window().col = vim.window().col - 1 |
| 107 | call assert_equal([0, 3, 3, 0], getpos('.')) |
| 108 | |
| 109 | call assert_fails('lua vim.window().line = 10', |
| 110 | \ '[string "vim chunk"]:1: line out of range') |
| 111 | bwipe! |
| 112 | endfunc |
| 113 | |
| 114 | " Test setting the current window |
| 115 | func Test_window_set_current() |
| 116 | new Xfoo1 |
| 117 | lua w1 = vim.window() |
| 118 | new Xfoo2 |
| 119 | lua w2 = vim.window() |
| 120 | |
| 121 | call assert_equal('Xfoo2', bufname('%')) |
| 122 | lua w1() |
| 123 | call assert_equal('Xfoo1', bufname('%')) |
| 124 | lua w2() |
| 125 | call assert_equal('Xfoo2', bufname('%')) |
| 126 | |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 127 | lua w1, w2 = nil |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 128 | %bwipe! |
| 129 | endfunc |
| 130 | |
| 131 | " Test vim.window().buffer |
| 132 | func Test_window_buffer() |
| 133 | new Xfoo1 |
| 134 | lua w1 = vim.window() |
| 135 | lua b1 = w1.buffer() |
| 136 | new Xfoo2 |
| 137 | lua w2 = vim.window() |
| 138 | lua b2 = w2.buffer() |
| 139 | |
| 140 | lua b1() |
| 141 | call assert_equal('Xfoo1', bufname('%')) |
| 142 | lua b2() |
| 143 | call assert_equal('Xfoo2', bufname('%')) |
| 144 | |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 145 | lua b1, b2, w1, w2 = nil |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 146 | %bwipe! |
| 147 | endfunc |
| 148 | |
| 149 | " Test vim.window():previous() and vim.window():next() |
| 150 | func Test_window_next_previous() |
| 151 | new Xfoo1 |
| 152 | new Xfoo2 |
| 153 | new Xfoo3 |
| 154 | wincmd j |
| 155 | |
| 156 | call assert_equal('Xfoo2', luaeval('vim.window().buffer().name')) |
| 157 | call assert_equal('Xfoo1', luaeval('vim.window():next():buffer().name')) |
| 158 | call assert_equal('Xfoo3', luaeval('vim.window():previous():buffer().name')) |
| 159 | |
| 160 | %bwipe! |
| 161 | endfunc |
| 162 | |
| 163 | " Test vim.window():isvalid() |
| 164 | func Test_window_isvalid() |
| 165 | new Xfoo |
| 166 | lua w = vim.window() |
| 167 | call assert_true(luaeval('w:isvalid()')) |
| 168 | |
| 169 | " FIXME: how to test the case when isvalid() returns v:false? |
| 170 | " isvalid() gives errors when the window is deleted. Is it a bug? |
| 171 | |
| 172 | lua w = nil |
| 173 | bwipe! |
| 174 | endfunc |
| 175 | |
| 176 | " Test vim.buffer() with and without argument |
| 177 | func Test_buffer() |
| 178 | new Xfoo1 |
| 179 | let bn1 = bufnr('%') |
| 180 | new Xfoo2 |
| 181 | let bn2 = bufnr('%') |
| 182 | |
| 183 | " Test vim.buffer() without argument. |
| 184 | call assert_equal('Xfoo2', luaeval("vim.buffer().name")) |
| 185 | |
| 186 | " Test vim.buffer() with string argument. |
| 187 | call assert_equal('Xfoo1', luaeval("vim.buffer('Xfoo1').name")) |
| 188 | call assert_equal('Xfoo2', luaeval("vim.buffer('Xfoo2').name")) |
| 189 | |
| 190 | " Test vim.buffer() with integer argument. |
| 191 | call assert_equal('Xfoo1', luaeval("vim.buffer(" . bn1 . ").name")) |
| 192 | call assert_equal('Xfoo2', luaeval("vim.buffer(" . bn2 . ").name")) |
| 193 | |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 194 | lua bn1, bn2 = nil |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 195 | %bwipe! |
| 196 | endfunc |
| 197 | |
| 198 | " Test vim.buffer().name and vim.buffer().fname |
| 199 | func Test_buffer_name() |
| 200 | new |
Bram Moolenaar | fe08df4 | 2018-07-07 23:07:41 +0200 | [diff] [blame] | 201 | call assert_equal('', luaeval('vim.buffer().name')) |
| 202 | call assert_equal('', luaeval('vim.buffer().fname')) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 203 | bwipe! |
| 204 | |
| 205 | new Xfoo |
| 206 | call assert_equal('Xfoo', luaeval('vim.buffer().name')) |
| 207 | call assert_equal(expand('%:p'), luaeval('vim.buffer().fname')) |
| 208 | bwipe! |
| 209 | endfunc |
| 210 | |
| 211 | " Test vim.buffer().number |
| 212 | func Test_buffer_number() |
| 213 | " All numbers in Lua are floating points number (no integers). |
| 214 | call assert_equal(bufnr('%'), float2nr(luaeval('vim.buffer().number'))) |
| 215 | endfunc |
| 216 | |
| 217 | " Test inserting lines in buffer. |
| 218 | func Test_buffer_insert() |
| 219 | new |
| 220 | lua vim.buffer()[1] = '3' |
| 221 | lua vim.buffer():insert('1', 0) |
| 222 | lua vim.buffer():insert('2', 1) |
| 223 | lua vim.buffer():insert('4', 10) |
| 224 | |
| 225 | call assert_equal(['1', '2', '3', '4'], getline(1, '$')) |
| 226 | bwipe! |
| 227 | endfunc |
| 228 | |
| 229 | " Test deleting line in buffer |
| 230 | func Test_buffer_delete() |
| 231 | new |
| 232 | call setline(1, ['1', '2', '3']) |
| 233 | lua vim.buffer()[2] = nil |
| 234 | call assert_equal(['1', '3'], getline(1, '$')) |
| 235 | |
| 236 | call assert_fails('lua vim.buffer()[3] = nil', |
| 237 | \ '[string "vim chunk"]:1: invalid line number') |
| 238 | bwipe! |
| 239 | endfunc |
| 240 | |
| 241 | " Test #vim.buffer() i.e. number of lines in buffer |
| 242 | func Test_buffer_number_lines() |
| 243 | new |
| 244 | call setline(1, ['a', 'b', 'c']) |
| 245 | call assert_equal(3.0, luaeval('#vim.buffer()')) |
| 246 | bwipe! |
| 247 | endfunc |
| 248 | |
| 249 | " Test vim.buffer():next() and vim.buffer():previous() |
| 250 | " Note that these functions get the next or previous buffers |
| 251 | " but do not switch buffer. |
| 252 | func Test_buffer_next_previous() |
| 253 | new Xfoo1 |
| 254 | new Xfoo2 |
| 255 | new Xfoo3 |
| 256 | b Xfoo2 |
| 257 | |
| 258 | lua bn = vim.buffer():next() |
| 259 | lua bp = vim.buffer():previous() |
| 260 | |
| 261 | call assert_equal('Xfoo2', luaeval('vim.buffer().name')) |
| 262 | call assert_equal('Xfoo1', luaeval('bp.name')) |
| 263 | call assert_equal('Xfoo3', luaeval('bn.name')) |
| 264 | |
| 265 | call assert_equal('Xfoo2', bufname('%')) |
| 266 | |
| 267 | lua bn() |
| 268 | call assert_equal('Xfoo3', luaeval('vim.buffer().name')) |
| 269 | call assert_equal('Xfoo3', bufname('%')) |
| 270 | |
| 271 | lua bp() |
| 272 | call assert_equal('Xfoo1', luaeval('vim.buffer().name')) |
| 273 | call assert_equal('Xfoo1', bufname('%')) |
| 274 | |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 275 | lua bn, bp = nil |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 276 | %bwipe! |
| 277 | endfunc |
| 278 | |
| 279 | " Test vim.buffer():isvalid() |
| 280 | func Test_buffer_isvalid() |
| 281 | new Xfoo |
| 282 | lua b = vim.buffer() |
| 283 | call assert_true(luaeval('b:isvalid()')) |
| 284 | |
| 285 | " FIXME: how to test the case when isvalid() returns v:false? |
| 286 | " isvalid() gives errors when the buffer is wiped. Is it a bug? |
| 287 | |
| 288 | lua b = nil |
| 289 | bwipe! |
| 290 | endfunc |
| 291 | |
| 292 | func Test_list() |
| 293 | call assert_equal([], luaeval('vim.list()')) |
| 294 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 295 | let l = [] |
| 296 | lua l = vim.eval('l') |
| 297 | lua l:add(123) |
| 298 | lua l:add('abc') |
| 299 | lua l:add(true) |
| 300 | lua l:add(false) |
| 301 | lua l:add(vim.eval("[1, 2, 3]")) |
| 302 | lua l:add(vim.eval("{'a':1, 'b':2, 'c':3}")) |
| 303 | call assert_equal([123.0, 'abc', v:true, v:false, [1, 2, 3], {'a': 1, 'b': 2, 'c': 3}], l) |
| 304 | call assert_equal(6.0, luaeval('#l')) |
Bram Moolenaar | a8a60d0 | 2018-07-02 22:54:36 +0200 | [diff] [blame] | 305 | call assert_match('^list: \%(0x\)\?\x\+$', luaeval('tostring(l)')) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 306 | |
| 307 | lua l[0] = 124 |
| 308 | lua l[4] = nil |
| 309 | lua l:insert('first') |
| 310 | lua l:insert('xx', 3) |
| 311 | call assert_equal(['first', 124.0, 'abc', 'xx', v:true, v:false, {'a': 1, 'b': 2, 'c': 3}], l) |
| 312 | |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 313 | lockvar 1 l |
| 314 | call assert_fails('lua l:add("x")', '[string "vim chunk"]:1: list is locked') |
| 315 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 316 | lua l = nil |
| 317 | endfunc |
| 318 | |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 319 | func Test_list_table() |
| 320 | " See :help lua-vim |
| 321 | " Non-numeric keys should not be used to initialize the list |
| 322 | " so say = 'hi' should be ignored. |
| 323 | lua t = {3.14, 'hello', false, true, say = 'hi'} |
| 324 | call assert_equal([3.14, 'hello', v:false, v:true], luaeval('vim.list(t)')) |
| 325 | lua t = nil |
| 326 | |
| 327 | call assert_fails('lua vim.list(1)', '[string "vim chunk"]:1: table expected, got number') |
| 328 | call assert_fails('lua vim.list("x")', '[string "vim chunk"]:1: table expected, got string') |
| 329 | call assert_fails('lua vim.list(print)', '[string "vim chunk"]:1: table expected, got function') |
| 330 | call assert_fails('lua vim.list(true)', '[string "vim chunk"]:1: table expected, got boolean') |
| 331 | endfunc |
| 332 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 333 | " Test l() i.e. iterator on list |
| 334 | func Test_list_iter() |
| 335 | lua l = vim.list():add('foo'):add('bar') |
| 336 | lua str = '' |
| 337 | lua for v in l() do str = str .. v end |
| 338 | call assert_equal('foobar', luaeval('str')) |
| 339 | |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 340 | lua str, l = nil |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 341 | endfunc |
| 342 | |
| 343 | func Test_recursive_list() |
| 344 | lua l = vim.list():add(1):add(2) |
| 345 | lua l = l:add(l) |
| 346 | |
| 347 | call assert_equal(1.0, luaeval('l[0]')) |
| 348 | call assert_equal(2.0, luaeval('l[1]')) |
| 349 | |
| 350 | call assert_equal(1.0, luaeval('l[2][0]')) |
| 351 | call assert_equal(2.0, luaeval('l[2][1]')) |
| 352 | |
| 353 | call assert_equal(1.0, luaeval('l[2][2][0]')) |
| 354 | call assert_equal(2.0, luaeval('l[2][2][1]')) |
| 355 | |
| 356 | call assert_equal('[1.0, 2.0, [...]]', string(luaeval('l'))) |
| 357 | |
Bram Moolenaar | a8a60d0 | 2018-07-02 22:54:36 +0200 | [diff] [blame] | 358 | call assert_match('^list: \%(0x\)\?\x\+$', luaeval('tostring(l)')) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 359 | call assert_equal(luaeval('tostring(l)'), luaeval('tostring(l[2])')) |
| 360 | |
| 361 | call assert_equal(luaeval('l'), luaeval('l[2]')) |
| 362 | call assert_equal(luaeval('l'), luaeval('l[2][2]')) |
| 363 | |
| 364 | lua l = nil |
| 365 | endfunc |
| 366 | |
| 367 | func Test_dict() |
| 368 | call assert_equal({}, luaeval('vim.dict()')) |
| 369 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 370 | let d = {} |
| 371 | lua d = vim.eval('d') |
| 372 | lua d[0] = 123 |
| 373 | lua d[1] = "abc" |
| 374 | lua d[2] = true |
| 375 | lua d[3] = false |
| 376 | lua d[4] = vim.eval("[1, 2, 3]") |
| 377 | lua d[5] = vim.eval("{'a':1, 'b':2, 'c':3}") |
| 378 | call assert_equal({'0':123.0, '1':'abc', '2':v:true, '3':v:false, '4': [1, 2, 3], '5': {'a':1, 'b':2, 'c':3}}, d) |
| 379 | call assert_equal(6.0, luaeval('#d')) |
Bram Moolenaar | a8a60d0 | 2018-07-02 22:54:36 +0200 | [diff] [blame] | 380 | call assert_match('^dict: \%(0x\)\?\x\+$', luaeval('tostring(d)')) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 381 | |
| 382 | call assert_equal('abc', luaeval('d[1]')) |
| 383 | |
| 384 | lua d[0] = 124 |
| 385 | lua d[4] = nil |
| 386 | call assert_equal({'0':124.0, '1':'abc', '2':v:true, '3':v:false, '5': {'a':1, 'b':2, 'c':3}}, d) |
| 387 | |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 388 | lockvar 1 d |
| 389 | call assert_fails('lua d[6] = 1', '[string "vim chunk"]:1: dict is locked') |
| 390 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 391 | lua d = nil |
| 392 | endfunc |
| 393 | |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 394 | func Test_dict_table() |
| 395 | lua t = {key1 = 'x', key2 = 3.14, key3 = true, key4 = false} |
| 396 | call assert_equal({'key1': 'x', 'key2': 3.14, 'key3': v:true, 'key4': v:false}, |
| 397 | \ luaeval('vim.dict(t)')) |
| 398 | |
| 399 | " Same example as in :help lua-vim. |
| 400 | lua t = {math.pi, false, say = 'hi'} |
| 401 | " FIXME: commented out as it currently does not work as documented: |
| 402 | " Expected {'say': 'hi'} |
| 403 | " but got {'1': 3.141593, '2': v:false, 'say': 'hi'} |
| 404 | " Is the documentation or the code wrong? |
| 405 | "call assert_equal({'say' : 'hi'}, luaeval('vim.dict(t)')) |
| 406 | lua t = nil |
| 407 | |
| 408 | call assert_fails('lua vim.dict(1)', '[string "vim chunk"]:1: table expected, got number') |
| 409 | call assert_fails('lua vim.dict("x")', '[string "vim chunk"]:1: table expected, got string') |
| 410 | call assert_fails('lua vim.dict(print)', '[string "vim chunk"]:1: table expected, got function') |
| 411 | call assert_fails('lua vim.dict(true)', '[string "vim chunk"]:1: table expected, got boolean') |
| 412 | endfunc |
| 413 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 414 | " Test d() i.e. iterator on dictionary |
| 415 | func Test_dict_iter() |
| 416 | let d = {'a': 1, 'b':2} |
| 417 | lua d = vim.eval('d') |
| 418 | lua str = '' |
| 419 | lua for k,v in d() do str = str .. k ..':' .. v .. ',' end |
| 420 | call assert_equal('a:1,b:2,', luaeval('str')) |
| 421 | |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 422 | lua str, d = nil |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 423 | endfunc |
| 424 | |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 425 | func Test_funcref() |
| 426 | function I(x) |
| 427 | return a:x |
| 428 | endfunction |
| 429 | let R = function('I') |
| 430 | lua i1 = vim.funcref"I" |
| 431 | lua i2 = vim.eval"R" |
| 432 | lua msg = "funcref|test|" .. (#i2(i1) == #i1(i2) and "OK" or "FAIL") |
| 433 | lua msg = vim.funcref"tr"(msg, "|", " ") |
| 434 | call assert_equal("funcref test OK", luaeval('msg')) |
| 435 | |
| 436 | " dict funcref |
| 437 | function Mylen() dict |
| 438 | return len(self.data) |
| 439 | endfunction |
| 440 | let l = [0, 1, 2, 3] |
| 441 | let mydict = {'data': l} |
| 442 | lua d = vim.eval"mydict" |
| 443 | lua d.len = vim.funcref"Mylen" -- assign d as 'self' |
| 444 | lua res = (d.len() == vim.funcref"len"(vim.eval"l")) and "OK" or "FAIL" |
| 445 | call assert_equal("OK", luaeval('res')) |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 446 | |
| 447 | lua i1, i2, msg, d, res = nil |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 448 | endfunc |
| 449 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 450 | " Test vim.type() |
| 451 | func Test_type() |
| 452 | " The following values are identical to Lua's type function. |
| 453 | call assert_equal('string', luaeval('vim.type("foo")')) |
| 454 | call assert_equal('number', luaeval('vim.type(1)')) |
| 455 | call assert_equal('number', luaeval('vim.type(1.2)')) |
| 456 | call assert_equal('function', luaeval('vim.type(print)')) |
| 457 | call assert_equal('table', luaeval('vim.type({})')) |
| 458 | call assert_equal('boolean', luaeval('vim.type(true)')) |
| 459 | call assert_equal('boolean', luaeval('vim.type(false)')) |
| 460 | call assert_equal('nil', luaeval('vim.type(nil)')) |
| 461 | |
| 462 | " The following values are specific to Vim. |
| 463 | call assert_equal('window', luaeval('vim.type(vim.window())')) |
| 464 | call assert_equal('buffer', luaeval('vim.type(vim.buffer())')) |
| 465 | call assert_equal('list', luaeval('vim.type(vim.list())')) |
| 466 | call assert_equal('dict', luaeval('vim.type(vim.dict())')) |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 467 | call assert_equal('funcref', luaeval('vim.type(vim.funcref("Test_type"))')) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 468 | endfunc |
| 469 | |
| 470 | " Test vim.open() |
| 471 | func Test_open() |
| 472 | call assert_notmatch('XOpen', execute('ls')) |
| 473 | |
| 474 | " Open a buffer XOpen1, but do not jump to it. |
| 475 | lua b = vim.open('XOpen1') |
| 476 | call assert_equal('XOpen1', luaeval('b.name')) |
| 477 | call assert_equal('', bufname('%')) |
| 478 | |
| 479 | call assert_match('XOpen1', execute('ls')) |
| 480 | call assert_notequal('XOpen2', bufname('%')) |
| 481 | |
| 482 | " Open a buffer XOpen2 and jump to it. |
| 483 | lua b = vim.open('XOpen2')() |
| 484 | call assert_equal('XOpen2', luaeval('b.name')) |
| 485 | call assert_equal('XOpen2', bufname('%')) |
| 486 | |
| 487 | lua b = nil |
| 488 | %bwipe! |
| 489 | endfunc |
| 490 | |
| 491 | " Test vim.line() |
| 492 | func Test_line() |
| 493 | new |
| 494 | call setline(1, ['first line', 'second line']) |
| 495 | 1 |
| 496 | call assert_equal('first line', luaeval('vim.line()')) |
| 497 | 2 |
| 498 | call assert_equal('second line', luaeval('vim.line()')) |
| 499 | bwipe! |
| 500 | endfunc |
| 501 | |
| 502 | " Test vim.beep() |
| 503 | func Test_beep() |
| 504 | call assert_beeps('lua vim.beep()') |
| 505 | endfunc |
| 506 | |
| 507 | " Test errors in luaeval() |
| 508 | func Test_luaeval_error() |
| 509 | " Compile error |
| 510 | call assert_fails("call luaeval('-nil')", |
| 511 | \ '[string "luaeval"]:1: attempt to perform arithmetic on a nil value') |
| 512 | call assert_fails("call luaeval(']')", |
| 513 | \ "[string \"luaeval\"]:1: unexpected symbol near ']'") |
| 514 | endfunc |
| 515 | |
| 516 | " Test :luafile foo.lua |
| 517 | func Test_luafile() |
| 518 | call delete('Xlua_file') |
| 519 | call writefile(["str = 'hello'", "num = 123.0" ], 'Xlua_file') |
| 520 | call setfperm('Xlua_file', 'r-xr-xr-x') |
| 521 | |
| 522 | luafile Xlua_file |
| 523 | call assert_equal('hello', luaeval('str')) |
| 524 | call assert_equal(123.0, luaeval('num')) |
| 525 | |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 526 | lua str, num = nil |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 527 | call delete('Xlua_file') |
| 528 | endfunc |
| 529 | |
| 530 | " Test :luafile % |
| 531 | func Test_luafile_percent() |
| 532 | new Xlua_file |
| 533 | append |
| 534 | str, num = 'foo', 321.0 |
| 535 | print(string.format('str=%s, num=%d', str, num)) |
| 536 | . |
| 537 | w! |
| 538 | luafile % |
| 539 | let msg = split(execute('message'), "\n")[-1] |
| 540 | call assert_equal('str=foo, num=321', msg) |
| 541 | |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 542 | lua str, num = nil |
| 543 | call delete('Xlua_file') |
| 544 | bwipe! |
| 545 | endfunc |
| 546 | |
| 547 | " Test :luafile with syntax error |
| 548 | func Test_luafile_error() |
| 549 | new Xlua_file |
| 550 | call writefile(['nil = 0' ], 'Xlua_file') |
| 551 | call setfperm('Xlua_file', 'r-xr-xr-x') |
| 552 | |
| 553 | call assert_fails('luafile Xlua_file', "Xlua_file:1: unexpected symbol near 'nil'") |
| 554 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 555 | call delete('Xlua_file') |
Bram Moolenaar | d58f03b | 2017-01-29 22:48:45 +0100 | [diff] [blame] | 556 | bwipe! |
| 557 | endfunc |
Bram Moolenaar | 5390144 | 2018-07-25 22:02:36 +0200 | [diff] [blame] | 558 | |
| 559 | func Test_set_cursor() |
| 560 | " Check that setting the cursor position works. |
| 561 | new |
| 562 | call setline(1, ['first line', 'second line']) |
| 563 | normal gg |
| 564 | lua << EOF |
| 565 | w = vim.window() |
| 566 | w.line = 1 |
| 567 | w.col = 5 |
| 568 | EOF |
| 569 | call assert_equal([1, 5], [line('.'), col('.')]) |
| 570 | |
| 571 | " Check that movement after setting cursor position keeps current column. |
| 572 | normal j |
| 573 | call assert_equal([2, 5], [line('.'), col('.')]) |
| 574 | endfunc |