robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

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