From 05535cb2cfe3ce5c960f65784896d40109572f89 Mon Sep 17 00:00:00 2001 From: Sergii Getman Date: Sat, 1 Jul 2023 21:40:37 +0100 Subject: [PATCH] Add support for map table test (#55) --- lua/neotest-go/init.lua | 33 +++++++++++++++-- lua/spec/neotest-go/init_spec.lua | 59 +++++++++++++++++++++++++++++++ neotest_go/map_table_test.go | 40 +++++++++++++++++++++ 3 files changed, 130 insertions(+), 2 deletions(-) create mode 100644 neotest_go/map_table_test.go diff --git a/lua/neotest-go/init.lua b/lua/neotest-go/init.lua index 00b6e13..7f9cb70 100644 --- a/lua/neotest-go/init.lua +++ b/lua/neotest-go/init.lua @@ -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 @@ -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 @@ -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 diff --git a/lua/spec/neotest-go/init_spec.lua b/lua/spec/neotest-go/init_spec.lua index 10cf3b2..f1fd5c6 100644 --- a/lua/spec/neotest-go/init_spec.lua +++ b/lua/spec/neotest-go/init_spec.lua @@ -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() diff --git a/neotest_go/map_table_test.go b/neotest_go/map_table_test.go new file mode 100644 index 0000000..2fcba40 --- /dev/null +++ b/neotest_go/map_table_test.go @@ -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) +}