test(iferr): add test

chore(ci): install go, and go bins for tests
This commit is contained in:
Oleksandr Smirnov 2025-02-17 17:57:41 +02:00
parent ecb9919e02
commit 8b1bb40baa
No known key found for this signature in database
6 changed files with 103 additions and 6 deletions

View file

@ -23,7 +23,8 @@ jobs:
version: 3.x version: 3.x
repo-token: ${{ secrets.GITHUB_TOKEN }} repo-token: ${{ secrets.GITHUB_TOKEN }}
- uses: actions/checkout@v3 - name: Install Go
uses: actions/setup-go@v5
- name: Cache .tests - name: Cache .tests
uses: actions/cache@v4 uses: actions/cache@v4
@ -37,6 +38,11 @@ jobs:
neovim: true neovim: true
version: ${{ matrix.version }} version: ${{ matrix.version }}
- uses: actions/checkout@v3
- name: Install Go bins
run: nvim --headless -u "./scripts/minimal_init.lua" -c "GoInstallDeps" -c "qa!"
- name: Run Tests - name: Run Tests
run: | run: |
nvim --version nvim --version

9
spec/fixtures/iferr/iferr_input.go vendored Normal file
View file

@ -0,0 +1,9 @@
package main
func test() error {
return nil
}
func main() {
err := test()
}

12
spec/fixtures/iferr/iferr_output.go vendored Normal file
View file

@ -0,0 +1,12 @@
package main
func test() error {
return nil
}
func main() {
err := test()
if err != nil {
return
}
}

View file

@ -0,0 +1,27 @@
local t = require "spec.testutils"
local child = MiniTest.new_child_neovim()
local T = MiniTest.new_set {
hooks = {
post_once = child.stop,
pre_case = function()
child.restart { "-u", "scripts/minimal_init.lua" }
end,
},
}
T["iferr"] = MiniTest.new_set {}
T["iferr"]["works"] = function()
local tmp = vim.env.HOME .. "/test.go"
local fixtures = t.fixtures.read "iferr/iferr"
t.fixtures.write(tmp, fixtures.input)
child.cmd("silent edit " .. tmp)
child.fn.setpos(".", { child.fn.bufnr "%", 8, 2, 0 })
child.cmd "GoIfErr"
child.cmd "write"
t.eq(t.readfile(tmp), fixtures.output)
end
return T

View file

@ -1,9 +1,27 @@
local t = require "spec.testutils" local t = require "spec.testutils"
local T = MiniTest.new_set {} local child = MiniTest.new_child_neovim()
local T = MiniTest.new_set {
hooks = {
post_once = child.stop,
pre_case = function()
child.restart { "-u", "scripts/minimal_init.lua" }
end,
},
}
T["struct_tags"] = MiniTest.new_set {} T["struct_tags"] = MiniTest.new_set {}
T["struct_tags"]["fuck it"] = function() T["struct_tags"][".add"] = function()
t.eq(1, 1) local tmp = vim.env.HOME .. "/test.go"
local fixtures = t.fixtures.read "tags/add"
t.fixtures.write(tmp, fixtures.input)
child.cmd("silent edit " .. tmp)
child.fn.setpos(".", { child.fn.bufnr "%", 3, 6, 0 })
child.cmd "GoTagAdd json"
child.cmd "write"
t.eq(t.readfile(tmp), fixtures.output)
end end
return T return T

View file

@ -12,7 +12,32 @@ function testutils.eq(a, b)
return MiniTest.expect.equality(a, b) return MiniTest.expect.equality(a, b)
end end
-- TODO: continue with writing fixtures helpers ---@return string
-- https://github.com/olexsmir/gopher.nvim/pull/71/files function testutils.tmpfile()
return vim.fn.tempname() .. ".go"
end
---@param path string
---@return string
function testutils.readfile(path)
return vim.fn.join(vim.fn.readfile(path), "\n")
end
testutils.fixtures = {}
---@param fixture string
---@return {input: string, output: string}
function testutils.fixtures.read(fixture)
return {
input = testutils.readfile(fixtures_dir .. fixture .. "_input.go"),
output = testutils.readfile(fixtures_dir .. fixture .. "_output.go"),
}
end
---@param fpath string
---@param fixture string
function testutils.fixtures.write(fpath, fixture)
vim.fn.writefile(vim.split(fixture, "\n"), fpath)
end
return testutils return testutils