看文档是支持泛型函数指定类型 mock 的,但是自己尝试下来确不支持,求解答,谢谢
报错:
panic: func not instrumented by xgo, see
https://github.com/xhd2015/xgo/tree/master/doc/ERR_NOT_INSTRUMENTED.md: demo_test.MyFuncT[int]
```go
package demo_test
import (
"testing"
"
github.com/xhd2015/xgo/runtime/mock"
)
func MyFunc() string {
return "my func"
}
func MyFuncT[T any](t T) T {
return t
}
var MyFuncTInt = MyFuncT[int]
func TestFuncMock(t *testing.T) {
mock.Patch(MyFunc, func() string {
return "mock func"
})
text := MyFunc()
if text != "mock func" {
t.Fatalf("expect MyFunc() to be 'mock func', actual: %s", text)
}
}
func TestFuncMockIntA(t *testing.T) {
mock.Patch(MyFuncTInt, func(t int) int {
return 2
})
text := MyFuncTInt(1)
if text != 2 {
t.Fatalf("expect intA() to be '2', actual: %d", text)
}
}
func TestFuncMockT(t *testing.T) {
println("111111")
mock.Patch(MyFuncTInt, func(t int) int {
println("333333")
return 2
})
println("222222")
text := MyFuncTInt(1)
if text != 2 {
t.Fatalf("expect MyFuncT() to be '2', actual: %d", text)
}
}
```
```console
xgo test -v ./
=== RUN TestFuncMock
--- PASS: TestFuncMock (0.00s)
=== RUN TestFuncMockIntA
--- FAIL: TestFuncMockIntA (0.00s)
panic: func not instrumented by xgo, see
https://github.com/xhd2015/xgo/tree/master/doc/ERR_NOT_INSTRUMENTED.md: demo_test.MyFuncT[int] [recovered]
panic: func not instrumented by xgo, see
https://github.com/xhd2015/xgo/tree/master/doc/ERR_NOT_INSTRUMENTED.md: demo_test.MyFuncT[int]
goroutine 19 [running]:
testing.tRunner.func1.2({0x100c85be0, 0x140000a0ac0})
/Users/lula/.xgo/go-instrument/go1.24.2_op_ho_Ce_go_1._li_9f53f045/go1.24.2/src/testing/testing.go:1734 +0x1ac
testing.tRunner.func1()
/Users/lula/.xgo/go-instrument/go1.24.2_op_ho_Ce_go_1._li_9f53f045/go1.24.2/src/testing/testing.go:1737 +0x334
panic({0x100c85be0?, 0x140000a0ac0?})
/Users/lula/.xgo/go-instrument/go1.24.2_op_ho_Ce_go_1._li_9f53f045/go1.24.2/src/runtime/panic.go:792 +0x124
github.com/xhd2015/xgo/runtime/internal/trap.Inspect({0x100c75480, 0x100cb6540})
/Users/lula/Library/Go/pkg/mod/
github.com/xhd2015/xgo/[email protected]/internal/trap/inspect.go:68 +0x530
github.com/xhd2015/xgo/runtime/internal/trap.pushMockReplacer({0x100c75480, 0x100cb6540}, {0x100c75480, 0x100cb6550})
/Users/lula/Library/Go/pkg/mod/
github.com/xhd2015/xgo/[email protected]/internal/trap/mock.go:160 +0x3d0
github.com/xhd2015/xgo/runtime/internal/trap.PushMockReplacer(...) /Users/lula/Library/Go/pkg/mod/
github.com/xhd2015/xgo/[email protected]/internal/trap/mock.go:47github.com/xhd2015/xgo/runtime/mock.Patch(...) /Users/lula/Library/Go/pkg/mod/
github.com/xhd2015/xgo/[email protected]/mock/patch.go:15demo_test.TestFuncMockIntA(0x14000146380)
/Users/lula/Code/Go/go-learn/demo/demo_test.go:30 +0x14c
testing.tRunner(0x14000146380, 0x100cb5c50)
/Users/lula/.xgo/go-instrument/go1.24.2_op_ho_Ce_go_1._li_9f53f045/go1.24.2/src/testing/testing.go:1792 +0xe4
created by testing.(*T).Run in goroutine 1
/Users/lula/.xgo/go-instrument/go1.24.2_op_ho_Ce_go_1._li_9f53f045/go1.24.2/src/testing/testing.go:1851 +0x374
FAIL demo 0.259s
FAIL
```