buffer_spec.lua 6.6 KB

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