github.com/tencent/goom@v1.0.1/internal/proxy/func_unexport_short_test.go (about)

     1  // Package proxy_test 对 proxy 包的测试
     2  package proxy_test
     3  
     4  import (
     5  	"fmt"
     6  	"testing"
     7  
     8  	"github.com/tencent/goom/internal/proxy"
     9  )
    10  
    11  // TestPrintMock 测试 mock fmt.Print
    12  // 在bazel 构建环境下, 因为系统库不支持开启 gcflags=-l ,所以暂不支持系统库中的短函数 mock
    13  func TestPrintMock(t *testing.T) {
    14  	var trampoline = func(a ...interface{}) (n int, err error) {
    15  		return 0, nil
    16  	}
    17  
    18  	// 静态代理函数
    19  	patch, err := proxy.FuncName("fmt.Print", func(a ...interface{}) (n int, err error) {
    20  		// 调用原来的函数
    21  		return fmt.Println("called fmt.Print, args:", a)
    22  	}, &trampoline)
    23  	if err != nil {
    24  		t.Error("mock print err:", err)
    25  	}
    26  
    27  	fmt.Println("ok", "1")
    28  	patch.Unpatch()
    29  	fmt.Println("unpatched")
    30  	fmt.Println("ok", "2")
    31  }