Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow single quotes in test name #19

Merged
merged 5 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions lua/neotest-golang/convert.lua
Original file line number Diff line number Diff line change
@@ -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
28 changes: 4 additions & 24 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,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
Expand Down
5 changes: 5 additions & 0 deletions tests/go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
5 changes: 0 additions & 5 deletions tests/go/positions.go

This file was deleted.

1 change: 1 addition & 0 deletions tests/go/positions_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
63 changes: 63 additions & 0 deletions tests/go/testname_spec.lua
Original file line number Diff line number Diff line change
@@ -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)
24 changes: 24 additions & 0 deletions tests/go/testname_test.go
Original file line number Diff line number Diff line change
@@ -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()
}
})
}
Loading