Skip to content

Commit d30677e

Browse files
committed
test(spec): refactor tests & specs logs to be more readable
1 parent 3dfc842 commit d30677e

33 files changed

Lines changed: 180 additions & 136 deletions

script/run_tests.lua

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,19 @@ local failures = {}
1010

1111
for _, test in ipairs(tests) do
1212
local name = test and test.name or "anonymous"
13+
helpers.start_spec(name)
14+
vim.api.nvim_echo({ { "spec: " .. name .. "\n", "MoreMsg" } }, false, {})
1315
local ok, err = pcall(function()
1416
if test and test.run then
1517
test.run()
1618
else
1719
error("test missing run()")
1820
end
1921
end)
22+
local spec_total = helpers.finish_spec(name)
23+
vim.api.nvim_echo({
24+
{ string.format("spec: %s (cases: %d)\n", name, spec_total), "MoreMsg" },
25+
}, false, {})
2026
if not ok then
2127
table.insert(failures, { name = name, err = err })
2228
end
@@ -28,6 +34,8 @@ if #failures > 0 then
2834
end
2935
vim.cmd.cq()
3036
else
31-
vim.api.nvim_echo({ { "All tests passed\n", "MoreMsg" } }, false, {})
37+
vim.api.nvim_echo({
38+
{ string.format("All tests passed (total cases: %d)\n", helpers.total_cases()), "MoreMsg" },
39+
}, false, {})
3240
vim.cmd("qa!")
3341
end

script/test_utils.lua

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
local M = {}
2+
M._counts = {
3+
total = 0,
4+
spec = {},
5+
current = nil,
6+
}
27

38
function M.count_table_entries(value)
49
return vim.tbl_count(value or {})
@@ -21,6 +26,7 @@ function M.setup_global_state()
2126
vim.o.shada = ""
2227
vim.o.updatecount = 0
2328
vim.o.hidden = true
29+
vim.o.showmode = false
2430
end
2531

2632
function M.setup_runtime()
@@ -288,7 +294,10 @@ function M.with_cwd(dir_path, callback)
288294
end
289295

290296
function M.run_test_case(name, callback)
291-
print(string.format("running: %s", tostring(name)))
297+
print(string.format("case: %s", tostring(name)))
298+
local current = M._counts.current or "unknown"
299+
M._counts.total = M._counts.total + 1
300+
M._counts.spec[current] = (M._counts.spec[current] or 0) + 1
292301
M.reset_state()
293302
local ok, err = pcall(callback)
294303
M.reset_state()
@@ -297,6 +306,21 @@ function M.run_test_case(name, callback)
297306
end
298307
end
299308

309+
function M.start_spec(name)
310+
M._counts.current = tostring(name or "unknown")
311+
M._counts.spec[M._counts.current] = M._counts.spec[M._counts.current] or 0
312+
end
313+
314+
function M.finish_spec(name)
315+
local spec_name = tostring(name or M._counts.current or "unknown")
316+
M._counts.current = nil
317+
return M._counts.spec[spec_name] or 0
318+
end
319+
320+
function M.total_cases()
321+
return M._counts.total or 0
322+
end
323+
300324
function M.with_mock(target, key, value, callback)
301325
local original = target[key]
302326
target[key] = value

tests/blines_spec.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ local helpers = require("script.test_utils")
44
local M = { name = "blines" }
55

66
function M.run()
7-
helpers.run_test_case("blines", function()
7+
helpers.run_test_case("blines_basic", function()
88
local buf = helpers.create_named_buffer("blines.txt", {
99
"first",
1010
"second",

tests/btags_spec.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ local helpers = require("script.test_utils")
44
local M = { name = "btags" }
55

66
function M.run()
7-
helpers.run_test_case("btags", function()
7+
helpers.run_test_case("btags_basic", function()
88
local buf = helpers.create_named_buffer("btags.txt", { "alpha", "beta" })
99
vim.api.nvim_set_current_buf(buf)
1010
local tag_list = {

tests/buffers_spec.lua

Lines changed: 72 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -133,17 +133,19 @@ function M.run()
133133
}),
134134
}
135135

136-
run_test_case("default", {
137-
sort_lastused = true,
138-
ignore_current_buffer = false,
139-
show_unlisted = true,
140-
show_unloaded = true,
141-
preview = false,
142-
icons = false,
143-
}, {
144-
include = { bufs.buf1, bufs.buf2 },
145-
display = display,
146-
})
136+
helpers.run_test_case("buffers_default", function()
137+
run_test_case("default", {
138+
sort_lastused = true,
139+
ignore_current_buffer = false,
140+
show_unlisted = true,
141+
show_unloaded = true,
142+
preview = false,
143+
icons = false,
144+
}, {
145+
include = { bufs.buf1, bufs.buf2 },
146+
display = display,
147+
})
148+
end)
147149

148150
helpers.run_test_case("buffers_highlights", function()
149151
local picker = helpers.open_buffers_picker({
@@ -161,59 +163,69 @@ function M.run()
161163
picker:close()
162164
end)
163165

164-
run_test_case("ignore_current", {
165-
ignore_current_buffer = true,
166-
show_unlisted = true,
167-
show_unloaded = true,
168-
preview = false,
169-
icons = false,
170-
}, {
171-
exclude = { vim.api.nvim_get_current_buf() },
172-
include = { bufs.buf2 },
173-
display = display,
174-
})
166+
helpers.run_test_case("buffers_ignore_current", function()
167+
run_test_case("ignore_current", {
168+
ignore_current_buffer = true,
169+
show_unlisted = true,
170+
show_unloaded = true,
171+
preview = false,
172+
icons = false,
173+
}, {
174+
exclude = { vim.api.nvim_get_current_buf() },
175+
include = { bufs.buf2 },
176+
display = display,
177+
})
178+
end)
175179

176-
run_test_case("unlisted", {
177-
show_unlisted = false,
178-
show_unloaded = true,
179-
preview = false,
180-
icons = false,
181-
}, {
182-
exclude = { bufs.buf3 },
183-
display = display,
184-
})
180+
helpers.run_test_case("buffers_unlisted", function()
181+
run_test_case("unlisted", {
182+
show_unlisted = false,
183+
show_unloaded = true,
184+
preview = false,
185+
icons = false,
186+
}, {
187+
exclude = { bufs.buf3 },
188+
display = display,
189+
})
190+
end)
185191

186-
run_test_case("listed", {
187-
show_unlisted = true,
188-
show_unloaded = true,
189-
preview = false,
190-
icons = false,
191-
}, {
192-
include = { bufs.buf3 },
193-
display = display,
194-
})
192+
helpers.run_test_case("buffers_listed", function()
193+
run_test_case("listed", {
194+
show_unlisted = true,
195+
show_unloaded = true,
196+
preview = false,
197+
icons = false,
198+
}, {
199+
include = { bufs.buf3 },
200+
display = display,
201+
})
202+
end)
195203

196-
run_test_case("query", {
197-
show_unlisted = true,
198-
show_unloaded = true,
199-
preview = false,
200-
icons = false,
201-
}, {
202-
query = "buffer one",
203-
display = display,
204-
})
204+
helpers.run_test_case("buffers_query", function()
205+
run_test_case("query", {
206+
show_unlisted = true,
207+
show_unloaded = true,
208+
preview = false,
209+
icons = false,
210+
}, {
211+
query = "buffer one",
212+
display = display,
213+
})
214+
end)
205215

206-
run_test_case("cwd", {
207-
show_unlisted = true,
208-
show_unloaded = true,
209-
preview = false,
210-
icons = false,
211-
cwd = cwd_dir,
212-
}, {
213-
include = { cwd_buf },
214-
exclude = { other_buf },
215-
display = display,
216-
})
216+
helpers.run_test_case("buffers_cwd", function()
217+
run_test_case("cwd", {
218+
show_unlisted = true,
219+
show_unloaded = true,
220+
preview = false,
221+
icons = false,
222+
cwd = cwd_dir,
223+
}, {
224+
include = { cwd_buf },
225+
exclude = { other_buf },
226+
display = display,
227+
})
228+
end)
217229

218230
helpers.run_test_case("cwd_uses_content_arg", function()
219231
local Picker = require("fuzzy.picker")

tests/changes_spec.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ local helpers = require("script.test_utils")
44
local M = { name = "changes" }
55

66
function M.run()
7-
helpers.run_test_case("changes", function()
7+
helpers.run_test_case("changes_basic", function()
88
local buf = helpers.create_named_buffer("changes.txt", { "one", "two" })
99
vim.api.nvim_set_current_buf(buf)
1010
local change_list = {

tests/colorscheme_spec.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ local helpers = require("script.test_utils")
44
local M = { name = "colorscheme" }
55

66
function M.run()
7-
helpers.run_test_case("colorscheme", function()
7+
helpers.run_test_case("colorscheme_basic", function()
88
helpers.with_mock(vim.fn, "getcompletion", function(_, _)
99
return { "colorscheme-a" }
1010
end, function()

tests/command_history_spec.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ local helpers = require("script.test_utils")
44
local M = { name = "command_history" }
55

66
function M.run()
7-
helpers.run_test_case("command_history", function()
7+
helpers.run_test_case("command_history_basic", function()
88
helpers.with_mock_map(vim.fn, {
99
histnr = function()
1010
return 2

tests/commands_spec.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ local helpers = require("script.test_utils")
44
local M = { name = "commands" }
55

66
function M.run()
7-
helpers.run_test_case("commands", function()
7+
helpers.run_test_case("commands_basic", function()
88
helpers.with_mock(vim.api, "nvim_get_commands", function(opts)
99
if opts and opts.builtin then
1010
return { edit = {}, write = {} }

tests/files_spec.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ local Picker = require("fuzzy.picker")
66
local M = { name = "files" }
77

88
function M.run()
9-
helpers.run_test_case("files", function()
9+
helpers.run_test_case("files_basic", function()
1010
helpers.with_mock(util, "command_is_available", function(command_name)
1111
if command_name == "rg" or command_name == "fd" then
1212
return false

0 commit comments

Comments
 (0)