Skip to content

Commit

Permalink
Add support for map table test (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
sergii4 committed Jul 1, 2023
1 parent 4e7a879 commit 05535cb
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 2 deletions.
33 changes: 31 additions & 2 deletions lua/neotest-go/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function adapter.discover_positions(path)
if get_experimental_opts().test_table then
query = query
.. [[
;; query
;; query for list table tests
(block
(short_var_declaration
left: (expression_list
Expand All @@ -80,7 +80,7 @@ function adapter.discover_positions(path)
(literal_value
(literal_element
(literal_value
.(keyed_element
(keyed_element
(literal_element
(identifier) @test.field.name)
(literal_element
Expand All @@ -102,6 +102,35 @@ function adapter.discover_positions(path)
(#eq? @test.case @test.case1)
field: (field_identifier) @test.field.name1
(#eq? @test.field.name @test.field.name1)))))))
;; query for map table tests
(block
(short_var_declaration
left: (expression_list
(identifier) @test.cases)
right: (expression_list
(composite_literal
(literal_value
(keyed_element
(literal_element
(interpreted_string_literal) @test.name)
(literal_element
(literal_value) @test.definition))))))
(for_statement
(range_clause
left: (expression_list
((identifier) @test.key.name)
((identifier) @test.case))
right: (identifier) @test.cases1
(#eq? @test.cases @test.cases1))
body: (block
(call_expression
function: (selector_expression
field: (field_identifier) @test.method)
(#match? @test.method "^Run$")
arguments: (argument_list
((identifier) @test.key.name1
(#eq? @test.key.name @test.key.name1)))))))
]]
end

Expand Down
59 changes: 59 additions & 0 deletions lua/spec/neotest-go/init_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,65 @@ describe("discover_positions", function()

assert.are.same(positions, expected_positions)
end)
async.it("discovers positions in unit tests in map_table_test.go", function()
local path = vim.loop.cwd() .. "/neotest_go/map_table_test.go"
local positions = plugin.discover_positions(path):to_list()
local expected_positions = {
{
id = "/Users/sgetman/.local/share/nvim/site/pack/packer/start/neotest-go/neotest_go/map_table_test.go",
name = "map_table_test.go",
path = "/Users/sgetman/.local/share/nvim/site/pack/packer/start/neotest-go/neotest_go/map_table_test.go",
range = { 0, 0, 40, 0 },
type = "file",
},
{
{
id = "/Users/sgetman/.local/share/nvim/site/pack/packer/start/neotest-go/neotest_go/map_table_test.go::TestSplit",
name = "TestSplit",
path = "/Users/sgetman/.local/share/nvim/site/pack/packer/start/neotest-go/neotest_go/map_table_test.go",
range = { 8, 0, 28, 1 },
type = "test",
},
{
{
id = "/Users/sgetman/.local/share/nvim/site/pack/packer/start/neotest-go/neotest_go/map_table_test.go::TestSplit::simple",
name = '"simple"',
path = "/Users/sgetman/.local/share/nvim/site/pack/packer/start/neotest-go/neotest_go/map_table_test.go",
range = { 14, 18, 14, 75 },
type = "test",
},
},
{
{
id = "/Users/sgetman/.local/share/nvim/site/pack/packer/start/neotest-go/neotest_go/map_table_test.go::TestSplit::wrong_sep",
name = '"wrong sep"',
path = "/Users/sgetman/.local/share/nvim/site/pack/packer/start/neotest-go/neotest_go/map_table_test.go",
range = { 15, 18, 15, 69 },
type = "test",
},
},
{
{
id = "/Users/sgetman/.local/share/nvim/site/pack/packer/start/neotest-go/neotest_go/map_table_test.go::TestSplit::no_sep",
name = '"no sep"',
path = "/Users/sgetman/.local/share/nvim/site/pack/packer/start/neotest-go/neotest_go/map_table_test.go",
range = { 16, 18, 16, 65 },
type = "test",
},
},
{
{
id = "/Users/sgetman/.local/share/nvim/site/pack/packer/start/neotest-go/neotest_go/map_table_test.go::TestSplit::trailing_sep",
name = '"trailing sep"',
path = "/Users/sgetman/.local/share/nvim/site/pack/packer/start/neotest-go/neotest_go/map_table_test.go",
range = { 17, 18, 17, 76 },
type = "test",
},
},
},
}
assert.are.same(positions, expected_positions)
end)
end)

describe("prepare_results", function()
Expand Down
40 changes: 40 additions & 0 deletions neotest_go/map_table_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package main

import (
"reflect"
"strings"
"testing"
)

func TestSplit(t *testing.T) {
tests := map[string]struct {
input string
sep string
want []string
}{
"simple": {input: "a/b/c", sep: "/", want: []string{"a", "b", "c"}},
"wrong sep": {input: "a/b/c", sep: ",", want: []string{"a/b/c"}},
"no sep": {input: "abc", sep: "/", want: []string{"abc"}},
"trailing sep": {input: "a/b/c/", sep: "/", want: []string{"a", "b", "c"}},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
got := Split(tc.input, tc.sep)
if !reflect.DeepEqual(tc.want, got) {
t.Fatalf("%s: expected: %v, got: %v", name, tc.want, got)
}
})
}
}

func Split(s, sep string) []string {
var result []string
i := strings.Index(s, sep)
for i > -1 {
result = append(result, s[:i])
s = s[i+len(sep):]
i = strings.Index(s, sep)
}
return append(result, s)
}

0 comments on commit 05535cb

Please sign in to comment.