bytedance / mockey

a simple and easy-to-use golang mock library
Apache License 2.0
556 stars 22 forks source link

origin call does not work properly with go 1.15 and lower #24

Closed Sychorius closed 9 months ago

Sychorius commented 1 year ago

Describe the bug

Short functions not run properly when fallback to original function call in go1.15 or lower env.

To Reproduce

Steps to reproduce the behavior:

  1. Make sure go version <= 1.15
  2. Run this code
    
    // go test -gcflags="all=-l -N" ./... -count 1
    func TestXxx(t *testing.T) {
    PatchConvey("xxx", t, func() {
        fn := func(i int) string {
            return "fn:not here"
        }
        Mock(fn).When(func(i int) bool { return i > 0 && i < 3 }).To(func(i int) string {
            panic("to:not here")
        }).Build()
        So(fn(0), ShouldEqual, "fn:not here")
    })
    }
3. Got this error

Expected: 'fn:not here' Actual: '' (Should be equal)

**Expected behavior**
Pass all tests

**Screenshots**

If applicable, add screenshots to help explain your problem.

**Mockey version:** 
v1.2.2

**Environment:**
The output of `go env`.
go version go1.14.13 darwin/amd64
go version go1.15.6 darwin/amd64

**Additional Info:**
This is may because the target function is too stort in go 1.15 and lower. When change code above to shown below, the test will pass:
```diff
func TestXxx(t *testing.T) {
    PatchConvey("xxx", t, func() {
        fn := func(i int) string {
+           fmt.Println()
            return "fn:not here"
        }
        Mock(fn).When(func(i int) bool { return i > 0 && i < 3 }).To(func(i int) string {
            panic("to:not here")
        }).Build()
        So(fn(0), ShouldEqual, "fn:not here")
    })
}