Skip to content

Commit

Permalink
refactor: break out conversion into its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikaverpil committed May 22, 2024
1 parent 8e9bf4d commit d78508f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 23 deletions.
26 changes: 26 additions & 0 deletions lua/neotest-golang/convert.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
local M = {}

-- Converts the AST-detected test name into the 'go test' command test name format.
---@param pos_id string
---@return string
function M.to_gotest_test_name(pos_id)
-- construct the test name
local test_name = pos_id
-- Remove the path before ::
test_name = test_name:match("::(.*)$")
-- Replace :: with /
test_name = test_name:gsub("::", "/")
-- Remove double quotes (single quotes are supported)
test_name = test_name:gsub('"', "")
-- Replace any special characters with . so to avoid breaking regexp
test_name = test_name:gsub("%[", ".")
test_name = test_name:gsub("%]", ".")
test_name = test_name:gsub("%(", ".")
test_name = test_name:gsub("%)", ".")
-- Replace any spaces with _
test_name = test_name:gsub(" ", "_")

return test_name
end

return M
27 changes: 4 additions & 23 deletions lua/neotest-golang/init.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
local lib = require("neotest.lib")
local async = require("neotest.async")

local convert = require("neotest-golang.convert")

local M = {}

---@class neotest.Adapter
Expand Down Expand Up @@ -326,7 +329,7 @@ end
---@return neotest.RunSpec
function M.build_single_test_runspec(pos, strategy)
---@type string
local test_name = M.test_name_from_pos_id(pos.id)
local test_name = convert.to_gotest_test_name(pos.id)
---@type string
local test_folder_absolute_path = string.match(pos.path, "(.+)/")

Expand Down Expand Up @@ -402,28 +405,6 @@ function M.table_is_empty(t)
return next(t) == nil
end

---@param pos_id string
---@return string
function M.test_name_from_pos_id(pos_id)
-- construct the test name
local test_name = pos_id
-- Remove the path before ::
test_name = test_name:match("::(.*)$")
-- Replace :: with /
test_name = test_name:gsub("::", "/")
-- Remove double quotes (single quotes are supported)
test_name = test_name:gsub('"', "")
-- Replace any special characters with . so to avoid breaking regexp
test_name = test_name:gsub("%[", ".")
test_name = test_name:gsub("%]", ".")
test_name = test_name:gsub("%(", ".")
test_name = test_name:gsub("%)", ".")
-- Replace any spaces with _
test_name = test_name:gsub(" ", "_")

return test_name
end

--- Process JSON and return objects of interest
---@param raw_output table
---@return table
Expand Down

0 comments on commit d78508f

Please sign in to comment.