diff --git a/patch.go b/patch.go index 25b82cd..c62d448 100644 --- a/patch.go +++ b/patch.go @@ -2,10 +2,11 @@ package gomonkey import ( "fmt" - "github.com/agiledragon/gomonkey/v2/creflect" "reflect" "syscall" "unsafe" + + "github.com/agiledragon/gomonkey/v2/creflect" ) type Patches struct { @@ -335,7 +336,11 @@ func funcToMethod(funcType reflect.Type, doubleFunc interface{}) reflect.Value { } vf := reflect.ValueOf(doubleFunc) return reflect.MakeFunc(funcType, func(in []reflect.Value) []reflect.Value { - return vf.Call(in[1:]) + if funcType.IsVariadic() { + return vf.CallSlice(in[1:]) + } else { + return vf.Call(in[1:]) + } }) } diff --git a/test/apply_method_func_test.go b/test/apply_method_func_test.go index 62eef46..0dee818 100755 --- a/test/apply_method_func_test.go +++ b/test/apply_method_func_test.go @@ -82,5 +82,18 @@ func TestApplyMethodFunc(t *testing.T) { So(len(slice), ShouldEqual, 1) So(slice[0], ShouldEqual, 4) }) + + Convey("for variadic method", func() { + slice = fake.NewSlice() + count := slice.Append(1, 2, 3) + So(count, ShouldEqual, 3) + patches := ApplyMethodFunc(s, "Append", func(_ ...int) int { + return 0 + }) + defer patches.Reset() + count = slice.Append(4, 5, 6) + So(count, ShouldEqual, 0) + So(len(slice), ShouldEqual, 3) + }) }) } diff --git a/test/fake/fake.go b/test/fake/fake.go index 2c817e0..5ebec5e 100644 --- a/test/fake/fake.go +++ b/test/fake/fake.go @@ -88,6 +88,12 @@ func (this* Slice) Remove(elem int) error { return nil } +func (this *Slice) Append(elems ...int) int { + *this = append(*this, elems...) + fmt.Printf("Slice: Append elem: %v succ\n", elems) + return len(elems) +} + func ReadLeaf(url string) (string, error) { output := fmt.Sprintf("%s, %s!", "Hello", "World") return output, nil