robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3local Screen =require ( 'test.functional.ui.screen' ) 4 5local describe , it , before_each , pending =t . describe , t . it , t . before_each , t . pending 6local NIL =vim . NIL 7local feed =n . feed 8local command =n . command 9local clear =n . clear 10local api =n . api 11local eq =t . eq 12local eval =n . eval 13local exec =n . exec 14local exec_lua =n . exec_lua 15local fn =n . fn 16local matches =t . matches 17local mkdir_p =n . mkdir_p 18local pcall_err =t . pcall_err 19local rmdir =n . rmdir 20local write_file =t . write_file 21 22local function eq_exec_lua ( expected , f ) 23eq ( expected , exec_lua ( f )) 24end 25 26describe ( 'lua stdlib' , function () 27before_each ( clear ) 28 29describe ( 'variables' , function () 30it ( 'vim.g' , function () 31exec_lua ( function () 32vim . api . nvim_set_var ( 'testing' , 'hi' ) 33vim . api . nvim_set_var ( 'other' , 123 ) 34vim . api . nvim_set_var ( 'floaty' , 5120.1 ) 35vim . api . nvim_set_var ( 'nullvar' , vim . NIL ) 36vim . api . nvim_set_var ( 'to_delete' , { hello ='world' } ) 37end ) 38 39eq ( 'hi' , fn . luaeval 'vim.g.testing' ) 40eq ( 123 , fn . luaeval 'vim.g.other' ) 41eq ( 5120.1 , fn . luaeval 'vim.g.floaty' ) 42eq ( NIL , fn . luaeval 'vim.g.nonexistent' ) 43eq ( NIL , fn . luaeval 'vim.g.nullvar' ) 44-- lost over RPC, so test locally: 45eq_exec_lua ( { false , true } , function () 46return { vim . g . nonexistent == vim . NIL , vim . g . nullvar == vim . NIL } 47end ) 48 49eq ( { hello ='world' } , fn . luaeval 'vim.g.to_delete' ) 50exec_lua [[ 51vim.g.to_delete = nil 52]] 53eq ( NIL , fn . luaeval 'vim.g.to_delete' ) 54 55matches ( [[attempt to index .* nil value]] , pcall_err ( exec_lua , 'return vim.g[0].testing' )) 56 57exec_lua ( function () 58local counter =0 59local function add_counter () 60counter =counter + 1 61end 62local function get_counter () 63return counter 64end 65vim . g . AddCounter =add_counter 66vim . g . GetCounter =get_counter 67vim . g . fn ={ add =add_counter , get =get_counter } 68vim . g . AddParens =function ( s ) 69return '(' .. s .. ')' 70end 71end ) 72 73eq ( 0 , eval ( 'g:GetCounter()' )) 74eval ( 'g:AddCounter()' ) 75eq ( 1 , eval ( 'g:GetCounter()' )) 76eval ( 'g:AddCounter()' ) 77eq ( 2 , eval ( 'g:GetCounter()' )) 78exec_lua ( [[vim.g.AddCounter()]] ) 79eq ( 3 , exec_lua ( [[return vim.g.GetCounter()]] )) 80exec_lua ( [[vim.api.nvim_get_var('AddCounter')()]] ) 81eq ( 4 , exec_lua ( [[return vim.api.nvim_get_var('GetCounter')()]] )) 82exec_lua ( [[vim.g.fn.add()]] ) 83eq ( 5 , exec_lua ( [[return vim.g.fn.get()]] )) 84exec_lua ( [[vim.api.nvim_get_var('fn').add()]] ) 85eq ( 6 , exec_lua ( [[return vim.api.nvim_get_var('fn').get()]] )) 86eq ( '((foo))' , eval ( [['foo'->AddParens()->AddParens()]] )) 87 88exec_lua ( function () 89local counter =0 90local function add_counter () 91counter =counter + 1 92end 93local function get_counter () 94return counter 95end 96vim . api . nvim_set_var ( 'AddCounter' , add_counter ) 97vim . api . nvim_set_var ( 'GetCounter' , get_counter ) 98vim . api . nvim_set_var ( 'fn' , { add =add_counter , get =get_counter } ) 99vim . api . nvim_set_var ( 'AddParens' , function ( s ) 100return '(' .. s .. ')' 101end ) 102end ) 103 104eq ( 0 , eval ( 'g:GetCounter()' )) 105eval ( 'g:AddCounter()' ) 106eq ( 1 , eval ( 'g:GetCounter()' )) 107eval ( 'g:AddCounter()' ) 108eq ( 2 , eval ( 'g:GetCounter()' )) 109exec_lua ( [[vim.g.AddCounter()]] ) 110eq ( 3 , exec_lua ( [[return vim.g.GetCounter()]] )) 111exec_lua ( [[vim.api.nvim_get_var('AddCounter')()]] ) 112eq ( 4 , exec_lua ( [[return vim.api.nvim_get_var('GetCounter')()]] )) 113exec_lua ( [[vim.g.fn.add()]] ) 114eq ( 5 , exec_lua ( [[return vim.g.fn.get()]] )) 115exec_lua ( [[vim.api.nvim_get_var('fn').add()]] ) 116eq ( 6 , exec_lua ( [[return vim.api.nvim_get_var('fn').get()]] )) 117eq ( '((foo))' , eval ( [['foo'->AddParens()->AddParens()]] )) 118 119exec ( [[ 120function Test() 121endfunction 122function s:Test() 123endfunction 124let g:Unknown_func = function('Test') 125let g:Unknown_script_func = function('s:Test') 126]] ) 127eq ( NIL , exec_lua ( [[return vim.g.Unknown_func]] )) 128eq ( NIL , exec_lua ( [[return vim.g.Unknown_script_func]] )) 129 130-- Check if autoload works properly 131local pathsep =n . get_pathsep () 132local xhome ='Xhome_lua' 133local xconfig =xhome .. pathsep .. 'Xconfig' 134local xdata =xhome .. pathsep .. 'Xdata' 135local autoload_folder =table . concat ( { xconfig , 'nvim' , 'autoload' } , pathsep ) 136local autoload_file =table . concat ( { autoload_folder , 'testload.vim' } , pathsep ) 137mkdir_p ( autoload_folder ) 138write_file ( autoload_file , [[let testload#value = 2]] ) 139 140clear { args_rm ={ '-u' } , env ={ XDG_CONFIG_HOME =xconfig , XDG_DATA_HOME =xdata } } 141 142eq ( 2 , exec_lua ( "return vim.g['testload#value']" )) 143rmdir ( xhome ) 144end ) 145 146it ( 'vim.b' , function () 147exec_lua ( function () 148vim . api . nvim_buf_set_var ( 0 , 'testing' , 'hi' ) 149vim . api . nvim_buf_set_var ( 0 , 'other' , 123 ) 150vim . api . nvim_buf_set_var ( 0 , 'floaty' , 5120.1 ) 151vim . api . nvim_buf_set_var ( 0 , 'nullvar' , vim . NIL ) 152vim . api . nvim_buf_set_var ( 0 , 'to_delete' , { hello ='world' } ) 153_G . BUF =vim . api . nvim_create_buf ( false , true ) 154vim . api . nvim_buf_set_var ( _G . BUF , 'testing' , 'bye' ) 155end ) 156 157eq ( 'hi' , fn . luaeval 'vim.b.testing' ) 158eq ( 'bye' , fn . luaeval 'vim.b[BUF].testing' ) 159eq ( 123 , fn . luaeval 'vim.b.other' ) 160eq ( 5120.1 , fn . luaeval 'vim.b.floaty' ) 161eq ( NIL , fn . luaeval 'vim.b.nonexistent' ) 162eq ( NIL , fn . luaeval 'vim.b[BUF].nonexistent' ) 163eq ( NIL , fn . luaeval 'vim.b.nullvar' ) 164-- lost over RPC, so test locally: 165eq_exec_lua ( { false , true } , function () 166return { vim . b . nonexistent == vim . NIL , vim . b . nullvar == vim . NIL } 167end ) 168 169matches ( 170[[attempt to index .* nil value]] , 171pcall_err ( exec_lua , 'return vim.b[BUF][0].testing' ) 172) 173 174eq ( { hello ='world' } , fn . luaeval 'vim.b.to_delete' ) 175exec_lua [[ 176vim.b.to_delete = nil 177]] 178eq ( NIL , fn . luaeval 'vim.b.to_delete' ) 179 180exec_lua ( function () 181local counter =0 182local function add_counter () 183counter =counter + 1 184end 185local function get_counter () 186return counter 187end 188vim . b . AddCounter =add_counter 189vim . b . GetCounter =get_counter 190vim . b . fn ={ add =add_counter , get =get_counter } 191vim . b . AddParens =function ( s ) 192return '(' .. s .. ')' 193end 194end ) 195 196eq ( 0 , eval ( 'b:GetCounter()' )) 197eval ( 'b:AddCounter()' ) 198eq ( 1 , eval ( 'b:GetCounter()' )) 199eval ( 'b:AddCounter()' ) 200eq ( 2 , eval ( 'b:GetCounter()' )) 201exec_lua ( [[vim.b.AddCounter()]] ) 202eq ( 3 , exec_lua ( [[return vim.b.GetCounter()]] )) 203exec_lua ( [[vim.api.nvim_buf_get_var(0, 'AddCounter')()]] ) 204eq ( 4 , exec_lua ( [[return vim.api.nvim_buf_get_var(0, 'GetCounter')()]] )) 205exec_lua ( [[vim.b.fn.add()]] ) 206eq ( 5 , exec_lua ( [[return vim.b.fn.get()]] )) 207exec_lua ( [[vim.api.nvim_buf_get_var(0, 'fn').add()]] ) 208eq ( 6 , exec_lua ( [[return vim.api.nvim_buf_get_var(0, 'fn').get()]] )) 209eq ( '((foo))' , eval ( [['foo'->b:AddParens()->b:AddParens()]] )) 210 211exec_lua ( function () 212local counter =0 213local function add_counter () 214counter =counter + 1 215end 216local function get_counter () 217return counter 218end 219vim . api . nvim_buf_set_var ( 0 , 'AddCounter' , add_counter ) 220vim . api . nvim_buf_set_var ( 0 , 'GetCounter' , get_counter ) 221vim . api . nvim_buf_set_var ( 0 , 'fn' , { add =add_counter , get =get_counter } ) 222vim . api . nvim_buf_set_var ( 0 , 'AddParens' , function ( s ) 223return '(' .. s .. ')' 224end ) 225end ) 226 227eq ( 0 , eval ( 'b:GetCounter()' )) 228eval ( 'b:AddCounter()' ) 229eq ( 1 , eval ( 'b:GetCounter()' )) 230eval ( 'b:AddCounter()' ) 231eq ( 2 , eval ( 'b:GetCounter()' )) 232exec_lua ( [[vim.b.AddCounter()]] ) 233eq ( 3 , exec_lua ( [[return vim.b.GetCounter()]] )) 234exec_lua ( [[vim.api.nvim_buf_get_var(0, 'AddCounter')()]] ) 235eq ( 4 , exec_lua ( [[return vim.api.nvim_buf_get_var(0, 'GetCounter')()]] )) 236exec_lua ( [[vim.b.fn.add()]] ) 237eq ( 5 , exec_lua ( [[return vim.b.fn.get()]] )) 238exec_lua ( [[vim.api.nvim_buf_get_var(0, 'fn').add()]] ) 239eq ( 6 , exec_lua ( [[return vim.api.nvim_buf_get_var(0, 'fn').get()]] )) 240eq ( '((foo))' , eval ( [['foo'->b:AddParens()->b:AddParens()]] )) 241 242exec ( [[ 243function Test() 244endfunction 245function s:Test() 246endfunction 247let b:Unknown_func = function('Test') 248let b:Unknown_script_func = function('s:Test') 249]] ) 250eq ( NIL , exec_lua ( [[return vim.b.Unknown_func]] )) 251eq ( NIL , exec_lua ( [[return vim.b.Unknown_script_func]] )) 252 253exec_lua [[ 254vim.cmd "vnew" 255]] 256 257eq ( NIL , fn . luaeval 'vim.b.testing' ) 258eq ( NIL , fn . luaeval 'vim.b.other' ) 259eq ( NIL , fn . luaeval 'vim.b.nonexistent' ) 260end ) 261 262it ( 'vim.w' , function () 263exec_lua [[ 264vim.api.nvim_win_set_var(0, "testing", "hi") 265vim.api.nvim_win_set_var(0, "other", 123) 266vim.api.nvim_win_set_var(0, "to_delete", {hello="world"}) 267BUF = vim.api.nvim_create_buf(false, true) 268WIN = vim.api.nvim_open_win(BUF, false, { 269width=10, height=10, 270relative='win', row=0, col=0 271}) 272vim.api.nvim_win_set_var(WIN, "testing", "bye") 273]] 274 275eq ( 'hi' , fn . luaeval 'vim.w.testing' ) 276eq ( 'bye' , fn . luaeval 'vim.w[WIN].testing' ) 277eq ( 123 , fn . luaeval 'vim.w.other' ) 278eq ( NIL , fn . luaeval 'vim.w.nonexistent' ) 279eq ( NIL , fn . luaeval 'vim.w[WIN].nonexistent' ) 280 281matches ( 282[[attempt to index .* nil value]] , 283pcall_err ( exec_lua , 'return vim.w[WIN][0].testing' ) 284) 285 286eq ( { hello ='world' } , fn . luaeval 'vim.w.to_delete' ) 287exec_lua [[ 288vim.w.to_delete = nil 289]] 290eq ( NIL , fn . luaeval 'vim.w.to_delete' ) 291 292exec_lua [[ 293local counter = 0 294local function add_counter() counter = counter + 1 end 295local function get_counter() return counter end 296vim.w.AddCounter = add_counter 297vim.w.GetCounter = get_counter 298vim.w.fn = {add = add_counter, get = get_counter} 299vim.w.AddParens = function(s) return '(' .. s .. ')' end 300]] 301 302eq ( 0 , eval ( 'w:GetCounter()' )) 303eval ( 'w:AddCounter()' ) 304eq ( 1 , eval ( 'w:GetCounter()' )) 305eval ( 'w:AddCounter()' ) 306eq ( 2 , eval ( 'w:GetCounter()' )) 307exec_lua ( [[vim.w.AddCounter()]] ) 308eq ( 3 , exec_lua ( [[return vim.w.GetCounter()]] )) 309exec_lua ( [[vim.api.nvim_win_get_var(0, 'AddCounter')()]] ) 310eq ( 4 , exec_lua ( [[return vim.api.nvim_win_get_var(0, 'GetCounter')()]] )) 311exec_lua ( [[vim.w.fn.add()]] ) 312eq ( 5 , exec_lua ( [[return vim.w.fn.get()]] )) 313exec_lua ( [[vim.api.nvim_win_get_var(0, 'fn').add()]] ) 314eq ( 6 , exec_lua ( [[return vim.api.nvim_win_get_var(0, 'fn').get()]] )) 315eq ( '((foo))' , eval ( [['foo'->w:AddParens()->w:AddParens()]] )) 316 317exec_lua [[ 318local counter = 0 319local function add_counter() counter = counter + 1 end 320local function get_counter() return counter end 321vim.api.nvim_win_set_var(0, 'AddCounter', add_counter) 322vim.api.nvim_win_set_var(0, 'GetCounter', get_counter) 323vim.api.nvim_win_set_var(0, 'fn', {add = add_counter, get = get_counter}) 324vim.api.nvim_win_set_var(0, 'AddParens', function(s) return '(' .. s .. ')' end) 325]] 326 327eq ( 0 , eval ( 'w:GetCounter()' )) 328eval ( 'w:AddCounter()' ) 329eq ( 1 , eval ( 'w:GetCounter()' )) 330eval ( 'w:AddCounter()' ) 331eq ( 2 , eval ( 'w:GetCounter()' )) 332exec_lua ( [[vim.w.AddCounter()]] ) 333eq ( 3 , exec_lua ( [[return vim.w.GetCounter()]] )) 334exec_lua ( [[vim.api.nvim_win_get_var(0, 'AddCounter')()]] ) 335eq ( 4 , exec_lua ( [[return vim.api.nvim_win_get_var(0, 'GetCounter')()]] )) 336exec_lua ( [[vim.w.fn.add()]] ) 337eq ( 5 , exec_lua ( [[return vim.w.fn.get()]] )) 338exec_lua ( [[vim.api.nvim_win_get_var(0, 'fn').add()]] ) 339eq ( 6 , exec_lua ( [[return vim.api.nvim_win_get_var(0, 'fn').get()]] )) 340eq ( '((foo))' , eval ( [['foo'->w:AddParens()->w:AddParens()]] )) 341 342exec ( [[ 343function Test() 344endfunction 345function s:Test() 346endfunction 347let w:Unknown_func = function('Test') 348let w:Unknown_script_func = function('s:Test') 349]] ) 350eq ( NIL , exec_lua ( [[return vim.w.Unknown_func]] )) 351eq ( NIL , exec_lua ( [[return vim.w.Unknown_script_func]] )) 352 353exec_lua [[ 354vim.cmd "vnew" 355]] 356 357eq ( NIL , fn . luaeval 'vim.w.testing' ) 358eq ( NIL , fn . luaeval 'vim.w.other' ) 359eq ( NIL , fn . luaeval 'vim.w.nonexistent' ) 360end ) 361 362it ( 'vim.t' , function () 363exec_lua [[ 364vim.api.nvim_tabpage_set_var(0, "testing", "hi") 365vim.api.nvim_tabpage_set_var(0, "other", 123) 366vim.api.nvim_tabpage_set_var(0, "to_delete", {hello="world"}) 367]] 368 369eq ( 'hi' , fn . luaeval 'vim.t.testing' ) 370eq ( 123 , fn . luaeval 'vim.t.other' ) 371eq ( NIL , fn . luaeval 'vim.t.nonexistent' ) 372eq ( 'hi' , fn . luaeval 'vim.t[0].testing' ) 373eq ( 123 , fn . luaeval 'vim.t[0].other' ) 374eq ( NIL , fn . luaeval 'vim.t[0].nonexistent' ) 375 376matches ( [[attempt to index .* nil value]] , pcall_err ( exec_lua , 'return vim.t[0][0].testing' )) 377 378eq ( { hello ='world' } , fn . luaeval 'vim.t.to_delete' ) 379exec_lua [[ 380vim.t.to_delete = nil 381]] 382eq ( NIL , fn . luaeval 'vim.t.to_delete' ) 383 384exec_lua [[ 385local counter = 0 386local function add_counter() counter = counter + 1 end 387local function get_counter() return counter end 388vim.t.AddCounter = add_counter 389vim.t.GetCounter = get_counter 390vim.t.fn = {add = add_counter, get = get_counter} 391vim.t.AddParens = function(s) return '(' .. s .. ')' end 392]] 393 394eq ( 0 , eval ( 't:GetCounter()' )) 395eval ( 't:AddCounter()' ) 396eq ( 1 , eval ( 't:GetCounter()' )) 397eval ( 't:AddCounter()' ) 398eq ( 2 , eval ( 't:GetCounter()' )) 399exec_lua ( [[vim.t.AddCounter()]] ) 400eq ( 3 , exec_lua ( [[return vim.t.GetCounter()]] )) 401exec_lua ( [[vim.api.nvim_tabpage_get_var(0, 'AddCounter')()]] ) 402eq ( 4 , exec_lua ( [[return vim.api.nvim_tabpage_get_var(0, 'GetCounter')()]] )) 403exec_lua ( [[vim.t.fn.add()]] ) 404eq ( 5 , exec_lua ( [[return vim.t.fn.get()]] )) 405exec_lua ( [[vim.api.nvim_tabpage_get_var(0, 'fn').add()]] ) 406eq ( 6 , exec_lua ( [[return vim.api.nvim_tabpage_get_var(0, 'fn').get()]] )) 407eq ( '((foo))' , eval ( [['foo'->t:AddParens()->t:AddParens()]] )) 408 409exec_lua [[ 410local counter = 0 411local function add_counter() counter = counter + 1 end 412local function get_counter() return counter end 413vim.api.nvim_tabpage_set_var(0, 'AddCounter', add_counter) 414vim.api.nvim_tabpage_set_var(0, 'GetCounter', get_counter) 415vim.api.nvim_tabpage_set_var(0, 'fn', {add = add_counter, get = get_counter}) 416vim.api.nvim_tabpage_set_var(0, 'AddParens', function(s) return '(' .. s .. ')' end) 417]] 418 419eq ( 0 , eval ( 't:GetCounter()' )) 420eval ( 't:AddCounter()' ) 421eq ( 1 , eval ( 't:GetCounter()' )) 422eval ( 't:AddCounter()' ) 423eq ( 2 , eval ( 't:GetCounter()' )) 424exec_lua ( [[vim.t.AddCounter()]] ) 425eq ( 3 , exec_lua ( [[return vim.t.GetCounter()]] )) 426exec_lua ( [[vim.api.nvim_tabpage_get_var(0, 'AddCounter')()]] ) 427eq ( 4 , exec_lua ( [[return vim.api.nvim_tabpage_get_var(0, 'GetCounter')()]] )) 428exec_lua ( [[vim.t.fn.add()]] ) 429eq ( 5 , exec_lua ( [[return vim.t.fn.get()]] )) 430exec_lua ( [[vim.api.nvim_tabpage_get_var(0, 'fn').add()]] ) 431eq ( 6 , exec_lua ( [[return vim.api.nvim_tabpage_get_var(0, 'fn').get()]] )) 432eq ( '((foo))' , eval ( [['foo'->t:AddParens()->t:AddParens()]] )) 433 434exec_lua [[ 435vim.cmd "tabnew" 436]] 437 438eq ( NIL , fn . luaeval 'vim.t.testing' ) 439eq ( NIL , fn . luaeval 'vim.t.other' ) 440eq ( NIL , fn . luaeval 'vim.t.nonexistent' ) 441end ) 442 443it ( 'vim.env' , function () 444exec_lua ( [[vim.fn.setenv('A', 123)]] ) 445eq ( '123' , fn . luaeval ( 'vim.env.A' )) 446exec_lua ( [[vim.env.A = 456]] ) 447eq ( '456' , fn . luaeval ( 'vim.env.A' )) 448exec_lua ( [[vim.env.A = nil]] ) 449eq ( NIL , fn . luaeval ( 'vim.env.A' )) 450 451eq ( true , fn . luaeval ( 'vim.env.B == nil' )) 452 453command ( [[let $HOME = 'foo']] ) 454eq ( 'foo' , fn . expand ( '~' )) 455eq ( 'foo' , fn . luaeval ( 'vim.env.HOME' )) 456exec_lua ( [[vim.env.HOME = nil]] ) 457eq ( 'foo' , fn . expand ( '~' )) 458exec_lua ( [[vim.env.HOME = 'bar']] ) 459eq ( 'bar' , fn . expand ( '~' )) 460eq ( 'bar' , fn . luaeval ( 'vim.env.HOME' )) 461end ) 462 463it ( 'vim.v' , function () 464eq ( fn . luaeval "vim.api.nvim_get_vvar('progpath')" , fn . luaeval 'vim.v.progpath' ) 465eq ( false , fn . luaeval "vim.v['false']" ) 466eq ( NIL , fn . luaeval 'vim.v.null' ) 467matches ( [[attempt to index .* nil value]] , pcall_err ( exec_lua , 'return vim.v[0].progpath' )) 468matches ( 'Key is read%-only: count$' , pcall_err ( exec_lua , [[vim.v.count = 42]] )) 469matches ( 'Dict is locked$' , pcall_err ( exec_lua , [[vim.v.nosuchvar = 42]] )) 470matches ( 'Key is fixed: errmsg$' , pcall_err ( exec_lua , [[vim.v.errmsg = nil]] )) 471exec_lua ( [[vim.v.errmsg = 'set by Lua']] ) 472eq ( 'set by Lua' , eval ( 'v:errmsg' )) 473exec_lua ( [[vim.v.errmsg = 42]] ) 474eq ( '42' , eval ( 'v:errmsg' )) 475exec_lua ( [[vim.v.oldfiles = { 'one', 'two' }]] ) 476eq ( { 'one' , 'two' } , eval ( 'v:oldfiles' )) 477exec_lua ( [[vim.v.oldfiles = {}]] ) 478eq ( {} , eval ( 'v:oldfiles' )) 479matches ( 480'Setting v:oldfiles to value with wrong type$' , 481pcall_err ( exec_lua , [[vim.v.oldfiles = 'a']] ) 482) 483eq ( {} , eval ( 'v:oldfiles' )) 484 485feed ( 'i foo foo foo<Esc>0/foo<CR>' ) 486eq ( { 1 , 1 } , api . nvim_win_get_cursor ( 0 )) 487eq ( 1 , eval ( 'v:searchforward' )) 488feed ( 'n' ) 489eq ( { 1 , 5 } , api . nvim_win_get_cursor ( 0 )) 490exec_lua ( [[vim.v.searchforward = 0]] ) 491eq ( 0 , eval ( 'v:searchforward' )) 492feed ( 'n' ) 493eq ( { 1 , 1 } , api . nvim_win_get_cursor ( 0 )) 494exec_lua ( [[vim.v.searchforward = 1]] ) 495eq ( 1 , eval ( 'v:searchforward' )) 496feed ( 'n' ) 497eq ( { 1 , 5 } , api . nvim_win_get_cursor ( 0 )) 498 499local screen =Screen . new ( 60 , 3 ) 500eq ( 1 , eval ( 'v:hlsearch' )) 501screen : expect { 502grid =[[ 503{10:foo} {10:^foo} {10:foo} | 504{1:~ }| 505| 506]] , 507} 508exec_lua ( [[vim.v.hlsearch = 0]] ) 509eq ( 0 , eval ( 'v:hlsearch' )) 510screen : expect { 511grid =[[ 512foo ^foo foo | 513{1:~ }| 514| 515]] , 516} 517exec_lua ( [[vim.v.hlsearch = 1]] ) 518eq ( 1 , eval ( 'v:hlsearch' )) 519screen : expect { 520grid =[[ 521{10:foo} {10:^foo} {10:foo} | 522{1:~ }| 523| 524]] , 525} 526end ) 527end ) 528 529describe ( 'options' , function () 530describe ( 'vim.o' , function () 531it ( 'works for boolean values' , function () 532eq_exec_lua ( false , function () 533vim . o . number =false 534return vim . o . number 535end ) 536end ) 537 538it ( 'works for number values' , function () 539eq_exec_lua ( 10 , function () 540vim . o . tabstop =10 541return vim . o . tabstop 542end ) 543end ) 544 545it ( 'works for string values' , function () 546eq_exec_lua ( 'hello world' , function () 547vim . o . makeprg ='hello world' 548return vim . o . makeprg 549end ) 550end ) 551 552it ( 'works for array type options' , function () 553eq_exec_lua ( 'hello,world' , function () 554vim . o . wildignore ={ 'hello' , 'world' } 555return vim . o . wildignore 556end ) 557end ) 558 559it ( 'works for key:value type options using string' , function () 560eq_exec_lua ( 'eol:~,space:-' , function () 561vim . o . listchars ='eol:~,space:-' 562return vim . o . listchars 563end ) 564end ) 565 566it ( 'works for key:value type options using list of strings' , function () 567eq_exec_lua ( 'eol:~,space:-' , function () 568vim . o . listchars ={ 'eol:~' , 'space:-' } 569return vim . o . listchars 570end ) 571end ) 572 573it ( 'works for key:value type options using table' , function () 574eq_exec_lua ( 'eol:~,space:.' , function () 575vim . o . listchars ={ eol ='~' , space ='.' } 576return vim . o . listchars 577end ) 578end ) 579 580it ( 'works for set type flaglists' , function () 581eq_exec_lua ( 'tcro' , function () 582vim . o . formatoptions ='tcro' 583return vim . o . formatoptions 584end ) 585end ) 586 587it ( 'works for set type flaglists using tables' , function () 588eq_exec_lua ( { t =true , c =true , r =true , o =true } , function () 589vim . o . formatoptions ={ t =true , c =true , r =true , o =true } 590-- use vim.opt to convert to lua value 591return vim . opt . formatoptions : get () 592end ) 593end ) 594 595it ( 'works for weird number options' , function () 596eq_exec_lua ( 9 , function () 597vim . o . wildchar ='<Tab>' 598return vim . o . wildchar 599end ) 600end ) 601 602it ( 'expands env vars' , function () 603eq_exec_lua ( '/dev/null' , function () 604vim . fn . setenv ( 'INCLUDE' , '/dev/null' ) 605vim . o . path ='$INCLUDE' 606return vim . o . path 607end ) 608end ) 609 610it ( 'expands ~' , function () 611eq_exec_lua ( '/dev/null' , function () 612vim . fn . setenv ( 'HOME' , '/dev/null' ) 613vim . o . rtp ='~' 614return vim . o . rtp 615end ) 616end ) 617end ) 618 619describe ( 'vim.bo' , function () 620it ( 'can get and set options' , function () 621eq ( '' , fn . luaeval 'vim.bo.filetype' ) 622exec_lua ( function () 623_G . BUF =vim . api . nvim_create_buf ( false , true ) 624vim . api . nvim_set_option_value ( 'filetype' , 'markdown' , {} ) 625vim . api . nvim_set_option_value ( 'modifiable' , false , { buf =_G . BUF } ) 626end ) 627eq ( false , fn . luaeval 'vim.bo.modified' ) 628eq ( 'markdown' , fn . luaeval 'vim.bo.filetype' ) 629eq ( false , fn . luaeval 'vim.bo[BUF].modifiable' ) 630exec_lua ( function () 631vim . bo . filetype ='' 632vim . bo [ _G . BUF ]. modifiable =true 633end ) 634eq ( '' , fn . luaeval 'vim.bo.filetype' ) 635eq ( true , fn . luaeval 'vim.bo[BUF].modifiable' ) 636end ) 637 638it ( 'errors' , function () 639matches ( "Unknown option 'nosuchopt'$" , pcall_err ( exec_lua , 'return vim.bo.nosuchopt' )) 640matches ( 'Expected Lua string$' , pcall_err ( exec_lua , 'return vim.bo[0][0].autoread' )) 641matches ( 'Invalid buffer id: %-1$' , pcall_err ( exec_lua , 'return vim.bo[-1].filetype' )) 642end ) 643end ) 644 645describe ( 'vim.wo' , function () 646it ( 'can get and set options' , function () 647exec_lua ( function () 648vim . api . nvim_set_option_value ( 'cole' , 2 , {} ) 649vim . cmd 'split' 650vim . api . nvim_set_option_value ( 'cole' , 2 , {} ) 651end ) 652eq ( 2 , fn . luaeval 'vim.wo.cole' ) 653exec_lua ( function () 654vim . wo . conceallevel =0 655end ) 656eq ( 0 , fn . luaeval 'vim.wo.cole' ) 657eq ( 0 , fn . luaeval 'vim.wo[0].cole' ) 658eq ( 0 , fn . luaeval 'vim.wo[1001].cole' ) 659matches ( "Unknown option 'notanopt'$" , pcall_err ( exec_lua , 'return vim.wo.notanopt' )) 660matches ( 'Invalid window id: %-1$' , pcall_err ( exec_lua , 'return vim.wo[-1].list' )) 661eq ( 2 , fn . luaeval 'vim.wo[1000].cole' ) 662exec_lua ( function () 663vim . wo [ 1000 ]. cole =0 664end ) 665eq ( 0 , fn . luaeval 'vim.wo[1000].cole' ) 666 667-- Can handle global-local values 668exec_lua [[vim.o.scrolloff = 100]] 669exec_lua [[vim.wo.scrolloff = 200]] 670eq ( 200 , fn . luaeval 'vim.wo.scrolloff' ) 671exec_lua [[vim.wo.scrolloff = -1]] 672eq ( 100 , fn . luaeval 'vim.wo.scrolloff' ) 673exec_lua ( function () 674vim . wo [ 0 ][ 0 ]. scrolloff =200 675vim . cmd 'enew' 676end ) 677eq ( 100 , fn . luaeval 'vim.wo.scrolloff' ) 678end ) 679 680it ( 'errors' , function () 681matches ( 'only bufnr=0 is supported' , pcall_err ( exec_lua , 'vim.wo[0][10].signcolumn = "no"' )) 682matches ( 683'only bufnr=0 is supported' , 684pcall_err ( exec_lua , 'local a = vim.wo[0][10].signcolumn' ) 685) 686end ) 687end ) 688 689describe ( 'vim.opt' , function () 690-- TODO: We still need to write some tests for optlocal, opt and then getting the options 691-- Probably could also do some stuff with getting things from viml side as well to confirm behavior is the same. 692 693it ( 'allows setting number values' , function () 694local scrolloff =exec_lua [[ 695vim.opt.scrolloff = 10 696return vim.o.scrolloff 697]] 698eq ( 10 , scrolloff ) 699end ) 700 701pending ( 'handles STUPID window things' , function () 702eq_exec_lua ( {} , function () 703return { 704vim . api . nvim_get_option_value ( 'scrolloff' , { scope ='global' } ), 705vim . api . nvim_get_option_value ( 'scrolloff' , { win =0 } ), 706} 707end ) 708end ) 709 710it ( 'allows setting tables' , function () 711eq_exec_lua ( 'hello,world' , function () 712vim . opt . wildignore ={ 'hello' , 'world' } 713return vim . o . wildignore 714end ) 715end ) 716 717it ( 'allows setting tables with shortnames' , function () 718eq_exec_lua ( 'hello,world' , function () 719vim . opt . wig ={ 'hello' , 'world' } 720return vim . o . wildignore 721end ) 722end ) 723 724it ( 'errors when you attempt to set string values to numeric options' , function () 725eq_exec_lua ( false , function () 726return ( { 727pcall ( function () 728vim . opt . textwidth ='hello world' 729end ), 730} )[ 1 ] 731end ) 732end ) 733 734it ( 'errors when you attempt to setlocal a global value' , function () 735eq_exec_lua ( false , function () 736return pcall ( function () 737vim . opt_local . clipboard ='hello' 738end ) 739end ) 740end ) 741 742it ( 'allows you to set boolean values' , function () 743eq_exec_lua ( { true , false , true } , function () 744local results ={} 745 746vim . opt . autoindent =true 747table . insert ( results , vim . bo . autoindent ) 748 749vim . opt . autoindent =false 750table . insert ( results , vim . bo . autoindent ) 751 752vim . opt . autoindent =not vim . opt . autoindent : get () 753table . insert ( results , vim . bo . autoindent ) 754 755return results 756end ) 757end ) 758 759it ( 'changes current buffer values and defaults for global local values' , function () 760local result =exec_lua ( function () 761local result ={} 762 763vim . opt . makeprg ='global-local' 764table . insert ( result , vim . go . makeprg ) 765table . insert ( result , vim . api . nvim_get_option_value ( 'makeprg' , { buf =0 } )) 766 767vim . opt_local . mp ='only-local' 768table . insert ( result , vim . go . makeprg ) 769table . insert ( result , vim . api . nvim_get_option_value ( 'makeprg' , { buf =0 } )) 770 771vim . opt_global . makeprg ='only-global' 772table . insert ( result , vim . go . makeprg ) 773table . insert ( result , vim . api . nvim_get_option_value ( 'makeprg' , { buf =0 } )) 774 775vim . opt . makeprg ='global-local' 776table . insert ( result , vim . go . makeprg ) 777table . insert ( result , vim . api . nvim_get_option_value ( 'makeprg' , { buf =0 } )) 778return result 779end ) 780 781-- Set -> global & local 782eq ( 'global-local' , result [ 1 ]) 783eq ( '' , result [ 2 ]) 784 785-- Setlocal -> only local 786eq ( 'global-local' , result [ 3 ]) 787eq ( 'only-local' , result [ 4 ]) 788 789-- Setglobal -> only global 790eq ( 'only-global' , result [ 5 ]) 791eq ( 'only-local' , result [ 6 ]) 792 793-- Set -> sets global value and resets local value 794eq ( 'global-local' , result [ 7 ]) 795eq ( '' , result [ 8 ]) 796end ) 797 798it ( 'allows you to retrieve window opts even if they have not been set' , function () 799eq_exec_lua ( { false , false , true , true } , function () 800local result ={} 801table . insert ( result , vim . opt . number : get ()) 802table . insert ( result , vim . opt_local . number : get ()) 803 804vim . opt_local . number =true 805table . insert ( result , vim . opt . number : get ()) 806table . insert ( result , vim . opt_local . number : get ()) 807 808return result 809end ) 810end ) 811 812it ( 'allows all sorts of string manipulation' , function () 813eq_exec_lua ( { 'hello' , 'hello world' , 'start hello world' } , function () 814local results ={} 815 816vim . opt . makeprg ='hello' 817table . insert ( results , vim . o . makeprg ) 818 819vim . opt . makeprg =vim . opt . makeprg + ' world' 820table . insert ( results , vim . o . makeprg ) 821 822vim . opt . makeprg =vim . opt . makeprg ^ 'start ' 823table . insert ( results , vim . o . makeprg ) 824 825return results 826end ) 827end ) 828 829describe ( 'option:get()' , function () 830it ( 'works for boolean values' , function () 831eq_exec_lua ( false , function () 832vim . opt . number =false 833return vim . opt . number : get () 834end ) 835end ) 836 837it ( 'works for number values' , function () 838eq_exec_lua ( 10 , function () 839vim . opt . tabstop =10 840return vim . opt . tabstop : get () 841end ) 842end ) 843 844it ( 'works for string values' , function () 845eq_exec_lua ( 'hello world' , function () 846vim . opt . makeprg ='hello world' 847return vim . opt . makeprg : get () 848end ) 849end ) 850 851it ( 'works for set type flaglists' , function () 852local formatoptions =exec_lua ( function () 853vim . opt . formatoptions ='tcro' 854return vim . opt . formatoptions : get () 855end ) 856 857eq ( true , formatoptions . t ) 858eq ( true , not formatoptions . q ) 859end ) 860 861it ( 'works for set type flaglists' , function () 862local formatoptions =exec_lua ( function () 863vim . opt . formatoptions ={ t =true , c =true , r =true , o =true } 864return vim . opt . formatoptions : get () 865end ) 866 867eq ( true , formatoptions . t ) 868eq ( true , not formatoptions . q ) 869end ) 870 871it ( 'works for comma list type options' , function () 872local wildignore =exec_lua ( function () 873vim . opt . wildignore ='*.c,*.o,__pycache__' 874return vim . opt . wildignore : get () 875end ) 876 877eq ( 3 , # wildignore ) 878eq ( '*.c' , wildignore [ 1 ]) 879end ) 880 881it ( 'works for array list type options' , function () 882eq_exec_lua ( { eol ='~' , space ='-' } , function () 883vim . opt . listchars ={ 'eol:~' , 'space:-' } 884return vim . opt . listchars : get () 885end ) 886end ) 887 888it ( 'works for options that are both commalist and flaglist' , function () 889eq_exec_lua ( { b =true , s =true } , function () 890vim . opt . whichwrap ='b,s' 891return vim . opt . whichwrap : get () 892end ) 893 894eq_exec_lua ( { b =true , h =true } , function () 895vim . opt . whichwrap ={ b =true , s =false , h =true } 896return vim . opt . whichwrap : get () 897end ) 898end ) 899 900it ( "returns structured values for a dict option ('diffopt')" , function () 901-- Bare flags -> true; sub-values stay strings (option sub-value types are not exposed). 902eq_exec_lua ( { internal =true , filler =true , context ='4' } , function () 903vim . opt . diffopt ='internal,filler,context:4' 904return vim . opt . diffopt : get () 905end ) 906end ) 907 908it ( 'roundtrips a dict option through :set' , function () 909eq_exec_lua ( 'context:4,filler,internal' , function () 910vim . opt . diffopt ='internal,filler,context:4' 911vim . opt . diffopt =vim . opt . diffopt : get () 912return vim . go . diffopt 913end ) 914end ) 915 916-- Dict option accepts a table and :get() returns a map. #18875 917it ( "roundtrips a window-local dict option ('breakindentopt')" , function () 918eq_exec_lua ( { sbr =true , shift ='3' } , function () 919vim . opt . breakindentopt ={ sbr =true , shift =3 } 920return vim . opt . breakindentopt : get () 921end ) 922end ) 923 924it ( "roundtrips a table for 'mousescroll'" , function () 925eq_exec_lua ( { hor ='6' , ver ='3' } , function () 926vim . opt . mousescroll ={ hor =6 , ver =3 } 927return vim . opt . mousescroll : get () 928end ) 929end ) 930 931it ( 'works for key-value pair options' , function () 932eq_exec_lua ( { tab ='> ' , space ='_' } , function () 933vim . opt . listchars ='tab:> ,space:_' 934return vim . opt . listchars : get () 935end ) 936end ) 937 938it ( 'allows you to add numeric options' , function () 939eq_exec_lua ( 16 , function () 940vim . opt . tabstop =12 941vim . opt . tabstop =vim . opt . tabstop + 4 942return vim . bo . tabstop 943end ) 944end ) 945 946it ( 'allows you to subtract numeric options' , function () 947eq_exec_lua ( 2 , function () 948vim . opt . tabstop =4 949vim . opt . tabstop =vim . opt . tabstop - 2 950return vim . bo . tabstop 951end ) 952end ) 953end ) 954 955describe ( 'key:value style options' , function () 956it ( 'handles dict style' , function () 957eq_exec_lua ( 'eol:~,space:.' , function () 958vim . opt . listchars ={ eol ='~' , space ='.' } 959return vim . o . listchars 960end ) 961end ) 962 963it ( 'allows adding dict style' , function () 964eq_exec_lua ( 'eol:~,space:-' , function () 965vim . opt . listchars ={ eol ='~' , space ='.' } 966vim . opt . listchars =vim . opt . listchars + { space ='-' } 967return vim . o . listchars 968end ) 969end ) 970 971it ( 'does not allow adding dict style multiple times' , function () 972matches ( 973'Multiple vim.opt infix operations unsupported' , 974pcall_err ( 975exec_lua , 976[[ 977vim.opt.listchars = { eol = '~', space = '.' } 978vim.opt.listchars = vim.opt.listchars + { space = '-' } + { space = '_' } 979]] 980) 981) 982end ) 983 984it ( 'allows completely new keys' , function () 985eq_exec_lua ( 'eol:~,space:.,tab:>>>' , function () 986vim . opt . listchars ={ eol ='~' , space ='.' } 987vim . opt . listchars =vim . opt . listchars + { tab ='>>>' } 988return vim . o . listchars 989end ) 990end ) 991 992it ( 'allows subtracting dict style' , function () 993eq_exec_lua ( 'eol:~' , function () 994vim . opt . listchars ={ eol ='~' , space ='.' } 995vim . opt . listchars =vim . opt . listchars - 'space' 996return vim . o . listchars 997end ) 998end ) 999 1000it ( 'does not allow subtracting dict style multiple times' , function () 1001matches ( 1002'Multiple vim.opt infix operations unsupported' , 1003pcall_err ( 1004exec_lua , 1005[[ 1006vim.opt.listchars = { eol = '~', space = '.' } 1007vim.opt.listchars = vim.opt.listchars - 'space' - 'eol' 1008]] 1009) 1010) 1011end ) 1012 1013it ( 'does not allow subtracting dict style multiple times with the same key' , function () 1014matches ( 1015'Multiple vim.opt infix operations unsupported' , 1016pcall_err ( 1017exec_lua , 1018[[ 1019vim.opt.listchars = { eol = '~', space = '.' } 1020vim.opt.listchars = vim.opt.listchars - 'space' - 'space' 1021]] 1022) 1023) 1024end ) 1025 1026it ( 'allows adding a key:value string to a listchars' , function () 1027eq_exec_lua ( 'eol:~,space:.,tab:>~' , function () 1028vim . opt . listchars ={ eol ='~' , space ='.' } 1029vim . opt . listchars =vim . opt . listchars + 'tab:>~' 1030return vim . o . listchars 1031end ) 1032end ) 1033 1034it ( 'allows prepending a key:value string to a listchars' , function () 1035eq_exec_lua ( 'eol:~,space:.,tab:>~' , function () 1036vim . opt . listchars ={ eol ='~' , space ='.' } 1037-- Operator overloading is cursed. 1038vim . opt . listchars =vim . opt . listchars ^ 'tab:>~' 1039return vim . o . listchars 1040end ) 1041end ) 1042end ) 1043 1044it ( 'automatically sets when calling remove' , function () 1045eq_exec_lua ( 'foo,baz' , function () 1046vim . opt . wildignore ='foo,bar,baz' 1047vim . opt . wildignore : remove ( 'bar' ) 1048return vim . o . wildignore 1049end ) 1050end ) 1051 1052it ( 'automatically sets when calling remove with a table' , function () 1053eq_exec_lua ( 'foo' , function () 1054vim . opt . wildignore ='foo,bar,baz' 1055vim . opt . wildignore : remove { 'bar' , 'baz' } 1056return vim . o . wildignore 1057end ) 1058end ) 1059 1060it ( 'removes flags from a set when calling remove with a table: #41953' , function () 1061eq_exec_lua ( { j =true , q =true , l =true } , function () 1062vim . opt . formatoptions ='jcroql' 1063vim . opt . formatoptions : remove { 'c' , 'r' , 'o' } 1064return vim . opt . formatoptions : get () 1065end ) 1066end ) 1067 1068it ( 'automatically sets when calling append' , function () 1069eq_exec_lua ( 'foo,bar,baz,bing' , function () 1070vim . opt . wildignore ='foo,bar,baz' 1071vim . opt . wildignore : append ( 'bing' ) 1072return vim . o . wildignore 1073end ) 1074end ) 1075 1076it ( 'automatically sets when calling append with a table' , function () 1077eq_exec_lua ( 'foo,bar,baz,bing,zap' , function () 1078vim . opt . wildignore ='foo,bar,baz' 1079vim . opt . wildignore : append { 'bing' , 'zap' } 1080return vim . o . wildignore 1081end ) 1082end ) 1083 1084it ( 'allows adding tables' , function () 1085eq_exec_lua ( 'foo' , function () 1086vim . opt . wildignore ='foo' 1087return vim . o . wildignore 1088end ) 1089 1090eq_exec_lua ( 'foo,bar,baz' , function () 1091vim . opt . wildignore =vim . opt . wildignore + { 'bar' , 'baz' } 1092return vim . o . wildignore 1093end ) 1094end ) 1095 1096it ( 'handles adding duplicates' , function () 1097eq_exec_lua ( 'foo' , function () 1098vim . opt . wildignore ='foo' 1099return vim . o . wildignore 1100end ) 1101 1102eq_exec_lua ( 'foo,bar,baz' , function () 1103vim . opt . wildignore =vim . opt . wildignore + { 'bar' , 'baz' } 1104return vim . o . wildignore 1105end ) 1106 1107eq_exec_lua ( 'foo,bar,baz' , function () 1108vim . opt . wildignore =vim . opt . wildignore + { 'bar' , 'baz' } 1109return vim . o . wildignore 1110end ) 1111end ) 1112 1113it ( 'does not allow adding multiple times' , function () 1114matches ( 1115'Multiple vim.opt infix operations unsupported' , 1116pcall_err ( 1117exec_lua , 1118[[ 1119vim.opt.wildignore = 'foo' 1120vim.opt.wildignore = vim.opt.wildignore + 'bar' + 'baz' 1121]] 1122) 1123) 1124end ) 1125 1126it ( 'removes values when you use minus' , function () 1127eq_exec_lua ( 'foo' , function () 1128vim . opt . wildignore ='foo' 1129return vim . o . wildignore 1130end ) 1131 1132eq_exec_lua ( 'foo,bar,baz' , function () 1133vim . opt . wildignore =vim . opt . wildignore + { 'bar' , 'baz' } 1134return vim . o . wildignore 1135end ) 1136 1137eq_exec_lua ( 'foo,baz' , function () 1138vim . opt . wildignore =vim . opt . wildignore - 'bar' 1139return vim . o . wildignore 1140end ) 1141end ) 1142 1143it ( 'prepends values when using ^' , function () 1144eq_exec_lua ( 'first,foo' , function () 1145vim . opt . wildignore ='foo' 1146vim . opt . wildignore =vim . opt . wildignore ^ 'first' 1147return vim . o . wildignore 1148end ) 1149 1150eq_exec_lua ( 'super_first,first,foo' , function () 1151vim . opt . wildignore =vim . opt . wildignore ^ 'super_first' 1152return vim . o . wildignore 1153end ) 1154end ) 1155 1156it ( 'does not remove duplicates from wildmode: #14708' , function () 1157eq_exec_lua ( 'full,list,full' , function () 1158vim . opt . wildmode ={ 'full' , 'list' , 'full' } 1159return vim . o . wildmode 1160end ) 1161end ) 1162 1163describe ( 'option types' , function () 1164it ( 'allows to set option with numeric value' , function () 1165eq_exec_lua ( 4 , function () 1166vim . opt . tabstop =4 1167return vim . bo . tabstop 1168end ) 1169 1170matches ( 1171"Invalid 'tabstop': expected a valid type, got String" , 1172pcall_err ( exec_lua , [[vim.opt.tabstop = '4']] ) 1173) 1174matches ( 1175"Invalid 'tabstop': expected a valid type, got Boolean" , 1176pcall_err ( exec_lua , [[vim.opt.tabstop = true]] ) 1177) 1178matches ( 1179"Invalid 'tabstop': expected a valid type, got Array" , 1180pcall_err ( exec_lua , [[vim.opt.tabstop = {4, 2}]] ) 1181) 1182matches ( 1183"Invalid 'tabstop': expected a valid type, got Function" , 1184pcall_err ( exec_lua , [[vim.opt.tabstop = function() return 4 end]] ) 1185) 1186end ) 1187 1188it ( 'allows to set option with boolean value' , function () 1189eq_exec_lua ( true , function () 1190vim . opt . undofile =true 1191return vim . bo . undofile 1192end ) 1193 1194matches ( 1195"Invalid 'undofile': expected a valid type, got Integer" , 1196pcall_err ( exec_lua , [[vim.opt.undofile = 0]] ) 1197) 1198matches ( 1199"Invalid 'undofile': expected a valid type, got Array" , 1200pcall_err ( exec_lua , [[vim.opt.undofile = {true}]] ) 1201) 1202matches ( 1203"Invalid 'undofile': expected a valid type, got String" , 1204pcall_err ( exec_lua , [[vim.opt.undofile = 'true']] ) 1205) 1206matches ( 1207"Invalid 'undofile': expected a valid type, got Function" , 1208pcall_err ( exec_lua , [[vim.opt.undofile = function() return true end]] ) 1209) 1210end ) 1211 1212it ( 'allows to set option with array or string value' , function () 1213eq_exec_lua ( 'indent,eol,start' , function () 1214vim . opt . backspace ={ 'indent' , 'eol' , 'start' } 1215return vim . go . backspace 1216end ) 1217 1218eq_exec_lua ( 'indent,eol,start' , function () 1219vim . opt . backspace ='indent,eol,start' 1220return vim . go . backspace 1221end ) 1222 1223matches ( 1224"Invalid 'backspace': expected a valid type, got Boolean" , 1225pcall_err ( exec_lua , [[vim.opt.backspace = true]] ) 1226) 1227matches ( 1228"Invalid 'backspace': expected a valid type, got Integer" , 1229pcall_err ( exec_lua , [[vim.opt.backspace = 2]] ) 1230) 1231matches ( 1232"Invalid 'backspace': expected a valid type, got Function" , 1233pcall_err ( exec_lua , [[vim.opt.backspace = function() return 'indent,eol,start' end]] ) 1234) 1235end ) 1236 1237it ( 'allows set option with map or string value' , function () 1238eq_exec_lua ( 'eol:~,space:.' , function () 1239vim . opt . listchars ={ eol ='~' , space ='.' } 1240return vim . o . listchars 1241end ) 1242 1243eq_exec_lua ( 'eol:~,space:.,tab:>~' , function () 1244vim . opt . listchars ='eol:~,space:.,tab:>~' 1245return vim . o . listchars 1246end ) 1247 1248matches ( 1249"Invalid 'listchars': expected a valid type, got Boolean" , 1250pcall_err ( exec_lua , [[vim.opt.listchars = true]] ) 1251) 1252matches ( 1253"Invalid 'listchars': expected a valid type, got Integer" , 1254pcall_err ( exec_lua , [[vim.opt.listchars = 2]] ) 1255) 1256matches ( 1257"Invalid 'listchars': expected a valid type, got Function" , 1258pcall_err ( 1259exec_lua , 1260[[vim.opt.listchars = function() return "eol:~,space:.,tab:>~" end]] 1261) 1262) 1263end ) 1264 1265it ( 'allows set option with set or string value' , function () 1266eq_exec_lua ( 'b,s' , function () 1267vim . opt . whichwrap ={ b =true , s =1 } 1268return vim . go . whichwrap 1269end ) 1270 1271eq_exec_lua ( 'b,s,<,>,[,]' , function () 1272vim . opt . whichwrap ='b,s,<,>,[,]' 1273return vim . go . whichwrap 1274end ) 1275 1276matches ( 1277"Invalid 'whichwrap': expected a valid type, got Boolean" , 1278pcall_err ( exec_lua , [[vim.opt.whichwrap = true]] ) 1279) 1280matches ( 1281"Invalid 'whichwrap': expected a valid type, got Integer" , 1282pcall_err ( exec_lua , [[vim.opt.whichwrap = 2]] ) 1283) 1284matches ( 1285"Invalid 'whichwrap': expected a valid type, got Function" , 1286pcall_err ( exec_lua , [[vim.opt.whichwrap = function() return "b,s,<,>,[,]" end]] ) 1287) 1288end ) 1289end ) 1290 1291-- The raw string keeps the ",," literal-comma convention; the structured view splits on every 1292-- comma and does not reconstruct literal commas. 1293it ( 'can handle isfname ,,,' , function () 1294eq_exec_lua ( { { 'a' , 'b' , 'c' } , 'a,b,,,c' } , function () 1295vim . opt . isfname ='a,b,,,c' 1296return { vim . opt . isfname : get (), vim . go . isfname } 1297end ) 1298end ) 1299 1300it ( 'can handle isfname ,^,,' , function () 1301eq_exec_lua ( { { 'a' , 'b' , '^' , 'c' } , 'a,b,^,,c' } , function () 1302vim . opt . isfname ='a,b,^,,c' 1303return { vim . opt . isfname : get (), vim . go . isfname } 1304end ) 1305end ) 1306 1307describe ( 'https://github.com/neovim/neovim/issues/14828' , function () 1308it ( 'gives empty list when item is empty:array' , function () 1309eq_exec_lua ( {} , function () 1310vim . cmd ( 'set wildignore=' ) 1311return vim . opt . wildignore : get () 1312end ) 1313 1314eq_exec_lua ( {} , function () 1315vim . opt . wildignore ={} 1316return vim . opt . wildignore : get () 1317end ) 1318end ) 1319 1320it ( 'gives empty list when item is empty:set' , function () 1321eq_exec_lua ( {} , function () 1322vim . cmd ( 'set formatoptions=' ) 1323return vim . opt . formatoptions : get () 1324end ) 1325 1326eq_exec_lua ( {} , function () 1327vim . opt . formatoptions ={} 1328return vim . opt . formatoptions : get () 1329end ) 1330end ) 1331 1332it ( 'does not append to empty item' , function () 1333eq_exec_lua ( { '*.foo' , '*.bar' } , function () 1334vim . opt . wildignore ={} 1335vim . opt . wildignore : append { '*.foo' , '*.bar' } 1336return vim . opt . wildignore : get () 1337end ) 1338end ) 1339 1340it ( 'does not prepend to empty item' , function () 1341eq_exec_lua ( { '*.foo' , '*.bar' } , function () 1342vim . opt . wildignore ={} 1343vim . opt . wildignore : prepend { '*.foo' , '*.bar' } 1344return vim . opt . wildignore : get () 1345end ) 1346end ) 1347 1348it ( 'append to empty set' , function () 1349eq_exec_lua ( { t =true } , function () 1350vim . opt . formatoptions ={} 1351vim . opt . formatoptions : append ( 't' ) 1352return vim . opt . formatoptions : get () 1353end ) 1354end ) 1355 1356it ( 'prepend to empty set' , function () 1357eq_exec_lua ( { t =true } , function () 1358vim . opt . formatoptions ={} 1359vim . opt . formatoptions : prepend ( 't' ) 1360return vim . opt . formatoptions : get () 1361end ) 1362end ) 1363end ) 1364 1365it ( 'expands env vars' , function () 1366eq_exec_lua ( { '/dev/null' } , function () 1367vim . env . INCLUDE ='/dev/null' 1368vim . opt . path ='$INCLUDE' 1369return vim . opt . path : get () 1370end ) 1371end ) 1372 1373it ( 'expands ~' , function () 1374eq_exec_lua ( { '/dev/null' } , function () 1375vim . env . HOME ='/dev/null' 1376vim . opt . rtp ='~' 1377return vim . opt . rtp : get () 1378end ) 1379end ) 1380end ) -- vim.opt 1381 1382describe ( 'vim.opt_local' , function () 1383it ( 'appends into global value when changing local option value' , function () 1384eq_exec_lua ( 'foo,bar,baz,qux' , function () 1385vim . opt . tags ='foo,bar' 1386vim . opt_local . tags : append ( 'baz' ) 1387vim . opt_local . tags : append ( 'qux' ) 1388return vim . bo . tags 1389end ) 1390end ) 1391 1392it ( 'removes a table of flags from a flag-list option: #41953' , function () 1393eq_exec_lua ( 'jql' , function () 1394vim . opt_local . formatoptions ='jcroql' 1395vim . opt_local . formatoptions : remove { 'c' , 'r' , 'o' } 1396return vim . bo . formatoptions 1397end ) 1398end ) 1399end ) 1400 1401describe ( 'vim.opt_global' , function () 1402it ( 'gets current global option value' , function () 1403eq_exec_lua ( { 'yes' } , function () 1404vim . cmd 'setglobal signcolumn=yes' 1405return { vim . opt_global . signcolumn : get () } 1406end ) 1407end ) 1408end ) 1409end ) 1410end )