github.com/zebozhuang/go@v0.0.0-20200207033046-f8a98f6f5c5d/src/go/types/object_test.go (about)

     1  // Copyright 2016 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package types
     6  
     7  import "testing"
     8  
     9  func TestIsAlias(t *testing.T) {
    10  	check := func(obj *TypeName, want bool) {
    11  		if got := obj.IsAlias(); got != want {
    12  			t.Errorf("%v: got IsAlias = %v; want %v", obj, got, want)
    13  		}
    14  	}
    15  
    16  	// predeclared types
    17  	check(Unsafe.Scope().Lookup("Pointer").(*TypeName), false)
    18  	for _, name := range Universe.Names() {
    19  		if obj, _ := Universe.Lookup(name).(*TypeName); obj != nil {
    20  			check(obj, name == "byte" || name == "rune")
    21  		}
    22  	}
    23  
    24  	// various other types
    25  	pkg := NewPackage("p", "p")
    26  	t1 := NewTypeName(0, pkg, "t1", nil)
    27  	n1 := NewNamed(t1, new(Struct), nil)
    28  	for _, test := range []struct {
    29  		name  *TypeName
    30  		alias bool
    31  	}{
    32  		{NewTypeName(0, nil, "t0", nil), false}, // no type yet
    33  		{NewTypeName(0, pkg, "t0", nil), false}, // no type yet
    34  		{t1, false},                             // type name refers to named type and vice versa
    35  		{NewTypeName(0, nil, "t2", new(Interface)), true}, // type name refers to unnamed type
    36  		{NewTypeName(0, pkg, "t3", n1), true},             // type name refers to named type with different type name
    37  		{NewTypeName(0, nil, "t4", Typ[Int32]), true},     // type name refers to basic type with different name
    38  		{NewTypeName(0, nil, "int32", Typ[Int32]), false}, // type name refers to basic type with same name
    39  		{NewTypeName(0, pkg, "int32", Typ[Int32]), true},  // type name is declared in user-defined package (outside Universe)
    40  		{NewTypeName(0, nil, "rune", Typ[Rune]), true},    // type name refers to basic type rune which is an alias already
    41  	} {
    42  		check(test.name, test.alias)
    43  	}
    44  }