robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
1local t =require ( 'test.unit.testutil' ) 2local describe , setup , teardown =t . describe , t . setup , t . teardown 3local itp =t . gen_itp ( t . it ) 4 5local to_cstr =t . to_cstr 6local eq =t . eq 7local NULL =t . NULL 8 9local buffer =t . cimport ( './src/nvim/buffer.h' ) 10 11describe ( 'buffer functions' , function () 12local buflist_new =function ( file , flags ) 13local c_file =to_cstr ( file ) 14return buffer . buflist_new ( c_file , c_file , 1 , flags ) 15end 16 17local close_buffer =function ( ... ) 18return buffer . close_buffer ( ... ) 19end 20 21local path1 ='test_file_path' 22local path2 ='file_path_test' 23local path3 ='path_test_file' 24 25setup ( function () 26-- create the files 27io . open ( path1 , 'w' ): close () 28io . open ( path2 , 'w' ): close () 29io . open ( path3 , 'w' ): close () 30end ) 31 32teardown ( function () 33os . remove ( path1 ) 34os . remove ( path2 ) 35os . remove ( path3 ) 36end ) 37 38describe ( 'buf_valid' , function () 39itp ( 'should view NULL as an invalid buffer' , function () 40eq ( false , buffer . buf_valid ( NULL )) 41end ) 42 43itp ( 'should view an open buffer as valid' , function () 44local buf =buflist_new ( path1 , buffer . BLN_LISTED ) 45 46eq ( true , buffer . buf_valid ( buf )) 47end ) 48 49itp ( 'should view a closed and hidden buffer as valid' , function () 50local buf =buflist_new ( path1 , buffer . BLN_LISTED ) 51 52close_buffer ( NULL , buf , 0 , 0 , 0 , 0 ) 53 54eq ( true , buffer . buf_valid ( buf )) 55end ) 56 57itp ( 'should view a closed and unloaded buffer as valid' , function () 58local buf =buflist_new ( path1 , buffer . BLN_LISTED ) 59 60close_buffer ( NULL , buf , buffer . DOBUF_UNLOAD , 0 , 0 , 0 ) 61 62eq ( true , buffer . buf_valid ( buf )) 63end ) 64 65itp ( 'should view a closed and wiped buffer as invalid' , function () 66local buf =buflist_new ( path1 , buffer . BLN_LISTED ) 67 68close_buffer ( NULL , buf , buffer . DOBUF_WIPE , 0 , 0 , 0 ) 69 70eq ( false , buffer . buf_valid ( buf )) 71end ) 72end ) 73 74describe ( 'buflist_findpat' , function () 75local ALLOW_UNLISTED =1 76local ONLY_LISTED =0 77 78local buflist_findpat =function ( pat , allow_unlisted ) 79return buffer . buflist_findpat ( to_cstr ( pat ), NULL , allow_unlisted , 0 , 0 ) 80end 81 82itp ( 'should find exact matches' , function () 83local buf =buflist_new ( path1 , buffer . BLN_LISTED ) 84 85eq ( buf . handle , buflist_findpat ( path1 , ONLY_LISTED )) 86 87close_buffer ( NULL , buf , buffer . DOBUF_WIPE , 0 , 0 , 0 ) 88end ) 89 90itp ( 'should prefer to match the start of a file path' , function () 91local buf1 =buflist_new ( path1 , buffer . BLN_LISTED ) 92local buf2 =buflist_new ( path2 , buffer . BLN_LISTED ) 93local buf3 =buflist_new ( path3 , buffer . BLN_LISTED ) 94 95eq ( buf1 . handle , buflist_findpat ( 'test' , ONLY_LISTED )) 96eq ( buf2 . handle , buflist_findpat ( 'file' , ONLY_LISTED )) 97eq ( buf3 . handle , buflist_findpat ( 'path' , ONLY_LISTED )) 98 99close_buffer ( NULL , buf1 , buffer . DOBUF_WIPE , 0 , 0 , 0 ) 100close_buffer ( NULL , buf2 , buffer . DOBUF_WIPE , 0 , 0 , 0 ) 101close_buffer ( NULL , buf3 , buffer . DOBUF_WIPE , 0 , 0 , 0 ) 102end ) 103 104itp ( 'should prefer to match the end of a file over the middle' , function () 105--{ Given: Two buffers, where 'test' appears in both 106-- And: 'test' appears at the end of buf3 but in the middle of buf2 107local buf2 =buflist_new ( path2 , buffer . BLN_LISTED ) 108local buf3 =buflist_new ( path3 , buffer . BLN_LISTED ) 109 110-- Then: buf2 is the buffer that is found 111eq ( buf2 . handle , buflist_findpat ( 'test' , ONLY_LISTED )) 112--} 113 114--{ When: We close buf2 115close_buffer ( NULL , buf2 , buffer . DOBUF_WIPE , 0 , 0 , 0 ) 116 117-- And: Open buf1, which has 'file' in the middle of its name 118local buf1 =buflist_new ( path1 , buffer . BLN_LISTED ) 119 120-- Then: buf3 is found since 'file' appears at the end of the name 121eq ( buf3 . handle , buflist_findpat ( 'file' , ONLY_LISTED )) 122--} 123 124close_buffer ( NULL , buf1 , buffer . DOBUF_WIPE , 0 , 0 , 0 ) 125close_buffer ( NULL , buf3 , buffer . DOBUF_WIPE , 0 , 0 , 0 ) 126end ) 127 128itp ( 'should match a unique fragment of a file path' , function () 129local buf1 =buflist_new ( path1 , buffer . BLN_LISTED ) 130local buf2 =buflist_new ( path2 , buffer . BLN_LISTED ) 131local buf3 =buflist_new ( path3 , buffer . BLN_LISTED ) 132 133eq ( buf3 . handle , buflist_findpat ( '_test_' , ONLY_LISTED )) 134 135close_buffer ( NULL , buf1 , buffer . DOBUF_WIPE , 0 , 0 , 0 ) 136close_buffer ( NULL , buf2 , buffer . DOBUF_WIPE , 0 , 0 , 0 ) 137close_buffer ( NULL , buf3 , buffer . DOBUF_WIPE , 0 , 0 , 0 ) 138end ) 139 140itp ( 'should include / ignore unlisted buffers based on the flag.' , function () 141--{ Given: A buffer 142local buf3 =buflist_new ( path3 , buffer . BLN_LISTED ) 143 144-- Then: We should find the buffer when it is given a unique pattern 145eq ( buf3 . handle , buflist_findpat ( '_test_' , ONLY_LISTED )) 146--} 147 148--{ When: We unlist the buffer 149close_buffer ( NULL , buf3 , buffer . DOBUF_DEL , 0 , 0 , 0 ) 150 151-- Then: It should not find the buffer when searching only listed buffers 152eq ( - 1 , buflist_findpat ( '_test_' , ONLY_LISTED )) 153 154-- And: It should find the buffer when including unlisted buffers 155eq ( buf3 . handle , buflist_findpat ( '_test_' , ALLOW_UNLISTED )) 156--} 157 158--{ When: We wipe the buffer 159close_buffer ( NULL , buf3 , buffer . DOBUF_WIPE , 0 , 0 , 0 ) 160 161-- Then: It should not find the buffer at all 162eq ( - 1 , buflist_findpat ( '_test_' , ONLY_LISTED )) 163eq ( - 1 , buflist_findpat ( '_test_' , ALLOW_UNLISTED )) 164--} 165end ) 166 167itp ( 'should prefer listed buffers to unlisted buffers.' , function () 168--{ Given: Two buffers that match a pattern 169local buf1 =buflist_new ( path1 , buffer . BLN_LISTED ) 170local buf2 =buflist_new ( path2 , buffer . BLN_LISTED ) 171 172-- Then: The first buffer is preferred when both are listed 173eq ( buf1 . handle , buflist_findpat ( 'test' , ONLY_LISTED )) 174--} 175 176--{ When: The first buffer is unlisted 177close_buffer ( NULL , buf1 , buffer . DOBUF_DEL , 0 , 0 , 0 ) 178 179-- Then: The second buffer is preferred because 180-- unlisted buffers are not allowed 181eq ( buf2 . handle , buflist_findpat ( 'test' , ONLY_LISTED )) 182--} 183 184--{ When: We allow unlisted buffers 185-- Then: The second buffer is still preferred 186-- because listed buffers are preferred to unlisted 187eq ( buf2 . handle , buflist_findpat ( 'test' , ALLOW_UNLISTED )) 188--} 189 190--{ When: We unlist the second buffer 191close_buffer ( NULL , buf2 , buffer . DOBUF_DEL , 0 , 0 , 0 ) 192 193-- Then: The first buffer is preferred again 194-- because buf1 matches better which takes precedence 195-- when both buffers have the same listing status. 196eq ( buf1 . handle , buflist_findpat ( 'test' , ALLOW_UNLISTED )) 197 198-- And: Neither buffer is returned when ignoring unlisted 199eq ( - 1 , buflist_findpat ( 'test' , ONLY_LISTED )) 200--} 201 202close_buffer ( NULL , buf1 , buffer . DOBUF_WIPE , 0 , 0 , 0 ) 203close_buffer ( NULL , buf2 , buffer . DOBUF_WIPE , 0 , 0 , 0 ) 204end ) 205end ) 206end )