github.com/leromarinvit/typist@v0.0.0-20170209001249-15cee4975bef/from_string_test.go (about)

     1  package typist
     2  
     3  import (
     4  	"os"
     5  	"reflect"
     6  	"testing"
     7  )
     8  
     9  func TestRtypeAssumption(t *testing.T) {
    10  	pRtypeType := reflect.ValueOf(reflect.TypeOf(0)).Type()
    11  	if pRtypeType.String() != "*reflect.rtype" {
    12  		t.Fatal("Expected *reflect.rtype, got", pRtypeType.String())
    13  	}
    14  }
    15  
    16  func TestValueAssumption(t *testing.T) {
    17  	field, ok := reflect.TypeOf(reflect.Value{}).FieldByName("ptr")
    18  	if !ok {
    19  		t.Fatal("reflect.Value has no 'ptr' field")
    20  	}
    21  	if field.Type.String() != "unsafe.Pointer" {
    22  		t.Fatalf("reflect.Value.ptr has wrong type %s (expected unsafe.Pointer)", field.Type.String())
    23  	}
    24  }
    25  
    26  func TestTypeByString(t *testing.T) {
    27  	typeName := "*os.File"
    28  	var value *os.File
    29  
    30  	typ, err := TypeByString(typeName)
    31  
    32  	if err != nil {
    33  		t.Fatal(err)
    34  	}
    35  
    36  	if typ.String() != typeName {
    37  		t.Fatalf("Expected %s, got %s", typeName, typ.String())
    38  	}
    39  
    40  	if !typ.AssignableTo(reflect.TypeOf(value)) {
    41  		t.Fatalf("%s not assignable to %s", typ.String(), reflect.TypeOf(value).String())
    42  	}
    43  }