Black Lives Matter. Support the Equal Justice Initiative.

Source file src/go/types/object_test.go

Documentation: go/types

     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 (
     8  	"go/ast"
     9  	"go/parser"
    10  	"go/token"
    11  	"testing"
    12  )
    13  
    14  func TestIsAlias(t *testing.T) {
    15  	check := func(obj *TypeName, want bool) {
    16  		if got := obj.IsAlias(); got != want {
    17  			t.Errorf("%v: got IsAlias = %v; want %v", obj, got, want)
    18  		}
    19  	}
    20  
    21  	// predeclared types
    22  	check(Unsafe.Scope().Lookup("Pointer").(*TypeName), false)
    23  	for _, name := range Universe.Names() {
    24  		if obj, _ := Universe.Lookup(name).(*TypeName); obj != nil {
    25  			check(obj, name == "byte" || name == "rune")
    26  		}
    27  	}
    28  
    29  	// various other types
    30  	pkg := NewPackage("p", "p")
    31  	t1 := NewTypeName(0, pkg, "t1", nil)
    32  	n1 := NewNamed(t1, new(Struct), nil)
    33  	for _, test := range []struct {
    34  		name  *TypeName
    35  		alias bool
    36  	}{
    37  		{NewTypeName(0, nil, "t0", nil), false},            // no type yet
    38  		{NewTypeName(0, pkg, "t0", nil), false},            // no type yet
    39  		{t1, false},                                        // type name refers to named type and vice versa
    40  		{NewTypeName(0, nil, "t2", &emptyInterface), true}, // type name refers to unnamed type
    41  		{NewTypeName(0, pkg, "t3", n1), true},              // type name refers to named type with different type name
    42  		{NewTypeName(0, nil, "t4", Typ[Int32]), true},      // type name refers to basic type with different name
    43  		{NewTypeName(0, nil, "int32", Typ[Int32]), false},  // type name refers to basic type with same name
    44  		{NewTypeName(0, pkg, "int32", Typ[Int32]), true},   // type name is declared in user-defined package (outside Universe)
    45  		{NewTypeName(0, nil, "rune", Typ[Rune]), true},     // type name refers to basic type rune which is an alias already
    46  	} {
    47  		check(test.name, test.alias)
    48  	}
    49  }
    50  
    51  // TestEmbeddedMethod checks that an embedded method is represented by
    52  // the same Func Object as the original method. See also issue #34421.
    53  func TestEmbeddedMethod(t *testing.T) {
    54  	const src = `package p; type I interface { error }`
    55  
    56  	// type-check src
    57  	fset := token.NewFileSet()
    58  	f, err := parser.ParseFile(fset, "", src, 0)
    59  	if err != nil {
    60  		t.Fatalf("parse failed: %s", err)
    61  	}
    62  	var conf Config
    63  	pkg, err := conf.Check(f.Name.Name, fset, []*ast.File{f}, nil)
    64  	if err != nil {
    65  		t.Fatalf("typecheck failed: %s", err)
    66  	}
    67  
    68  	// get original error.Error method
    69  	eface := Universe.Lookup("error")
    70  	orig, _, _ := LookupFieldOrMethod(eface.Type(), false, nil, "Error")
    71  	if orig == nil {
    72  		t.Fatalf("original error.Error not found")
    73  	}
    74  
    75  	// get embedded error.Error method
    76  	iface := pkg.Scope().Lookup("I")
    77  	embed, _, _ := LookupFieldOrMethod(iface.Type(), false, nil, "Error")
    78  	if embed == nil {
    79  		t.Fatalf("embedded error.Error not found")
    80  	}
    81  
    82  	// original and embedded Error object should be identical
    83  	if orig != embed {
    84  		t.Fatalf("%s (%p) != %s (%p)", orig, orig, embed, embed)
    85  	}
    86  }
    87  

View as plain text