Skip to content

Commit

Permalink
refactor: remove unused, fix docstring
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikaverpil committed Jun 15, 2024
1 parent afd62b5 commit 1924f9b
Showing 1 changed file with 16 additions and 68 deletions.
84 changes: 16 additions & 68 deletions lua/neotest-golang/convert.lua
Original file line number Diff line number Diff line change
@@ -1,74 +1,22 @@
local M = {}

-- Converts the AST-detected Neotest node 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 spaces with _
test_name = test_name:gsub(" ", "_")

return test_name
end

-- Converts the `go test` command test name into Neotest node test name format.
-- Note that a pattern can returned, not the exact test name, so to support
-- escaped quotes etc.
-- NOTE: double quotes must be removed from the string matching against.

---@param go_test_name string
---@return string
function M.to_neotest_test_name_pattern(go_test_name)
-- construct the test name
local test_name = go_test_name
-- Add :: before the test name
test_name = "::" .. test_name
-- Replace / with ::
test_name = test_name:gsub("/", "::")

-- Replace _ with space
test_name = test_name:gsub("_", " ")

-- Mark the end of the test name pattern
test_name = test_name .. "$"

-- Percentage sign must be escaped
test_name = test_name:gsub("%%", "%%%%")

-- Literal brackets and parantheses must be escaped
test_name = test_name:gsub("%[", "%%[")
test_name = test_name:gsub("%]", "%%]")
test_name = test_name:gsub("%(", "%%(")
test_name = test_name:gsub("%)", "%%)")

return test_name
end

--- Escape characters, for usage of string as pattern in Lua..
--- - `.` (matches any character)
--- - `%` (used to escape special characters)
--- - `+` (matches 1 or more of the previous character or class)
--- - `*` (matches 0 or more of the previous character or class)
--- - `-` (matches 0 or more of the previous character or class, in the shortest sequence)
--- - `?` (makes the previous character or class optional)
--- - `^` (at the start of a pattern, matches the start of the string; in a character class `[]`, negates the class)
--- - `$` (matches the end of the string)
--- - `[]` (defines a character class)
--- - `()` (defines a capture)
--- - `:` (used in certain pattern items like `%b()`)
--- - `=` (used in certain pattern items like `%b()`)
--- - `<` (used in certain pattern items like `%b<>`)
--- - `>` (used in certain pattern items like `%b<>`)
--- @param str string
function M.to_lua_pattern(str)
-- Escape characters, for usage of string as pattern.
-- - `.` (matches any character)
-- - `%` (used to escape special characters)
-- - `+` (matches 1 or more of the previous character or class)
-- - `*` (matches 0 or more of the previous character or class)
-- - `-` (matches 0 or more of the previous character or class, in the shortest sequence)
-- - `?` (makes the previous character or class optional)
-- - `^` (at the start of a pattern, matches the start of the string; in a character class `[]`, negates the class)
-- - `$` (matches the end of the string)
-- - `[]` (defines a character class)
-- - `()` (defines a capture)
-- - `:` (used in certain pattern items like `%b()`)
-- - `=` (used in certain pattern items like `%b()`)
-- - `<` (used in certain pattern items like `%b<>`)
-- - `>` (used in certain pattern items like `%b<>`)

local special_characters = {
".",
"%",
Expand Down

0 comments on commit 1924f9b

Please sign in to comment.