Source file
src/go/types/object_test.go
1
2
3
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
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
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},
38 {NewTypeName(0, pkg, "t0", nil), false},
39 {t1, false},
40 {NewTypeName(0, nil, "t2", &emptyInterface), true},
41 {NewTypeName(0, pkg, "t3", n1), true},
42 {NewTypeName(0, nil, "t4", Typ[Int32]), true},
43 {NewTypeName(0, nil, "int32", Typ[Int32]), false},
44 {NewTypeName(0, pkg, "int32", Typ[Int32]), true},
45 {NewTypeName(0, nil, "rune", Typ[Rune]), true},
46 } {
47 check(test.name, test.alias)
48 }
49 }
50
51
52
53 func TestEmbeddedMethod(t *testing.T) {
54 const src = `package p; type I interface { error }`
55
56
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
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
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
83 if orig != embed {
84 t.Fatalf("%s (%p) != %s (%p)", orig, orig, embed, embed)
85 }
86 }
87
View as plain text