github.com/jxskiss/gopkg@v0.17.3/forceexport/type_test.go (about)

     1  package forceexport
     2  
     3  import (
     4  	"reflect"
     5  	"strings"
     6  	"testing"
     7  	"unsafe"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  
    11  	"github.com/jxskiss/gopkg/reflectx"
    12  )
    13  
    14  type TestStruct struct {
    15  	// pass
    16  }
    17  
    18  func TestScanType(t *testing.T) {
    19  	got := make([]string, 0)
    20  	ScanType(func(name string, typ *reflectx.RType) {
    21  		if strings.HasPrefix(name, "github.com/jxskiss/gopkg") {
    22  			got = append(got, name)
    23  		}
    24  	})
    25  	assert.Contains(t, got, "github.com/jxskiss/gopkg/forceexport.iface")
    26  	assert.Contains(t, got, "github.com/jxskiss/gopkg/forceexport.moduledata")
    27  	assert.Contains(t, got, "github.com/jxskiss/gopkg/forceexport.TestStruct")
    28  	assert.Contains(t, got, "github.com/jxskiss/gopkg/reflectx.RType")
    29  }
    30  
    31  func TestRuntimeModuledata(t *testing.T) {
    32  	var rtmdtype *reflectx.RType
    33  	assert.NotPanics(t, func() {
    34  		rtmdtype = GetType("runtime.moduledata")
    35  	})
    36  	assert.Equal(t, reflect.Struct, rtmdtype.Kind())
    37  
    38  	nextfield, ok := rtmdtype.FieldByName("next")
    39  	assert.True(t, ok)
    40  	assert.Equal(t, reflect.Ptr, nextfield.Type.Kind())
    41  }
    42  
    43  func TestTypeEquality(t *testing.T) {
    44  	ifacetype := GetType("github.com/jxskiss/gopkg/forceexport.iface").ToType()
    45  	assert.Equal(t, reflect.TypeOf(iface{}), ifacetype)
    46  
    47  	structtype := GetType("github.com/jxskiss/gopkg/forceexport.TestStruct").ToType()
    48  	assert.Equal(t, reflect.TypeOf(TestStruct{}), structtype)
    49  }
    50  
    51  // iface is a copy type of runtime.iface.
    52  type iface struct {
    53  	tab  unsafe.Pointer // *itab
    54  	data unsafe.Pointer
    55  }