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