-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(poc): support running testify suite test function
This uses vim.treesitter instead of nvim-treesitter. There are hacks injected all over the place and the solution is brittle.
- Loading branch information
1 parent
b521556
commit 72bec4a
Showing
9 changed files
with
294 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
local M = {} | ||
|
||
--- Build runspec for a namespace. | ||
--- @param pos neotest.Position | ||
--- @return neotest.RunSpec | neotest.RunSpec[] | nil | ||
function M.build(pos) | ||
-- vim.notify(vim.inspect(pos), vim.levels.log.DEBUG) -- FIXME: remove when done implementing/debugging | ||
|
||
-- TODO: Implement a runspec for a namespace of tests. | ||
-- A bare return will delegate test execution to per-test execution, which | ||
-- will have to do for now. | ||
return | ||
end | ||
|
||
return M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
local M = {} | ||
|
||
--- A lookup map between receiver method name and suite name. | ||
--- Example: | ||
|
||
local lookup_map = {} | ||
|
||
function M.get() | ||
return lookup_map | ||
end | ||
|
||
function M.add(file_name, suite_name, receiver_name) | ||
if not lookup_map[file_name] then | ||
lookup_map[file_name] = {} | ||
end | ||
table.insert( | ||
lookup_map[file_name], | ||
{ suite = suite_name, receiver = receiver_name } | ||
) | ||
end | ||
|
||
function M.clear() | ||
lookup_map = {} | ||
end | ||
|
||
function M.merge_duplicate_namespaces(node) | ||
if not node._children or #node._children == 0 then | ||
return node | ||
end | ||
|
||
local namespaces = {} | ||
local new_children = {} | ||
|
||
for _, child in ipairs(node._children) do | ||
if child._data.type == "namespace" then | ||
local existing = namespaces[child._data.name] | ||
if existing then | ||
-- Merge children of duplicate namespace | ||
for _, grandchild in ipairs(child._children) do | ||
table.insert(existing._children, grandchild) | ||
grandchild._parent = existing | ||
end | ||
else | ||
namespaces[child._data.name] = child | ||
table.insert(new_children, child) | ||
end | ||
else | ||
table.insert(new_children, child) | ||
end | ||
end | ||
|
||
-- Recursively process children | ||
for _, child in ipairs(new_children) do | ||
M.merge_duplicate_namespaces(child) | ||
end | ||
|
||
node._children = new_children | ||
return node | ||
end | ||
|
||
function M.find_parent_function(node) | ||
while node do | ||
if node:type() == "function_declaration" then | ||
return node | ||
end | ||
node = node:parent() | ||
end | ||
return nil | ||
end | ||
|
||
function M.get_function_name(func_node, content) | ||
for child in func_node:iter_children() do | ||
if child:type() == "identifier" then | ||
return vim.treesitter.get_node_text(child, content) | ||
end | ||
end | ||
return "anonymous" | ||
end | ||
|
||
function M.run_query_on_file(filepath, query_string) | ||
local file = io.open(filepath, "r") | ||
if not file then | ||
error("Could not open file: " .. filepath) | ||
end | ||
local content = file:read("*all") | ||
file:close() | ||
|
||
local lang = "go" | ||
local parser = vim.treesitter.get_string_parser(content, lang) | ||
local tree = parser:parse()[1] | ||
local root = tree:root() | ||
|
||
local query = vim.treesitter.query.parse(lang, query_string) | ||
local matches = {} | ||
|
||
for id, node, metadata in query:iter_captures(root, content, 0, -1) do | ||
local name = query.captures[id] | ||
local text = vim.treesitter.get_node_text(node, content) | ||
|
||
local func_node = M.find_parent_function(node) | ||
if func_node then | ||
local func_name = M.get_function_name(func_node, content) | ||
if not matches[func_name] then | ||
matches[func_name] = {} | ||
end | ||
table.insert( | ||
matches[func_name], | ||
{ name = name, node = node, text = text } | ||
) | ||
else | ||
if not matches["global"] then | ||
matches["global"] = {} | ||
end | ||
table.insert(matches["global"], { name = name, node = node, text = text }) | ||
end | ||
end | ||
|
||
return matches | ||
end | ||
|
||
return M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,11 @@ | ||
module github.com/fredrikaverpil/neotest-golang | ||
|
||
go 1.22.2 | ||
|
||
require github.com/stretchr/testify v1.9.0 | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= | ||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package main | ||
|
||
// Basic imports | ||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
// Define the suite, and absorb the built-in basic suite | ||
// functionality from testify - including a T() method which | ||
// returns the current testing context | ||
type ExampleTestSuite struct { | ||
suite.Suite | ||
VariableThatShouldStartAtFive int | ||
} | ||
|
||
// Make sure that VariableThatShouldStartAtFive is set to five | ||
// before each test | ||
func (suite *ExampleTestSuite) SetupTest() { | ||
suite.VariableThatShouldStartAtFive = 5 | ||
} | ||
|
||
// All methods that begin with "Test" are run as tests within a | ||
// suite. | ||
func (suite *ExampleTestSuite) TestExample() { | ||
assert.Equal(suite.T(), 5, suite.VariableThatShouldStartAtFive) | ||
suite.Equal(5, suite.VariableThatShouldStartAtFive) | ||
} | ||
|
||
// In order for 'go test' to run this suite, we need to create | ||
// a normal test function and pass our suite to suite.Run | ||
func TestExampleTestSuite(t *testing.T) { | ||
suite.Run(t, new(ExampleTestSuite)) | ||
} |