diff --git a/lua/neotest-golang/convert.lua b/lua/neotest-golang/convert.lua new file mode 100644 index 00000000..bf39f828 --- /dev/null +++ b/lua/neotest-golang/convert.lua @@ -0,0 +1,21 @@ +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 spaces with _ + test_name = test_name:gsub(" ", "_") + + return test_name +end + +return M diff --git a/lua/neotest-golang/init.lua b/lua/neotest-golang/init.lua index c78cd88d..b22fd006 100644 --- a/lua/neotest-golang/init.lua +++ b/lua/neotest-golang/init.lua @@ -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 @@ -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, "(.+)/") @@ -402,29 +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 any quotes - test_name = test_name:gsub('"', "") - 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 diff --git a/tests/go/main.go b/tests/go/main.go index a0a49bdc..0a46b1d7 100644 --- a/tests/go/main.go +++ b/tests/go/main.go @@ -6,3 +6,8 @@ import "fmt" func main() { fmt.Println("Hello, World!") } + +// Function used for tests. +func Add(a, b int) int { + return a + b +} diff --git a/tests/go/positions.go b/tests/go/positions.go deleted file mode 100644 index 23a7f59e..00000000 --- a/tests/go/positions.go +++ /dev/null @@ -1,5 +0,0 @@ -package main - -func Add(a, b int) int { - return a + b -} diff --git a/tests/go/positions_spec.lua b/tests/go/positions_spec.lua index 7607b4c9..50362fdb 100644 --- a/tests/go/positions_spec.lua +++ b/tests/go/positions_spec.lua @@ -25,6 +25,7 @@ describe("Discovery of test positions", function() } -- Act + ---@type neotest.Tree local tree = nio.tests.with_async_context(adapter.discover_positions, test_filepath) diff --git a/tests/go/testname_spec.lua b/tests/go/testname_spec.lua new file mode 100644 index 00000000..ea510e39 --- /dev/null +++ b/tests/go/testname_spec.lua @@ -0,0 +1,63 @@ +local nio = require("nio") +local adapter = require("neotest-golang") +local convert = require("neotest-golang.convert") + +describe("Subtest name conversion", function() + -- Arrange + local test_filepath = vim.loop.cwd() .. "/tests/go/testname_test.go" + + ---@type neotest.Tree + local tree = + nio.tests.with_async_context(adapter.discover_positions, test_filepath) + + it("Mixed case with space", function() + local expected_subtest_name = '"Mixed case with space"' + local expected_gotest_name = "TestNames/Mixed_case_with_space" + + -- Act + local pos = tree:node(3):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) + + it("Special characters", function() + local expected_subtest_name = '"Comma , and \' are ok to use"' + local expected_gotest_name = "TestNames/Comma_,_and_'_are_ok_to_use" + + -- Act + local pos = tree:node(4):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) + + it("Brackets", function() + local expected_subtest_name = '"Brackets [1] (2) {3} are ok"' + local expected_gotest_name = "TestNames/Brackets_[1]_(2)_{3}_are_ok" + + -- Act + local pos = tree:node(5):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) diff --git a/tests/go/testname_test.go b/tests/go/testname_test.go new file mode 100644 index 00000000..0d880dfe --- /dev/null +++ b/tests/go/testname_test.go @@ -0,0 +1,24 @@ +package main + +import "testing" + +// A dummy test, just to assert that Go tests can run. +func TestNames(t *testing.T) { + t.Run("Mixed case with space", func(t *testing.T) { + if Add(1, 2) != 3 { + t.Fail() + } + }) + + t.Run("Comma , and ' are ok to use", func(t *testing.T) { + if Add(1, 2) != 3 { + t.Fail() + } + }) + + t.Run("Brackets [1] (2) {3} are ok", func(t *testing.T) { + if Add(1, 2) != 3 { + t.Fail() + } + }) +}