robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
30b4c1b40d
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3 4local describe , it , before_each , after_each =t . describe , t . it , t . before_each , t . after_each 5local eq =t . eq 6local clear =n . clear 7local fn =n . fn 8local api =n . api 9local command =n . command 10local rmdir =n . rmdir 11local pcall_err =t . pcall_err 12local mkdir =t . mkdir 13 14local fname ='Xtest-functional-eval-buf_functions' 15local fname2 =fname .. '.2' 16local dirname =fname .. '.d' 17 18before_each ( clear ) 19 20for _ , func in ipairs ( { 21'bufname(%s)' , 22'bufnr(%s)' , 23'bufwinnr(%s)' , 24'getbufline(%s, 1)' , 25'getbufvar(%s, "changedtick")' , 26'setbufvar(%s, "f", 0)' , 27} ) do 28local funcname =func : match ( '%w+' ) 29describe ( funcname .. '() function' , function () 30it ( 'errors out when receives v:true/v:false/v:null' , function () 31-- Not compatible with Vim: in Vim it always results in buffer not found 32-- without any error messages. 33for _ , var in ipairs ( { 'v:true' , 'v:false' } ) do 34eq ( 35'Vim(call):E5299: Expected a Number or a String, Boolean found' , 36pcall_err ( command , 'call ' .. func : format ( var )) 37) 38end 39eq ( 40'Vim(call):E5300: Expected a Number or a String' , 41pcall_err ( command , 'call ' .. func : format ( 'v:null' )) 42) 43end ) 44it ( 'errors out when receives invalid argument' , function () 45eq ( 46'Vim(call):E745: Expected a Number or a String, List found' , 47pcall_err ( command , 'call ' .. func : format ( '[]' )) 48) 49eq ( 50'Vim(call):E728: Expected a Number or a String, Dictionary found' , 51pcall_err ( command , 'call ' .. func : format ( '{}' )) 52) 53eq ( 54'Vim(call):E805: Expected a Number or a String, Float found' , 55pcall_err ( command , 'call ' .. func : format ( '0.0' )) 56) 57eq ( 58'Vim(call):E703: Expected a Number or a String, Funcref found' , 59pcall_err ( command , 'call ' .. func : format ( 'function("tr")' )) 60) 61end ) 62end ) 63end 64 65describe ( 'bufname() function' , function () 66it ( 'returns empty string when buffer was not found' , function () 67command ( 'file ' .. fname ) 68eq ( '' , fn . bufname ( 2 )) 69eq ( '' , fn . bufname ( 'non-existent-buffer' )) 70eq ( '' , fn . bufname ( '#' )) 71command ( 'edit ' .. fname2 ) 72eq ( 2 , fn . bufnr ( '%' )) 73eq ( '' , fn . bufname ( 'X' )) 74end ) 75before_each ( function () 76mkdir ( dirname ) 77end ) 78after_each ( function () 79rmdir ( dirname ) 80end ) 81it ( 'returns expected buffer name' , function () 82eq ( '' , fn . bufname ( '%' )) -- Buffer has no name yet 83command ( 'file ' .. fname ) 84local wd =assert ( t . fix_slashes ( assert ( vim . uv . cwd ()))) 85local sep ='/' 86local curdirname =fn . fnamemodify ( wd , ':t' ) 87for _ , arg in ipairs ( { '%' , 1 , 'X' , wd } ) do 88eq ( fname , fn . bufname ( arg )) 89api . nvim_set_current_dir ( '..' ) 90eq ( curdirname .. sep .. fname , fn . bufname ( arg )) 91api . nvim_set_current_dir ( curdirname ) 92api . nvim_set_current_dir ( dirname ) 93eq ( wd .. sep .. fname , fn . bufname ( arg )) 94api . nvim_set_current_dir ( '..' ) 95eq ( fname , fn . bufname ( arg )) 96command ( 'enew' ) 97end 98eq ( '' , fn . bufname ( '%' )) 99eq ( '' , fn . bufname ( '$' )) 100eq ( 2 , fn . bufnr ( '%' )) 101end ) 102end ) 103 104describe ( 'bufnr() function' , function () 105it ( 'returns -1 when buffer was not found' , function () 106command ( 'file ' .. fname ) 107eq ( - 1 , fn . bufnr ( 2 )) 108eq ( - 1 , fn . bufnr ( 'non-existent-buffer' )) 109eq ( - 1 , fn . bufnr ( '#' )) 110command ( 'edit ' .. fname2 ) 111eq ( 2 , fn . bufnr ( '%' )) 112eq ( - 1 , fn . bufnr ( 'X' )) 113end ) 114it ( 'returns expected buffer number' , function () 115eq ( 1 , fn . bufnr ( '%' )) 116command ( 'file ' .. fname ) 117local wd =vim . uv . cwd () 118local curdirname =fn . fnamemodify ( wd , ':t' ) 119eq ( 1 , fn . bufnr ( fname )) 120eq ( 1 , fn . bufnr ( wd )) 121eq ( 1 , fn . bufnr ( curdirname )) 122eq ( 1 , fn . bufnr ( 'X' )) 123end ) 124it ( 'returns number of last buffer with "$"' , function () 125eq ( 1 , fn . bufnr ( '$' )) 126command ( 'new' ) 127eq ( 2 , fn . bufnr ( '$' )) 128command ( 'new' ) 129eq ( 3 , fn . bufnr ( '$' )) 130command ( 'only' ) 131eq ( 3 , fn . bufnr ( '$' )) 132eq ( 3 , fn . bufnr ( '%' )) 133command ( 'buffer 1' ) 134eq ( 3 , fn . bufnr ( '$' )) 135eq ( 1 , fn . bufnr ( '%' )) 136command ( 'bwipeout 2' ) 137eq ( 3 , fn . bufnr ( '$' )) 138eq ( 1 , fn . bufnr ( '%' )) 139command ( 'bwipeout 3' ) 140eq ( 1 , fn . bufnr ( '$' )) 141eq ( 1 , fn . bufnr ( '%' )) 142command ( 'new' ) 143eq ( 4 , fn . bufnr ( '$' )) 144end ) 145end ) 146 147describe ( 'bufwinnr() function' , function () 148it ( 'returns -1 when buffer was not found' , function () 149command ( 'file ' .. fname ) 150eq ( - 1 , fn . bufwinnr ( 2 )) 151eq ( - 1 , fn . bufwinnr ( 'non-existent-buffer' )) 152eq ( - 1 , fn . bufwinnr ( '#' )) 153command ( 'split ' .. fname2 ) -- It would be OK if there was one window 154eq ( 2 , fn . bufnr ( '%' )) 155eq ( - 1 , fn . bufwinnr ( 'X' )) 156end ) 157before_each ( function () 158mkdir ( dirname ) 159end ) 160after_each ( function () 161rmdir ( dirname ) 162end ) 163it ( 'returns expected window number' , function () 164eq ( 1 , fn . bufwinnr ( '%' )) 165command ( 'file ' .. fname ) 166command ( 'vsplit' ) 167command ( 'split ' .. fname2 ) 168eq ( 2 , fn . bufwinnr ( fname )) 169eq ( 1 , fn . bufwinnr ( fname2 )) 170eq ( - 1 , fn . bufwinnr ( fname : sub ( 1 , # fname - 1 ))) 171api . nvim_set_current_dir ( dirname ) 172eq ( 2 , fn . bufwinnr ( fname )) 173eq ( 1 , fn . bufwinnr ( fname2 )) 174eq ( - 1 , fn . bufwinnr ( fname : sub ( 1 , # fname - 1 ))) 175eq ( 1 , fn . bufwinnr ( '%' )) 176eq ( 2 , fn . bufwinnr ( 1 )) 177eq ( 1 , fn . bufwinnr ( 2 )) 178eq ( - 1 , fn . bufwinnr ( 3 )) 179eq ( 1 , fn . bufwinnr ( '$' )) 180end ) 181end ) 182 183describe ( 'getbufline() function' , function () 184it ( 'returns empty list when buffer was not found' , function () 185command ( 'file ' .. fname ) 186eq ( {} , fn . getbufline ( 2 , 1 )) 187eq ( {} , fn . getbufline ( 'non-existent-buffer' , 1 )) 188eq ( {} , fn . getbufline ( '#' , 1 )) 189command ( 'edit ' .. fname2 ) 190eq ( 2 , fn . bufnr ( '%' )) 191eq ( {} , fn . getbufline ( 'X' , 1 )) 192end ) 193it ( 'returns empty list when range is invalid' , function () 194eq ( {} , fn . getbufline ( 1 , 0 )) 195eq ( {} , fn . getbufline ( - 1 , 1 , '$' )) 196eq ( {} , fn . getbufline ( - 1 , '$' , '$' )) 197api . nvim_buf_set_lines ( 0 , 0 , 1 , false , { 'foo' , 'bar' , 'baz' } ) 198eq ( {} , fn . getbufline ( 1 , 2 , 1 )) 199eq ( {} , fn . getbufline ( 1 , - 10 , - 20 )) 200eq ( {} , fn . getbufline ( 1 , - 2 , - 1 )) 201eq ( {} , fn . getbufline ( 1 , - 1 , 9999 )) 202eq ( {} , fn . getbufline ( - 1 , 1 , '$' )) 203eq ( {} , fn . getbufline ( - 1 , '$' , '$' )) 204end ) 205it ( 'returns expected lines' , function () 206api . nvim_set_option_value ( 'hidden' , true , {} ) 207command ( 'file ' .. fname ) 208api . nvim_buf_set_lines ( 0 , 0 , 1 , false , { 'foo\0' , '\0bar' , 'baz' } ) 209command ( 'edit ' .. fname2 ) 210api . nvim_buf_set_lines ( 0 , 0 , 1 , false , { 'abc\0' , '\0def' , 'ghi' } ) 211eq ( { 'foo\n' , '\nbar' , 'baz' } , fn . getbufline ( 1 , 1 , 9999 )) 212eq ( { 'abc\n' , '\ndef' , 'ghi' } , fn . getbufline ( 2 , 1 , 9999 )) 213eq ( { 'foo\n' , '\nbar' , 'baz' } , fn . getbufline ( 1 , 1 , '$' )) 214eq ( { 'baz' } , fn . getbufline ( 1 , '$' , '$' )) 215eq ( { 'baz' } , fn . getbufline ( 1 , '$' , 9999 )) 216end ) 217end ) 218 219describe ( 'getbufvar() function' , function () 220it ( 'returns empty list when buffer was not found' , function () 221command ( 'file ' .. fname ) 222eq ( '' , fn . getbufvar ( 2 , '&autoindent' )) 223eq ( '' , fn . getbufvar ( 'non-existent-buffer' , '&autoindent' )) 224eq ( '' , fn . getbufvar ( '#' , '&autoindent' )) 225command ( 'edit ' .. fname2 ) 226eq ( 2 , fn . bufnr ( '%' )) 227eq ( '' , fn . getbufvar ( 'X' , '&autoindent' )) 228end ) 229it ( 'returns empty list when variable/option/etc was not found' , function () 230command ( 'file ' .. fname ) 231eq ( '' , fn . getbufvar ( 1 , '&autondent' )) 232eq ( '' , fn . getbufvar ( 1 , 'changedtic' )) 233end ) 234it ( 'returns expected option value' , function () 235eq ( 0 , fn . getbufvar ( 1 , '&autoindent' )) 236eq ( 0 , fn . getbufvar ( 1 , '&l:autoindent' )) 237eq ( 0 , fn . getbufvar ( 1 , '&g:autoindent' )) 238-- Also works with global-only options 239eq ( 1 , fn . getbufvar ( 1 , '&hidden' )) 240eq ( 1 , fn . getbufvar ( 1 , '&l:hidden' )) 241eq ( 1 , fn . getbufvar ( 1 , '&g:hidden' )) 242-- Also works with window-local options 243eq ( 0 , fn . getbufvar ( 1 , '&number' )) 244eq ( 0 , fn . getbufvar ( 1 , '&l:number' )) 245eq ( 0 , fn . getbufvar ( 1 , '&g:number' )) 246command ( 'new' ) 247-- But with window-local options it probably does not what you expect 248command ( 'setl number' ) 249-- (note that current window’s buffer is 2, but getbufvar() receives 1) 250eq ( 2 , api . nvim_win_get_buf ( 0 )) 251eq ( 1 , fn . getbufvar ( 1 , '&number' )) 252eq ( 1 , fn . getbufvar ( 1 , '&l:number' )) 253-- You can get global value though, if you find this useful. 254eq ( 0 , fn . getbufvar ( 1 , '&g:number' )) 255end ) 256it ( 'returns expected variable value' , function () 257eq ( 2 , fn . getbufvar ( 1 , 'changedtick' )) 258api . nvim_buf_set_lines ( 0 , 0 , 1 , false , { 'abc\0' , '\0def' , 'ghi' } ) 259eq ( 3 , fn . getbufvar ( 1 , 'changedtick' )) 260api . nvim_buf_set_var ( 0 , 'test' , true ) 261eq ( true , fn . getbufvar ( 1 , 'test' )) 262eq ( { test =true , changedtick =3 } , fn . getbufvar ( 1 , '' )) 263command ( 'new' ) 264eq ( 3 , fn . getbufvar ( 1 , 'changedtick' )) 265eq ( true , fn . getbufvar ( 1 , 'test' )) 266eq ( { test =true , changedtick =3 } , fn . getbufvar ( 1 , '' )) 267end ) 268end ) 269 270describe ( 'setbufvar() function' , function () 271it ( 'throws the error or ignores the input when buffer was not found' , function () 272command ( 'file ' .. fname ) 273command ( 'call setbufvar(2, "&autoindent", 0)' ) 274eq ( 275'Vim(call):E94: No matching buffer for non-existent-buffer' , 276pcall_err ( command , 'call setbufvar("non-existent-buffer", "&autoindent", 0)' ) 277) 278command ( 'call setbufvar("#", "&autoindent", 0)' ) 279command ( 'edit ' .. fname2 ) 280eq ( 2 , fn . bufnr ( '%' )) 281eq ( 282'Vim(call):E93: More than one match for X' , 283pcall_err ( command , 'call setbufvar("X", "&autoindent", 0)' ) 284) 285end ) 286it ( 'may set options, including window-local and global values' , function () 287local buf1 =api . nvim_get_current_buf () 288eq ( false , api . nvim_get_option_value ( 'number' , {} )) 289command ( 'split' ) 290command ( 'new' ) 291eq ( 2 , api . nvim_buf_get_number ( api . nvim_win_get_buf ( 0 ))) 292fn . setbufvar ( 1 , '&number' , true ) 293local windows =api . nvim_tabpage_list_wins ( 0 ) 294eq ( false , api . nvim_get_option_value ( 'number' , { win =windows [ 1 ] } )) 295eq ( true , api . nvim_get_option_value ( 'number' , { win =windows [ 2 ] } )) 296eq ( false , api . nvim_get_option_value ( 'number' , { win =windows [ 3 ] } )) 297eq ( false , api . nvim_get_option_value ( 'number' , { win =api . nvim_get_current_win () } )) 298 299eq ( true , api . nvim_get_option_value ( 'hidden' , {} )) 300fn . setbufvar ( 1 , '&hidden' , 0 ) 301eq ( false , api . nvim_get_option_value ( 'hidden' , {} )) 302 303eq ( false , api . nvim_get_option_value ( 'autoindent' , { buf =buf1 } )) 304fn . setbufvar ( 1 , '&autoindent' , true ) 305eq ( true , api . nvim_get_option_value ( 'autoindent' , { buf =buf1 } )) 306eq ( 'Vim(call):E355: Unknown option: xxx' , pcall_err ( command , 'call setbufvar(1, "&xxx", 0)' )) 307end ) 308it ( 'may set variables' , function () 309local buf1 =api . nvim_get_current_buf () 310command ( 'split' ) 311command ( 'new' ) 312eq ( 2 , api . nvim_buf_get_number ( 0 )) 313fn . setbufvar ( 1 , 'number' , true ) 314eq ( true , api . nvim_buf_get_var ( buf1 , 'number' )) 315eq ( 'Vim(call):E461: Illegal variable name: b:' , pcall_err ( command , 'call setbufvar(1, "", 0)' )) 316eq ( true , api . nvim_buf_get_var ( buf1 , 'number' )) 317eq ( 318'Vim:E46: Cannot change read-only variable "b:changedtick"' , 319pcall_err ( fn . setbufvar , 1 , 'changedtick' , true ) 320) 321eq ( 2 , fn . getbufvar ( 1 , 'changedtick' )) 322end ) 323it ( 'throws error when setting a string option to a boolean value vim-patch:9.0.0090' , function () 324eq ( 'Vim:E928: String required' , pcall_err ( fn . setbufvar , '' , '&errorformat' , true )) 325end ) 326end ) 327 328describe ( 'bufload() function' , function () 329before_each ( function () 330t . write_file ( fname , 'foo\n' ) 331end ) 332after_each ( function () 333os . remove ( fname ) 334end ) 335it ( 'does not set hidden buffer as modified #41148' , function () 336local buf =fn . bufadd ( fname ) 337fn . bufload ( buf ) 338api . nvim_command ( 'edit ' .. fname ) 339eq ( true , api . nvim_buf_is_loaded ( buf )) 340eq ( false , api . nvim_get_option_value ( 'modified' , { buf =buf } )) 341end ) 342end )