Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support get private member method #36

Merged
merged 1 commit into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions exp/utils_above_1_18.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//go:build go1.18
// +build go1.18

/*
* Copyright 2023 ByteDance Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package exp

import (
"unsafe"

"github.com/bytedance/mockey/internal/tool"
"github.com/bytedance/mockey/internal/unsafereflect"
)

// GetPrivateMemberMethod resolve a method from an instance, include private method.
//
// F must fit the shape of specific method, include receiver as the first argument.
// Especially, the receiver can be replaced as interface when F is declaring,
// this will be very useful when receiver type is not exported for other packages.
//
// for example:
//
// GetPrivateMemberMethod[func(*bytes.Buffer) bool](&bytes.Buffer{}, "empty")
// GetPrivateMemberMethod[func(hash.Hash) [sha256.Size]byte](sha256.New(), "checkSum")
func GetPrivateMemberMethod[F interface{}](instance interface{}, methodName string) interface{} {
tfn, ok := unsafereflect.MethodByName(instance, methodName)
if !ok {
tool.Assert(false, "can't reflect instance method :%v", methodName)
return nil
}
// return with (unsafe) function type cast
return *(*F)(unsafe.Pointer(&tfn))
}
69 changes: 69 additions & 0 deletions exp/utils_above_1_18_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//go:build go1.18
// +build go1.18

/*
* Copyright 2023 ByteDance Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package exp

import (
"bytes"
"testing"

"github.com/bytedance/mockey"
"github.com/bytedance/mockey/internal/tool"
"github.com/smartystreets/goconvey/convey"
)

func TestGetPrivateMemberMethod(t *testing.T) {
mockey.PatchConvey("FakeMethod", t, func() {
convey.So(func() {
GetPrivateMemberMethod[func()](&bytes.Buffer{}, "FakeMethod")
}, convey.ShouldPanicWith, "can't reflect instance method :FakeMethod")
})

mockey.PatchConvey("OriginalGetMethod", t, func() {
convey.So(func() {
mockey.GetMethod(&bytes.Buffer{}, "empty")
}, convey.ShouldPanicWith, "can't reflect instance method :empty")
})

mockey.PatchConvey("ExportFunc", t, func() {
convey.So(func() {
exportedFunc := GetPrivateMemberMethod[func(*bytes.Buffer) int](&bytes.Buffer{}, "Len")
var mocked bool
mockey.Mock(exportedFunc).To(func(buffer *bytes.Buffer) int {
mocked = true
return 0
}).Build()
_ = new(bytes.Buffer).Len()
tool.Assert(mocked, "function should be mocked")
}, convey.ShouldNotPanic)
})

mockey.PatchConvey("PrivateFunc", t, func() {
convey.So(func() {
privateFunc := GetPrivateMemberMethod[func(*bytes.Buffer) bool](&bytes.Buffer{}, "empty")
var mocked bool
mockey.Mock(privateFunc).To(func(buffer *bytes.Buffer) bool {
mocked = true
return true
}).Build()
_, _ = new(bytes.Buffer).ReadByte()
tool.Assert(mocked, "function should be mocked")
}, convey.ShouldNotPanic)
})
}
53 changes: 53 additions & 0 deletions internal/unsafereflect/name_above_1_17.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//go:build go1.17
// +build go1.17

/*
* Copyright 2023 ByteDance Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package unsafereflect

import "unsafe"

// name is an encoded type name with optional extra data.
type name struct {
bytes *byte
}

func (n name) data(off int, whySafe string) *byte {
return (*byte)(add(unsafe.Pointer(n.bytes), uintptr(off), whySafe))
}

func (n name) readVarint(off int) (int, int) {
v := 0
for i := 0; ; i++ {
x := *n.data(off+i, "read varint")
v += int(x&0x7f) << (7 * i)
if x&0x80 == 0 {
return i + 1, v
}
}
}

func (n name) name() (s string) {
if n.bytes == nil {
return
}
i, l := n.readVarint(1)
hdr := (*_String)(unsafe.Pointer(&s))
hdr.Data = unsafe.Pointer(n.data(1+i, "non-empty string"))
hdr.Len = l
return
}
41 changes: 41 additions & 0 deletions internal/unsafereflect/name_below_1_17.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//go:build !go1.17
// +build !go1.17

/*
* Copyright 2023 ByteDance Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package unsafereflect

import (
"unsafe"
)

// name is an encoded type name with optional extra data.
type name struct {
bytes *byte
}

func (n name) name() (s string) {
if n.bytes == nil {
return
}
b := (*[4]byte)(unsafe.Pointer(n.bytes))

hdr := (*_String)(unsafe.Pointer(&s))
hdr.Data = unsafe.Pointer(&b[3])
hdr.Len = int(b[1])<<8 | int(b[2])
return s
}
Loading
Loading