From dd0b641a02595afedea1ae983f0cfac6e9198d84 Mon Sep 17 00:00:00 2001 From: Fredrik Averpil Date: Sat, 29 Jun 2024 20:47:48 +0200 Subject: [PATCH] fix: regexp character escaping --- lua/neotest-golang/convert.lua | 8 +++++++- tests/go/testname_spec.lua | 20 ++++++++++++++++++++ tests/go/testname_test.go | 6 ++++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/lua/neotest-golang/convert.lua b/lua/neotest-golang/convert.lua index e6eb1dfa..8880751e 100644 --- a/lua/neotest-golang/convert.lua +++ b/lua/neotest-golang/convert.lua @@ -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) @@ -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) diff --git a/tests/go/testname_spec.lua b/tests/go/testname_spec.lua index e878f93a..9bfa821b 100644 --- a/tests/go/testname_spec.lua +++ b/tests/go/testname_spec.lua @@ -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 @@ -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() diff --git a/tests/go/testname_test.go b/tests/go/testname_test.go index 926afecc..bbcf388a 100644 --- a/tests/go/testname_test.go +++ b/tests/go/testname_test.go @@ -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() + } + }) }