github.com/henrylee2cn/go-forceexport@v0.0.0-20200725083357-a18b02135c5b/forceexport_test.go (about) 1 package forceexport 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 ) 9 10 func TestSymtabNamesOfActiveFunc(t *testing.T) { 11 t.Log(SymtabNamesOfActiveFunc()) 12 } 13 14 func TestTimeNow(t *testing.T) { 15 var timeNowFunc func() (int64, int32) 16 GetFunc(&timeNowFunc, "time.now") 17 sec, nsec := timeNowFunc() 18 assert.NotEqual(t, 0, sec) 19 assert.NotEqual(t, 0, nsec) 20 t.Logf("sec:%d, nsec:%d", sec, nsec) 21 } 22 23 // Note that we need to disable inlining here, or else the function won't be 24 // compiled into the binary. We also need to call it from the test so that the 25 // compiler doesn't remove it because it's unused. 26 //go:noinline 27 func addOne(x int) int { 28 return x + 1 29 } 30 31 func TestAddOne(t *testing.T) { 32 assert.Equal(t, 4, addOne(3)) 33 var addOneFunc func(x int) int 34 err := GetFunc(&addOneFunc, "github.com/henrylee2cn/go-forceexport.addOne") 35 assert.NoError(t, err) 36 assert.Equal(t, 4, addOneFunc(3)) 37 } 38 39 func TestGetSelf(t *testing.T) { 40 var getFunc func(interface{}, string) error 41 err := GetFunc(&getFunc, "github.com/henrylee2cn/go-forceexport.GetFunc") 42 assert.NoError(t, err) 43 // The two functions should share the same code pointer, so they should 44 // have the same string representation. 45 assert.Equal(t, fmt.Sprintf("%p", GetFunc), fmt.Sprintf("%p", getFunc)) 46 // if fmt.Sprintf("%p", getFunc) != fmt.Sprintf("%p", GetFunc) { 47 // t.Fatalf("Expected ") 48 // } 49 // Call it again on itself! 50 err = getFunc(&getFunc, "github.com/henrylee2cn/go-forceexport.GetFunc") 51 assert.NoError(t, err) 52 assert.Equal(t, fmt.Sprintf("%p", GetFunc), fmt.Sprintf("%p", getFunc)) 53 } 54 55 func TestInvalidFunc(t *testing.T) { 56 var invalidFunc func() 57 err := GetFunc(&invalidFunc, "invalidpackage.invalidfunction") 58 assert.EqualError(t, err, "invalid function name: invalidpackage.invalidfunction") 59 assert.Nil(t, invalidFunc) 60 }