blob: f6cdae18d6e378488eb72c1b2f5ba3f359289046 [file] [log] [blame]
Bram Moolenaard58f03b2017-01-29 22:48:45 +01001" Tests for Lua.
Bram Moolenaard58f03b2017-01-29 22:48:45 +01002
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02003source check.vim
Bram Moolenaarc8970b92020-10-26 20:18:08 +01004
5" This test also works without the lua feature.
6func Test_skip_lua()
7 if 0
8 lua print("Not executed")
9 endif
10endfunc
11
Bram Moolenaarb46fecd2019-06-15 17:58:09 +020012CheckFeature lua
Bram Moolenaar5feabe02020-01-30 18:24:53 +010013CheckFeature float
Bram Moolenaard58f03b2017-01-29 22:48:45 +010014
Bram Moolenaarf65ed862021-04-03 14:13:33 +020015" Depending on the lua version, the error messages are different.
16let s:luaver = split(split(systemlist('lua -v')[0], ' ')[1], '\.')
Bram Moolenaare49b8e82020-07-01 13:52:55 +020017let s:major = str2nr(s:luaver[0])
18let s:minor = str2nr(s:luaver[1])
Bram Moolenaarf65ed862021-04-03 14:13:33 +020019let s:patch = str2nr(s:luaver[2])
20let s:lua_53_or_later = 0
21let s:lua_543_or_later = 0
22if (s:major == 5 && s:minor >= 3) || s:major > 5
Bram Moolenaare49b8e82020-07-01 13:52:55 +020023 let s:lua_53_or_later = 1
Bram Moolenaarf65ed862021-04-03 14:13:33 +020024 if (s:major == 5
25 \ && ((s:minor == 4 && s:patch >= 3) || s:minor > 4))
26 \ || s:major > 5
27 let s:lua_543_or_later = 1
28 endif
Bram Moolenaare49b8e82020-07-01 13:52:55 +020029endif
30
Bram Moolenaare165f632019-03-10 09:48:59 +010031func TearDown()
32 " Run garbage collection after each test to exercise luaV_setref().
33 call test_garbagecollect_now()
34endfunc
35
Bram Moolenaar4ff48142018-06-30 21:50:25 +020036" Check that switching to another buffer does not trigger ml_get error.
Bram Moolenaar5feabe02020-01-30 18:24:53 +010037func Test_lua_command_new_no_ml_get_error()
Bram Moolenaard58f03b2017-01-29 22:48:45 +010038 new
39 let wincount = winnr('$')
40 call setline(1, ['one', 'two', 'three'])
41 luado vim.command("new")
42 call assert_equal(wincount + 1, winnr('$'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +020043 %bwipe!
44endfunc
45
46" Test vim.command()
Bram Moolenaar5feabe02020-01-30 18:24:53 +010047func Test_lua_command()
Bram Moolenaar4ff48142018-06-30 21:50:25 +020048 new
49 call setline(1, ['one', 'two', 'three'])
50 luado vim.command("1,2d_")
51 call assert_equal(['three'], getline(1, '$'))
Bram Moolenaard58f03b2017-01-29 22:48:45 +010052 bwipe!
Bram Moolenaar4ff48142018-06-30 21:50:25 +020053endfunc
54
Bram Moolenaare49b8e82020-07-01 13:52:55 +020055func Test_lua_luado()
56 new
57 call setline(1, ['one', 'two'])
58 luado return(linenr)
59 call assert_equal(['1', '2'], getline(1, '$'))
60 close!
61
62 " Error cases
63 call assert_fails('luado string.format()',
64 \ "[string \"vim chunk\"]:1: bad argument #1 to 'format' (string expected, got no value)")
Bram Moolenaarf65ed862021-04-03 14:13:33 +020065 if s:lua_543_or_later
66 let msg = "[string \"vim chunk\"]:1: global 'func' is not callable (a nil value)"
67 elseif s:lua_53_or_later
68 let msg = "[string \"vim chunk\"]:1: attempt to call a nil value (global 'func')"
69 else
70 let msg = "[string \"vim chunk\"]:1: attempt to call global 'func' (a nil value)"
71 endif
72 call assert_fails('luado func()', msg)
Bram Moolenaare49b8e82020-07-01 13:52:55 +020073 call assert_fails('luado error("failed")', "[string \"vim chunk\"]:1: failed")
74endfunc
75
Bram Moolenaar4ff48142018-06-30 21:50:25 +020076" Test vim.eval()
Bram Moolenaar5feabe02020-01-30 18:24:53 +010077func Test_lua_eval()
Bram Moolenaar4ff48142018-06-30 21:50:25 +020078 " lua.eval with a number
79 lua v = vim.eval('123')
80 call assert_equal('number', luaeval('vim.type(v)'))
Bram Moolenaareb04f082020-05-17 14:32:35 +020081 call assert_equal(123, luaeval('v'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +020082
83 " lua.eval with a string
84 lua v = vim.eval('"abc"')
Bram Moolenaar02b31112019-08-31 22:16:38 +020085 call assert_equal('string', 'vim.type(v)'->luaeval())
Bram Moolenaar4ff48142018-06-30 21:50:25 +020086 call assert_equal('abc', luaeval('v'))
87
88 " lua.eval with a list
89 lua v = vim.eval("['a']")
90 call assert_equal('list', luaeval('vim.type(v)'))
91 call assert_equal(['a'], luaeval('v'))
92
93 " lua.eval with a dict
94 lua v = vim.eval("{'a':'b'}")
95 call assert_equal('dict', luaeval('vim.type(v)'))
96 call assert_equal({'a':'b'}, luaeval('v'))
97
Bram Moolenaarb7828692019-03-23 13:57:02 +010098 " lua.eval with a blob
99 lua v = vim.eval("0z00112233.deadbeef")
100 call assert_equal('blob', luaeval('vim.type(v)'))
101 call assert_equal(0z00112233.deadbeef, luaeval('v'))
102
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200103 " lua.eval with a float
104 lua v = vim.eval('3.14')
105 call assert_equal('number', luaeval('vim.type(v)'))
106 call assert_equal(3.14, luaeval('v'))
107
108 " lua.eval with a bool
109 lua v = vim.eval('v:true')
110 call assert_equal('number', luaeval('vim.type(v)'))
111 call assert_equal(1, luaeval('v'))
112 lua v = vim.eval('v:false')
113 call assert_equal('number', luaeval('vim.type(v)'))
114 call assert_equal(0, luaeval('v'))
115
116 " lua.eval with a null
117 lua v = vim.eval('v:null')
118 call assert_equal('nil', luaeval('vim.type(v)'))
119 call assert_equal(v:null, luaeval('v'))
120
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200121 call assert_fails('lua v = vim.eval(nil)',
122 \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got nil)")
123 call assert_fails('lua v = vim.eval(true)',
124 \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got boolean)")
125 call assert_fails('lua v = vim.eval({})',
126 \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got table)")
127 call assert_fails('lua v = vim.eval(print)',
128 \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got function)")
129 call assert_fails('lua v = vim.eval(vim.buffer())',
130 \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got userdata)")
131
132 lua v = nil
133endfunc
134
Bram Moolenaar86c3a212021-03-08 19:50:24 +0100135" Test luaeval() with lambda
136func Test_luaeval_with_lambda()
137 lua function hello_luaeval_lambda(a, cb) return a .. cb() end
138 call assert_equal('helloworld',
139 \ luaeval('hello_luaeval_lambda(_A[1], _A[2])',
140 \ ['hello', {->'world'}]))
141 lua hello_luaeval_lambda = nil
142endfunc
143
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200144" Test vim.window()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100145func Test_lua_window()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200146 e Xfoo2
147 new Xfoo1
148
149 " Window 1 (top window) contains Xfoo1
150 " Window 2 (bottom window) contains Xfoo2
151 call assert_equal('Xfoo1', luaeval('vim.window(1):buffer().name'))
152 call assert_equal('Xfoo2', luaeval('vim.window(2):buffer().name'))
153
154 " Window 3 does not exist so vim.window(3) should return nil
155 call assert_equal('nil', luaeval('tostring(vim.window(3))'))
156
Bram Moolenaarf65ed862021-04-03 14:13:33 +0200157 if s:lua_543_or_later
158 let msg = "[string \"luaeval\"]:1: field 'xyz' is not callable (a nil value)"
159 elseif s:lua_53_or_later
160 let msg = "[string \"luaeval\"]:1: attempt to call a nil value (field 'xyz')"
161 else
162 let msg = "[string \"luaeval\"]:1: attempt to call field 'xyz' (a nil value)"
163 endif
164 call assert_fails("let n = luaeval('vim.window().xyz()')", msg)
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200165 call assert_fails('lua vim.window().xyz = 1',
166 \ "[string \"vim chunk\"]:1: invalid window property: `xyz'")
167
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200168 %bwipe!
169endfunc
170
171" Test vim.window().height
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100172func Test_lua_window_height()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200173 new
174 lua vim.window().height = 2
175 call assert_equal(2, winheight(0))
176 lua vim.window().height = vim.window().height + 1
177 call assert_equal(3, winheight(0))
178 bwipe!
179endfunc
180
181" Test vim.window().width
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100182func Test_lua_window_width()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200183 vert new
184 lua vim.window().width = 2
185 call assert_equal(2, winwidth(0))
186 lua vim.window().width = vim.window().width + 1
187 call assert_equal(3, winwidth(0))
188 bwipe!
189endfunc
190
191" Test vim.window().line and vim.window.col
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100192func Test_lua_window_line_col()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200193 new
194 call setline(1, ['line1', 'line2', 'line3'])
195 lua vim.window().line = 2
196 lua vim.window().col = 4
197 call assert_equal([0, 2, 4, 0], getpos('.'))
198 lua vim.window().line = vim.window().line + 1
199 lua vim.window().col = vim.window().col - 1
200 call assert_equal([0, 3, 3, 0], getpos('.'))
201
202 call assert_fails('lua vim.window().line = 10',
203 \ '[string "vim chunk"]:1: line out of range')
204 bwipe!
205endfunc
206
Bram Moolenaareb04f082020-05-17 14:32:35 +0200207" Test vim.call
208func Test_lua_call()
209 call assert_equal(has('lua'), luaeval('vim.call("has", "lua")'))
210 call assert_equal(printf("Hello %s", "vim"), luaeval('vim.call("printf", "Hello %s", "vim")'))
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200211
212 " Error cases
213 call assert_fails("call luaeval('vim.call(\"min\", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21)')",
Bram Moolenaarb898a022020-07-12 18:33:53 +0200214 \ s:lua_53_or_later
215 \ ? '[string "luaeval"]:1: Function called with too many arguments'
216 \ : 'Function called with too many arguments')
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200217 lua co = coroutine.create(function () print("hi") end)
218 call assert_fails("call luaeval('vim.call(\"type\", co)')",
Bram Moolenaarb898a022020-07-12 18:33:53 +0200219 \ s:lua_53_or_later
220 \ ? '[string "luaeval"]:1: lua: cannot convert value'
221 \ : 'lua: cannot convert value')
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200222 lua co = nil
Bram Moolenaarb898a022020-07-12 18:33:53 +0200223 call assert_fails("call luaeval('vim.call(\"abc\")')",
224 \ ['E117:', s:lua_53_or_later ? '\[string "luaeval"]:1: lua: call_vim_function failed'
225 \ : 'lua: call_vim_function failed'])
Bram Moolenaareb04f082020-05-17 14:32:35 +0200226endfunc
227
228" Test vim.fn.*
229func Test_lua_fn()
230 call assert_equal(has('lua'), luaeval('vim.fn.has("lua")'))
231 call assert_equal(printf("Hello %s", "vim"), luaeval('vim.fn.printf("Hello %s", "vim")'))
232endfunc
233
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200234" Test setting the current window
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100235func Test_lua_window_set_current()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200236 new Xfoo1
237 lua w1 = vim.window()
238 new Xfoo2
239 lua w2 = vim.window()
240
241 call assert_equal('Xfoo2', bufname('%'))
242 lua w1()
243 call assert_equal('Xfoo1', bufname('%'))
244 lua w2()
245 call assert_equal('Xfoo2', bufname('%'))
246
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200247 lua w1, w2 = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200248 %bwipe!
249endfunc
250
251" Test vim.window().buffer
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100252func Test_lua_window_buffer()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200253 new Xfoo1
254 lua w1 = vim.window()
255 lua b1 = w1.buffer()
256 new Xfoo2
257 lua w2 = vim.window()
258 lua b2 = w2.buffer()
259
260 lua b1()
261 call assert_equal('Xfoo1', bufname('%'))
262 lua b2()
263 call assert_equal('Xfoo2', bufname('%'))
264
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200265 lua b1, b2, w1, w2 = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200266 %bwipe!
267endfunc
268
269" Test vim.window():previous() and vim.window():next()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100270func Test_lua_window_next_previous()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200271 new Xfoo1
272 new Xfoo2
273 new Xfoo3
274 wincmd j
275
276 call assert_equal('Xfoo2', luaeval('vim.window().buffer().name'))
277 call assert_equal('Xfoo1', luaeval('vim.window():next():buffer().name'))
278 call assert_equal('Xfoo3', luaeval('vim.window():previous():buffer().name'))
279
280 %bwipe!
281endfunc
282
283" Test vim.window():isvalid()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100284func Test_lua_window_isvalid()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200285 new Xfoo
286 lua w = vim.window()
287 call assert_true(luaeval('w:isvalid()'))
288
289 " FIXME: how to test the case when isvalid() returns v:false?
290 " isvalid() gives errors when the window is deleted. Is it a bug?
291
292 lua w = nil
293 bwipe!
294endfunc
295
296" Test vim.buffer() with and without argument
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100297func Test_lua_buffer()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200298 new Xfoo1
299 let bn1 = bufnr('%')
300 new Xfoo2
301 let bn2 = bufnr('%')
302
303 " Test vim.buffer() without argument.
304 call assert_equal('Xfoo2', luaeval("vim.buffer().name"))
305
306 " Test vim.buffer() with string argument.
307 call assert_equal('Xfoo1', luaeval("vim.buffer('Xfoo1').name"))
308 call assert_equal('Xfoo2', luaeval("vim.buffer('Xfoo2').name"))
309
310 " Test vim.buffer() with integer argument.
311 call assert_equal('Xfoo1', luaeval("vim.buffer(" . bn1 . ").name"))
312 call assert_equal('Xfoo2', luaeval("vim.buffer(" . bn2 . ").name"))
313
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200314 lua bn1, bn2 = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200315 %bwipe!
316endfunc
317
318" Test vim.buffer().name and vim.buffer().fname
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100319func Test_lua_buffer_name()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200320 new
Bram Moolenaarfe08df42018-07-07 23:07:41 +0200321 call assert_equal('', luaeval('vim.buffer().name'))
322 call assert_equal('', luaeval('vim.buffer().fname'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200323 bwipe!
324
325 new Xfoo
326 call assert_equal('Xfoo', luaeval('vim.buffer().name'))
327 call assert_equal(expand('%:p'), luaeval('vim.buffer().fname'))
328 bwipe!
329endfunc
330
331" Test vim.buffer().number
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100332func Test_lua_buffer_number()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200333 " All numbers in Lua are floating points number (no integers).
334 call assert_equal(bufnr('%'), float2nr(luaeval('vim.buffer().number')))
335endfunc
336
337" Test inserting lines in buffer.
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100338func Test_lua_buffer_insert()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200339 new
340 lua vim.buffer()[1] = '3'
341 lua vim.buffer():insert('1', 0)
342 lua vim.buffer():insert('2', 1)
343 lua vim.buffer():insert('4', 10)
344
345 call assert_equal(['1', '2', '3', '4'], getline(1, '$'))
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200346 call assert_equal('4', luaeval('vim.buffer()[4]'))
347 call assert_equal(v:null, luaeval('vim.buffer()[5]'))
348 call assert_equal(v:null, luaeval('vim.buffer()[{}]'))
Bram Moolenaarf65ed862021-04-03 14:13:33 +0200349 if s:lua_543_or_later
350 let msg = "[string \"vim chunk\"]:1: method 'xyz' is not callable (a nil value)"
351 elseif s:lua_53_or_later
352 let msg = "[string \"vim chunk\"]:1: attempt to call a nil value (method 'xyz')"
353 else
354 let msg = "[string \"vim chunk\"]:1: attempt to call method 'xyz' (a nil value)"
355 endif
356 call assert_fails('lua vim.buffer():xyz()', msg)
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200357 call assert_fails('lua vim.buffer()[1] = {}',
358 \ '[string "vim chunk"]:1: wrong argument to change')
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200359 bwipe!
360endfunc
361
362" Test deleting line in buffer
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100363func Test_lua_buffer_delete()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200364 new
365 call setline(1, ['1', '2', '3'])
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200366 call cursor(3, 1)
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200367 lua vim.buffer()[2] = nil
368 call assert_equal(['1', '3'], getline(1, '$'))
369
370 call assert_fails('lua vim.buffer()[3] = nil',
371 \ '[string "vim chunk"]:1: invalid line number')
372 bwipe!
373endfunc
374
375" Test #vim.buffer() i.e. number of lines in buffer
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100376func Test_lua_buffer_number_lines()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200377 new
378 call setline(1, ['a', 'b', 'c'])
Bram Moolenaareb04f082020-05-17 14:32:35 +0200379 call assert_equal(3, luaeval('#vim.buffer()'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200380 bwipe!
381endfunc
382
383" Test vim.buffer():next() and vim.buffer():previous()
384" Note that these functions get the next or previous buffers
385" but do not switch buffer.
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100386func Test_lua_buffer_next_previous()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200387 new Xfoo1
388 new Xfoo2
389 new Xfoo3
390 b Xfoo2
391
392 lua bn = vim.buffer():next()
393 lua bp = vim.buffer():previous()
394
395 call assert_equal('Xfoo2', luaeval('vim.buffer().name'))
396 call assert_equal('Xfoo1', luaeval('bp.name'))
397 call assert_equal('Xfoo3', luaeval('bn.name'))
398
399 call assert_equal('Xfoo2', bufname('%'))
400
401 lua bn()
402 call assert_equal('Xfoo3', luaeval('vim.buffer().name'))
403 call assert_equal('Xfoo3', bufname('%'))
404
405 lua bp()
406 call assert_equal('Xfoo1', luaeval('vim.buffer().name'))
407 call assert_equal('Xfoo1', bufname('%'))
408
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200409 lua bn, bp = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200410 %bwipe!
411endfunc
412
413" Test vim.buffer():isvalid()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100414func Test_lua_buffer_isvalid()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200415 new Xfoo
416 lua b = vim.buffer()
417 call assert_true(luaeval('b:isvalid()'))
418
419 " FIXME: how to test the case when isvalid() returns v:false?
420 " isvalid() gives errors when the buffer is wiped. Is it a bug?
421
422 lua b = nil
423 bwipe!
424endfunc
425
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100426func Test_lua_list()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200427 call assert_equal([], luaeval('vim.list()'))
428
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200429 let l = []
430 lua l = vim.eval('l')
431 lua l:add(123)
432 lua l:add('abc')
433 lua l:add(true)
434 lua l:add(false)
Bram Moolenaar9067cd62019-01-01 00:41:54 +0100435 lua l:add(nil)
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200436 lua l:add(vim.eval("[1, 2, 3]"))
437 lua l:add(vim.eval("{'a':1, 'b':2, 'c':3}"))
Bram Moolenaareb04f082020-05-17 14:32:35 +0200438 call assert_equal([123, 'abc', v:true, v:false, v:null, [1, 2, 3], {'a': 1, 'b': 2, 'c': 3}], l)
439 call assert_equal(7, luaeval('#l'))
Bram Moolenaara8a60d02018-07-02 22:54:36 +0200440 call assert_match('^list: \%(0x\)\?\x\+$', luaeval('tostring(l)'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200441
Bram Moolenaarbd846172020-06-27 12:32:57 +0200442 lua l[1] = 124
443 lua l[6] = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200444 lua l:insert('first')
445 lua l:insert('xx', 3)
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200446 call assert_fails('lua l:insert("xx", -20)',
447 \ '[string "vim chunk"]:1: invalid position')
Bram Moolenaareb04f082020-05-17 14:32:35 +0200448 call assert_equal(['first', 124, 'abc', 'xx', v:true, v:false, v:null, {'a': 1, 'b': 2, 'c': 3}], l)
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200449
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200450 lockvar 1 l
451 call assert_fails('lua l:add("x")', '[string "vim chunk"]:1: list is locked')
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200452 call assert_fails('lua l:insert(2)', '[string "vim chunk"]:1: list is locked')
453 call assert_fails('lua l[9] = 1', '[string "vim chunk"]:1: list is locked')
454
455 unlockvar l
456 let l = [1, 2]
457 lua ll = vim.eval('l')
458 let x = luaeval("ll[3]")
459 call assert_equal(v:null, x)
Bram Moolenaarf65ed862021-04-03 14:13:33 +0200460 if s:lua_543_or_later
461 let msg = "[string \"luaeval\"]:1: method 'xyz' is not callable (a nil value)"
462 elseif s:lua_53_or_later
463 let msg = "[string \"luaeval\"]:1: attempt to call a nil value (method 'xyz')"
464 else
465 let msg = "[string \"luaeval\"]:1: attempt to call method 'xyz' (a nil value)"
466 endif
467 call assert_fails('let x = luaeval("ll:xyz(3)")', msg)
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200468 let y = luaeval("ll[{}]")
469 call assert_equal(v:null, y)
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200470
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200471 lua l = nil
472endfunc
473
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100474func Test_lua_list_table()
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200475 " See :help lua-vim
476 " Non-numeric keys should not be used to initialize the list
477 " so say = 'hi' should be ignored.
478 lua t = {3.14, 'hello', false, true, say = 'hi'}
479 call assert_equal([3.14, 'hello', v:false, v:true], luaeval('vim.list(t)'))
480 lua t = nil
481
482 call assert_fails('lua vim.list(1)', '[string "vim chunk"]:1: table expected, got number')
483 call assert_fails('lua vim.list("x")', '[string "vim chunk"]:1: table expected, got string')
484 call assert_fails('lua vim.list(print)', '[string "vim chunk"]:1: table expected, got function')
485 call assert_fails('lua vim.list(true)', '[string "vim chunk"]:1: table expected, got boolean')
486endfunc
487
Bram Moolenaara1f9f862020-06-28 22:41:26 +0200488func Test_lua_list_table_insert_remove()
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200489 if !s:lua_53_or_later
Bram Moolenaara1f9f862020-06-28 22:41:26 +0200490 throw 'Skipped: Lua version < 5.3'
491 endif
492
Bram Moolenaarf65ed862021-04-03 14:13:33 +0200493 let l = [1, 2]
Bram Moolenaara1f9f862020-06-28 22:41:26 +0200494 lua t = vim.eval('l')
495 lua table.insert(t, 10)
496 lua t[#t + 1] = 20
497 lua table.insert(t, 2, 30)
498 call assert_equal(l, [1, 30, 2, 10, 20])
499 lua table.remove(t, 2)
500 call assert_equal(l, [1, 2, 10, 20])
501 lua t[3] = nil
502 call assert_equal(l, [1, 2, 20])
503 lua removed_value = table.remove(t, 3)
504 call assert_equal(luaeval('removed_value'), 20)
505 lua t = nil
506 lua removed_value = nil
507 unlet l
508endfunc
509
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200510" Test l() i.e. iterator on list
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100511func Test_lua_list_iter()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200512 lua l = vim.list():add('foo'):add('bar')
513 lua str = ''
514 lua for v in l() do str = str .. v end
515 call assert_equal('foobar', luaeval('str'))
516
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200517 lua str, l = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200518endfunc
519
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100520func Test_lua_recursive_list()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200521 lua l = vim.list():add(1):add(2)
522 lua l = l:add(l)
523
Bram Moolenaarbd846172020-06-27 12:32:57 +0200524 call assert_equal(1, luaeval('l[1]'))
525 call assert_equal(2, luaeval('l[2]'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200526
Bram Moolenaarbd846172020-06-27 12:32:57 +0200527 call assert_equal(1, luaeval('l[3][1]'))
528 call assert_equal(2, luaeval('l[3][2]'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200529
Bram Moolenaarbd846172020-06-27 12:32:57 +0200530 call assert_equal(1, luaeval('l[3][3][1]'))
531 call assert_equal(2, luaeval('l[3][3][2]'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200532
Bram Moolenaareb04f082020-05-17 14:32:35 +0200533 call assert_equal('[1, 2, [...]]', string(luaeval('l')))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200534
Bram Moolenaara8a60d02018-07-02 22:54:36 +0200535 call assert_match('^list: \%(0x\)\?\x\+$', luaeval('tostring(l)'))
Bram Moolenaarbd846172020-06-27 12:32:57 +0200536 call assert_equal(luaeval('tostring(l)'), luaeval('tostring(l[3])'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200537
Bram Moolenaarbd846172020-06-27 12:32:57 +0200538 call assert_equal(luaeval('l'), luaeval('l[3]'))
539 call assert_equal(luaeval('l'), luaeval('l[3][3]'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200540
541 lua l = nil
542endfunc
543
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100544func Test_lua_dict()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200545 call assert_equal({}, luaeval('vim.dict()'))
546
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200547 let d = {}
548 lua d = vim.eval('d')
549 lua d[0] = 123
550 lua d[1] = "abc"
551 lua d[2] = true
552 lua d[3] = false
553 lua d[4] = vim.eval("[1, 2, 3]")
554 lua d[5] = vim.eval("{'a':1, 'b':2, 'c':3}")
Bram Moolenaareb04f082020-05-17 14:32:35 +0200555 call assert_equal({'0':123, '1':'abc', '2':v:true, '3':v:false, '4': [1, 2, 3], '5': {'a':1, 'b':2, 'c':3}}, d)
556 call assert_equal(6, luaeval('#d'))
Bram Moolenaara8a60d02018-07-02 22:54:36 +0200557 call assert_match('^dict: \%(0x\)\?\x\+$', luaeval('tostring(d)'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200558
559 call assert_equal('abc', luaeval('d[1]'))
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200560 call assert_equal(v:null, luaeval('d[22]'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200561
562 lua d[0] = 124
563 lua d[4] = nil
Bram Moolenaareb04f082020-05-17 14:32:35 +0200564 call assert_equal({'0':124, '1':'abc', '2':v:true, '3':v:false, '5': {'a':1, 'b':2, 'c':3}}, d)
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200565
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200566 lockvar 1 d
567 call assert_fails('lua d[6] = 1', '[string "vim chunk"]:1: dict is locked')
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200568 unlockvar d
569
570 " Error case
571 lua d = {}
572 lua d[''] = 10
573 call assert_fails("let t = luaeval('vim.dict(d)')",
Bram Moolenaarb898a022020-07-12 18:33:53 +0200574 \ s:lua_53_or_later
575 \ ? '[string "luaeval"]:1: table has empty key'
576 \ : 'table has empty key')
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200577 let d = {}
578 lua x = vim.eval('d')
579 call assert_fails("lua x[''] = 10", '[string "vim chunk"]:1: empty key')
580 lua x['a'] = nil
581 call assert_equal({}, d)
582
583 " cannot assign funcrefs in the global scope
584 lua x = vim.eval('g:')
585 call assert_fails("lua x['min'] = vim.funcref('max')",
586 \ '[string "vim chunk"]:1: cannot assign funcref to builtin scope')
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200587
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200588 lua d = nil
589endfunc
590
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100591func Test_lua_dict_table()
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200592 lua t = {key1 = 'x', key2 = 3.14, key3 = true, key4 = false}
593 call assert_equal({'key1': 'x', 'key2': 3.14, 'key3': v:true, 'key4': v:false},
594 \ luaeval('vim.dict(t)'))
595
596 " Same example as in :help lua-vim.
597 lua t = {math.pi, false, say = 'hi'}
598 " FIXME: commented out as it currently does not work as documented:
599 " Expected {'say': 'hi'}
600 " but got {'1': 3.141593, '2': v:false, 'say': 'hi'}
601 " Is the documentation or the code wrong?
602 "call assert_equal({'say' : 'hi'}, luaeval('vim.dict(t)'))
603 lua t = nil
604
605 call assert_fails('lua vim.dict(1)', '[string "vim chunk"]:1: table expected, got number')
606 call assert_fails('lua vim.dict("x")', '[string "vim chunk"]:1: table expected, got string')
607 call assert_fails('lua vim.dict(print)', '[string "vim chunk"]:1: table expected, got function')
608 call assert_fails('lua vim.dict(true)', '[string "vim chunk"]:1: table expected, got boolean')
609endfunc
610
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200611" Test d() i.e. iterator on dictionary
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100612func Test_lua_dict_iter()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200613 let d = {'a': 1, 'b':2}
614 lua d = vim.eval('d')
615 lua str = ''
616 lua for k,v in d() do str = str .. k ..':' .. v .. ',' end
617 call assert_equal('a:1,b:2,', luaeval('str'))
618
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200619 lua str, d = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200620endfunc
621
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100622func Test_lua_blob()
Bram Moolenaarb7828692019-03-23 13:57:02 +0100623 call assert_equal(0z, luaeval('vim.blob("")'))
624 call assert_equal(0z31326162, luaeval('vim.blob("12ab")'))
625 call assert_equal(0z00010203, luaeval('vim.blob("\x00\x01\x02\x03")'))
626 call assert_equal(0z8081FEFF, luaeval('vim.blob("\x80\x81\xfe\xff")'))
627
628 lua b = vim.blob("\x00\x00\x00\x00")
629 call assert_equal(0z00000000, luaeval('b'))
Bram Moolenaareb04f082020-05-17 14:32:35 +0200630 call assert_equal(4, luaeval('#b'))
Bram Moolenaarb7828692019-03-23 13:57:02 +0100631 lua b[0], b[1], b[2], b[3] = 1, 32, 256, 0xff
632 call assert_equal(0z012000ff, luaeval('b'))
633 lua b[4] = string.byte("z", 1)
634 call assert_equal(0z012000ff.7a, luaeval('b'))
Bram Moolenaareb04f082020-05-17 14:32:35 +0200635 call assert_equal(5, luaeval('#b'))
Bram Moolenaarb7828692019-03-23 13:57:02 +0100636 call assert_fails('lua b[#b+1] = 0x80', '[string "vim chunk"]:1: index out of range')
637 lua b:add("12ab")
638 call assert_equal(0z012000ff.7a313261.62, luaeval('b'))
Bram Moolenaareb04f082020-05-17 14:32:35 +0200639 call assert_equal(9, luaeval('#b'))
Bram Moolenaarb7828692019-03-23 13:57:02 +0100640 call assert_fails('lua b:add(nil)', '[string "vim chunk"]:1: string expected, got nil')
641 call assert_fails('lua b:add(true)', '[string "vim chunk"]:1: string expected, got boolean')
642 call assert_fails('lua b:add({})', '[string "vim chunk"]:1: string expected, got table')
643 lua b = nil
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200644
645 let b = 0z0102
646 lua lb = vim.eval('b')
647 let n = luaeval('lb[1]')
648 call assert_equal(2, n)
649 let n = luaeval('lb[6]')
650 call assert_equal(v:null, n)
Bram Moolenaarf65ed862021-04-03 14:13:33 +0200651 if s:lua_543_or_later
652 let msg = "[string \"luaeval\"]:1: method 'xyz' is not callable (a nil value)"
653 elseif s:lua_53_or_later
654 let msg = "[string \"luaeval\"]:1: attempt to call a nil value (method 'xyz')"
655 else
656 let msg = "[string \"luaeval\"]:1: attempt to call method 'xyz' (a nil value)"
657 endif
658 call assert_fails('let x = luaeval("lb:xyz(3)")', msg)
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200659 let y = luaeval("lb[{}]")
660 call assert_equal(v:null, y)
661
662 lockvar b
663 call assert_fails('lua lb[1] = 2', '[string "vim chunk"]:1: blob is locked')
664 call assert_fails('lua lb:add("12")', '[string "vim chunk"]:1: blob is locked')
665
666 " Error cases
667 lua t = {}
668 call assert_fails('lua b = vim.blob(t)',
669 \ '[string "vim chunk"]:1: string expected, got table')
Bram Moolenaarb7828692019-03-23 13:57:02 +0100670endfunc
671
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100672func Test_lua_funcref()
Bram Moolenaarca06da92018-07-01 15:12:05 +0200673 function I(x)
674 return a:x
675 endfunction
676 let R = function('I')
677 lua i1 = vim.funcref"I"
678 lua i2 = vim.eval"R"
679 lua msg = "funcref|test|" .. (#i2(i1) == #i1(i2) and "OK" or "FAIL")
680 lua msg = vim.funcref"tr"(msg, "|", " ")
681 call assert_equal("funcref test OK", luaeval('msg'))
682
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200683 " Error cases
684 call assert_fails('lua f1 = vim.funcref("")',
685 \ '[string "vim chunk"]:1: invalid function name: ')
686 call assert_fails('lua f1 = vim.funcref("10")',
687 \ '[string "vim chunk"]:1: invalid function name: 10')
688 let fname = test_null_string()
689 call assert_fails('lua f1 = vim.funcref(fname)',
690 \ "[string \"vim chunk\"]:1: bad argument #1 to 'funcref' (string expected, got nil)")
691 call assert_fails('lua vim.funcref("abc")()',
Bram Moolenaarecdd14a2020-07-11 22:49:59 +0200692 \ ['E117:', '\[string "vim chunk"]:1: cannot call funcref'])
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200693
Bram Moolenaarca06da92018-07-01 15:12:05 +0200694 " dict funcref
695 function Mylen() dict
696 return len(self.data)
697 endfunction
698 let l = [0, 1, 2, 3]
699 let mydict = {'data': l}
700 lua d = vim.eval"mydict"
701 lua d.len = vim.funcref"Mylen" -- assign d as 'self'
702 lua res = (d.len() == vim.funcref"len"(vim.eval"l")) and "OK" or "FAIL"
703 call assert_equal("OK", luaeval('res'))
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100704 call assert_equal(function('Mylen', {'data': l, 'len': function('Mylen')}), mydict.len)
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200705
706 lua i1, i2, msg, d, res = nil
Bram Moolenaarca06da92018-07-01 15:12:05 +0200707endfunc
708
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200709" Test vim.type()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100710func Test_lua_type()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200711 " The following values are identical to Lua's type function.
712 call assert_equal('string', luaeval('vim.type("foo")'))
713 call assert_equal('number', luaeval('vim.type(1)'))
714 call assert_equal('number', luaeval('vim.type(1.2)'))
715 call assert_equal('function', luaeval('vim.type(print)'))
716 call assert_equal('table', luaeval('vim.type({})'))
717 call assert_equal('boolean', luaeval('vim.type(true)'))
718 call assert_equal('boolean', luaeval('vim.type(false)'))
719 call assert_equal('nil', luaeval('vim.type(nil)'))
720
721 " The following values are specific to Vim.
722 call assert_equal('window', luaeval('vim.type(vim.window())'))
723 call assert_equal('buffer', luaeval('vim.type(vim.buffer())'))
724 call assert_equal('list', luaeval('vim.type(vim.list())'))
725 call assert_equal('dict', luaeval('vim.type(vim.dict())'))
Bram Moolenaarca06da92018-07-01 15:12:05 +0200726 call assert_equal('funcref', luaeval('vim.type(vim.funcref("Test_type"))'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200727endfunc
728
729" Test vim.open()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100730func Test_lua_open()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200731 call assert_notmatch('XOpen', execute('ls'))
732
733 " Open a buffer XOpen1, but do not jump to it.
734 lua b = vim.open('XOpen1')
735 call assert_equal('XOpen1', luaeval('b.name'))
736 call assert_equal('', bufname('%'))
737
738 call assert_match('XOpen1', execute('ls'))
739 call assert_notequal('XOpen2', bufname('%'))
740
741 " Open a buffer XOpen2 and jump to it.
742 lua b = vim.open('XOpen2')()
743 call assert_equal('XOpen2', luaeval('b.name'))
744 call assert_equal('XOpen2', bufname('%'))
745
746 lua b = nil
747 %bwipe!
748endfunc
749
Bram Moolenaar788fbb42020-05-31 14:08:12 +0200750func Test_update_package_paths()
751 set runtimepath+=./testluaplugin
752 call assert_equal("hello from lua", luaeval("require('testluaplugin').hello()"))
753endfunc
754
Bram Moolenaar801ab062020-06-25 19:27:56 +0200755func Vim_func_call_lua_callback(Concat, Cb)
756 let l:message = a:Concat("hello", "vim")
757 call a:Cb(l:message)
758endfunc
759
760func Test_pass_lua_callback_to_vim_from_lua()
761 lua pass_lua_callback_to_vim_from_lua_result = ""
762 call assert_equal("", luaeval("pass_lua_callback_to_vim_from_lua_result"))
763 lua <<EOF
764 vim.funcref('Vim_func_call_lua_callback')(
765 function(greeting, message)
766 return greeting .. " " .. message
767 end,
768 function(message)
769 pass_lua_callback_to_vim_from_lua_result = message
770 end)
771EOF
772 call assert_equal("hello vim", luaeval("pass_lua_callback_to_vim_from_lua_result"))
773endfunc
774
775func Vim_func_call_metatable_lua_callback(Greet)
776 return a:Greet("world")
777endfunc
778
779func Test_pass_lua_metatable_callback_to_vim_from_lua()
780 let result = luaeval("vim.funcref('Vim_func_call_metatable_lua_callback')(setmetatable({ space = ' '}, { __call = function(tbl, msg) return 'hello' .. tbl.space .. msg end }) )")
781 call assert_equal("hello world", result)
782endfunc
783
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200784" Test vim.line()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100785func Test_lua_line()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200786 new
787 call setline(1, ['first line', 'second line'])
788 1
789 call assert_equal('first line', luaeval('vim.line()'))
790 2
791 call assert_equal('second line', luaeval('vim.line()'))
792 bwipe!
793endfunc
794
795" Test vim.beep()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100796func Test_lua_beep()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200797 call assert_beeps('lua vim.beep()')
798endfunc
799
800" Test errors in luaeval()
801func Test_luaeval_error()
802 " Compile error
803 call assert_fails("call luaeval('-nil')",
804 \ '[string "luaeval"]:1: attempt to perform arithmetic on a nil value')
805 call assert_fails("call luaeval(']')",
806 \ "[string \"luaeval\"]:1: unexpected symbol near ']'")
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200807 lua co = coroutine.create(function () print("hi") end)
808 call assert_fails('let i = luaeval("co")', 'luaeval: cannot convert value')
809 lua co = nil
810 call assert_fails('let m = luaeval("{}")', 'luaeval: cannot convert value')
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200811endfunc
812
813" Test :luafile foo.lua
814func Test_luafile()
815 call delete('Xlua_file')
Bram Moolenaareb04f082020-05-17 14:32:35 +0200816 call writefile(["str = 'hello'", "num = 123" ], 'Xlua_file')
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200817 call setfperm('Xlua_file', 'r-xr-xr-x')
818
819 luafile Xlua_file
820 call assert_equal('hello', luaeval('str'))
Bram Moolenaareb04f082020-05-17 14:32:35 +0200821 call assert_equal(123, luaeval('num'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200822
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200823 lua str, num = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200824 call delete('Xlua_file')
825endfunc
826
827" Test :luafile %
828func Test_luafile_percent()
829 new Xlua_file
830 append
831 str, num = 'foo', 321.0
832 print(string.format('str=%s, num=%d', str, num))
833.
834 w!
835 luafile %
836 let msg = split(execute('message'), "\n")[-1]
837 call assert_equal('str=foo, num=321', msg)
838
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200839 lua str, num = nil
840 call delete('Xlua_file')
841 bwipe!
842endfunc
843
844" Test :luafile with syntax error
845func Test_luafile_error()
846 new Xlua_file
847 call writefile(['nil = 0' ], 'Xlua_file')
848 call setfperm('Xlua_file', 'r-xr-xr-x')
849
850 call assert_fails('luafile Xlua_file', "Xlua_file:1: unexpected symbol near 'nil'")
851
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200852 call delete('Xlua_file')
Bram Moolenaard58f03b2017-01-29 22:48:45 +0100853 bwipe!
854endfunc
Bram Moolenaar53901442018-07-25 22:02:36 +0200855
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200856" Test for dealing with strings containing newlines and null character
857func Test_lua_string_with_newline()
858 let x = execute('lua print("Hello\nWorld")')
859 call assert_equal("\nHello\nWorld", x)
860 new
861 lua k = vim.buffer(vim.eval('bufnr()'))
862 lua k:insert("Hello\0World", 0)
863 call assert_equal(["Hello\nWorld", ''], getline(1, '$'))
864 close!
865endfunc
866
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100867func Test_lua_set_cursor()
Bram Moolenaar53901442018-07-25 22:02:36 +0200868 " Check that setting the cursor position works.
869 new
870 call setline(1, ['first line', 'second line'])
871 normal gg
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200872 lua << trim EOF
873 w = vim.window()
874 w.line = 1
875 w.col = 5
876 EOF
Bram Moolenaar53901442018-07-25 22:02:36 +0200877 call assert_equal([1, 5], [line('.'), col('.')])
878
879 " Check that movement after setting cursor position keeps current column.
880 normal j
881 call assert_equal([2, 5], [line('.'), col('.')])
882endfunc
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200883
884" Test for various heredoc syntax
885func Test_lua_heredoc()
886 lua << END
887vim.command('let s = "A"')
888END
889 lua <<
890vim.command('let s ..= "B"')
891.
892 lua << trim END
893 vim.command('let s ..= "C"')
894 END
895 lua << trim
896 vim.command('let s ..= "D"')
897 .
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200898 lua << trim eof
899 vim.command('let s ..= "E"')
900 eof
901 call assert_equal('ABCDE', s)
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200902endfunc
903
904" vim: shiftwidth=2 sts=2 expandtab