Skip to content

Commit ba27e82

Browse files
committed
test(specs): expand the match and stream specs with more tests
1 parent d30677e commit ba27e82

2 files changed

Lines changed: 510 additions & 34 deletions

File tree

tests/match_spec.lua

Lines changed: 240 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ local function run_basic_case()
1212
local match = new_match({ step = 1, timer = 2 })
1313
helpers.assert_ok(match:options() ~= nil, "match options")
1414
helpers.eq(match:options().step, 1, "match options reference")
15-
local list = { "alpha", "beta", "gamma", "delta" }
15+
local list = {}
16+
for i = 1, 500 do
17+
list[i] = string.format("item-%03d", i)
18+
end
19+
list[1] = "alpha"
1620
match:match(list, "a", function() end)
1721

1822
local results = match:wait(1500)
@@ -29,6 +33,9 @@ local function run_transform_key()
2933
{ text = "alpha" },
3034
{ text = "beta" },
3135
}
36+
for i = 3, 300 do
37+
list[i] = { text = string.format("item-%03d", i) }
38+
end
3239

3340
match:match(list, "alp", function() end, "text")
3441
local results = match:wait(1500)
@@ -40,11 +47,13 @@ end
4047

4148
local function run_text_cb()
4249
local match = new_match({ step = 1, timer = 2 })
43-
local list = {
44-
{ text = "foo" },
45-
{ text = "bar" },
46-
{ text = "baz" },
47-
}
50+
local list = {}
51+
for i = 1, 400 do
52+
list[i] = { text = string.format("item-%03d", i) }
53+
end
54+
list[10] = { text = "foo" }
55+
list[20] = { text = "bar" }
56+
list[30] = { text = "baz" }
4857

4958
local expected = vim.fn.matchfuzzypos(list, "foo", {
5059
text_cb = function(entry)
@@ -66,7 +75,7 @@ end
6675
local function run_stop_case()
6776
local match = new_match({ step = 1, timer = 50 })
6877
local list = {}
69-
for i = 1, 200 do
78+
for i = 1, 2000 do
7079
list[i] = "item-" .. i
7180
end
7281

@@ -80,7 +89,10 @@ end
8089

8190
local function run_destroy_case()
8291
local match = new_match({ step = 2, timer = 2 })
83-
local list = { "alpha", "beta" }
92+
local list = {}
93+
for i = 1, 1000 do
94+
list[i] = string.format("item-%03d", i)
95+
end
8496

8597
match:match(list, "a", function() end)
8698
match:wait(1500)
@@ -111,13 +123,233 @@ local function run_merge_case()
111123
helpers.eq(merged[3][3], 7, "merge")
112124
end
113125

126+
local function run_no_results_case()
127+
local match = new_match({ step = 2, timer = 2 })
128+
local list = {}
129+
for i = 1, 1000 do
130+
list[i] = string.format("item-%03d", i)
131+
end
132+
local calls = 0
133+
local saw_nil = false
134+
135+
match:match(list, "zzz", function(results)
136+
if results == nil then
137+
saw_nil = true
138+
else
139+
calls = calls + 1
140+
end
141+
end)
142+
143+
local results = match:wait(1500)
144+
helpers.assert_ok(results ~= nil, "no results nil")
145+
helpers.eq(#(results or {}), 0, "no results empty")
146+
helpers.eq(calls, 0, "no results callback")
147+
helpers.assert_ok(saw_nil, "no results nil callback")
148+
end
149+
150+
local function run_callback_nil_case()
151+
local match = new_match({ step = 50, timer = 1 })
152+
local list = {}
153+
for i = 1, 800 do
154+
list[i] = string.format("item-%03d", i)
155+
end
156+
list[1] = "alpha"
157+
local saw_nil = false
158+
local saw_non_nil = false
159+
160+
match:match(list, "item", function(results)
161+
if results == nil then
162+
saw_nil = true
163+
else
164+
saw_non_nil = true
165+
end
166+
end)
167+
168+
match:wait(4000)
169+
helpers.wait_for(function()
170+
return saw_nil
171+
end, 4000)
172+
helpers.assert_ok(saw_non_nil, "callback non-nil")
173+
helpers.assert_ok(saw_nil, "callback nil")
174+
end
175+
176+
local function run_multi_chunk_case()
177+
local match = new_match({ step = 1, timer = 2 })
178+
local list = {}
179+
for i = 1, 200 do
180+
list[i] = string.format("item-%03d", i)
181+
end
182+
local calls = 0
183+
local saw_sizes = {}
184+
185+
match:match(list, "item", function(results)
186+
if results ~= nil then
187+
calls = calls + 1
188+
saw_sizes[#saw_sizes + 1] = #results[1]
189+
end
190+
end)
191+
local results = match:wait(1500)
192+
helpers.assert_ok(results ~= nil, "multi chunk nil")
193+
helpers.assert_ok(calls >= 2, "multi chunk calls")
194+
helpers.eq(#results[1], 200, "multi chunk results")
195+
helpers.assert_ok(saw_sizes[1] ~= nil, "multi chunk size track")
196+
helpers.assert_ok(saw_sizes[#saw_sizes] == 200, "multi chunk final size")
197+
end
198+
199+
local function run_transform_function_case()
200+
local match = new_match({ step = 2, timer = 2 })
201+
local list = {}
202+
for i = 1, 600 do
203+
list[i] = { name = string.format("item-%03d", i), id = i }
204+
end
205+
list[1] = { name = "alpha", id = 1 }
206+
match:match(list, "alp", function() end, function(entry)
207+
return entry.name
208+
end)
209+
local results = match:wait(1500)
210+
helpers.assert_ok(results ~= nil, "transform fn nil")
211+
helpers.eq(results[1][1].id, 1, "transform fn entry")
212+
end
213+
214+
local function run_wait_timeout_case()
215+
local match = new_match({ step = 1, timer = 1000, timeout = 1 })
216+
local list = {}
217+
for i = 1, 5000 do
218+
list[i] = "item-" .. i
219+
end
220+
local saw_results = false
221+
match:match(list, "item", function() end)
222+
local results = match:wait(1)
223+
helpers.assert_ok(results ~= nil, "wait timeout results")
224+
helpers.assert_ok(match:running() == false, "wait timeout stopped")
225+
helpers.assert_ok(results and results[1] and #results[1] > 0, "wait timeout entries")
226+
saw_results = results ~= nil
227+
helpers.assert_ok(saw_results, "wait timeout saw results")
228+
end
229+
230+
local function run_restart_ephemeral_true_case()
231+
local match = new_match({ step = 1, timer = 2, ephemeral = true })
232+
local first = {}
233+
local second = {}
234+
for i = 1, 500 do
235+
first[i] = string.format("alpha-%03d", i)
236+
second[i] = string.format("gamma-%03d", i)
237+
end
238+
239+
match:match(first, "a", function() end)
240+
match:match(second, "g", function() end)
241+
local results = match:wait(1500)
242+
helpers.assert_ok(results ~= nil, "restart ephemeral nil")
243+
local found_gamma = false
244+
local found_alpha = false
245+
for _, entry in ipairs(results[1] or {}) do
246+
if entry:find("gamma-", 1, true) then
247+
found_gamma = true
248+
end
249+
if entry:find("alpha-", 1, true) then
250+
found_alpha = true
251+
end
252+
end
253+
helpers.assert_ok(found_gamma, "restart ephemeral results")
254+
helpers.assert_ok(not found_alpha, "restart ephemeral old")
255+
end
256+
257+
local function run_restart_ephemeral_false_case()
258+
local match = new_match({ step = 1, timer = 2, ephemeral = false })
259+
local first = {}
260+
local second = {}
261+
for i = 1, 500 do
262+
first[i] = string.format("alpha-%03d", i)
263+
second[i] = string.format("gamma-%03d", i)
264+
end
265+
266+
match:match(first, "a", function() end)
267+
match:wait(1500)
268+
match:match(second, "g", function() end)
269+
local results = match:wait(1500)
270+
helpers.assert_ok(results ~= nil, "restart non-ephemeral nil")
271+
local found_gamma = false
272+
local found_alpha = false
273+
for _, entry in ipairs(results[1] or {}) do
274+
if entry:find("gamma-", 1, true) then
275+
found_gamma = true
276+
end
277+
if entry:find("alpha-", 1, true) then
278+
found_alpha = true
279+
end
280+
end
281+
helpers.assert_ok(found_gamma, "restart non-ephemeral results")
282+
helpers.assert_ok(not found_alpha, "restart non-ephemeral old")
283+
end
284+
285+
local function run_tail_chunk_case()
286+
local match = new_match({ step = 2, timer = 2 })
287+
local list = {}
288+
for i = 1, 503 do
289+
list[i] = string.format("item-%03d", i)
290+
end
291+
match:match(list, "item", function() end)
292+
local results = match:wait(1500)
293+
helpers.assert_ok(results ~= nil, "tail chunk nil")
294+
helpers.eq(#results[1], 503, "tail chunk results")
295+
end
296+
297+
local function run_positions_shape_case()
298+
local match = new_match({ step = 2, timer = 2 })
299+
local list = {}
300+
for i = 1, 700 do
301+
list[i] = string.format("item-%03d", i)
302+
end
303+
list[1] = "alpha"
304+
list[2] = "beta"
305+
list[3] = "gamma"
306+
match:match(list, "a", function() end)
307+
local results = match:wait(1500)
308+
helpers.assert_ok(results ~= nil, "positions nil")
309+
local positions = results[2] or {}
310+
for _, pos in ipairs(positions) do
311+
helpers.assert_ok(#pos % 2 == 0, "positions even pairs")
312+
end
313+
end
314+
315+
local function run_merge_equal_score_case()
316+
local source = {
317+
{ "", "" },
318+
{ {}, {} },
319+
{ 0, 0 },
320+
}
321+
local left = {
322+
{ "a" },
323+
{ { 1 } },
324+
{ 5 },
325+
}
326+
local right = {
327+
{ "b" },
328+
{ { 2 } },
329+
{ 5 },
330+
}
331+
local merged = Match.merge(source, left, right)
332+
helpers.eq(merged[1][1], "b", "merge equal score prefers right")
333+
helpers.eq(merged[1][2], "a", "merge equal score order")
334+
end
335+
114336
function M.run()
115337
helpers.run_test_case("match_basic", run_basic_case)
116338
helpers.run_test_case("match_transform_key", run_transform_key)
117339
helpers.run_test_case("match_text_cb", run_text_cb)
118340
helpers.run_test_case("match_stop", run_stop_case)
119341
helpers.run_test_case("match_destroy", run_destroy_case)
120342
helpers.run_test_case("match_merge", run_merge_case)
343+
helpers.run_test_case("match_no_results", run_no_results_case)
344+
helpers.run_test_case("match_callback_nil", run_callback_nil_case)
345+
helpers.run_test_case("match_multi_chunk", run_multi_chunk_case)
346+
helpers.run_test_case("match_transform_function", run_transform_function_case)
347+
helpers.run_test_case("match_wait_timeout", run_wait_timeout_case)
348+
helpers.run_test_case("match_restart_ephemeral_true", run_restart_ephemeral_true_case)
349+
helpers.run_test_case("match_restart_ephemeral_false", run_restart_ephemeral_false_case)
350+
helpers.run_test_case("match_tail_chunk", run_tail_chunk_case)
351+
helpers.run_test_case("match_positions_shape", run_positions_shape_case)
352+
helpers.run_test_case("match_merge_equal_score", run_merge_equal_score_case)
121353
end
122354

123355
return M

0 commit comments

Comments
 (0)