buffer_spec.lua 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. local helpers = require("test.unit.helpers")(after_each)
  2. local itp = helpers.gen_itp(it)
  3. local to_cstr = helpers.to_cstr
  4. local get_str = helpers.ffi.string
  5. local eq = helpers.eq
  6. local NULL = helpers.NULL
  7. local globals = helpers.cimport("./src/nvim/globals.h")
  8. local buffer = helpers.cimport("./src/nvim/buffer.h")
  9. local stl = helpers.cimport("./src/nvim/statusline.h")
  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. local close_buffer = function(win, buf, action, abort_if_last, ignore_abort)
  16. return buffer.close_buffer(win, buf, action, abort_if_last, ignore_abort)
  17. end
  18. local path1 = 'test_file_path'
  19. local path2 = 'file_path_test'
  20. local path3 = 'path_test_file'
  21. setup(function()
  22. -- create the files
  23. io.open(path1, 'w').close()
  24. io.open(path2, 'w').close()
  25. io.open(path3, 'w').close()
  26. end)
  27. teardown(function()
  28. os.remove(path1)
  29. os.remove(path2)
  30. os.remove(path3)
  31. end)
  32. describe('buf_valid', function()
  33. itp('should view NULL as an invalid buffer', function()
  34. eq(false, buffer.buf_valid(NULL))
  35. end)
  36. itp('should view an open buffer as valid', function()
  37. local buf = buflist_new(path1, buffer.BLN_LISTED)
  38. eq(true, buffer.buf_valid(buf))
  39. end)
  40. itp('should view a closed and hidden buffer as valid', function()
  41. local buf = buflist_new(path1, buffer.BLN_LISTED)
  42. close_buffer(NULL, buf, 0, 0, 0)
  43. eq(true, buffer.buf_valid(buf))
  44. end)
  45. itp('should view a closed and unloaded buffer as valid', function()
  46. local buf = buflist_new(path1, buffer.BLN_LISTED)
  47. close_buffer(NULL, buf, buffer.DOBUF_UNLOAD, 0, 0)
  48. eq(true, buffer.buf_valid(buf))
  49. end)
  50. itp('should view a closed and wiped buffer as invalid', function()
  51. local buf = buflist_new(path1, buffer.BLN_LISTED)
  52. close_buffer(NULL, buf, buffer.DOBUF_WIPE, 0, 0)
  53. eq(false, buffer.buf_valid(buf))
  54. end)
  55. end)
  56. describe('buflist_findpat', function()
  57. local ALLOW_UNLISTED = 1
  58. local ONLY_LISTED = 0
  59. local buflist_findpat = function(pat, allow_unlisted)
  60. return buffer.buflist_findpat(to_cstr(pat), NULL, allow_unlisted, 0, 0)
  61. end
  62. itp('should find exact matches', function()
  63. local buf = buflist_new(path1, buffer.BLN_LISTED)
  64. eq(buf.handle, buflist_findpat(path1, ONLY_LISTED))
  65. close_buffer(NULL, buf, buffer.DOBUF_WIPE, 0, 0)
  66. end)
  67. itp('should prefer to match the start of a file path', function()
  68. local buf1 = buflist_new(path1, buffer.BLN_LISTED)
  69. local buf2 = buflist_new(path2, buffer.BLN_LISTED)
  70. local buf3 = buflist_new(path3, buffer.BLN_LISTED)
  71. eq(buf1.handle, buflist_findpat("test", ONLY_LISTED))
  72. eq(buf2.handle, buflist_findpat("file", ONLY_LISTED))
  73. eq(buf3.handle, buflist_findpat("path", ONLY_LISTED))
  74. close_buffer(NULL, buf1, buffer.DOBUF_WIPE, 0, 0)
  75. close_buffer(NULL, buf2, buffer.DOBUF_WIPE, 0, 0)
  76. close_buffer(NULL, buf3, buffer.DOBUF_WIPE, 0, 0)
  77. end)
  78. itp('should prefer to match the end of a file over the middle', function()
  79. --{ Given: Two buffers, where 'test' appears in both
  80. -- And: 'test' appears at the end of buf3 but in the middle of buf2
  81. local buf2 = buflist_new(path2, buffer.BLN_LISTED)
  82. local buf3 = buflist_new(path3, buffer.BLN_LISTED)
  83. -- Then: buf2 is the buffer that is found
  84. eq(buf2.handle, buflist_findpat("test", ONLY_LISTED))
  85. --}
  86. --{ When: We close buf2
  87. close_buffer(NULL, buf2, buffer.DOBUF_WIPE, 0, 0)
  88. -- And: Open buf1, which has 'file' in the middle of its name
  89. local buf1 = buflist_new(path1, buffer.BLN_LISTED)
  90. -- Then: buf3 is found since 'file' appears at the end of the name
  91. eq(buf3.handle, buflist_findpat("file", ONLY_LISTED))
  92. --}
  93. close_buffer(NULL, buf1, buffer.DOBUF_WIPE, 0, 0)
  94. close_buffer(NULL, buf3, buffer.DOBUF_WIPE, 0, 0)
  95. end)
  96. itp('should match a unique fragment of a file path', function()
  97. local buf1 = buflist_new(path1, buffer.BLN_LISTED)
  98. local buf2 = buflist_new(path2, buffer.BLN_LISTED)
  99. local buf3 = buflist_new(path3, buffer.BLN_LISTED)
  100. eq(buf3.handle, buflist_findpat("_test_", ONLY_LISTED))
  101. close_buffer(NULL, buf1, buffer.DOBUF_WIPE, 0, 0)
  102. close_buffer(NULL, buf2, buffer.DOBUF_WIPE, 0, 0)
  103. close_buffer(NULL, buf3, buffer.DOBUF_WIPE, 0, 0)
  104. end)
  105. itp('should include / ignore unlisted buffers based on the flag.', function()
  106. --{ Given: A buffer
  107. local buf3 = buflist_new(path3, buffer.BLN_LISTED)
  108. -- Then: We should find the buffer when it is given a unique pattern
  109. eq(buf3.handle, buflist_findpat("_test_", ONLY_LISTED))
  110. --}
  111. --{ When: We unlist the buffer
  112. close_buffer(NULL, buf3, buffer.DOBUF_DEL, 0, 0)
  113. -- Then: It should not find the buffer when searching only listed buffers
  114. eq(-1, buflist_findpat("_test_", ONLY_LISTED))
  115. -- And: It should find the buffer when including unlisted buffers
  116. eq(buf3.handle, buflist_findpat("_test_", ALLOW_UNLISTED))
  117. --}
  118. --{ When: We wipe the buffer
  119. close_buffer(NULL, buf3, buffer.DOBUF_WIPE, 0, 0)
  120. -- Then: It should not find the buffer at all
  121. eq(-1, buflist_findpat("_test_", ONLY_LISTED))
  122. eq(-1, buflist_findpat("_test_", ALLOW_UNLISTED))
  123. --}
  124. end)
  125. itp('should prefer listed buffers to unlisted buffers.', function()
  126. --{ Given: Two buffers that match a pattern
  127. local buf1 = buflist_new(path1, buffer.BLN_LISTED)
  128. local buf2 = buflist_new(path2, buffer.BLN_LISTED)
  129. -- Then: The first buffer is preferred when both are listed
  130. eq(buf1.handle, buflist_findpat("test", ONLY_LISTED))
  131. --}
  132. --{ When: The first buffer is unlisted
  133. close_buffer(NULL, buf1, buffer.DOBUF_DEL, 0, 0)
  134. -- Then: The second buffer is preferred because
  135. -- unlisted buffers are not allowed
  136. eq(buf2.handle, buflist_findpat("test", ONLY_LISTED))
  137. --}
  138. --{ When: We allow unlisted buffers
  139. -- Then: The second buffer is still preferred
  140. -- because listed buffers are preferred to unlisted
  141. eq(buf2.handle, buflist_findpat("test", ALLOW_UNLISTED))
  142. --}
  143. --{ When: We unlist the second buffer
  144. close_buffer(NULL, buf2, buffer.DOBUF_DEL, 0, 0)
  145. -- Then: The first buffer is preferred again
  146. -- because buf1 matches better which takes precedence
  147. -- when both buffers have the same listing status.
  148. eq(buf1.handle, buflist_findpat("test", ALLOW_UNLISTED))
  149. -- And: Neither buffer is returned when ignoring unlisted
  150. eq(-1, buflist_findpat("test", ONLY_LISTED))
  151. --}
  152. close_buffer(NULL, buf1, buffer.DOBUF_WIPE, 0, 0)
  153. close_buffer(NULL, buf2, buffer.DOBUF_WIPE, 0, 0)
  154. end)
  155. end)
  156. describe('build_stl_str_hl', function()
  157. local buffer_byte_size = 100
  158. local STL_INITIAL_ITEMS = 20
  159. local output_buffer = ''
  160. -- This function builds the statusline
  161. --
  162. -- @param arg Optional arguments are:
  163. -- .pat The statusline format string
  164. -- .fillchar The fill character used in the statusline
  165. -- .maximum_cell_count The number of cells available in the statusline
  166. local function build_stl_str_hl(arg)
  167. output_buffer = to_cstr(string.rep(" ", buffer_byte_size))
  168. local pat = arg.pat or ''
  169. local fillchar = arg.fillchar or (' '):byte()
  170. local maximum_cell_count = arg.maximum_cell_count or buffer_byte_size
  171. return stl.build_stl_str_hl(globals.curwin,
  172. output_buffer,
  173. buffer_byte_size,
  174. to_cstr(pat),
  175. false,
  176. fillchar,
  177. maximum_cell_count,
  178. NULL,
  179. NULL)
  180. end
  181. -- Use this function to simplify testing the comparison between
  182. -- the format string and the resulting statusline.
  183. --
  184. -- @param description The description of what the test should be doing
  185. -- @param statusline_cell_count The number of cells available in the statusline
  186. -- @param input_stl The format string for the statusline
  187. -- @param expected_stl The expected result string for the statusline
  188. --
  189. -- @param arg Options can be placed in an optional dictionary as the last parameter
  190. -- .expected_cell_count The expected number of cells build_stl_str_hl will return
  191. -- .expected_byte_length The expected byte length of the string (defaults to byte length of expected_stl)
  192. -- .file_name The name of the file to be tested (useful in %f type tests)
  193. -- .fillchar The character that will be used to fill any 'extra' space in the stl
  194. local function statusline_test (description,
  195. statusline_cell_count,
  196. input_stl,
  197. expected_stl,
  198. arg)
  199. -- arg is the optional parameter
  200. -- so we either fill in option with arg or an empty dictionary
  201. local option = arg or {}
  202. local fillchar = option.fillchar or (' '):byte()
  203. local expected_cell_count = option.expected_cell_count or statusline_cell_count
  204. local expected_byte_length = option.expected_byte_length or #expected_stl
  205. itp(description, function()
  206. if option.file_name then
  207. buffer.setfname(globals.curbuf, to_cstr(option.file_name), NULL, 1)
  208. else
  209. buffer.setfname(globals.curbuf, nil, NULL, 1)
  210. end
  211. local result_cell_count = build_stl_str_hl{pat=input_stl,
  212. maximum_cell_count=statusline_cell_count,
  213. fillchar=fillchar}
  214. eq(expected_stl, get_str(output_buffer, expected_byte_length))
  215. eq(expected_cell_count, result_cell_count)
  216. end)
  217. end
  218. -- expression testing
  219. statusline_test('Should expand expression', 2,
  220. '%!expand(20+1)', '21')
  221. statusline_test('Should expand broken expression to itself', 11,
  222. '%!expand(20+1', 'expand(20+1')
  223. -- file name testing
  224. statusline_test('should print no file name', 10,
  225. '%f', '[No Name]',
  226. {expected_cell_count=9})
  227. statusline_test('should print the relative file name', 30,
  228. '%f', 'test/unit/buffer_spec.lua',
  229. {file_name='test/unit/buffer_spec.lua', expected_cell_count=25})
  230. statusline_test('should print the full file name', 40,
  231. '%F', '/test/unit/buffer_spec.lua',
  232. {file_name='/test/unit/buffer_spec.lua', expected_cell_count=26})
  233. -- fillchar testing
  234. statusline_test('should handle `!` as a fillchar', 10,
  235. 'abcde%=', 'abcde!!!!!',
  236. {fillchar=('!'):byte()})
  237. statusline_test('should handle `~` as a fillchar', 10,
  238. '%=abcde', '~~~~~abcde',
  239. {fillchar=('~'):byte()})
  240. statusline_test('should put fillchar `!` in between text', 10,
  241. 'abc%=def', 'abc!!!!def',
  242. {fillchar=('!'):byte()})
  243. statusline_test('should put fillchar `~` in between text', 10,
  244. 'abc%=def', 'abc~~~~def',
  245. {fillchar=('~'):byte()})
  246. statusline_test('should put fillchar `━` in between text', 10,
  247. 'abc%=def', 'abc━━━━def',
  248. {fillchar=0x2501})
  249. statusline_test('should handle zero-fillchar as a space', 10,
  250. 'abcde%=', 'abcde ',
  251. {fillchar=0})
  252. statusline_test('should print the tail file name', 80,
  253. '%t', 'buffer_spec.lua',
  254. {file_name='test/unit/buffer_spec.lua', expected_cell_count=15})
  255. -- standard text testing
  256. statusline_test('should copy plain text', 80,
  257. 'this is a test', 'this is a test',
  258. {expected_cell_count=14})
  259. -- line number testing
  260. statusline_test('should print the buffer number', 80,
  261. '%n', '1',
  262. {expected_cell_count=1})
  263. statusline_test('should print the current line number in the buffer', 80,
  264. '%l', '0',
  265. {expected_cell_count=1})
  266. statusline_test('should print the number of lines in the buffer', 80,
  267. '%L', '1',
  268. {expected_cell_count=1})
  269. -- truncation testing
  270. statusline_test('should truncate when standard text pattern is too long', 10,
  271. '0123456789abcde', '<6789abcde')
  272. statusline_test('should truncate when using =', 10,
  273. 'abcdef%=ghijkl', 'abcdef<jkl')
  274. statusline_test('should truncate centered text when using ==', 10,
  275. 'abcde%=gone%=fghij', 'abcde<ghij')
  276. statusline_test('should respect the `<` marker', 10,
  277. 'abc%<defghijkl', 'abc<ghijkl')
  278. statusline_test('should truncate at `<` with one `=`, test 1', 10,
  279. 'abc%<def%=ghijklmno', 'abc<jklmno')
  280. statusline_test('should truncate at `<` with one `=`, test 2', 10,
  281. 'abcdef%=ghijkl%<mno', 'abcdefghi>')
  282. statusline_test('should truncate at `<` with one `=`, test 3', 10,
  283. 'abc%<def%=ghijklmno', 'abc<jklmno')
  284. statusline_test('should truncate at `<` with one `=`, test 4', 10,
  285. 'abc%<def%=ghij', 'abcdefghij')
  286. statusline_test('should truncate at `<` with one `=`, test 4', 10,
  287. 'abc%<def%=ghijk', 'abc<fghijk')
  288. statusline_test('should truncate at `<` with many `=`, test 4', 10,
  289. 'ab%<cdef%=g%=h%=ijk', 'ab<efghijk')
  290. statusline_test('should truncate at the first `<`', 10,
  291. 'abc%<def%<ghijklm', 'abc<hijklm')
  292. statusline_test('should ignore trailing %', 3, 'abc%', 'abc')
  293. -- alignment testing with fillchar
  294. local function statusline_test_align (description,
  295. statusline_cell_count,
  296. input_stl,
  297. expected_stl,
  298. arg)
  299. arg = arg or {}
  300. statusline_test(description .. ' without fillchar',
  301. statusline_cell_count, input_stl, expected_stl:gsub('%~', ' '), arg)
  302. arg.fillchar = ('!'):byte()
  303. statusline_test(description .. ' with fillchar `!`',
  304. statusline_cell_count, input_stl, expected_stl:gsub('%~', '!'), arg)
  305. arg.fillchar = 0x2501
  306. statusline_test(description .. ' with fillchar `━`',
  307. statusline_cell_count, input_stl, expected_stl:gsub('%~', '━'), arg)
  308. end
  309. statusline_test_align('should right align when using =', 20,
  310. 'neo%=vim', 'neo~~~~~~~~~~~~~~vim')
  311. statusline_test_align('should, when possible, center text when using %=text%=', 20,
  312. 'abc%=neovim%=def', 'abc~~~~neovim~~~~def')
  313. statusline_test_align('should handle uneven spacing in the buffer when using %=text%=', 20,
  314. 'abc%=neo_vim%=def', 'abc~~~neo_vim~~~~def')
  315. statusline_test_align('should have equal spaces even with non-equal sides when using =', 20,
  316. 'foobar%=test%=baz', 'foobar~~~test~~~~baz')
  317. statusline_test_align('should have equal spaces even with longer right side when using =', 20,
  318. 'a%=test%=longtext', 'a~~~test~~~~longtext')
  319. statusline_test_align('should handle an empty left side when using ==', 20,
  320. '%=test%=baz', '~~~~~~test~~~~~~~baz')
  321. statusline_test_align('should handle an empty right side when using ==', 20,
  322. 'foobar%=test%=', 'foobar~~~~~test~~~~~')
  323. statusline_test_align('should handle consecutive empty ==', 20,
  324. '%=%=test%=', '~~~~~~~~~~test~~~~~~')
  325. statusline_test_align('should handle an = alone', 20,
  326. '%=', '~~~~~~~~~~~~~~~~~~~~')
  327. statusline_test_align('should right align text when it is alone with =', 20,
  328. '%=foo', '~~~~~~~~~~~~~~~~~foo')
  329. statusline_test_align('should left align text when it is alone with =', 20,
  330. 'foo%=', 'foo~~~~~~~~~~~~~~~~~')
  331. statusline_test_align('should approximately center text when using %=text%=', 21,
  332. 'abc%=neovim%=def', 'abc~~~~neovim~~~~~def')
  333. statusline_test_align('should completely fill the buffer when using %=text%=', 21,
  334. 'abc%=neo_vim%=def', 'abc~~~~neo_vim~~~~def')
  335. statusline_test_align('should have equal spacing even with non-equal sides when using =', 21,
  336. 'foobar%=test%=baz', 'foobar~~~~test~~~~baz')
  337. statusline_test_align('should have equal spacing even with longer right side when using =', 21,
  338. 'a%=test%=longtext', 'a~~~~test~~~~longtext')
  339. statusline_test_align('should handle an empty left side when using ==', 21,
  340. '%=test%=baz', '~~~~~~~test~~~~~~~baz')
  341. statusline_test_align('should handle an empty right side when using ==', 21,
  342. 'foobar%=test%=', 'foobar~~~~~test~~~~~~')
  343. statusline_test_align('should quadrant the text when using 3 %=', 40,
  344. 'abcd%=n%=eovim%=ef', 'abcd~~~~~~~~~n~~~~~~~~~eovim~~~~~~~~~~ef')
  345. statusline_test_align('should work well with %t', 40,
  346. '%t%=right_aligned', 'buffer_spec.lua~~~~~~~~~~~~right_aligned',
  347. {file_name='test/unit/buffer_spec.lua'})
  348. statusline_test_align('should work well with %t and regular text', 40,
  349. 'l%=m_l %t m_r%=r', 'l~~~~~~~m_l buffer_spec.lua m_r~~~~~~~~r',
  350. {file_name='test/unit/buffer_spec.lua'})
  351. statusline_test_align('should work well with %=, %t, %L, and %l', 40,
  352. '%t %= %L %= %l', 'buffer_spec.lua ~~~~~~~~~ 1 ~~~~~~~~~~ 0',
  353. {file_name='test/unit/buffer_spec.lua'})
  354. statusline_test_align('should quadrant the text when using 3 %=', 41,
  355. 'abcd%=n%=eovim%=ef', 'abcd~~~~~~~~~n~~~~~~~~~eovim~~~~~~~~~~~ef')
  356. statusline_test_align('should work well with %t', 41,
  357. '%t%=right_aligned', 'buffer_spec.lua~~~~~~~~~~~~~right_aligned',
  358. {file_name='test/unit/buffer_spec.lua'})
  359. statusline_test_align('should work well with %t and regular text', 41,
  360. 'l%=m_l %t m_r%=r', 'l~~~~~~~~m_l buffer_spec.lua m_r~~~~~~~~r',
  361. {file_name='test/unit/buffer_spec.lua'})
  362. statusline_test_align('should work well with %=, %t, %L, and %l', 41,
  363. '%t %= %L %= %l', 'buffer_spec.lua ~~~~~~~~~~ 1 ~~~~~~~~~~ 0',
  364. {file_name='test/unit/buffer_spec.lua'})
  365. statusline_test_align('should work with 10 %=', 50,
  366. 'aaaa%=b%=c%=d%=e%=fg%=hi%=jk%=lmnop%=qrstuv%=wxyz',
  367. 'aaaa~~b~~c~~d~~e~~fg~~hi~~jk~~lmnop~~qrstuv~~~wxyz')
  368. -- stl item testing
  369. local tabline = ''
  370. for i= 1, 1000 do
  371. tabline = tabline .. (i % 2 == 0 and '%#TabLineSel#' or '%#TabLineFill#') .. tostring(i % 2)
  372. end
  373. statusline_test('should handle a large amount of any items', 20,
  374. tabline,
  375. '<1010101010101010101') -- Should not show any error
  376. statusline_test('should handle a larger amount of = than stl initial item', 20,
  377. ('%='):rep(STL_INITIAL_ITEMS * 5),
  378. ' ') -- Should not show any error
  379. statusline_test('should handle many extra characters', 20,
  380. 'a' .. ('a'):rep(STL_INITIAL_ITEMS * 5),
  381. '<aaaaaaaaaaaaaaaaaaa') -- Does not show any error
  382. statusline_test('should handle many extra characters and flags', 20,
  383. 'a' .. ('%=a'):rep(STL_INITIAL_ITEMS * 2),
  384. 'a<aaaaaaaaaaaaaaaaaa') -- Should not show any error
  385. -- multi-byte testing
  386. statusline_test('should handle multibyte characters', 10,
  387. 'Ĉ%=x', 'Ĉ x')
  388. statusline_test('should handle multibyte characters and different fillchars', 10,
  389. 'Ą%=mid%=end', 'Ą@mid@@end',
  390. {fillchar=('@'):byte()})
  391. -- escaping % testing
  392. statusline_test('should handle escape of %', 4, 'abc%%', 'abc%')
  393. statusline_test('case where escaped % does not fit', 3, 'abc%%abcabc', '<bc')
  394. statusline_test('escaped % is first', 1, '%%', '%')
  395. end)
  396. end)