tlog.app/go/loc@v0.6.2-0.20231112073106-b6382a0ac518/func_test.go (about)

     1  package loc
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  	"unsafe"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  type eface struct {
    12  	_ unsafe.Pointer
    13  	_ unsafe.Pointer
    14  }
    15  
    16  func TestFuncFunc(t *testing.T) {
    17  	var f interface{}
    18  
    19  	f = TestSetCache
    20  	r := reflect.ValueOf(f)
    21  	t.Logf("reflect: %v %v %v %x", r.Kind(), r.Type(), r, *(*eface)(unsafe.Pointer(&f)))
    22  
    23  	f = TestFuncFunc
    24  	r = reflect.ValueOf(f)
    25  	t.Logf("reflect: %v %v %v %x", r.Kind(), r.Type(), r, *(*eface)(unsafe.Pointer(&f)))
    26  
    27  	pc := FuncEntryFromFunc(TestFuncFunc)
    28  
    29  	name, file, line := pc.NameFileLine()
    30  	t.Logf("pc: %v %v %v", name, file, line)
    31  
    32  	assert.Equal(t, FuncEntry(0), pc)
    33  
    34  	assert.Equal(t, FuncEntry(0), PC(reflect.ValueOf(TestFuncFunc).Pointer()))
    35  
    36  	name, file, line = FuncEntryFromFunc(nil).NameFileLine()
    37  	t.Logf("pc: %v %v %v", name, file, line)
    38  
    39  	var q func()
    40  
    41  	name, file, line = FuncEntryFromFunc(q).NameFileLine()
    42  	t.Logf("pc: %v %v %v", name, file, line)
    43  
    44  	var e PC
    45  	q = func() {
    46  		t.Logf("closure func")
    47  
    48  		e = FuncEntry(0)
    49  	}
    50  
    51  	q()
    52  
    53  	rt := reflect.ValueOf(q).Type()
    54  	for i := 0; i < rt.NumIn(); i++ {
    55  		t.Logf("q in  %v", rt.In(i))
    56  	}
    57  	for i := 0; i < rt.NumOut(); i++ {
    58  		t.Logf("q out %v", rt.Out(i))
    59  	}
    60  
    61  	pc = FuncEntryFromFunc(q)
    62  
    63  	name, file, line = pc.NameFileLine()
    64  	t.Logf("pc: %v %v %v", name, file, line)
    65  
    66  	assert.Equal(t, e, pc)
    67  
    68  	assert.Panics(t, func() {
    69  		pc = FuncEntryFromFunc(3)
    70  	})
    71  }