Giter Club home page Giter Club logo

Comments (7)

xhd2015 avatar xhd2015 commented on May 25, 2024

Yes, I am working on it. Should be landed tonight.

from xgo.

xhd2015 avatar xhd2015 commented on May 25, 2024

The work to support method mocking is done, see commit 1c2f189.

With this, the xgo has been upgraded to v1.0.5, please run xgo upgrade to get the newest toolchain.

Also you may need to upgrade xgo/runtime:

go get github.com/xhd2015/xgo/runtime@latest

Here is an example:

type MyStruct struct {
    name string
}
func (c *MyStruct) Name() name{
    return c.name
}

func TestMethodMock(t *testing.T){
    myStruct := &MyStruct{
        name: "my struct",
    }
    otherStruct := &MyStruct{
        name: "other struct",
    }
    mock.Mock(myStruct.Name, func(ctx context.Context, fn *core.FuncInfo, args core.Object, results core.Object) error {
        results.GetFieldIndex(0).Set("mock struct")
        return nil
    })

    // myStruct is affected
    name := myStruct.Name()
    if name!="mock struct"{
        t.Fatalf("expect myStruct.Name() to be 'mock struct', actual: %s", name)
    }

    // otherStruct is not affected
    otherName := otherStruct.Name()
    if otherName!="other struct"{
        t.Fatalf("expect otherStruct.Name() to be 'other struct', actual: %s", otherName)
    }
}

from xgo.

xhd2015 avatar xhd2015 commented on May 25, 2024

In case you want to check detail of the implementation, here is how we check if a receiver pointer is the same as a method value's bound instance.

NOTE: So much go internals here ,I will write a document on the memory layout of method value later

// this function checks if the given
// `recvPtr` has the same value compared
// to the given `methodValue`.
// The `methodValue` should be passed as
// `file.Write“.
func isSameBoundMethod(recvPtr interface{}, methodValue interface{}) bool {
	// can also be a constant
	// size := unsafe.Sizeof(*(*large)(nil))
	size := reflect.TypeOf(recvPtr).Elem().Size()
	type _intfRecv struct {
		_    uintptr // type word
		data *byte   // data word
	}

	a := (*_intfRecv)(unsafe.Pointer(&recvPtr))
	type _methodValue struct {
		_    uintptr // pc
		recv byte
	}
	type _intf struct {
		_    uintptr // type word
		data *_methodValue
	}
	ppb := (*_intf)(unsafe.Pointer(&methodValue))
	pb := *ppb
	b := unsafe.Pointer(&pb.data.recv)

	return __xgo_link_mem_equal(unsafe.Pointer(a.data), b, size)
}

from xgo.

WAY29 avatar WAY29 commented on May 25, 2024

Is there another way to mock all struct instance methods instead of a single struct instance method?

from xgo.

xhd2015 avatar xhd2015 commented on May 25, 2024

Yes, simply write:

mock.Mock((*MyStruct).Name, ...)

Notice the args first field is the receiver instance.

from xgo.

xhd2015 avatar xhd2015 commented on May 25, 2024

As for unexported method, that should be also simple to support, I may later add two functions:

func MockByName('pkg', 'method', interceptor)
func MockMethodByName(instance,'method',interceptor)

from xgo.

WAY29 avatar WAY29 commented on May 25, 2024

Thanks, I will try it later.

from xgo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.