github.com/goccy/go-reflect@v1.2.1-0.20220925055700-4646ad15ec8a/reflect_test.go (about)

     1  package reflect_test
     2  
     3  import (
     4  	"fmt"
     5  	corereflect "reflect"
     6  	"testing"
     7  	"unsafe"
     8  
     9  	"github.com/goccy/go-reflect"
    10  )
    11  
    12  func TestTypeID(t *testing.T) {
    13  	intID := reflect.TypeID(int(0))
    14  	if intID != reflect.TypeID(int(0)) {
    15  		t.Fatal("failed to get typeid")
    16  	}
    17  }
    18  
    19  func TestTypeAndPtrOf(t *testing.T) {
    20  	typ, ptr := reflect.TypeAndPtrOf(int(10))
    21  	if typ.Kind() != reflect.Int {
    22  		t.Fatal("failed to get type")
    23  	}
    24  	if *(*int)(ptr) != 10 {
    25  		t.Fatal("failed to get ptr")
    26  	}
    27  }
    28  
    29  func TestValueNoEscapeOf(t *testing.T) {
    30  	v := reflect.ValueNoEscapeOf(&struct{ I int }{I: 10})
    31  	if v.Elem().Field(0).Int() != 10 {
    32  		t.Fatal("failed to create reflect.Value from ValueNoEscapeOf")
    33  	}
    34  }
    35  
    36  func TestToReflectType(t *testing.T) {
    37  	typ := reflect.ToReflectType(reflect.TypeOf(1))
    38  	if fmt.Sprintf("%T", typ) != "*reflect.rtype" {
    39  		t.Fatalf("failed to convert reflect.Type")
    40  	}
    41  }
    42  
    43  func TestToReflectValue(t *testing.T) {
    44  	v := reflect.ToReflectValue(reflect.ValueOf(1))
    45  	if fmt.Sprintf("%T", v) != "reflect.Value" {
    46  		t.Fatalf("failed to convert reflect.Value")
    47  	}
    48  }
    49  
    50  func TestToType(t *testing.T) {
    51  	typ := reflect.ToType(corereflect.TypeOf(1))
    52  	if fmt.Sprintf("%T", typ) != "*reflect.rtype" {
    53  		t.Fatalf("failed to convert reflect.Type")
    54  	}
    55  }
    56  
    57  func TestToValue(t *testing.T) {
    58  	v := reflect.ToValue(corereflect.ValueOf(1))
    59  	if fmt.Sprintf("%T", v) != "reflect.Value" {
    60  		t.Fatalf("failed to convert reflect.Value")
    61  	}
    62  }
    63  
    64  func TestNewAt(t *testing.T) {
    65  	var i int
    66  
    67  	ivalFromField := reflect.ValueOf(&struct{ foo int }{100}).Elem().Field(0)
    68  	ival := reflect.ValueOf(&i).Elem()
    69  
    70  	v := reflect.NewAt(ivalFromField.Type(), unsafe.Pointer(ivalFromField.UnsafeAddr())).Elem()
    71  	ival.Set(v)
    72  	v.Set(ival)
    73  	if v.Int() != 100 {
    74  		t.Fatal("failed to NewAt")
    75  	}
    76  }
    77  
    78  func TestTypeBits(t *testing.T) {
    79  	bits := reflect.TypeOf(1).Bits()
    80  	if bits != 1<<6 {
    81  		t.Fatal("failed to get bits from type")
    82  	}
    83  }
    84  
    85  func TestTypeIsVariadic(t *testing.T) {
    86  	if !reflect.TypeOf(func(...int) {}).IsVariadic() {
    87  		t.Fatal("doesn't work IsVariadic")
    88  	}
    89  }
    90  
    91  func TestTypeFieldByNameFunc(t *testing.T) {
    92  	_, ok := reflect.TypeOf(struct {
    93  		Foo int
    94  	}{}).FieldByNameFunc(func(name string) bool {
    95  		return name == "Foo"
    96  	})
    97  	if !ok {
    98  		t.Fatal("failed to FieldByNameFunc")
    99  	}
   100  }
   101  
   102  func TestTypeLen(t *testing.T) {
   103  	if reflect.TypeOf([3]int{}).Len() != 3 {
   104  		t.Fatal("failed to Type.Len")
   105  	}
   106  }
   107  
   108  func TestTypeOut(t *testing.T) {
   109  	if reflect.TypeOf(func() int { return 0 }).Out(0).Kind() != reflect.Int {
   110  		t.Fatal("failed to get output parameter")
   111  	}
   112  }
   113  
   114  func TestTypeCanInterface(t *testing.T) {
   115  	v := "hello"
   116  	if !reflect.ValueOf(v).CanInterface() {
   117  		t.Fatal("failed to Type.CanInterface")
   118  	}
   119  }
   120  
   121  func TestValueFieldByNameFunc(t *testing.T) {
   122  	field := reflect.ValueOf(struct {
   123  		Foo int
   124  	}{}).FieldByNameFunc(func(name string) bool {
   125  		return name == "Foo"
   126  	})
   127  	if field.Type().Kind() != reflect.Int {
   128  		t.Fatal("failed to FieldByNameFunc")
   129  	}
   130  }