Skip to content

Commit

Permalink
fix: regexp character escaping
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikaverpil committed Jun 29, 2024
1 parent 0b79c71 commit dd0b641
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lua/neotest-golang/convert.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@ local M = {}
---@return string
function M.to_gotest_regex_pattern(test_name)
local special_characters = {
"^",
"$",
"(",
")",
"[",
"]",
"{",
"}",
"|",
"?",
"+",
"*",
}
for _, character in ipairs(special_characters) do
test_name = test_name:gsub("%" .. character, "\\" .. character)
Expand All @@ -38,7 +44,7 @@ function M.to_gotest_test_name(pos_id)
return test_name
end

--- Escape characters, for usage of string as pattern in Lua..
--- 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)
Expand Down
20 changes: 20 additions & 0 deletions tests/go/testname_spec.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local nio = require("nio")
local adapter = require("neotest-golang")
local convert = require("neotest-golang.convert")
local _ = require("plenary")

describe("Neotest position to Go test name", function()
-- Arrange
Expand Down Expand Up @@ -62,6 +63,25 @@ describe("Neotest position to Go test name", function()
vim.inspect(actual_go_test_name)
)
end)

it("supports regexp characters", function()
local expected_subtest_name =
'"Regexp characters like ( ) [ ] - | ? + * ^ $ are ok"'
local expected_gotest_name =
"TestNames/Regexp_characters_like_(_)_[_]_-_|_?_+_*_^_$_are_ok"

-- Act
local pos = tree:node(8):data()
local actual_go_test_name = convert.to_gotest_test_name(pos.id)

-- Assert
local actual_name = pos.name
assert.are.same(expected_subtest_name, actual_name)
assert.are.same(
vim.inspect(expected_gotest_name),
vim.inspect(actual_go_test_name)
)
end)
end)

describe("Full Go test name conversion", function()
Expand Down
6 changes: 6 additions & 0 deletions tests/go/testname_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,10 @@ func TestNames(t *testing.T) {
t.Fail()
}
})

t.Run("Regexp characters like ( ) [ ] - | ? + * ^ $ are ok", func(t *testing.T) {
if Add(1, 2) != 3 {
t.Fail()
}
})
}

0 comments on commit dd0b641

Please sign in to comment.