Skip to content

Commit d446ed3

Browse files
committed
tests: add tests for go handler
1 parent 33af536 commit d446ed3

File tree

5 files changed

+42
-0
lines changed

5 files changed

+42
-0
lines changed

.gitmodules

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@
66
path = test/vendor/plenary.nvim
77
url = https://github.com/nvim-lua/plenary.nvim
88
ignore = dirty
9+
[submodule "test/vendor/nvim-treesitter"]
10+
path = test/vendor/nvim-treesitter
11+
url = https://github.com/nvim-treesitter/nvim-treesitter
12+
ignore = dirty

test/fixtures/test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package main
2+
3+
import "fmt"
4+
5+
import myio "io"
6+
7+
import (
8+
"github.com/joho/godotenv"
9+
corev1 "k8s.io/api/core/v1"
10+
)

test/spec.vim

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
set rtp^=test/vendor/plenary.nvim/
22
set rtp^=test/vendor/matcher_combinators.lua/
3+
set rtp^=test/vendor/nvim-treesitter/
34
set rtp^=./
45

56
runtime plugin/plenary.vim
@@ -11,3 +12,6 @@ lua require('matcher_combinators.luassert')
1112
runtime plugin/gx.lua
1213
" lua require('gx').setup({ name = 'Jane Doe' })
1314
lua require('gx').setup()
15+
16+
" needed for go handler testing
17+
lua require('nvim-treesitter.configs').setup({ ensure_installed = { "go" }, sync_install = true })
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
local handler = require("gx.handlers.go")
2+
3+
describe("go handler works", function()
4+
vim.cmd("edit test/fixtures/test.go")
5+
local function test_line(line, startpos, endpos, expected)
6+
for i = startpos, endpos do
7+
vim.api.nvim_win_set_cursor(0, { line, i })
8+
assert.equals(expected, handler.handle())
9+
end
10+
end
11+
it("parses single line imports", function()
12+
test_line(3, 0, 11, "https://pkg.go.dev/fmt")
13+
end)
14+
it("parses single line named imports", function()
15+
test_line(5, 0, 15, "https://pkg.go.dev/io")
16+
end)
17+
it("parses imports in list", function()
18+
test_line(8, 1, 26, "https://pkg.go.dev/github.com/joho/godotenv")
19+
end)
20+
it("parses named imports in list", function()
21+
test_line(9, 1, 27, "https://pkg.go.dev/k8s.io/api/core/v1")
22+
end)
23+
end)

test/vendor/nvim-treesitter

Submodule nvim-treesitter added at 19f69a5

0 commit comments

Comments
 (0)