Skip to content

Commit 06be9d1

Browse files
author
Roundlay
committed
Cover Lua API validation and formatexpr setup
Add Lua API validation cases for invalid wrap mode, non-boolean hyphenation, and empty hyphen markers. Also pin that `set_formatexpr` installs Wrappin's `formatexpr()` as the global default and that new buffers inherit that setting.
1 parent 84d1fc3 commit 06be9d1

2 files changed

Lines changed: 119 additions & 0 deletions

File tree

tests/suites/lifecycle.lua

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ local support = require("support")
1111

1212
local with_buffer = support.with_buffer
1313
local assert_lines = support.assert_lines
14+
local assert_raises = support.assert_raises
1415

1516
local function test_reflows_comment_block_and_restores_exact_original()
1617
with_buffer({
@@ -167,6 +168,104 @@ end
167168
-- current one. Cursor motion (restore_cursor) still targets the current
168169
-- window, which is a deliberate asymmetry that this test does not pin
169170
-- directly -- only buffer identity is asserted.
171+
-- Lua API validation: the three resolve_* helpers each raise on invalid
172+
-- input. The command parser's invalid-mode path was previously tested;
173+
-- these cases pin the same error strings fire when invalid values
174+
-- reach wrappin.run through the Lua API.
175+
local function test_run_rejects_unknown_wrap_mode()
176+
with_buffer({ "alpha beta" }, function()
177+
assert_raises(
178+
function()
179+
wrappin.run({
180+
start_line = 0,
181+
end_line = 0,
182+
max_width = 12,
183+
wrap_mode = "bogus",
184+
})
185+
end,
186+
"Wrappin wrap_mode must be 'normal', 'soft', or 'hard'",
187+
"wrappin.run should reject an unknown wrap_mode"
188+
)
189+
end)
190+
end
191+
192+
local function test_run_rejects_non_boolean_hyphenate()
193+
with_buffer({ "alpha beta" }, function()
194+
assert_raises(
195+
function()
196+
wrappin.run({
197+
start_line = 0,
198+
end_line = 0,
199+
max_width = 12,
200+
hard_wrap_hyphenate = "maybe",
201+
})
202+
end,
203+
"Wrappin hard_wrap_hyphenate must be a boolean",
204+
"wrappin.run should reject a non-boolean hyphenate"
205+
)
206+
end)
207+
end
208+
209+
local function test_run_rejects_empty_hyphenchar()
210+
with_buffer({ "alpha beta" }, function()
211+
assert_raises(
212+
function()
213+
wrappin.run({
214+
start_line = 0,
215+
end_line = 0,
216+
max_width = 12,
217+
hard_wrap_hyphenchar = "",
218+
})
219+
end,
220+
"Wrappin hard_wrap_hyphenchar must be a non-empty string",
221+
"wrappin.run should reject an empty hyphenchar"
222+
)
223+
end)
224+
end
225+
226+
-- setup({ set_formatexpr = true }) writes vim.o.formatexpr, which sets
227+
-- both the global default (vim.go) AND the current buffer's local
228+
-- (vim.bo). New buffers created afterwards inherit the global default.
229+
-- Pre-existing buffers that were loaded before setup() keep their
230+
-- stale local value -- documented in the plan as not-pinned here.
231+
--
232+
-- Test saves and restores vim.go.formatexpr under pcall so a buggy
233+
-- plugin change does not leave the global option polluted for later
234+
-- cases in this run.
235+
local function test_setup_set_formatexpr_installs_global_default()
236+
local saved_global = vim.go.formatexpr
237+
local expected = "v:lua.require'wrappin'.formatexpr()"
238+
239+
local ok, err = pcall(function()
240+
with_buffer({ "" }, function()
241+
wrappin.setup({ set_formatexpr = true })
242+
243+
if vim.go.formatexpr ~= expected then
244+
error("expected vim.go.formatexpr == " .. expected ..
245+
"\n got: " .. vim.inspect(vim.go.formatexpr))
246+
end
247+
248+
if vim.bo.formatexpr ~= expected then
249+
error("expected current-buffer vim.bo.formatexpr == " .. expected ..
250+
"\n got: " .. vim.inspect(vim.bo.formatexpr))
251+
end
252+
253+
local new = vim.api.nvim_create_buf(true, false)
254+
local new_bo = vim.bo[new].formatexpr
255+
pcall(vim.api.nvim_buf_delete, new, { force = true })
256+
257+
if new_bo ~= expected then
258+
error("new buffer should inherit global formatexpr\n got: " .. vim.inspect(new_bo))
259+
end
260+
end)
261+
end)
262+
263+
vim.go.formatexpr = saved_global
264+
vim.bo.formatexpr = saved_global
265+
266+
if not ok then error(err, 0) end
267+
end
268+
170269
local function test_explicit_bufnr_targets_named_buffer()
171270
-- with_buffer manages buf_a (the current buffer, wiped even on error).
172271
-- buf_b is a listed non-scratch buffer created by this test; it must
@@ -348,4 +447,8 @@ return {
348447
{ name = "context_scan_limit_zero_disables_inheritance", fn = test_context_scan_limit_zero_disables_inheritance },
349448
{ name = "two_independent_snapshots_restore_in_any_order", fn = test_two_independent_snapshots_restore_in_any_order },
350449
{ name = "explicit_bufnr_targets_named_buffer", fn = test_explicit_bufnr_targets_named_buffer },
450+
{ name = "run_rejects_unknown_wrap_mode", fn = test_run_rejects_unknown_wrap_mode },
451+
{ name = "run_rejects_non_boolean_hyphenate", fn = test_run_rejects_non_boolean_hyphenate },
452+
{ name = "run_rejects_empty_hyphenchar", fn = test_run_rejects_empty_hyphenchar },
453+
{ name = "setup_set_formatexpr_installs_global_default", fn = test_setup_set_formatexpr_installs_global_default },
351454
}

tests/support.lua

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,20 @@ function M.assert_command_error(cmd, expected_substring, label)
8484
end
8585
end
8686

87+
-- Run `fn` (zero-arg Lua function) and assert it raises with an error
88+
-- whose message contains `expected_substring`. Parallel to
89+
-- assert_command_error, for the Lua API surface instead of the Ex
90+
-- command surface.
91+
function M.assert_raises(fn, expected_substring, label)
92+
local ok, err = pcall(fn)
93+
94+
if ok then
95+
error(label .. "\nexpected call to raise with substring: " .. expected_substring .. "\nactual: call succeeded")
96+
end
97+
98+
if type(err) ~= "string" or err:find(expected_substring, 1, true) == nil then
99+
error(label .. "\nexpected error containing: " .. expected_substring .. "\nactual error: " .. tostring(err))
100+
end
101+
end
102+
87103
return M

0 commit comments

Comments
 (0)