12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229 |
- local t = require('test.testutil')
- local n = require('test.functional.testnvim')()
- local Screen = require('test.functional.ui.screen')
- local NIL = vim.NIL
- local feed = n.feed
- local command = n.command
- local clear = n.clear
- local api = n.api
- local eq = t.eq
- local eval = n.eval
- local exec = n.exec
- local exec_lua = n.exec_lua
- local fn = n.fn
- local matches = t.matches
- local mkdir_p = n.mkdir_p
- local pcall_err = t.pcall_err
- local rmdir = n.rmdir
- local write_file = t.write_file
- local function eq_exec_lua(expected, f)
- eq(expected, exec_lua(f))
- end
- describe('lua stdlib', function()
- before_each(clear)
- describe('variables', function()
- it('vim.g', function()
- exec_lua(function()
- vim.api.nvim_set_var('testing', 'hi')
- vim.api.nvim_set_var('other', 123)
- vim.api.nvim_set_var('floaty', 5120.1)
- vim.api.nvim_set_var('nullvar', vim.NIL)
- vim.api.nvim_set_var('to_delete', { hello = 'world' })
- end)
- eq('hi', fn.luaeval 'vim.g.testing')
- eq(123, fn.luaeval 'vim.g.other')
- eq(5120.1, fn.luaeval 'vim.g.floaty')
- eq(NIL, fn.luaeval 'vim.g.nonexistent')
- eq(NIL, fn.luaeval 'vim.g.nullvar')
- -- lost over RPC, so test locally:
- eq_exec_lua({ false, true }, function()
- return { vim.g.nonexistent == vim.NIL, vim.g.nullvar == vim.NIL }
- end)
- eq({ hello = 'world' }, fn.luaeval 'vim.g.to_delete')
- exec_lua [[
- vim.g.to_delete = nil
- ]]
- eq(NIL, fn.luaeval 'vim.g.to_delete')
- matches([[attempt to index .* nil value]], pcall_err(exec_lua, 'return vim.g[0].testing'))
- exec_lua(function()
- local counter = 0
- local function add_counter()
- counter = counter + 1
- end
- local function get_counter()
- return counter
- end
- vim.g.AddCounter = add_counter
- vim.g.GetCounter = get_counter
- vim.g.fn = { add = add_counter, get = get_counter }
- vim.g.AddParens = function(s)
- return '(' .. s .. ')'
- end
- end)
- eq(0, eval('g:GetCounter()'))
- eval('g:AddCounter()')
- eq(1, eval('g:GetCounter()'))
- eval('g:AddCounter()')
- eq(2, eval('g:GetCounter()'))
- exec_lua([[vim.g.AddCounter()]])
- eq(3, exec_lua([[return vim.g.GetCounter()]]))
- exec_lua([[vim.api.nvim_get_var('AddCounter')()]])
- eq(4, exec_lua([[return vim.api.nvim_get_var('GetCounter')()]]))
- exec_lua([[vim.g.fn.add()]])
- eq(5, exec_lua([[return vim.g.fn.get()]]))
- exec_lua([[vim.api.nvim_get_var('fn').add()]])
- eq(6, exec_lua([[return vim.api.nvim_get_var('fn').get()]]))
- eq('((foo))', eval([['foo'->AddParens()->AddParens()]]))
- exec_lua(function()
- local counter = 0
- local function add_counter()
- counter = counter + 1
- end
- local function get_counter()
- return counter
- end
- vim.api.nvim_set_var('AddCounter', add_counter)
- vim.api.nvim_set_var('GetCounter', get_counter)
- vim.api.nvim_set_var('fn', { add = add_counter, get = get_counter })
- vim.api.nvim_set_var('AddParens', function(s)
- return '(' .. s .. ')'
- end)
- end)
- eq(0, eval('g:GetCounter()'))
- eval('g:AddCounter()')
- eq(1, eval('g:GetCounter()'))
- eval('g:AddCounter()')
- eq(2, eval('g:GetCounter()'))
- exec_lua([[vim.g.AddCounter()]])
- eq(3, exec_lua([[return vim.g.GetCounter()]]))
- exec_lua([[vim.api.nvim_get_var('AddCounter')()]])
- eq(4, exec_lua([[return vim.api.nvim_get_var('GetCounter')()]]))
- exec_lua([[vim.g.fn.add()]])
- eq(5, exec_lua([[return vim.g.fn.get()]]))
- exec_lua([[vim.api.nvim_get_var('fn').add()]])
- eq(6, exec_lua([[return vim.api.nvim_get_var('fn').get()]]))
- eq('((foo))', eval([['foo'->AddParens()->AddParens()]]))
- exec([[
- function Test()
- endfunction
- function s:Test()
- endfunction
- let g:Unknown_func = function('Test')
- let g:Unknown_script_func = function('s:Test')
- ]])
- eq(NIL, exec_lua([[return vim.g.Unknown_func]]))
- eq(NIL, exec_lua([[return vim.g.Unknown_script_func]]))
- -- Check if autoload works properly
- local pathsep = n.get_pathsep()
- local xconfig = 'Xhome' .. pathsep .. 'Xconfig'
- local xdata = 'Xhome' .. pathsep .. 'Xdata'
- local autoload_folder = table.concat({ xconfig, 'nvim', 'autoload' }, pathsep)
- local autoload_file = table.concat({ autoload_folder, 'testload.vim' }, pathsep)
- mkdir_p(autoload_folder)
- write_file(autoload_file, [[let testload#value = 2]])
- clear { args_rm = { '-u' }, env = { XDG_CONFIG_HOME = xconfig, XDG_DATA_HOME = xdata } }
- eq(2, exec_lua("return vim.g['testload#value']"))
- rmdir('Xhome')
- end)
- it('vim.b', function()
- exec_lua(function()
- vim.api.nvim_buf_set_var(0, 'testing', 'hi')
- vim.api.nvim_buf_set_var(0, 'other', 123)
- vim.api.nvim_buf_set_var(0, 'floaty', 5120.1)
- vim.api.nvim_buf_set_var(0, 'nullvar', vim.NIL)
- vim.api.nvim_buf_set_var(0, 'to_delete', { hello = 'world' })
- _G.BUF = vim.api.nvim_create_buf(false, true)
- vim.api.nvim_buf_set_var(_G.BUF, 'testing', 'bye')
- end)
- eq('hi', fn.luaeval 'vim.b.testing')
- eq('bye', fn.luaeval 'vim.b[BUF].testing')
- eq(123, fn.luaeval 'vim.b.other')
- eq(5120.1, fn.luaeval 'vim.b.floaty')
- eq(NIL, fn.luaeval 'vim.b.nonexistent')
- eq(NIL, fn.luaeval 'vim.b[BUF].nonexistent')
- eq(NIL, fn.luaeval 'vim.b.nullvar')
- -- lost over RPC, so test locally:
- eq_exec_lua({ false, true }, function()
- return { vim.b.nonexistent == vim.NIL, vim.b.nullvar == vim.NIL }
- end)
- matches(
- [[attempt to index .* nil value]],
- pcall_err(exec_lua, 'return vim.b[BUF][0].testing')
- )
- eq({ hello = 'world' }, fn.luaeval 'vim.b.to_delete')
- exec_lua [[
- vim.b.to_delete = nil
- ]]
- eq(NIL, fn.luaeval 'vim.b.to_delete')
- exec_lua(function()
- local counter = 0
- local function add_counter()
- counter = counter + 1
- end
- local function get_counter()
- return counter
- end
- vim.b.AddCounter = add_counter
- vim.b.GetCounter = get_counter
- vim.b.fn = { add = add_counter, get = get_counter }
- vim.b.AddParens = function(s)
- return '(' .. s .. ')'
- end
- end)
- eq(0, eval('b:GetCounter()'))
- eval('b:AddCounter()')
- eq(1, eval('b:GetCounter()'))
- eval('b:AddCounter()')
- eq(2, eval('b:GetCounter()'))
- exec_lua([[vim.b.AddCounter()]])
- eq(3, exec_lua([[return vim.b.GetCounter()]]))
- exec_lua([[vim.api.nvim_buf_get_var(0, 'AddCounter')()]])
- eq(4, exec_lua([[return vim.api.nvim_buf_get_var(0, 'GetCounter')()]]))
- exec_lua([[vim.b.fn.add()]])
- eq(5, exec_lua([[return vim.b.fn.get()]]))
- exec_lua([[vim.api.nvim_buf_get_var(0, 'fn').add()]])
- eq(6, exec_lua([[return vim.api.nvim_buf_get_var(0, 'fn').get()]]))
- eq('((foo))', eval([['foo'->b:AddParens()->b:AddParens()]]))
- exec_lua(function()
- local counter = 0
- local function add_counter()
- counter = counter + 1
- end
- local function get_counter()
- return counter
- end
- vim.api.nvim_buf_set_var(0, 'AddCounter', add_counter)
- vim.api.nvim_buf_set_var(0, 'GetCounter', get_counter)
- vim.api.nvim_buf_set_var(0, 'fn', { add = add_counter, get = get_counter })
- vim.api.nvim_buf_set_var(0, 'AddParens', function(s)
- return '(' .. s .. ')'
- end)
- end)
- eq(0, eval('b:GetCounter()'))
- eval('b:AddCounter()')
- eq(1, eval('b:GetCounter()'))
- eval('b:AddCounter()')
- eq(2, eval('b:GetCounter()'))
- exec_lua([[vim.b.AddCounter()]])
- eq(3, exec_lua([[return vim.b.GetCounter()]]))
- exec_lua([[vim.api.nvim_buf_get_var(0, 'AddCounter')()]])
- eq(4, exec_lua([[return vim.api.nvim_buf_get_var(0, 'GetCounter')()]]))
- exec_lua([[vim.b.fn.add()]])
- eq(5, exec_lua([[return vim.b.fn.get()]]))
- exec_lua([[vim.api.nvim_buf_get_var(0, 'fn').add()]])
- eq(6, exec_lua([[return vim.api.nvim_buf_get_var(0, 'fn').get()]]))
- eq('((foo))', eval([['foo'->b:AddParens()->b:AddParens()]]))
- exec([[
- function Test()
- endfunction
- function s:Test()
- endfunction
- let b:Unknown_func = function('Test')
- let b:Unknown_script_func = function('s:Test')
- ]])
- eq(NIL, exec_lua([[return vim.b.Unknown_func]]))
- eq(NIL, exec_lua([[return vim.b.Unknown_script_func]]))
- exec_lua [[
- vim.cmd "vnew"
- ]]
- eq(NIL, fn.luaeval 'vim.b.testing')
- eq(NIL, fn.luaeval 'vim.b.other')
- eq(NIL, fn.luaeval 'vim.b.nonexistent')
- end)
- it('vim.w', function()
- exec_lua [[
- vim.api.nvim_win_set_var(0, "testing", "hi")
- vim.api.nvim_win_set_var(0, "other", 123)
- vim.api.nvim_win_set_var(0, "to_delete", {hello="world"})
- BUF = vim.api.nvim_create_buf(false, true)
- WIN = vim.api.nvim_open_win(BUF, false, {
- width=10, height=10,
- relative='win', row=0, col=0
- })
- vim.api.nvim_win_set_var(WIN, "testing", "bye")
- ]]
- eq('hi', fn.luaeval 'vim.w.testing')
- eq('bye', fn.luaeval 'vim.w[WIN].testing')
- eq(123, fn.luaeval 'vim.w.other')
- eq(NIL, fn.luaeval 'vim.w.nonexistent')
- eq(NIL, fn.luaeval 'vim.w[WIN].nonexistent')
- matches(
- [[attempt to index .* nil value]],
- pcall_err(exec_lua, 'return vim.w[WIN][0].testing')
- )
- eq({ hello = 'world' }, fn.luaeval 'vim.w.to_delete')
- exec_lua [[
- vim.w.to_delete = nil
- ]]
- eq(NIL, fn.luaeval 'vim.w.to_delete')
- exec_lua [[
- local counter = 0
- local function add_counter() counter = counter + 1 end
- local function get_counter() return counter end
- vim.w.AddCounter = add_counter
- vim.w.GetCounter = get_counter
- vim.w.fn = {add = add_counter, get = get_counter}
- vim.w.AddParens = function(s) return '(' .. s .. ')' end
- ]]
- eq(0, eval('w:GetCounter()'))
- eval('w:AddCounter()')
- eq(1, eval('w:GetCounter()'))
- eval('w:AddCounter()')
- eq(2, eval('w:GetCounter()'))
- exec_lua([[vim.w.AddCounter()]])
- eq(3, exec_lua([[return vim.w.GetCounter()]]))
- exec_lua([[vim.api.nvim_win_get_var(0, 'AddCounter')()]])
- eq(4, exec_lua([[return vim.api.nvim_win_get_var(0, 'GetCounter')()]]))
- exec_lua([[vim.w.fn.add()]])
- eq(5, exec_lua([[return vim.w.fn.get()]]))
- exec_lua([[vim.api.nvim_win_get_var(0, 'fn').add()]])
- eq(6, exec_lua([[return vim.api.nvim_win_get_var(0, 'fn').get()]]))
- eq('((foo))', eval([['foo'->w:AddParens()->w:AddParens()]]))
- exec_lua [[
- local counter = 0
- local function add_counter() counter = counter + 1 end
- local function get_counter() return counter end
- vim.api.nvim_win_set_var(0, 'AddCounter', add_counter)
- vim.api.nvim_win_set_var(0, 'GetCounter', get_counter)
- vim.api.nvim_win_set_var(0, 'fn', {add = add_counter, get = get_counter})
- vim.api.nvim_win_set_var(0, 'AddParens', function(s) return '(' .. s .. ')' end)
- ]]
- eq(0, eval('w:GetCounter()'))
- eval('w:AddCounter()')
- eq(1, eval('w:GetCounter()'))
- eval('w:AddCounter()')
- eq(2, eval('w:GetCounter()'))
- exec_lua([[vim.w.AddCounter()]])
- eq(3, exec_lua([[return vim.w.GetCounter()]]))
- exec_lua([[vim.api.nvim_win_get_var(0, 'AddCounter')()]])
- eq(4, exec_lua([[return vim.api.nvim_win_get_var(0, 'GetCounter')()]]))
- exec_lua([[vim.w.fn.add()]])
- eq(5, exec_lua([[return vim.w.fn.get()]]))
- exec_lua([[vim.api.nvim_win_get_var(0, 'fn').add()]])
- eq(6, exec_lua([[return vim.api.nvim_win_get_var(0, 'fn').get()]]))
- eq('((foo))', eval([['foo'->w:AddParens()->w:AddParens()]]))
- exec([[
- function Test()
- endfunction
- function s:Test()
- endfunction
- let w:Unknown_func = function('Test')
- let w:Unknown_script_func = function('s:Test')
- ]])
- eq(NIL, exec_lua([[return vim.w.Unknown_func]]))
- eq(NIL, exec_lua([[return vim.w.Unknown_script_func]]))
- exec_lua [[
- vim.cmd "vnew"
- ]]
- eq(NIL, fn.luaeval 'vim.w.testing')
- eq(NIL, fn.luaeval 'vim.w.other')
- eq(NIL, fn.luaeval 'vim.w.nonexistent')
- end)
- it('vim.t', function()
- exec_lua [[
- vim.api.nvim_tabpage_set_var(0, "testing", "hi")
- vim.api.nvim_tabpage_set_var(0, "other", 123)
- vim.api.nvim_tabpage_set_var(0, "to_delete", {hello="world"})
- ]]
- eq('hi', fn.luaeval 'vim.t.testing')
- eq(123, fn.luaeval 'vim.t.other')
- eq(NIL, fn.luaeval 'vim.t.nonexistent')
- eq('hi', fn.luaeval 'vim.t[0].testing')
- eq(123, fn.luaeval 'vim.t[0].other')
- eq(NIL, fn.luaeval 'vim.t[0].nonexistent')
- matches([[attempt to index .* nil value]], pcall_err(exec_lua, 'return vim.t[0][0].testing'))
- eq({ hello = 'world' }, fn.luaeval 'vim.t.to_delete')
- exec_lua [[
- vim.t.to_delete = nil
- ]]
- eq(NIL, fn.luaeval 'vim.t.to_delete')
- exec_lua [[
- local counter = 0
- local function add_counter() counter = counter + 1 end
- local function get_counter() return counter end
- vim.t.AddCounter = add_counter
- vim.t.GetCounter = get_counter
- vim.t.fn = {add = add_counter, get = get_counter}
- vim.t.AddParens = function(s) return '(' .. s .. ')' end
- ]]
- eq(0, eval('t:GetCounter()'))
- eval('t:AddCounter()')
- eq(1, eval('t:GetCounter()'))
- eval('t:AddCounter()')
- eq(2, eval('t:GetCounter()'))
- exec_lua([[vim.t.AddCounter()]])
- eq(3, exec_lua([[return vim.t.GetCounter()]]))
- exec_lua([[vim.api.nvim_tabpage_get_var(0, 'AddCounter')()]])
- eq(4, exec_lua([[return vim.api.nvim_tabpage_get_var(0, 'GetCounter')()]]))
- exec_lua([[vim.t.fn.add()]])
- eq(5, exec_lua([[return vim.t.fn.get()]]))
- exec_lua([[vim.api.nvim_tabpage_get_var(0, 'fn').add()]])
- eq(6, exec_lua([[return vim.api.nvim_tabpage_get_var(0, 'fn').get()]]))
- eq('((foo))', eval([['foo'->t:AddParens()->t:AddParens()]]))
- exec_lua [[
- local counter = 0
- local function add_counter() counter = counter + 1 end
- local function get_counter() return counter end
- vim.api.nvim_tabpage_set_var(0, 'AddCounter', add_counter)
- vim.api.nvim_tabpage_set_var(0, 'GetCounter', get_counter)
- vim.api.nvim_tabpage_set_var(0, 'fn', {add = add_counter, get = get_counter})
- vim.api.nvim_tabpage_set_var(0, 'AddParens', function(s) return '(' .. s .. ')' end)
- ]]
- eq(0, eval('t:GetCounter()'))
- eval('t:AddCounter()')
- eq(1, eval('t:GetCounter()'))
- eval('t:AddCounter()')
- eq(2, eval('t:GetCounter()'))
- exec_lua([[vim.t.AddCounter()]])
- eq(3, exec_lua([[return vim.t.GetCounter()]]))
- exec_lua([[vim.api.nvim_tabpage_get_var(0, 'AddCounter')()]])
- eq(4, exec_lua([[return vim.api.nvim_tabpage_get_var(0, 'GetCounter')()]]))
- exec_lua([[vim.t.fn.add()]])
- eq(5, exec_lua([[return vim.t.fn.get()]]))
- exec_lua([[vim.api.nvim_tabpage_get_var(0, 'fn').add()]])
- eq(6, exec_lua([[return vim.api.nvim_tabpage_get_var(0, 'fn').get()]]))
- eq('((foo))', eval([['foo'->t:AddParens()->t:AddParens()]]))
- exec_lua [[
- vim.cmd "tabnew"
- ]]
- eq(NIL, fn.luaeval 'vim.t.testing')
- eq(NIL, fn.luaeval 'vim.t.other')
- eq(NIL, fn.luaeval 'vim.t.nonexistent')
- end)
- it('vim.env', function()
- exec_lua([[vim.fn.setenv('A', 123)]])
- eq('123', fn.luaeval('vim.env.A'))
- exec_lua([[vim.env.A = 456]])
- eq('456', fn.luaeval('vim.env.A'))
- exec_lua([[vim.env.A = nil]])
- eq(NIL, fn.luaeval('vim.env.A'))
- eq(true, fn.luaeval('vim.env.B == nil'))
- command([[let $HOME = 'foo']])
- eq('foo', fn.expand('~'))
- eq('foo', fn.luaeval('vim.env.HOME'))
- exec_lua([[vim.env.HOME = nil]])
- eq('foo', fn.expand('~'))
- exec_lua([[vim.env.HOME = 'bar']])
- eq('bar', fn.expand('~'))
- eq('bar', fn.luaeval('vim.env.HOME'))
- end)
- it('vim.v', function()
- eq(fn.luaeval "vim.api.nvim_get_vvar('progpath')", fn.luaeval 'vim.v.progpath')
- eq(false, fn.luaeval "vim.v['false']")
- eq(NIL, fn.luaeval 'vim.v.null')
- matches([[attempt to index .* nil value]], pcall_err(exec_lua, 'return vim.v[0].progpath'))
- eq('Key is read-only: count', pcall_err(exec_lua, [[vim.v.count = 42]]))
- eq('Dict is locked', pcall_err(exec_lua, [[vim.v.nosuchvar = 42]]))
- eq('Key is fixed: errmsg', pcall_err(exec_lua, [[vim.v.errmsg = nil]]))
- exec_lua([[vim.v.errmsg = 'set by Lua']])
- eq('set by Lua', eval('v:errmsg'))
- exec_lua([[vim.v.errmsg = 42]])
- eq('42', eval('v:errmsg'))
- exec_lua([[vim.v.oldfiles = { 'one', 'two' }]])
- eq({ 'one', 'two' }, eval('v:oldfiles'))
- exec_lua([[vim.v.oldfiles = {}]])
- eq({}, eval('v:oldfiles'))
- eq(
- 'Setting v:oldfiles to value with wrong type',
- pcall_err(exec_lua, [[vim.v.oldfiles = 'a']])
- )
- eq({}, eval('v:oldfiles'))
- feed('i foo foo foo<Esc>0/foo<CR>')
- eq({ 1, 1 }, api.nvim_win_get_cursor(0))
- eq(1, eval('v:searchforward'))
- feed('n')
- eq({ 1, 5 }, api.nvim_win_get_cursor(0))
- exec_lua([[vim.v.searchforward = 0]])
- eq(0, eval('v:searchforward'))
- feed('n')
- eq({ 1, 1 }, api.nvim_win_get_cursor(0))
- exec_lua([[vim.v.searchforward = 1]])
- eq(1, eval('v:searchforward'))
- feed('n')
- eq({ 1, 5 }, api.nvim_win_get_cursor(0))
- local screen = Screen.new(60, 3)
- eq(1, eval('v:hlsearch'))
- screen:expect {
- grid = [[
- {10:foo} {10:^foo} {10:foo} |
- {1:~ }|
- |
- ]],
- }
- exec_lua([[vim.v.hlsearch = 0]])
- eq(0, eval('v:hlsearch'))
- screen:expect {
- grid = [[
- foo ^foo foo |
- {1:~ }|
- |
- ]],
- }
- exec_lua([[vim.v.hlsearch = 1]])
- eq(1, eval('v:hlsearch'))
- screen:expect {
- grid = [[
- {10:foo} {10:^foo} {10:foo} |
- {1:~ }|
- |
- ]],
- }
- end)
- end)
- describe('options', function()
- describe('vim.bo', function()
- it('can get and set options', function()
- eq('', fn.luaeval 'vim.bo.filetype')
- exec_lua(function()
- _G.BUF = vim.api.nvim_create_buf(false, true)
- vim.api.nvim_set_option_value('filetype', 'markdown', {})
- vim.api.nvim_set_option_value('modifiable', false, { buf = _G.BUF })
- end)
- eq(false, fn.luaeval 'vim.bo.modified')
- eq('markdown', fn.luaeval 'vim.bo.filetype')
- eq(false, fn.luaeval 'vim.bo[BUF].modifiable')
- exec_lua(function()
- vim.bo.filetype = ''
- vim.bo[_G.BUF].modifiable = true
- end)
- eq('', fn.luaeval 'vim.bo.filetype')
- eq(true, fn.luaeval 'vim.bo[BUF].modifiable')
- end)
- it('errors', function()
- matches("Unknown option 'nosuchopt'$", pcall_err(exec_lua, 'return vim.bo.nosuchopt'))
- matches('Expected Lua string$', pcall_err(exec_lua, 'return vim.bo[0][0].autoread'))
- matches('Invalid buffer id: %-1$', pcall_err(exec_lua, 'return vim.bo[-1].filetype'))
- end)
- end)
- describe('vim.wo', function()
- it('can get and set options', function()
- exec_lua(function()
- vim.api.nvim_set_option_value('cole', 2, {})
- vim.cmd 'split'
- vim.api.nvim_set_option_value('cole', 2, {})
- end)
- eq(2, fn.luaeval 'vim.wo.cole')
- exec_lua(function()
- vim.wo.conceallevel = 0
- end)
- eq(0, fn.luaeval 'vim.wo.cole')
- eq(0, fn.luaeval 'vim.wo[0].cole')
- eq(0, fn.luaeval 'vim.wo[1001].cole')
- matches("Unknown option 'notanopt'$", pcall_err(exec_lua, 'return vim.wo.notanopt'))
- matches('Invalid window id: %-1$', pcall_err(exec_lua, 'return vim.wo[-1].list'))
- eq(2, fn.luaeval 'vim.wo[1000].cole')
- exec_lua(function()
- vim.wo[1000].cole = 0
- end)
- eq(0, fn.luaeval 'vim.wo[1000].cole')
- -- Can handle global-local values
- exec_lua [[vim.o.scrolloff = 100]]
- exec_lua [[vim.wo.scrolloff = 200]]
- eq(200, fn.luaeval 'vim.wo.scrolloff')
- exec_lua [[vim.wo.scrolloff = -1]]
- eq(100, fn.luaeval 'vim.wo.scrolloff')
- exec_lua(function()
- vim.wo[0][0].scrolloff = 200
- vim.cmd 'enew'
- end)
- eq(100, fn.luaeval 'vim.wo.scrolloff')
- end)
- it('errors', function()
- matches('only bufnr=0 is supported', pcall_err(exec_lua, 'vim.wo[0][10].signcolumn = "no"'))
- matches(
- 'only bufnr=0 is supported',
- pcall_err(exec_lua, 'local a = vim.wo[0][10].signcolumn')
- )
- end)
- end)
- describe('vim.opt', function()
- -- TODO: We still need to write some tests for optlocal, opt and then getting the options
- -- Probably could also do some stuff with getting things from viml side as well to confirm behavior is the same.
- it('allows setting number values', function()
- local scrolloff = exec_lua [[
- vim.opt.scrolloff = 10
- return vim.o.scrolloff
- ]]
- eq(10, scrolloff)
- end)
- pending('handles STUPID window things', function()
- eq_exec_lua({}, function()
- return {
- vim.api.nvim_get_option_value('scrolloff', { scope = 'global' }),
- vim.api.nvim_get_option_value('scrolloff', { win = 0 }),
- }
- end)
- end)
- it('allows setting tables', function()
- eq_exec_lua('hello,world', function()
- vim.opt.wildignore = { 'hello', 'world' }
- return vim.o.wildignore
- end)
- end)
- it('allows setting tables with shortnames', function()
- eq_exec_lua('hello,world', function()
- vim.opt.wig = { 'hello', 'world' }
- return vim.o.wildignore
- end)
- end)
- it('errors when you attempt to set string values to numeric options', function()
- eq_exec_lua(false, function()
- return ({
- pcall(function()
- vim.opt.textwidth = 'hello world'
- end),
- })[1]
- end)
- end)
- it('errors when you attempt to setlocal a global value', function()
- eq_exec_lua(false, function()
- return pcall(function()
- vim.opt_local.clipboard = 'hello'
- end)
- end)
- end)
- it('allows you to set boolean values', function()
- eq_exec_lua({ true, false, true }, function()
- local results = {}
- vim.opt.autoindent = true
- table.insert(results, vim.bo.autoindent)
- vim.opt.autoindent = false
- table.insert(results, vim.bo.autoindent)
- vim.opt.autoindent = not vim.opt.autoindent:get()
- table.insert(results, vim.bo.autoindent)
- return results
- end)
- end)
- it('changes current buffer values and defaults for global local values', function()
- local result = exec_lua(function()
- local result = {}
- vim.opt.makeprg = 'global-local'
- table.insert(result, vim.go.makeprg)
- table.insert(result, vim.api.nvim_get_option_value('makeprg', { buf = 0 }))
- vim.opt_local.mp = 'only-local'
- table.insert(result, vim.go.makeprg)
- table.insert(result, vim.api.nvim_get_option_value('makeprg', { buf = 0 }))
- vim.opt_global.makeprg = 'only-global'
- table.insert(result, vim.go.makeprg)
- table.insert(result, vim.api.nvim_get_option_value('makeprg', { buf = 0 }))
- vim.opt.makeprg = 'global-local'
- table.insert(result, vim.go.makeprg)
- table.insert(result, vim.api.nvim_get_option_value('makeprg', { buf = 0 }))
- return result
- end)
- -- Set -> global & local
- eq('global-local', result[1])
- eq('', result[2])
- -- Setlocal -> only local
- eq('global-local', result[3])
- eq('only-local', result[4])
- -- Setglobal -> only global
- eq('only-global', result[5])
- eq('only-local', result[6])
- -- Set -> sets global value and resets local value
- eq('global-local', result[7])
- eq('', result[8])
- end)
- it('allows you to retrieve window opts even if they have not been set', function()
- eq_exec_lua({ false, false, true, true }, function()
- local result = {}
- table.insert(result, vim.opt.number:get())
- table.insert(result, vim.opt_local.number:get())
- vim.opt_local.number = true
- table.insert(result, vim.opt.number:get())
- table.insert(result, vim.opt_local.number:get())
- return result
- end)
- end)
- it('allows all sorts of string manipulation', function()
- eq_exec_lua({ 'hello', 'hello world', 'start hello world' }, function()
- local results = {}
- vim.opt.makeprg = 'hello'
- table.insert(results, vim.o.makeprg)
- vim.opt.makeprg = vim.opt.makeprg + ' world'
- table.insert(results, vim.o.makeprg)
- vim.opt.makeprg = vim.opt.makeprg ^ 'start '
- table.insert(results, vim.o.makeprg)
- return results
- end)
- end)
- describe('option:get()', function()
- it('works for boolean values', function()
- eq_exec_lua(false, function()
- vim.opt.number = false
- return vim.opt.number:get()
- end)
- end)
- it('works for number values', function()
- eq_exec_lua(10, function()
- vim.opt.tabstop = 10
- return vim.opt.tabstop:get()
- end)
- end)
- it('works for string values', function()
- eq_exec_lua('hello world', function()
- vim.opt.makeprg = 'hello world'
- return vim.opt.makeprg:get()
- end)
- end)
- it('works for set type flaglists', function()
- local formatoptions = exec_lua(function()
- vim.opt.formatoptions = 'tcro'
- return vim.opt.formatoptions:get()
- end)
- eq(true, formatoptions.t)
- eq(true, not formatoptions.q)
- end)
- it('works for set type flaglists', function()
- local formatoptions = exec_lua(function()
- vim.opt.formatoptions = { t = true, c = true, r = true, o = true }
- return vim.opt.formatoptions:get()
- end)
- eq(true, formatoptions.t)
- eq(true, not formatoptions.q)
- end)
- it('works for array list type options', function()
- local wildignore = exec_lua(function()
- vim.opt.wildignore = '*.c,*.o,__pycache__'
- return vim.opt.wildignore:get()
- end)
- eq(3, #wildignore)
- eq('*.c', wildignore[1])
- end)
- it('works for options that are both commalist and flaglist', function()
- eq_exec_lua({ b = true, s = true }, function()
- vim.opt.whichwrap = 'b,s'
- return vim.opt.whichwrap:get()
- end)
- eq_exec_lua({ b = true, h = true }, function()
- vim.opt.whichwrap = { b = true, s = false, h = true }
- return vim.opt.whichwrap:get()
- end)
- end)
- it('works for key-value pair options', function()
- eq_exec_lua({ tab = '> ', space = '_' }, function()
- vim.opt.listchars = 'tab:> ,space:_'
- return vim.opt.listchars:get()
- end)
- end)
- it('allows you to add numeric options', function()
- eq_exec_lua(16, function()
- vim.opt.tabstop = 12
- vim.opt.tabstop = vim.opt.tabstop + 4
- return vim.bo.tabstop
- end)
- end)
- it('allows you to subtract numeric options', function()
- eq_exec_lua(2, function()
- vim.opt.tabstop = 4
- vim.opt.tabstop = vim.opt.tabstop - 2
- return vim.bo.tabstop
- end)
- end)
- end)
- describe('key:value style options', function()
- it('handles dict style', function()
- eq_exec_lua('eol:~,space:.', function()
- vim.opt.listchars = { eol = '~', space = '.' }
- return vim.o.listchars
- end)
- end)
- it('allows adding dict style', function()
- eq_exec_lua('eol:~,space:-', function()
- vim.opt.listchars = { eol = '~', space = '.' }
- vim.opt.listchars = vim.opt.listchars + { space = '-' }
- return vim.o.listchars
- end)
- end)
- it('allows adding dict style', function()
- eq_exec_lua('eol:~,space:_', function()
- vim.opt.listchars = { eol = '~', space = '.' }
- vim.opt.listchars = vim.opt.listchars + { space = '-' } + { space = '_' }
- return vim.o.listchars
- end)
- end)
- it('allows completely new keys', function()
- eq_exec_lua('eol:~,space:.,tab:>>>', function()
- vim.opt.listchars = { eol = '~', space = '.' }
- vim.opt.listchars = vim.opt.listchars + { tab = '>>>' }
- return vim.o.listchars
- end)
- end)
- it('allows subtracting dict style', function()
- eq_exec_lua('eol:~', function()
- vim.opt.listchars = { eol = '~', space = '.' }
- vim.opt.listchars = vim.opt.listchars - 'space'
- return vim.o.listchars
- end)
- end)
- it('allows subtracting dict style', function()
- eq_exec_lua('', function()
- vim.opt.listchars = { eol = '~', space = '.' }
- vim.opt.listchars = vim.opt.listchars - 'space' - 'eol'
- return vim.o.listchars
- end)
- end)
- it('allows subtracting dict style multiple times', function()
- eq_exec_lua('eol:~', function()
- vim.opt.listchars = { eol = '~', space = '.' }
- vim.opt.listchars = vim.opt.listchars - 'space' - 'space'
- return vim.o.listchars
- end)
- end)
- it('allows adding a key:value string to a listchars', function()
- eq_exec_lua('eol:~,space:.,tab:>~', function()
- vim.opt.listchars = { eol = '~', space = '.' }
- vim.opt.listchars = vim.opt.listchars + 'tab:>~'
- return vim.o.listchars
- end)
- end)
- it('allows prepending a key:value string to a listchars', function()
- eq_exec_lua('eol:~,space:.,tab:>~', function()
- vim.opt.listchars = { eol = '~', space = '.' }
- vim.opt.listchars = vim.opt.listchars ^ 'tab:>~'
- return vim.o.listchars
- end)
- end)
- end)
- it('automatically sets when calling remove', function()
- eq_exec_lua('foo,baz', function()
- vim.opt.wildignore = 'foo,bar,baz'
- vim.opt.wildignore:remove('bar')
- return vim.o.wildignore
- end)
- end)
- it('automatically sets when calling remove with a table', function()
- eq_exec_lua('foo', function()
- vim.opt.wildignore = 'foo,bar,baz'
- vim.opt.wildignore:remove { 'bar', 'baz' }
- return vim.o.wildignore
- end)
- end)
- it('automatically sets when calling append', function()
- eq_exec_lua('foo,bar,baz,bing', function()
- vim.opt.wildignore = 'foo,bar,baz'
- vim.opt.wildignore:append('bing')
- return vim.o.wildignore
- end)
- end)
- it('automatically sets when calling append with a table', function()
- eq_exec_lua('foo,bar,baz,bing,zap', function()
- vim.opt.wildignore = 'foo,bar,baz'
- vim.opt.wildignore:append { 'bing', 'zap' }
- return vim.o.wildignore
- end)
- end)
- it('allows adding tables', function()
- eq_exec_lua('foo', function()
- vim.opt.wildignore = 'foo'
- return vim.o.wildignore
- end)
- eq_exec_lua('foo,bar,baz', function()
- vim.opt.wildignore = vim.opt.wildignore + { 'bar', 'baz' }
- return vim.o.wildignore
- end)
- end)
- it('handles adding duplicates', function()
- eq_exec_lua('foo', function()
- vim.opt.wildignore = 'foo'
- return vim.o.wildignore
- end)
- eq_exec_lua('foo,bar,baz', function()
- vim.opt.wildignore = vim.opt.wildignore + { 'bar', 'baz' }
- return vim.o.wildignore
- end)
- eq_exec_lua('foo,bar,baz', function()
- vim.opt.wildignore = vim.opt.wildignore + { 'bar', 'baz' }
- return vim.o.wildignore
- end)
- end)
- it('allows adding multiple times', function()
- eq_exec_lua('foo,bar,baz', function()
- vim.opt.wildignore = 'foo'
- vim.opt.wildignore = vim.opt.wildignore + 'bar' + 'baz'
- return vim.o.wildignore
- end)
- end)
- it('removes values when you use minus', function()
- eq_exec_lua('foo', function()
- vim.opt.wildignore = 'foo'
- return vim.o.wildignore
- end)
- eq_exec_lua('foo,bar,baz', function()
- vim.opt.wildignore = vim.opt.wildignore + { 'bar', 'baz' }
- return vim.o.wildignore
- end)
- eq_exec_lua('foo,baz', function()
- vim.opt.wildignore = vim.opt.wildignore - 'bar'
- return vim.o.wildignore
- end)
- end)
- it('prepends values when using ^', function()
- eq_exec_lua('first,foo', function()
- vim.opt.wildignore = 'foo'
- vim.opt.wildignore = vim.opt.wildignore ^ 'first'
- return vim.o.wildignore
- end)
- eq_exec_lua('super_first,first,foo', function()
- vim.opt.wildignore = vim.opt.wildignore ^ 'super_first'
- return vim.o.wildignore
- end)
- end)
- it('does not remove duplicates from wildmode: #14708', function()
- eq_exec_lua('full,list,full', function()
- vim.opt.wildmode = { 'full', 'list', 'full' }
- return vim.o.wildmode
- end)
- end)
- describe('option types', function()
- it('allows to set option with numeric value', function()
- eq_exec_lua(4, function()
- vim.opt.tabstop = 4
- return vim.bo.tabstop
- end)
- matches(
- "Invalid option type 'string' for 'tabstop'",
- pcall_err(exec_lua, [[vim.opt.tabstop = '4']])
- )
- matches(
- "Invalid option type 'boolean' for 'tabstop'",
- pcall_err(exec_lua, [[vim.opt.tabstop = true]])
- )
- matches(
- "Invalid option type 'table' for 'tabstop'",
- pcall_err(exec_lua, [[vim.opt.tabstop = {4, 2}]])
- )
- matches(
- "Invalid option type 'function' for 'tabstop'",
- pcall_err(exec_lua, [[vim.opt.tabstop = function() return 4 end]])
- )
- end)
- it('allows to set option with boolean value', function()
- eq_exec_lua(true, function()
- vim.opt.undofile = true
- return vim.bo.undofile
- end)
- matches(
- "Invalid option type 'number' for 'undofile'",
- pcall_err(exec_lua, [[vim.opt.undofile = 0]])
- )
- matches(
- "Invalid option type 'table' for 'undofile'",
- pcall_err(exec_lua, [[vim.opt.undofile = {true}]])
- )
- matches(
- "Invalid option type 'string' for 'undofile'",
- pcall_err(exec_lua, [[vim.opt.undofile = 'true']])
- )
- matches(
- "Invalid option type 'function' for 'undofile'",
- pcall_err(exec_lua, [[vim.opt.undofile = function() return true end]])
- )
- end)
- it('allows to set option with array or string value', function()
- eq_exec_lua('indent,eol,start', function()
- vim.opt.backspace = { 'indent', 'eol', 'start' }
- return vim.go.backspace
- end)
- eq_exec_lua('indent,eol,start', function()
- vim.opt.backspace = 'indent,eol,start'
- return vim.go.backspace
- end)
- matches(
- "Invalid option type 'boolean' for 'backspace'",
- pcall_err(exec_lua, [[vim.opt.backspace = true]])
- )
- matches(
- "Invalid option type 'number' for 'backspace'",
- pcall_err(exec_lua, [[vim.opt.backspace = 2]])
- )
- matches(
- "Invalid option type 'function' for 'backspace'",
- pcall_err(exec_lua, [[vim.opt.backspace = function() return 'indent,eol,start' end]])
- )
- end)
- it('allows set option with map or string value', function()
- eq_exec_lua('eol:~,space:.', function()
- vim.opt.listchars = { eol = '~', space = '.' }
- return vim.o.listchars
- end)
- eq_exec_lua('eol:~,space:.,tab:>~', function()
- vim.opt.listchars = 'eol:~,space:.,tab:>~'
- return vim.o.listchars
- end)
- matches(
- "Invalid option type 'boolean' for 'listchars'",
- pcall_err(exec_lua, [[vim.opt.listchars = true]])
- )
- matches(
- "Invalid option type 'number' for 'listchars'",
- pcall_err(exec_lua, [[vim.opt.listchars = 2]])
- )
- matches(
- "Invalid option type 'function' for 'listchars'",
- pcall_err(
- exec_lua,
- [[vim.opt.listchars = function() return "eol:~,space:.,tab:>~" end]]
- )
- )
- end)
- it('allows set option with set or string value', function()
- eq_exec_lua('b,s', function()
- vim.opt.whichwrap = { b = true, s = 1 }
- return vim.go.whichwrap
- end)
- eq_exec_lua('b,s,<,>,[,]', function()
- vim.opt.whichwrap = 'b,s,<,>,[,]'
- return vim.go.whichwrap
- end)
- matches(
- "Invalid option type 'boolean' for 'whichwrap'",
- pcall_err(exec_lua, [[vim.opt.whichwrap = true]])
- )
- matches(
- "Invalid option type 'number' for 'whichwrap'",
- pcall_err(exec_lua, [[vim.opt.whichwrap = 2]])
- )
- matches(
- "Invalid option type 'function' for 'whichwrap'",
- pcall_err(exec_lua, [[vim.opt.whichwrap = function() return "b,s,<,>,[,]" end]])
- )
- end)
- end)
- -- isfname=a,b,c,,,d,e,f
- it('can handle isfname ,,,', function()
- eq_exec_lua({ { ',', 'a', 'b', 'c' }, 'a,b,,,c' }, function()
- vim.opt.isfname = 'a,b,,,c'
- return { vim.opt.isfname:get(), vim.go.isfname }
- end)
- end)
- -- isfname=a,b,c,^,,def
- it('can handle isfname ,^,,', function()
- eq_exec_lua({ { '^,', 'a', 'b', 'c' }, 'a,b,^,,c' }, function()
- vim.opt.isfname = 'a,b,^,,c'
- return { vim.opt.isfname:get(), vim.go.isfname }
- end)
- end)
- describe('https://github.com/neovim/neovim/issues/14828', function()
- it('gives empty list when item is empty:array', function()
- eq_exec_lua({}, function()
- vim.cmd('set wildignore=')
- return vim.opt.wildignore:get()
- end)
- eq_exec_lua({}, function()
- vim.opt.wildignore = {}
- return vim.opt.wildignore:get()
- end)
- end)
- it('gives empty list when item is empty:set', function()
- eq_exec_lua({}, function()
- vim.cmd('set formatoptions=')
- return vim.opt.formatoptions:get()
- end)
- eq_exec_lua({}, function()
- vim.opt.formatoptions = {}
- return vim.opt.formatoptions:get()
- end)
- end)
- it('does not append to empty item', function()
- eq_exec_lua({ '*.foo', '*.bar' }, function()
- vim.opt.wildignore = {}
- vim.opt.wildignore:append { '*.foo', '*.bar' }
- return vim.opt.wildignore:get()
- end)
- end)
- it('does not prepend to empty item', function()
- eq_exec_lua({ '*.foo', '*.bar' }, function()
- vim.opt.wildignore = {}
- vim.opt.wildignore:prepend { '*.foo', '*.bar' }
- return vim.opt.wildignore:get()
- end)
- end)
- it('append to empty set', function()
- eq_exec_lua({ t = true }, function()
- vim.opt.formatoptions = {}
- vim.opt.formatoptions:append('t')
- return vim.opt.formatoptions:get()
- end)
- end)
- it('prepend to empty set', function()
- eq_exec_lua({ t = true }, function()
- vim.opt.formatoptions = {}
- vim.opt.formatoptions:prepend('t')
- return vim.opt.formatoptions:get()
- end)
- end)
- end)
- end) -- vim.opt
- describe('vim.opt_local', function()
- it('appends into global value when changing local option value', function()
- eq_exec_lua('foo,bar,baz,qux', function()
- vim.opt.tags = 'foo,bar'
- vim.opt_local.tags:append('baz')
- vim.opt_local.tags:append('qux')
- return vim.bo.tags
- end)
- end)
- end)
- describe('vim.opt_global', function()
- it('gets current global option value', function()
- eq_exec_lua({ 'yes' }, function()
- vim.cmd 'setglobal signcolumn=yes'
- return { vim.opt_global.signcolumn:get() }
- end)
- end)
- end)
- end)
- end)
|