Skip to content

Commit

Permalink
Additional typing, checks, and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
MisanthropicBit committed Mar 30, 2024
1 parent 9531d37 commit c6e697f
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 22 deletions.
32 changes: 24 additions & 8 deletions lua/neotest-busted/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@ local default_config = {

local _user_config = default_config

---@param value any
---@return boolean
local function is_non_empty_string(value)
return value == nil or (type(value) == "string" and #value > 0)
end

---@param value any
---@return boolean
---@return string?
local function is_optional_string_list(value)
if value == nil then
return true
Expand All @@ -24,9 +29,13 @@ local function is_optional_string_list(value)
return false
end

if not vim.tbl_islist(value) then
return false, "must be a list-like table"
end

for idx, item in ipairs(value) do
if type(item) ~= "string" then
return false, "at index " .. tostring(idx)
return false, "item at at index " .. tostring(idx)
end
end

Expand All @@ -43,27 +52,27 @@ function config.validate(_config)
busted_command = {
_config.busted_command,
is_non_empty_string,
"expected optional non-empty string"
"optional non-empty string"
},
busted_args = {
_config.busted_args,
is_optional_string_list,
"expected an optional string list",
"an optional string list",
},
busted_paths = {
_config.busted_paths,
is_optional_string_list,
"expected an optional string list",
"an optional string list",
},
busted_cpaths = {
_config.busted_cpaths,
is_optional_string_list,
"expected an optional string list",
"an optional string list",
},
minimal_init = {
_config.minimal_init,
is_non_empty_string,
"expected optional non-empty string"
"optional non-empty string"
},
})
-- stylua: ignore end
Expand All @@ -81,16 +90,23 @@ function config.validate(_config)
return ok
end

---@param user_config table<string, any>?
---@return boolean
---@return any?
function config.configure(user_config)
_user_config = vim.tbl_deep_extend("keep", user_config or {}, default_config)

local ok, error = config.validate(_user_config)

if not ok then
-- message.error("Errors found in config: " .. error)
vim.api.nvim_echo({
{ "[neotest-busted]: ", "ErrorMsg" },
{ "Invalid config: " },
{ error },
}, true, {})
end

return ok
return ok, error
end

return setmetatable(config, {
Expand Down
8 changes: 8 additions & 0 deletions tests/adapter_build_spec_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ local stub = require("luassert.stub")
local async = _async.tests

describe("adapter.build_spec", function()
before_each(function()
stub(vim.api, "nvim_echo")
end)

after_each(function()
vim.api.nvim_echo:revert()
end)

local function assert_spec_command(spec_command, items)
assert.are.same(#spec_command, #items)

Expand Down
52 changes: 38 additions & 14 deletions tests/config_spec.lua
Original file line number Diff line number Diff line change
@@ -1,49 +1,73 @@
local config = require("neotest-busted.config")
local stub = require("luassert.stub")

describe("config", function()
it("handles invalid configs", function()
local invalid_configs = {
local non_empty_string = "optional non-empty string"
local optional_string_list = "an optional string list"

local invalid_config_tests = {
{
busted_command = 1,
config = { busted_command = 1 },
error_message = non_empty_string,
},
{
busted_command = "",
config = { busted_command = "" },
error_message = non_empty_string,
},
{
busted_args = { 1, 2, 3 },
config = { busted_args = { 1, 2, 3 } },
error_message = optional_string_list,
},
{
busted_args = 1,
config = { busted_args = 1 },
error_message = optional_string_list,
},
{
busted_paths = { 1, 2, 3 },
config = { busted_paths = { 1, 2, 3 } },
error_message = optional_string_list,
},
{
busted_paths = 1,
config = { busted_paths = 1 },
error_message = optional_string_list,
},
{
busted_cpaths = { 1, 2, 3 },
config = { busted_cpaths = { 1, 2, 3 } },
error_message = optional_string_list,
},
{
busted_cpaths = 1,
config = { busted_cpaths = 1 },
error_message = optional_string_list,
},
{
minimal_init = false,
config = { minimal_init = false },
error_message = non_empty_string,
},
{
minimal_init = "",
config = { minimal_init = "" },
error_message = non_empty_string,
},
}

for _, invalid_config in ipairs(invalid_configs) do
local ok = config.configure(invalid_config)
stub(vim.api, "nvim_echo")

for _, invalid_config_test in ipairs(invalid_config_tests) do
local ok, error = config.configure(invalid_config_test.config)

if ok then
vim.print(invalid_config)
vim.print(invalid_config_test)
end

assert.is_false(ok)

assert.stub(vim.api.nvim_echo).was.called_with({
{ "[neotest-busted]: ", "ErrorMsg" },
{ "Invalid config: " },
{ error },
}, true, {})
end

vim.api.nvim_echo:revert()
end)

it("throws no errors for a valid config", function()
Expand Down

0 comments on commit c6e697f

Please sign in to comment.