go-kiss / monkey

Go语言猴子补丁框架
https://taoshu.in/go/monkey/
MIT License
119 stars 18 forks source link

结构体字段多导致 go test 卡住 #8

Closed shijie152 closed 2 years ago

shijie152 commented 2 years ago
// main.go
package main

type TreeNode struct {
    Name     string
    ID       int
    Children []*TreeNode
    A        int
    B        int
    C        int
    // 注释掉 go test 可以正常运行
    A1        int
    B2        int
    C1        int
}

func first[T any](a, b T) T { return a }

func second[T any](a, b T) T { return b }

// main_test.go
package main

import (
    "fmt"
    "reflect"
    "testing"

    "github.com/go-kiss/monkey"
)

func Test_main(t *testing.T) {
    t1 := TreeNode{ ID: 1 }
    t2 := TreeNode{ ID: 2 }

    monkey.Patch(first[TreeNode], second[TreeNode], monkey.OptGeneric)
    if reflect.DeepEqual(t2, first(t1, t2)) {
        fmt.Println("equal")
    } else {
        fmt.Println("not equal")
        t.Fatalf("not equal")
    }
}
taoso commented 2 years ago

@shijie152 非常感谢分享这个问题。之前对 Go 生成的机器码了解不够深入,代码确实有 BUG。你可以测试一下我提的 MR #10