From 42666991e584a51d0a4d571dde18d8d066dc527b Mon Sep 17 00:00:00 2001 From: pengwenxin Date: Thu, 19 May 2022 19:06:51 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=85=BC=E5=AE=B9=E5=8F=AF=E5=8F=98?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- patch.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) 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:]) + } }) } From 0ee58aa07b29c0cadf7c70e8744659188605aca7 Mon Sep 17 00:00:00 2001 From: pengwenxin Date: Thu, 19 May 2022 19:29:50 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/apply_method_func_test.go | 13 +++++++++++++ test/fake/fake.go | 6 ++++++ 2 files changed, 19 insertions(+) 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