test: gotests

This commit is contained in:
Oleksandr Smirnov 2025-02-24 14:15:30 +02:00
parent 4b0fd060f9
commit 9689af58ff
No known key found for this signature in database
6 changed files with 61 additions and 14 deletions

View file

@ -1,4 +1,4 @@
package for_test package fortest
func Add(x, y int) int { func Add(x, y int) int {
return 2 + x + y return 2 + x + y

View file

@ -1,10 +1,6 @@
package for_test package fortest
import ( import "testing"
"testing"
"gotest.tools/v3/assert"
)
func TestAdd(t *testing.T) { func TestAdd(t *testing.T) {
type args struct { type args struct {
@ -20,7 +16,9 @@ func TestAdd(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.want, Add(tt.args.x, tt.args.y)) if got := Add(tt.args.x, tt.args.y); got != tt.want {
t.Errorf("Add() = %v, want %v", got, tt.want)
}
}) })
} }
} }

7
spec/fixtures/tests/method_input.go vendored Normal file
View file

@ -0,0 +1,7 @@
package fortest
type ForTest struct{}
func (t *ForTest) Add(x, y int) int {
return 2 + x + y
}

26
spec/fixtures/tests/method_output.go vendored Normal file
View file

@ -0,0 +1,26 @@
package fortest
import "testing"
func TestForTest_Add(t *testing.T) {
type args struct {
x int
y int
}
tests := []struct {
name string
tr *ForTest
args args
want int
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tr := &ForTest{}
if got := tr.Add(tt.args.x, tt.args.y); got != tt.want {
t.Errorf("ForTest.Add() = %v, want %v", got, tt.want)
}
})
}
}

View file

@ -15,11 +15,27 @@ T["gotests"] = MiniTest.new_set {}
--- All other parts are handled `gotests` tool itself. --- All other parts are handled `gotests` tool itself.
T["gotests"]["should add test for function under cursor"] = function() T["gotests"]["should add test for function under cursor"] = function()
MiniTest.skip "come back daddy" local tmp = "/home/olex/2.go"
local fixtures = t.get_fixtures "tests/function"
t.writefile(tmp, fixtures.input)
child.cmd("silent edit " .. tmp)
child.fn.setpos(".", { child.fn.bufnr "%", 3, 6 })
child.cmd "GoTestAdd"
t.eq(fixtures.output, t.readfile(tmp:gsub(".go", "_test.go")))
end end
T["gotests"]["should add test for method under cursor"] = function() T["gotests"]["should add test for method under cursor"] = function()
MiniTest.skip "come back daddy" local tmp = "/home/olex/1.go"
local fixtures = t.get_fixtures "tests/method"
t.writefile(tmp, fixtures.input)
child.cmd("silent edit " .. tmp)
child.fn.setpos(".", { child.fn.bufnr "%", 5, 19 })
child.cmd "GoTestAdd"
t.eq(fixtures.output, t.readfile(tmp:gsub(".go", "_test.go")))
end end
return T return T

View file

@ -1,10 +1,10 @@
local base_dir = vim.env.GOPHER_DIR or vim.fn.expand "%:p:h" local base_dir = vim.env.GOPHER_DIR or vim.fn.expand "%:p:h"
local fixtures_dir = vim.fs.joinpath(base_dir, "/spec/fixtures/")
---@class gopher.TestUtils ---@class gopher.TestUtils
local testutils = {} local testutils = {}
testutils.mininit_path = vim.fs.joinpath(base_dir, "/scripts/minimal_init.lua") testutils.mininit_path = vim.fs.joinpath(base_dir, "scripts", "minimal_init.lua")
testutils.fixtures_dir = vim.fs.joinpath(base_dir, "spec/fixtures")
---@generic T ---@generic T
---@param a T ---@param a T
@ -35,8 +35,8 @@ end
---@return {input: string, output: string} ---@return {input: string, output: string}
function testutils.get_fixtures(fixture) function testutils.get_fixtures(fixture)
return { return {
input = testutils.readfile(fixtures_dir .. fixture .. "_input.go"), input = testutils.readfile(vim.fs.joinpath(testutils.fixtures_dir, fixture) .. "_input.go"),
output = testutils.readfile(fixtures_dir .. fixture .. "_output.go"), output = testutils.readfile(vim.fs.joinpath(testutils.fixtures_dir, fixture) .. "_output.go"),
} }
end end