comparison src/testdir/test_lua.vim @ 19249:2a017e9dc6da v8.2.0183

patch 8.2.0183: tests fail when the float feature is disabled Commit: https://github.com/vim/vim/commit/5feabe00c47fa66d5f4c95213f150488433f78e3 Author: Bram Moolenaar <Bram@vim.org> Date: Thu Jan 30 18:24:53 2020 +0100 patch 8.2.0183: tests fail when the float feature is disabled Problem: Tests fail when the float feature is disabled. Solution: Skip tests that don't work without float support.
author Bram Moolenaar <Bram@vim.org>
date Thu, 30 Jan 2020 18:30:04 +0100
parents 2e53305f2239
children 04ef2ccf2519
comparison
equal deleted inserted replaced
19248:5e803caf5e9c 19249:2a017e9dc6da
1 " Tests for Lua. 1 " Tests for Lua.
2 2
3 source check.vim 3 source check.vim
4 CheckFeature lua 4 CheckFeature lua
5 CheckFeature float
5 6
6 func TearDown() 7 func TearDown()
7 " Run garbage collection after each test to exercise luaV_setref(). 8 " Run garbage collection after each test to exercise luaV_setref().
8 call test_garbagecollect_now() 9 call test_garbagecollect_now()
9 endfunc 10 endfunc
10 11
11 " Check that switching to another buffer does not trigger ml_get error. 12 " Check that switching to another buffer does not trigger ml_get error.
12 func Test_command_new_no_ml_get_error() 13 func Test_lua_command_new_no_ml_get_error()
13 new 14 new
14 let wincount = winnr('$') 15 let wincount = winnr('$')
15 call setline(1, ['one', 'two', 'three']) 16 call setline(1, ['one', 'two', 'three'])
16 luado vim.command("new") 17 luado vim.command("new")
17 call assert_equal(wincount + 1, winnr('$')) 18 call assert_equal(wincount + 1, winnr('$'))
18 %bwipe! 19 %bwipe!
19 endfunc 20 endfunc
20 21
21 " Test vim.command() 22 " Test vim.command()
22 func Test_command() 23 func Test_lua_command()
23 new 24 new
24 call setline(1, ['one', 'two', 'three']) 25 call setline(1, ['one', 'two', 'three'])
25 luado vim.command("1,2d_") 26 luado vim.command("1,2d_")
26 call assert_equal(['three'], getline(1, '$')) 27 call assert_equal(['three'], getline(1, '$'))
27 bwipe! 28 bwipe!
28 endfunc 29 endfunc
29 30
30 " Test vim.eval() 31 " Test vim.eval()
31 func Test_eval() 32 func Test_lua_eval()
32 " lua.eval with a number 33 " lua.eval with a number
33 lua v = vim.eval('123') 34 lua v = vim.eval('123')
34 call assert_equal('number', luaeval('vim.type(v)')) 35 call assert_equal('number', luaeval('vim.type(v)'))
35 call assert_equal(123.0, luaeval('v')) 36 call assert_equal(123.0, luaeval('v'))
36 37
67 68
68 lua v = nil 69 lua v = nil
69 endfunc 70 endfunc
70 71
71 " Test vim.window() 72 " Test vim.window()
72 func Test_window() 73 func Test_lua_window()
73 e Xfoo2 74 e Xfoo2
74 new Xfoo1 75 new Xfoo1
75 76
76 " Window 1 (top window) contains Xfoo1 77 " Window 1 (top window) contains Xfoo1
77 " Window 2 (bottom window) contains Xfoo2 78 " Window 2 (bottom window) contains Xfoo2
83 84
84 %bwipe! 85 %bwipe!
85 endfunc 86 endfunc
86 87
87 " Test vim.window().height 88 " Test vim.window().height
88 func Test_window_height() 89 func Test_lua_window_height()
89 new 90 new
90 lua vim.window().height = 2 91 lua vim.window().height = 2
91 call assert_equal(2, winheight(0)) 92 call assert_equal(2, winheight(0))
92 lua vim.window().height = vim.window().height + 1 93 lua vim.window().height = vim.window().height + 1
93 call assert_equal(3, winheight(0)) 94 call assert_equal(3, winheight(0))
94 bwipe! 95 bwipe!
95 endfunc 96 endfunc
96 97
97 " Test vim.window().width 98 " Test vim.window().width
98 func Test_window_width() 99 func Test_lua_window_width()
99 vert new 100 vert new
100 lua vim.window().width = 2 101 lua vim.window().width = 2
101 call assert_equal(2, winwidth(0)) 102 call assert_equal(2, winwidth(0))
102 lua vim.window().width = vim.window().width + 1 103 lua vim.window().width = vim.window().width + 1
103 call assert_equal(3, winwidth(0)) 104 call assert_equal(3, winwidth(0))
104 bwipe! 105 bwipe!
105 endfunc 106 endfunc
106 107
107 " Test vim.window().line and vim.window.col 108 " Test vim.window().line and vim.window.col
108 func Test_window_line_col() 109 func Test_lua_window_line_col()
109 new 110 new
110 call setline(1, ['line1', 'line2', 'line3']) 111 call setline(1, ['line1', 'line2', 'line3'])
111 lua vim.window().line = 2 112 lua vim.window().line = 2
112 lua vim.window().col = 4 113 lua vim.window().col = 4
113 call assert_equal([0, 2, 4, 0], getpos('.')) 114 call assert_equal([0, 2, 4, 0], getpos('.'))
119 \ '[string "vim chunk"]:1: line out of range') 120 \ '[string "vim chunk"]:1: line out of range')
120 bwipe! 121 bwipe!
121 endfunc 122 endfunc
122 123
123 " Test setting the current window 124 " Test setting the current window
124 func Test_window_set_current() 125 func Test_lua_window_set_current()
125 new Xfoo1 126 new Xfoo1
126 lua w1 = vim.window() 127 lua w1 = vim.window()
127 new Xfoo2 128 new Xfoo2
128 lua w2 = vim.window() 129 lua w2 = vim.window()
129 130
136 lua w1, w2 = nil 137 lua w1, w2 = nil
137 %bwipe! 138 %bwipe!
138 endfunc 139 endfunc
139 140
140 " Test vim.window().buffer 141 " Test vim.window().buffer
141 func Test_window_buffer() 142 func Test_lua_window_buffer()
142 new Xfoo1 143 new Xfoo1
143 lua w1 = vim.window() 144 lua w1 = vim.window()
144 lua b1 = w1.buffer() 145 lua b1 = w1.buffer()
145 new Xfoo2 146 new Xfoo2
146 lua w2 = vim.window() 147 lua w2 = vim.window()
154 lua b1, b2, w1, w2 = nil 155 lua b1, b2, w1, w2 = nil
155 %bwipe! 156 %bwipe!
156 endfunc 157 endfunc
157 158
158 " Test vim.window():previous() and vim.window():next() 159 " Test vim.window():previous() and vim.window():next()
159 func Test_window_next_previous() 160 func Test_lua_window_next_previous()
160 new Xfoo1 161 new Xfoo1
161 new Xfoo2 162 new Xfoo2
162 new Xfoo3 163 new Xfoo3
163 wincmd j 164 wincmd j
164 165
168 169
169 %bwipe! 170 %bwipe!
170 endfunc 171 endfunc
171 172
172 " Test vim.window():isvalid() 173 " Test vim.window():isvalid()
173 func Test_window_isvalid() 174 func Test_lua_window_isvalid()
174 new Xfoo 175 new Xfoo
175 lua w = vim.window() 176 lua w = vim.window()
176 call assert_true(luaeval('w:isvalid()')) 177 call assert_true(luaeval('w:isvalid()'))
177 178
178 " FIXME: how to test the case when isvalid() returns v:false? 179 " FIXME: how to test the case when isvalid() returns v:false?
181 lua w = nil 182 lua w = nil
182 bwipe! 183 bwipe!
183 endfunc 184 endfunc
184 185
185 " Test vim.buffer() with and without argument 186 " Test vim.buffer() with and without argument
186 func Test_buffer() 187 func Test_lua_buffer()
187 new Xfoo1 188 new Xfoo1
188 let bn1 = bufnr('%') 189 let bn1 = bufnr('%')
189 new Xfoo2 190 new Xfoo2
190 let bn2 = bufnr('%') 191 let bn2 = bufnr('%')
191 192
203 lua bn1, bn2 = nil 204 lua bn1, bn2 = nil
204 %bwipe! 205 %bwipe!
205 endfunc 206 endfunc
206 207
207 " Test vim.buffer().name and vim.buffer().fname 208 " Test vim.buffer().name and vim.buffer().fname
208 func Test_buffer_name() 209 func Test_lua_buffer_name()
209 new 210 new
210 call assert_equal('', luaeval('vim.buffer().name')) 211 call assert_equal('', luaeval('vim.buffer().name'))
211 call assert_equal('', luaeval('vim.buffer().fname')) 212 call assert_equal('', luaeval('vim.buffer().fname'))
212 bwipe! 213 bwipe!
213 214
216 call assert_equal(expand('%:p'), luaeval('vim.buffer().fname')) 217 call assert_equal(expand('%:p'), luaeval('vim.buffer().fname'))
217 bwipe! 218 bwipe!
218 endfunc 219 endfunc
219 220
220 " Test vim.buffer().number 221 " Test vim.buffer().number
221 func Test_buffer_number() 222 func Test_lua_buffer_number()
222 " All numbers in Lua are floating points number (no integers). 223 " All numbers in Lua are floating points number (no integers).
223 call assert_equal(bufnr('%'), float2nr(luaeval('vim.buffer().number'))) 224 call assert_equal(bufnr('%'), float2nr(luaeval('vim.buffer().number')))
224 endfunc 225 endfunc
225 226
226 " Test inserting lines in buffer. 227 " Test inserting lines in buffer.
227 func Test_buffer_insert() 228 func Test_lua_buffer_insert()
228 new 229 new
229 lua vim.buffer()[1] = '3' 230 lua vim.buffer()[1] = '3'
230 lua vim.buffer():insert('1', 0) 231 lua vim.buffer():insert('1', 0)
231 lua vim.buffer():insert('2', 1) 232 lua vim.buffer():insert('2', 1)
232 lua vim.buffer():insert('4', 10) 233 lua vim.buffer():insert('4', 10)
234 call assert_equal(['1', '2', '3', '4'], getline(1, '$')) 235 call assert_equal(['1', '2', '3', '4'], getline(1, '$'))
235 bwipe! 236 bwipe!
236 endfunc 237 endfunc
237 238
238 " Test deleting line in buffer 239 " Test deleting line in buffer
239 func Test_buffer_delete() 240 func Test_lua_buffer_delete()
240 new 241 new
241 call setline(1, ['1', '2', '3']) 242 call setline(1, ['1', '2', '3'])
242 lua vim.buffer()[2] = nil 243 lua vim.buffer()[2] = nil
243 call assert_equal(['1', '3'], getline(1, '$')) 244 call assert_equal(['1', '3'], getline(1, '$'))
244 245
246 \ '[string "vim chunk"]:1: invalid line number') 247 \ '[string "vim chunk"]:1: invalid line number')
247 bwipe! 248 bwipe!
248 endfunc 249 endfunc
249 250
250 " Test #vim.buffer() i.e. number of lines in buffer 251 " Test #vim.buffer() i.e. number of lines in buffer
251 func Test_buffer_number_lines() 252 func Test_lua_buffer_number_lines()
252 new 253 new
253 call setline(1, ['a', 'b', 'c']) 254 call setline(1, ['a', 'b', 'c'])
254 call assert_equal(3.0, luaeval('#vim.buffer()')) 255 call assert_equal(3.0, luaeval('#vim.buffer()'))
255 bwipe! 256 bwipe!
256 endfunc 257 endfunc
257 258
258 " Test vim.buffer():next() and vim.buffer():previous() 259 " Test vim.buffer():next() and vim.buffer():previous()
259 " Note that these functions get the next or previous buffers 260 " Note that these functions get the next or previous buffers
260 " but do not switch buffer. 261 " but do not switch buffer.
261 func Test_buffer_next_previous() 262 func Test_lua_buffer_next_previous()
262 new Xfoo1 263 new Xfoo1
263 new Xfoo2 264 new Xfoo2
264 new Xfoo3 265 new Xfoo3
265 b Xfoo2 266 b Xfoo2
266 267
284 lua bn, bp = nil 285 lua bn, bp = nil
285 %bwipe! 286 %bwipe!
286 endfunc 287 endfunc
287 288
288 " Test vim.buffer():isvalid() 289 " Test vim.buffer():isvalid()
289 func Test_buffer_isvalid() 290 func Test_lua_buffer_isvalid()
290 new Xfoo 291 new Xfoo
291 lua b = vim.buffer() 292 lua b = vim.buffer()
292 call assert_true(luaeval('b:isvalid()')) 293 call assert_true(luaeval('b:isvalid()'))
293 294
294 " FIXME: how to test the case when isvalid() returns v:false? 295 " FIXME: how to test the case when isvalid() returns v:false?
296 297
297 lua b = nil 298 lua b = nil
298 bwipe! 299 bwipe!
299 endfunc 300 endfunc
300 301
301 func Test_list() 302 func Test_lua_list()
302 call assert_equal([], luaeval('vim.list()')) 303 call assert_equal([], luaeval('vim.list()'))
303 304
304 let l = [] 305 let l = []
305 lua l = vim.eval('l') 306 lua l = vim.eval('l')
306 lua l:add(123) 307 lua l:add(123)
324 call assert_fails('lua l:add("x")', '[string "vim chunk"]:1: list is locked') 325 call assert_fails('lua l:add("x")', '[string "vim chunk"]:1: list is locked')
325 326
326 lua l = nil 327 lua l = nil
327 endfunc 328 endfunc
328 329
329 func Test_list_table() 330 func Test_lua_list_table()
330 " See :help lua-vim 331 " See :help lua-vim
331 " Non-numeric keys should not be used to initialize the list 332 " Non-numeric keys should not be used to initialize the list
332 " so say = 'hi' should be ignored. 333 " so say = 'hi' should be ignored.
333 lua t = {3.14, 'hello', false, true, say = 'hi'} 334 lua t = {3.14, 'hello', false, true, say = 'hi'}
334 call assert_equal([3.14, 'hello', v:false, v:true], luaeval('vim.list(t)')) 335 call assert_equal([3.14, 'hello', v:false, v:true], luaeval('vim.list(t)'))
339 call assert_fails('lua vim.list(print)', '[string "vim chunk"]:1: table expected, got function') 340 call assert_fails('lua vim.list(print)', '[string "vim chunk"]:1: table expected, got function')
340 call assert_fails('lua vim.list(true)', '[string "vim chunk"]:1: table expected, got boolean') 341 call assert_fails('lua vim.list(true)', '[string "vim chunk"]:1: table expected, got boolean')
341 endfunc 342 endfunc
342 343
343 " Test l() i.e. iterator on list 344 " Test l() i.e. iterator on list
344 func Test_list_iter() 345 func Test_lua_list_iter()
345 lua l = vim.list():add('foo'):add('bar') 346 lua l = vim.list():add('foo'):add('bar')
346 lua str = '' 347 lua str = ''
347 lua for v in l() do str = str .. v end 348 lua for v in l() do str = str .. v end
348 call assert_equal('foobar', luaeval('str')) 349 call assert_equal('foobar', luaeval('str'))
349 350
350 lua str, l = nil 351 lua str, l = nil
351 endfunc 352 endfunc
352 353
353 func Test_recursive_list() 354 func Test_lua_recursive_list()
354 lua l = vim.list():add(1):add(2) 355 lua l = vim.list():add(1):add(2)
355 lua l = l:add(l) 356 lua l = l:add(l)
356 357
357 call assert_equal(1.0, luaeval('l[0]')) 358 call assert_equal(1.0, luaeval('l[0]'))
358 call assert_equal(2.0, luaeval('l[1]')) 359 call assert_equal(2.0, luaeval('l[1]'))
372 call assert_equal(luaeval('l'), luaeval('l[2][2]')) 373 call assert_equal(luaeval('l'), luaeval('l[2][2]'))
373 374
374 lua l = nil 375 lua l = nil
375 endfunc 376 endfunc
376 377
377 func Test_dict() 378 func Test_lua_dict()
378 call assert_equal({}, luaeval('vim.dict()')) 379 call assert_equal({}, luaeval('vim.dict()'))
379 380
380 let d = {} 381 let d = {}
381 lua d = vim.eval('d') 382 lua d = vim.eval('d')
382 lua d[0] = 123 383 lua d[0] = 123
399 call assert_fails('lua d[6] = 1', '[string "vim chunk"]:1: dict is locked') 400 call assert_fails('lua d[6] = 1', '[string "vim chunk"]:1: dict is locked')
400 401
401 lua d = nil 402 lua d = nil
402 endfunc 403 endfunc
403 404
404 func Test_dict_table() 405 func Test_lua_dict_table()
405 lua t = {key1 = 'x', key2 = 3.14, key3 = true, key4 = false} 406 lua t = {key1 = 'x', key2 = 3.14, key3 = true, key4 = false}
406 call assert_equal({'key1': 'x', 'key2': 3.14, 'key3': v:true, 'key4': v:false}, 407 call assert_equal({'key1': 'x', 'key2': 3.14, 'key3': v:true, 'key4': v:false},
407 \ luaeval('vim.dict(t)')) 408 \ luaeval('vim.dict(t)'))
408 409
409 " Same example as in :help lua-vim. 410 " Same example as in :help lua-vim.
420 call assert_fails('lua vim.dict(print)', '[string "vim chunk"]:1: table expected, got function') 421 call assert_fails('lua vim.dict(print)', '[string "vim chunk"]:1: table expected, got function')
421 call assert_fails('lua vim.dict(true)', '[string "vim chunk"]:1: table expected, got boolean') 422 call assert_fails('lua vim.dict(true)', '[string "vim chunk"]:1: table expected, got boolean')
422 endfunc 423 endfunc
423 424
424 " Test d() i.e. iterator on dictionary 425 " Test d() i.e. iterator on dictionary
425 func Test_dict_iter() 426 func Test_lua_dict_iter()
426 let d = {'a': 1, 'b':2} 427 let d = {'a': 1, 'b':2}
427 lua d = vim.eval('d') 428 lua d = vim.eval('d')
428 lua str = '' 429 lua str = ''
429 lua for k,v in d() do str = str .. k ..':' .. v .. ',' end 430 lua for k,v in d() do str = str .. k ..':' .. v .. ',' end
430 call assert_equal('a:1,b:2,', luaeval('str')) 431 call assert_equal('a:1,b:2,', luaeval('str'))
431 432
432 lua str, d = nil 433 lua str, d = nil
433 endfunc 434 endfunc
434 435
435 func Test_blob() 436 func Test_lua_blob()
436 call assert_equal(0z, luaeval('vim.blob("")')) 437 call assert_equal(0z, luaeval('vim.blob("")'))
437 call assert_equal(0z31326162, luaeval('vim.blob("12ab")')) 438 call assert_equal(0z31326162, luaeval('vim.blob("12ab")'))
438 call assert_equal(0z00010203, luaeval('vim.blob("\x00\x01\x02\x03")')) 439 call assert_equal(0z00010203, luaeval('vim.blob("\x00\x01\x02\x03")'))
439 call assert_equal(0z8081FEFF, luaeval('vim.blob("\x80\x81\xfe\xff")')) 440 call assert_equal(0z8081FEFF, luaeval('vim.blob("\x80\x81\xfe\xff")'))
440 441
454 call assert_fails('lua b:add(true)', '[string "vim chunk"]:1: string expected, got boolean') 455 call assert_fails('lua b:add(true)', '[string "vim chunk"]:1: string expected, got boolean')
455 call assert_fails('lua b:add({})', '[string "vim chunk"]:1: string expected, got table') 456 call assert_fails('lua b:add({})', '[string "vim chunk"]:1: string expected, got table')
456 lua b = nil 457 lua b = nil
457 endfunc 458 endfunc
458 459
459 func Test_funcref() 460 func Test_lua_funcref()
460 function I(x) 461 function I(x)
461 return a:x 462 return a:x
462 endfunction 463 endfunction
463 let R = function('I') 464 let R = function('I')
464 lua i1 = vim.funcref"I" 465 lua i1 = vim.funcref"I"
481 482
482 lua i1, i2, msg, d, res = nil 483 lua i1, i2, msg, d, res = nil
483 endfunc 484 endfunc
484 485
485 " Test vim.type() 486 " Test vim.type()
486 func Test_type() 487 func Test_lua_type()
487 " The following values are identical to Lua's type function. 488 " The following values are identical to Lua's type function.
488 call assert_equal('string', luaeval('vim.type("foo")')) 489 call assert_equal('string', luaeval('vim.type("foo")'))
489 call assert_equal('number', luaeval('vim.type(1)')) 490 call assert_equal('number', luaeval('vim.type(1)'))
490 call assert_equal('number', luaeval('vim.type(1.2)')) 491 call assert_equal('number', luaeval('vim.type(1.2)'))
491 call assert_equal('function', luaeval('vim.type(print)')) 492 call assert_equal('function', luaeval('vim.type(print)'))
501 call assert_equal('dict', luaeval('vim.type(vim.dict())')) 502 call assert_equal('dict', luaeval('vim.type(vim.dict())'))
502 call assert_equal('funcref', luaeval('vim.type(vim.funcref("Test_type"))')) 503 call assert_equal('funcref', luaeval('vim.type(vim.funcref("Test_type"))'))
503 endfunc 504 endfunc
504 505
505 " Test vim.open() 506 " Test vim.open()
506 func Test_open() 507 func Test_lua_open()
507 call assert_notmatch('XOpen', execute('ls')) 508 call assert_notmatch('XOpen', execute('ls'))
508 509
509 " Open a buffer XOpen1, but do not jump to it. 510 " Open a buffer XOpen1, but do not jump to it.
510 lua b = vim.open('XOpen1') 511 lua b = vim.open('XOpen1')
511 call assert_equal('XOpen1', luaeval('b.name')) 512 call assert_equal('XOpen1', luaeval('b.name'))
522 lua b = nil 523 lua b = nil
523 %bwipe! 524 %bwipe!
524 endfunc 525 endfunc
525 526
526 " Test vim.line() 527 " Test vim.line()
527 func Test_line() 528 func Test_lua_line()
528 new 529 new
529 call setline(1, ['first line', 'second line']) 530 call setline(1, ['first line', 'second line'])
530 1 531 1
531 call assert_equal('first line', luaeval('vim.line()')) 532 call assert_equal('first line', luaeval('vim.line()'))
532 2 533 2
533 call assert_equal('second line', luaeval('vim.line()')) 534 call assert_equal('second line', luaeval('vim.line()'))
534 bwipe! 535 bwipe!
535 endfunc 536 endfunc
536 537
537 " Test vim.beep() 538 " Test vim.beep()
538 func Test_beep() 539 func Test_lua_beep()
539 call assert_beeps('lua vim.beep()') 540 call assert_beeps('lua vim.beep()')
540 endfunc 541 endfunc
541 542
542 " Test errors in luaeval() 543 " Test errors in luaeval()
543 func Test_luaeval_error() 544 func Test_luaeval_error()
589 590
590 call delete('Xlua_file') 591 call delete('Xlua_file')
591 bwipe! 592 bwipe!
592 endfunc 593 endfunc
593 594
594 func Test_set_cursor() 595 func Test_lua_set_cursor()
595 " Check that setting the cursor position works. 596 " Check that setting the cursor position works.
596 new 597 new
597 call setline(1, ['first line', 'second line']) 598 call setline(1, ['first line', 'second line'])
598 normal gg 599 normal gg
599 lua << EOF 600 lua << EOF