extract: process interface wrapper method with variadic parameter

Fixes #1055
This commit is contained in:
エガオノキミヘ
2021-03-24 18:48:03 +08:00
committed by GitHub
parent 84ad46751a
commit 451c754068
6 changed files with 100 additions and 51 deletions

View File

@@ -202,8 +202,16 @@ func (e *Extractor) genContent(importPath string, p *types.Package) ([]byte, err
if args[j] = v.Name(); args[j] == "" {
args[j] = fmt.Sprintf("a%d", j)
}
// process interface method variadic parameter
if sign.Variadic() && j == len(args)-1 { // check is last arg
// only replace the first "[]" to "..."
at := types.TypeString(v.Type(), qualify)[2:]
params[j] = args[j] + " ..." + at
args[j] += "..."
} else {
params[j] = args[j] + " " + types.TypeString(v.Type(), qualify)
}
}
arg := "(" + strings.Join(args, ", ") + ")"
param := "(" + strings.Join(params, ", ") + ")"

View File

@@ -85,6 +85,41 @@ func TestPackages(t *testing.T) {
importPath: "guthib.com/baz",
expected: expectedOutput,
},
{
desc: "using relative path, interface method parameter is variadic",
wd: "./testdata/7/src/guthib.com/variadic",
arg: "../variadic",
importPath: "guthib.com/variadic",
expected: `
// Code generated by 'yaegi extract guthib.com/variadic'. DO NOT EDIT.
package variadic
import (
"guthib.com/variadic"
"reflect"
)
func init() {
Symbols["guthib.com/variadic"] = map[string]reflect.Value{
// type definitions
"Variadic": reflect.ValueOf((*variadic.Variadic)(nil)),
// interface wrapper definitions
"_Variadic": reflect.ValueOf((*_guthib_com_variadic_Variadic)(nil)),
}
}
// _guthib_com_variadic_Variadic is an interface wrapper for Variadic type
type _guthib_com_variadic_Variadic struct {
WCall func(method string, args ...[]interface{}) (interface{}, error)
}
func (W _guthib_com_variadic_Variadic) Call(method string, args ...[]interface{}) (interface{}, error) {
return W.WCall(method, args...)
}
`[1:],
},
}
for _, test := range testCases {

View File

@@ -0,0 +1 @@
module guthib.com/baz-baz/variadic

View File

@@ -0,0 +1,5 @@
package variadic
type Variadic interface {
Call(method string, args ...[]interface{}) (interface{}, error)
}

View File

@@ -47,38 +47,38 @@ func init() {
// _testing_TB is an interface wrapper for TB type
type _testing_TB struct {
WCleanup func(a0 func())
WError func(args []interface{})
WErrorf func(format string, args []interface{})
WError func(args ...interface{})
WErrorf func(format string, args ...interface{})
WFail func()
WFailNow func()
WFailed func() bool
WFatal func(args []interface{})
WFatalf func(format string, args []interface{})
WFatal func(args ...interface{})
WFatalf func(format string, args ...interface{})
WHelper func()
WLog func(args []interface{})
WLogf func(format string, args []interface{})
WLog func(args ...interface{})
WLogf func(format string, args ...interface{})
WName func() string
WSkip func(args []interface{})
WSkip func(args ...interface{})
WSkipNow func()
WSkipf func(format string, args []interface{})
WSkipf func(format string, args ...interface{})
WSkipped func() bool
WTempDir func() string
}
func (W _testing_TB) Cleanup(a0 func()) { W.WCleanup(a0) }
func (W _testing_TB) Error(args []interface{}) { W.WError(args) }
func (W _testing_TB) Errorf(format string, args []interface{}) { W.WErrorf(format, args) }
func (W _testing_TB) Error(args ...interface{}) { W.WError(args...) }
func (W _testing_TB) Errorf(format string, args ...interface{}) { W.WErrorf(format, args...) }
func (W _testing_TB) Fail() { W.WFail() }
func (W _testing_TB) FailNow() { W.WFailNow() }
func (W _testing_TB) Failed() bool { return W.WFailed() }
func (W _testing_TB) Fatal(args []interface{}) { W.WFatal(args) }
func (W _testing_TB) Fatalf(format string, args []interface{}) { W.WFatalf(format, args) }
func (W _testing_TB) Fatal(args ...interface{}) { W.WFatal(args...) }
func (W _testing_TB) Fatalf(format string, args ...interface{}) { W.WFatalf(format, args...) }
func (W _testing_TB) Helper() { W.WHelper() }
func (W _testing_TB) Log(args []interface{}) { W.WLog(args) }
func (W _testing_TB) Logf(format string, args []interface{}) { W.WLogf(format, args) }
func (W _testing_TB) Log(args ...interface{}) { W.WLog(args...) }
func (W _testing_TB) Logf(format string, args ...interface{}) { W.WLogf(format, args...) }
func (W _testing_TB) Name() string { return W.WName() }
func (W _testing_TB) Skip(args []interface{}) { W.WSkip(args) }
func (W _testing_TB) Skip(args ...interface{}) { W.WSkip(args...) }
func (W _testing_TB) SkipNow() { W.WSkipNow() }
func (W _testing_TB) Skipf(format string, args []interface{}) { W.WSkipf(format, args) }
func (W _testing_TB) Skipf(format string, args ...interface{}) { W.WSkipf(format, args...) }
func (W _testing_TB) Skipped() bool { return W.WSkipped() }
func (W _testing_TB) TempDir() string { return W.WTempDir() }

View File

@@ -47,38 +47,38 @@ func init() {
// _testing_TB is an interface wrapper for TB type
type _testing_TB struct {
WCleanup func(a0 func())
WError func(args []interface{})
WErrorf func(format string, args []interface{})
WError func(args ...interface{})
WErrorf func(format string, args ...interface{})
WFail func()
WFailNow func()
WFailed func() bool
WFatal func(args []interface{})
WFatalf func(format string, args []interface{})
WFatal func(args ...interface{})
WFatalf func(format string, args ...interface{})
WHelper func()
WLog func(args []interface{})
WLogf func(format string, args []interface{})
WLog func(args ...interface{})
WLogf func(format string, args ...interface{})
WName func() string
WSkip func(args []interface{})
WSkip func(args ...interface{})
WSkipNow func()
WSkipf func(format string, args []interface{})
WSkipf func(format string, args ...interface{})
WSkipped func() bool
WTempDir func() string
}
func (W _testing_TB) Cleanup(a0 func()) { W.WCleanup(a0) }
func (W _testing_TB) Error(args []interface{}) { W.WError(args) }
func (W _testing_TB) Errorf(format string, args []interface{}) { W.WErrorf(format, args) }
func (W _testing_TB) Error(args ...interface{}) { W.WError(args...) }
func (W _testing_TB) Errorf(format string, args ...interface{}) { W.WErrorf(format, args...) }
func (W _testing_TB) Fail() { W.WFail() }
func (W _testing_TB) FailNow() { W.WFailNow() }
func (W _testing_TB) Failed() bool { return W.WFailed() }
func (W _testing_TB) Fatal(args []interface{}) { W.WFatal(args) }
func (W _testing_TB) Fatalf(format string, args []interface{}) { W.WFatalf(format, args) }
func (W _testing_TB) Fatal(args ...interface{}) { W.WFatal(args...) }
func (W _testing_TB) Fatalf(format string, args ...interface{}) { W.WFatalf(format, args...) }
func (W _testing_TB) Helper() { W.WHelper() }
func (W _testing_TB) Log(args []interface{}) { W.WLog(args) }
func (W _testing_TB) Logf(format string, args []interface{}) { W.WLogf(format, args) }
func (W _testing_TB) Log(args ...interface{}) { W.WLog(args...) }
func (W _testing_TB) Logf(format string, args ...interface{}) { W.WLogf(format, args...) }
func (W _testing_TB) Name() string { return W.WName() }
func (W _testing_TB) Skip(args []interface{}) { W.WSkip(args) }
func (W _testing_TB) Skip(args ...interface{}) { W.WSkip(args...) }
func (W _testing_TB) SkipNow() { W.WSkipNow() }
func (W _testing_TB) Skipf(format string, args []interface{}) { W.WSkipf(format, args) }
func (W _testing_TB) Skipf(format string, args ...interface{}) { W.WSkipf(format, args...) }
func (W _testing_TB) Skipped() bool { return W.WSkipped() }
func (W _testing_TB) TempDir() string { return W.WTempDir() }