robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
d51fa783d7
master
1-- Test suite for vim.pos 2local t =require ( 'test.testutil' ) 3local n =require ( 'test.functional.testnvim' )() 4local describe , it , before_each =t . describe , t . it , t . before_each 5local eq =t . eq 6local dedent =t . dedent 7 8local clear =n . clear 9local exec_lua =n . exec_lua 10local insert =n . insert 11 12describe ( 'vim.pos' , function () 13before_each ( clear ) 14 15it ( 'creates a position' , function () 16local pos , buf =exec_lua ( function () 17local buf =vim . api . nvim_create_buf ( false , true ) 18return vim . pos ( buf , 3 , 5 ), buf 19end ) 20eq ( 3 , pos [ 1 ]) 21eq ( 5 , pos [ 2 ]) 22eq ( buf , pos [ 3 ]) 23end ) 24 25it ( 'creates a position with buf=0' , function () 26local pos , buf =exec_lua ( function () 27return vim . pos ( 0 , 3 , 5 ), vim . api . nvim_get_current_buf () 28end ) 29eq ( 3 , pos [ 1 ]) 30eq ( 5 , pos [ 2 ]) 31eq ( buf , pos [ 3 ]) 32end ) 33 34it ( 'creates a position from the window cursor' , function () 35local pos , pos_default , buf =exec_lua ( function () 36vim . api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'first' , 'second' } ) 37vim . api . nvim_win_set_cursor ( 0 , { 2 , 3 } ) 38return vim . pos . cursor ( 0 ), vim . pos . cursor (), vim . api . nvim_get_current_buf () 39end ) 40eq ( { 1 , 3 , buf } , pos ) 41eq ( { 1 , 3 , buf } , pos_default ) 42end ) 43 44it ( 'is modifiable' , function () 45local pos , buf =exec_lua ( function () 46local pos =vim . pos ( - 1 , 5 , 5 ) 47pos . buf =0 48pos . row =4 49pos . col =2 50return pos , vim . api . nvim_get_current_buf () 51end ) 52eq ( { 4 , 2 , buf } , pos ) 53end ) 54 55it ( 'comparisons by overloaded operators' , function () 56local buf =exec_lua ( function () 57return vim . api . nvim_create_buf ( false , true ) 58end ) 59eq ( 60true , 61exec_lua ( function () 62return vim . pos ( buf , 3 , 5 ) < vim . pos ( buf , 4 , 5 ) 63end ) 64) 65eq ( 66true , 67exec_lua ( function () 68return vim . pos ( buf , 3 , 5 ) <= vim . pos ( buf , 3 , 6 ) 69end ) 70) 71eq ( 72true , 73exec_lua ( function () 74return vim . pos ( buf , 3 , 5 ) > vim . pos ( buf , 2 , 5 ) 75end ) 76) 77eq ( 78true , 79exec_lua ( function () 80return vim . pos ( buf , 3 , 5 ) >= vim . pos ( buf , 3 , 5 ) 81end ) 82) 83eq ( 84true , 85exec_lua ( function () 86return vim . pos ( buf , 3 , 5 ) == vim . pos ( buf , 3 , 5 ) 87end ) 88) 89eq ( 90true , 91exec_lua ( function () 92return vim . pos ( buf , 3 , 5 ) ~= vim . pos ( buf , 3 , 6 ) 93end ) 94) 95end ) 96 97it ( 'converts between vim.Pos and lsp.Position' , function () 98local buf =exec_lua ( function () 99return vim . api . nvim_get_current_buf () 100end ) 101insert ( 'Neovim 是 Vim 的分支,专注于扩展性和可用性。' ) 102local lsp_pos =exec_lua ( function () 103local pos =vim . pos ( buf , 0 , 36 ) 104return pos : to_lsp ( 'utf-16' ) 105end ) 106eq ( { line =0 , character =20 } , lsp_pos ) 107local pos =exec_lua ( function () 108return vim . pos . lsp ( buf , lsp_pos , 'utf-16' ) 109end ) 110eq ( { 1110 , 11236 , 113buf , 114} , pos ) 115end ) 116 117it ( 'converts between vim.Pos and buffer offset' , function () 118local buf =exec_lua ( function () 119return vim . api . nvim_get_current_buf () 120end ) 121insert ( dedent [[ 122first 123second 124third 125]] ) 126 127local offsets =exec_lua ( function () 128return { 129vim . pos ( buf , 0 , 0 ): to_offset (), 130vim . pos ( buf , 0 , 3 ): to_offset (), 131vim . pos ( buf , 1 , 0 ): to_offset (), 132vim . pos ( buf , 3 , 0 ): to_offset (), 133} 134end ) 135eq ( { 0 , 3 , 6 , 19 } , offsets ) 136 137local positions =exec_lua ( function () 138return { 139vim . pos . offset ( buf , 0 ), 140vim . pos . offset ( buf , 3 ), 141vim . pos . offset ( buf , 6 ), 142vim . pos . offset ( buf , 19 ), 143} 144end ) 145eq ( { 146{ 0 , 0 , buf } , 147{ 0 , 3 , buf } , 148{ 1 , 0 , buf } , 149{ 3 , 0 , buf } , 150} , positions ) 151end ) 152end )