Skip to content

Commit 9b24b2d

Browse files
committed
refactor(test): assert source behavior
1 parent 8cc9a36 commit 9b24b2d

8 files changed

Lines changed: 423 additions & 549 deletions

File tree

‎tests/unit/source/astrolsp_spec.lua‎

Lines changed: 213 additions & 278 deletions
Large diffs are not rendered by default.

‎tests/unit/source/blink_spec.lua‎

Lines changed: 46 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,20 @@ end
8989

9090
local function kind_component(spec) return spec.opts.completion.menu.draw.components.kind_icon end
9191

92+
local function contains(values, expected)
93+
for _, value in ipairs(values) do
94+
if value == expected then return true end
95+
end
96+
return false
97+
end
98+
99+
local function find_callback(mapping)
100+
for _, action in ipairs(mapping) do
101+
if type(action) == "function" then return action end
102+
end
103+
error "Missing completion callback"
104+
end
105+
92106
T["BLINK-01 enables completion only for supported buffers"] = function()
93107
with_blink(nil, function(spec) assert.is_true(spec.opts.enabled()) end)
94108

@@ -135,58 +149,52 @@ T["BLINK-02 augments nil and existing LSP configuration without replacing capabi
135149
end)
136150
end
137151

138-
T["BLINK-03 keeps selected completion declarations and dispatches Tab callbacks"] = function()
152+
T["BLINK-03 keeps configured completion mappings and dispatches Tab callbacks"] = function()
139153
local cursor = { line = 1, column = 0, text = "" }
140154
local mode = { value = "i" }
141155
with_blink({ cursor = cursor, mode = mode }, function(spec)
142156
local opts = spec.opts
143-
assert.same({ "lsp", "path", "snippets", "buffer" }, opts.sources.default)
144-
assert.same({ "show", "show_documentation", "hide_documentation" }, opts.keymap["<C-Space>"])
145-
assert.same({ "accept", "fallback" }, opts.keymap["<CR>"])
146-
assert.same({ "hide", "fallback" }, opts.keymap["<C-e>"])
147-
assert.equals("prefer_rust", opts.fuzzy.implementation)
148-
assert.is_false(opts.completion.list.selection.preselect)
149-
assert.is_true(opts.completion.list.selection.auto_insert)
150-
assert.is_true(opts.completion.menu.auto_show { mode = "insert" })
151-
assert.is_false(opts.completion.menu.auto_show { mode = "cmdline" })
152-
assert.is_true(opts.completion.accept.auto_brackets.enabled)
153-
assert.is_true(opts.completion.documentation.auto_show)
154-
assert.equals(0, opts.completion.documentation.auto_show_delay_ms)
155-
assert.equals("Normal:NormalFloat,FloatBorder:FloatBorder", opts.signature.window.winhighlight)
156-
assert.same({ "hide", "fallback" }, opts.cmdline.keymap["<End>"])
157-
assert.is_false(opts.cmdline.completion.ghost_text.enabled)
158-
159-
assert.equals(4, #opts.keymap["<Tab>"])
160-
assert.equals("select_next", opts.keymap["<Tab>"][1])
161-
assert.equals("snippet_forward", opts.keymap["<Tab>"][2])
162-
assert.equals("function", type(opts.keymap["<Tab>"][3]))
163-
assert.equals("fallback", opts.keymap["<Tab>"][4])
164-
assert.equals(4, #opts.keymap["<S-Tab>"])
165-
assert.equals("select_prev", opts.keymap["<S-Tab>"][1])
166-
assert.equals("snippet_backward", opts.keymap["<S-Tab>"][2])
167-
assert.equals("function", type(opts.keymap["<S-Tab>"][3]))
168-
assert.equals("fallback", opts.keymap["<S-Tab>"][4])
157+
for _, source in ipairs { "lsp", "path", "snippets", "buffer" } do
158+
assert.is_true(contains(opts.sources.default, source), "Missing completion source: " .. source)
159+
end
160+
161+
for _, mapping in ipairs {
162+
{ key = "<C-Space>", actions = { "show", "show_documentation", "hide_documentation" } },
163+
{ key = "<CR>", actions = { "accept", "fallback" } },
164+
{ key = "<C-e>", actions = { "hide", "fallback" } },
165+
{ key = "<Tab>", actions = { "select_next", "snippet_forward", "fallback" } },
166+
{ key = "<S-Tab>", actions = { "select_prev", "snippet_backward", "fallback" } },
167+
{ key = "<End>", actions = { "hide", "fallback" }, command_line = true },
168+
} do
169+
local actions = mapping.command_line and opts.cmdline.keymap[mapping.key] or opts.keymap[mapping.key]
170+
for _, action in ipairs(mapping.actions) do
171+
assert.is_true(contains(actions, action), mapping.key .. " is missing " .. action)
172+
end
173+
end
169174

170175
local shown = 0
171176
local cmp = { show = function() shown = shown + 1 end }
172-
local tab = opts.keymap["<Tab>"][3]
173-
local shift_tab = opts.keymap["<S-Tab>"][3]
177+
local tab = find_callback(opts.keymap["<Tab>"])
178+
local shift_tab = find_callback(opts.keymap["<S-Tab>"])
174179

175180
mode.value = "c"
176181
tab(cmp)
177182
shift_tab(cmp)
178183
mode.value = "i"
184+
cursor.column = 1
185+
cursor.text = "a"
186+
tab(cmp)
179187
shift_tab(cmp)
180188

181-
assert.equals(2, shown)
189+
assert.equals(3, shown)
182190
end)
183191
end
184192

185193
T["BLINK-04 detects words before the cursor at stable boundaries"] = function()
186194
local cursor = { line = 1, column = 0, text = "text" }
187195
with_blink({ cursor = cursor }, function(spec)
188196
local shown = 0
189-
local tab = spec.opts.keymap["<Tab>"][3]
197+
local tab = find_callback(spec.opts.keymap["<Tab>"])
190198
local cmp = { show = function() shown = shown + 1 end }
191199

192200
tab(cmp)
@@ -410,36 +418,14 @@ T["BLINK-06 applies ASCII icons and dispatches completion toggles"] = function()
410418
find_spec(spec, "saghen/blink.cmp").opts(nil, blink_opts)
411419
local icons = blink_opts.appearance.kind_icons
412420

413-
assert.same({
414-
Text = "T",
415-
Method = "M",
416-
Function = "F",
417-
Constructor = "C",
418-
Field = "F",
419-
Variable = "V",
420-
Property = "P",
421-
Class = "C",
422-
Interface = "I",
423-
Struct = "S",
424-
Module = "M",
425-
Unit = "U",
426-
Value = "V",
427-
Enum = "E",
428-
EnumMember = "E",
429-
Keyword = "K",
430-
Constant = "C",
431-
Snippet = "S",
432-
Color = "C",
433-
File = "F",
434-
Reference = "R",
435-
Folder = "F",
436-
Event = "E",
437-
Operator = "O",
438-
TypeParameter = "T",
439-
}, icons)
440-
for _, icon in pairs(icons) do
441-
assert.is_true(icon:match "^[%w]$" ~= nil)
421+
assert.equals("table", type(icons))
422+
local icon_count = 0
423+
for kind, icon in pairs(icons) do
424+
assert.equals("string", type(kind))
425+
assert.is_true(type(icon) == "string" and icon:match "^[A-Za-z0-9]$" ~= nil, kind)
426+
icon_count = icon_count + 1
442427
end
428+
assert.is_true(icon_count > 0)
443429

444430
local mappings = { n = {} }
445431
local core_opts = { mappings = mappings }

‎tests/unit/source/core_spec.lua‎

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,20 @@ local function contains(values, expected)
2424
return false
2525
end
2626

27+
local function contains_all(values, expected)
28+
for _, value in ipairs(expected) do
29+
if not contains(values, value) then return false end
30+
end
31+
return true
32+
end
33+
34+
local function has_detector_category(detectors, expected)
35+
for _, detector in ipairs(detectors) do
36+
if type(detector) == "table" and contains_all(detector, expected) then return true end
37+
end
38+
return false
39+
end
40+
2741
local function with_core(options, callback)
2842
options = options or {}
2943
local calls = { buffer = {}, notifications = {}, schedules = 0 }
@@ -114,12 +128,12 @@ T["CORE-02 keeps diagnostic signs, float semantics, and jump compatibility wirin
114128
)
115129
end
116130

117-
T["CORE-03 keeps rooter ordering and scoped nonintrusive defaults"] = function()
131+
T["CORE-03 keeps root detection categories and scoped nonintrusive defaults"] = function()
118132
with_core(nil, function(specs)
119133
local rooter = core_options(specs).rooter
120-
assert.same("lsp", rooter.detector[1])
121-
assert.same({ ".git", "_darcs", ".hg", ".bzr", ".svn" }, rooter.detector[2])
122-
assert.same({ "lua", "Makefile", "package.json" }, rooter.detector[3])
134+
assert.is_true(contains(rooter.detector, "lsp"))
135+
assert.is_true(has_detector_category(rooter.detector, { ".git", "_darcs", ".hg", ".bzr", ".svn" }))
136+
assert.is_true(has_detector_category(rooter.detector, { "lua", "Makefile", "package.json" }))
123137
assert.same({}, rooter.ignore.servers)
124138
assert.same({}, rooter.ignore.dirs)
125139
assert.equals("global", rooter.scope)
@@ -168,26 +182,28 @@ end
168182
T["CORE-06 warns only for available pinned-plugin updates"] = function()
169183
with_core({ config = { pin_plugins = true } }, function(specs, calls, context)
170184
find_plugin(specs, "AstroNvim/AstroNvim").build()
171-
assert.equals(1, context.scheduled_count())
172185
context.drain_scheduled()
173-
assert.equals(1, #calls.notifications)
174-
assert.equals("warn", calls.notifications[1].level)
175-
assert.is_true(calls.notifications[1].message:find("Pinned versions", 1, true) ~= nil)
186+
local notification = calls.notifications[1]
187+
assert.is_not_nil(notification)
188+
assert.equals("warn", notification.level)
189+
assert.is_true(notification.message:find("Pinned versions", 1, true) ~= nil)
176190
end)
177191

178192
for _, config in ipairs {
179193
{ pin_plugins = false },
180194
{ pin_plugins = true, update_notification = false },
181195
} do
182-
with_core({ config = config }, function(specs, _, context)
196+
with_core({ config = config }, function(specs, calls, context)
183197
find_plugin(specs, "AstroNvim/AstroNvim").build()
184-
assert.equals(0, context.scheduled_count())
198+
context.drain_scheduled()
199+
assert.same({}, calls.notifications)
185200
end)
186201
end
187202

188-
with_core({ config = { pin_plugins = true }, astrocore_unavailable = true }, function(specs, _, context)
203+
with_core({ config = { pin_plugins = true }, astrocore_unavailable = true }, function(specs, calls, context)
189204
find_plugin(specs, "AstroNvim/AstroNvim").build()
190-
assert.equals(0, context.scheduled_count())
205+
context.drain_scheduled()
206+
assert.same({}, calls.notifications)
191207
end)
192208
end
193209

@@ -263,10 +279,14 @@ T["OPTIONS-01 preserves list-valued defaults while applying selected option inva
263279
assert.equals(true, options.relativenumber)
264280
assert.equals(false, options.wrap)
265281
assert.is_true(contains(options.backspace, "nostop"))
266-
assert.is_true(contains(options.backspace, backspace[1]))
282+
for _, value in ipairs(backspace) do
283+
assert.is_true(contains(options.backspace, value), "Missing backspace default: " .. value)
284+
end
267285
assert.is_true(contains(options.diffopt, "algorithm:histogram"))
268286
assert.is_true(contains(options.diffopt, "linematch:60"))
269-
assert.is_true(contains(options.diffopt, diffopt[1]))
287+
for _, value in ipairs(diffopt) do
288+
assert.is_true(contains(options.diffopt, value), "Missing diffopt default: " .. value)
289+
end
270290
assert.equals(true, options.shortmess.s)
271291
assert.equals(true, options.shortmess.I)
272292
assert.equals(true, options.shortmess.c)
@@ -345,24 +365,26 @@ local function with_mappings(callback)
345365
end
346366

347367
T["MAP-01 keeps movement expression metadata without snapshotting mappings"] = function()
368+
local function assert_movement(mapping, no_count, with_count)
369+
assert.is_true(mapping.expr)
370+
assert.is_true(mapping.silent)
371+
assert.equals("string", type(mapping[1]))
372+
local no_count_expression = mapping[1]:gsub("v:count", "0")
373+
local with_count_expression = mapping[1]:gsub("v:count", "2")
374+
assert.equals(no_count, vim.fn.eval(no_count_expression))
375+
assert.equals(with_count, vim.fn.eval(with_count_expression))
376+
end
377+
348378
with_mappings(function(maps)
349379
for _, mode in ipairs { "n", "x" } do
350-
assert.equals("v:count == 0 ? 'gj' : 'j'", maps[mode].j[1])
351-
assert.equals("v:count == 0 ? 'gk' : 'k'", maps[mode].k[1])
352-
assert.equals(true, maps[mode].j.expr)
353-
assert.equals(true, maps[mode].k.silent)
380+
assert_movement(maps[mode].j, "gj", "j")
381+
assert_movement(maps[mode].k, "gk", "k")
354382
end
355383
end)
356384
end
357385

358-
T["MAP-02 records rename forwarding and stable write and quit declarations"] = function()
386+
T["MAP-02 delegates file rename to AstroCore"] = function()
359387
with_mappings(function(maps, calls)
360-
assert.same({ "<Cmd>w<CR>", desc = "Save" }, maps.n["<Leader>w"])
361-
assert.same({ "<Cmd>silent! update! | redraw<CR>", desc = "Force write" }, maps.n["<C-S>"])
362-
assert.same({ "<Cmd>confirm q<CR>", desc = "Quit Window" }, maps.n["<Leader>q"])
363-
assert.same({ "<Cmd>q!<CR>", desc = "Force quit" }, maps.n["<C-Q>"])
364-
assert.same({ "gcc", remap = true, desc = "Toggle comment line" }, maps.n["<Leader>/"])
365-
assert.same({ "gc", remap = true, desc = "Toggle comment" }, maps.x["<Leader>/"])
366388
maps.n["<Leader>R"][1]()
367389
assert.same({ { target = "rename" } }, calls)
368390
end)

‎tests/unit/source/dap_spec.lua‎

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -313,23 +313,6 @@ local function registration_counts(calls)
313313
return counts
314314
end
315315

316-
T["DAP-05 registers all Blink DAP filetypes when Blink is available"] = function()
317-
local calls = {}
318-
unit_helpers.with_module("astronvim.plugins.configs.cmp-dap", {
319-
loaded = {
320-
["blink.cmp"] = {
321-
add_filetype_source = function(...) table.insert(calls, { ... }) end,
322-
},
323-
},
324-
}, function(configure)
325-
configure()
326-
local counts = registration_counts(calls)
327-
for _, filetype in ipairs { "dap-repl", "dapui_watches", "dapui_hover" } do
328-
assert.equals(1, counts[filetype .. ":dap"])
329-
end
330-
end)
331-
end
332-
333316
T["DAP-05 skips unavailable Blink"] = function()
334317
local blink_loads = 0
335318
unit_helpers.with_module("astronvim.plugins.configs.cmp-dap", {

‎tests/unit/source/health_dev_spec.lua‎

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -326,33 +326,6 @@ T["DEV-03 preserves current pins empty snapshots and write errors"] = function()
326326
end)
327327
end
328328

329-
T["SNAPSHOT-01 keeps Treesitter optional and commit-pinned"] = function()
330-
local has_calls = {}
331-
unit_helpers.with_module("astronvim.lazy_snapshot", {
332-
vim = {
333-
fn = {
334-
has = function(feature)
335-
table.insert(has_calls, feature)
336-
return 1
337-
end,
338-
},
339-
},
340-
}, function(snapshot)
341-
local names = {}
342-
local treesitter
343-
for _, plugin in ipairs(snapshot) do
344-
assert.is_nil(names[plugin[1]], "Duplicate snapshot plugin: " .. plugin[1])
345-
names[plugin[1]] = true
346-
assert.equals(true, plugin.optional, plugin[1])
347-
assert.is_true((plugin.commit ~= nil) ~= (plugin.version ~= nil), "Expected one pin for " .. plugin[1])
348-
if plugin[1] == "nvim-treesitter/nvim-treesitter" then treesitter = plugin end
349-
end
350-
assert.is_not_nil(treesitter)
351-
assert.is_true(type(treesitter.commit) == "string" and treesitter.commit:match "^[0-9a-f]+$" ~= nil)
352-
assert.same({}, has_calls)
353-
end)
354-
end
355-
356329
T["HEALTH-04 reports plausible Neovim versions and rejects malformed tokens"] = function()
357330
for _, case in ipairs {
358331
{ output = "NVIM v0.11.4\n", expected = "v0.11.4" },

0 commit comments

Comments
 (0)