Skip to content

Commit

Permalink
tests: add tests for go handler
Browse files Browse the repository at this point in the history
  • Loading branch information
sportshead committed Apr 28, 2024
1 parent 33af536 commit d446ed3
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@
path = test/vendor/plenary.nvim
url = https://github.com/nvim-lua/plenary.nvim
ignore = dirty
[submodule "test/vendor/nvim-treesitter"]
path = test/vendor/nvim-treesitter
url = https://github.com/nvim-treesitter/nvim-treesitter
ignore = dirty
10 changes: 10 additions & 0 deletions test/fixtures/test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

import "fmt"

import myio "io"

import (
"github.com/joho/godotenv"
corev1 "k8s.io/api/core/v1"
)
4 changes: 4 additions & 0 deletions test/spec.vim
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
set rtp^=test/vendor/plenary.nvim/
set rtp^=test/vendor/matcher_combinators.lua/
set rtp^=test/vendor/nvim-treesitter/
set rtp^=./

runtime plugin/plenary.vim
Expand All @@ -11,3 +12,6 @@ lua require('matcher_combinators.luassert')
runtime plugin/gx.lua
" lua require('gx').setup({ name = 'Jane Doe' })
lua require('gx').setup()

" needed for go handler testing
lua require('nvim-treesitter.configs').setup({ ensure_installed = { "go" }, sync_install = true })
23 changes: 23 additions & 0 deletions test/spec/gx/handlers/go_handler_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
local handler = require("gx.handlers.go")

describe("go handler works", function()
vim.cmd("edit test/fixtures/test.go")
local function test_line(line, startpos, endpos, expected)
for i = startpos, endpos do
vim.api.nvim_win_set_cursor(0, { line, i })
assert.equals(expected, handler.handle())
end
end
it("parses single line imports", function()
test_line(3, 0, 11, "https://pkg.go.dev/fmt")
end)
it("parses single line named imports", function()
test_line(5, 0, 15, "https://pkg.go.dev/io")
end)
it("parses imports in list", function()
test_line(8, 1, 26, "https://pkg.go.dev/github.com/joho/godotenv")
end)
it("parses named imports in list", function()
test_line(9, 1, 27, "https://pkg.go.dev/k8s.io/api/core/v1")
end)
end)
1 change: 1 addition & 0 deletions test/vendor/nvim-treesitter
Submodule nvim-treesitter added at 19f69a

0 comments on commit d446ed3

Please sign in to comment.