| 1 |
// Copyright 2012 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_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"internal/testenv" |
| 9 |
"testing" |
| 10 |
|
| 11 |
. "go/types" |
| 12 |
) |
| 13 |
|
| 14 |
const filename = "<src>" |
| 15 |
|
| 16 |
type testEntry struct { |
| 17 |
src, str string |
| 18 |
} |
| 19 |
|
| 20 |
// dup returns a testEntry where both src and str are the same. |
| 21 |
func dup(s string) testEntry { |
| 22 |
return testEntry{s, s} |
| 23 |
} |
| 24 |
|
| 25 |
// types that don't depend on any other type declarations |
| 26 |
var independentTestTypes = []testEntry{ |
| 27 |
// basic types |
| 28 |
dup("int"), |
| 29 |
dup("float32"), |
| 30 |
dup("string"), |
| 31 |
|
| 32 |
// arrays |
| 33 |
dup("[10]int"), |
| 34 |
|
| 35 |
// slices |
| 36 |
dup("[]int"), |
| 37 |
dup("[][]int"), |
| 38 |
|
| 39 |
// structs |
| 40 |
dup("struct{}"), |
| 41 |
dup("struct{x int}"), |
| 42 |
{`struct { |
| 43 |
x, y int |
| 44 |
z float32 "foo" |
| 45 |
}`, `struct{x int; y int; z float32 "foo"}`}, |
| 46 |
{`struct { |
| 47 |
string |
| 48 |
elems []complex128 |
| 49 |
}`, `struct{string; elems []complex128}`}, |
| 50 |
|
| 51 |
// pointers |
| 52 |
dup("*int"), |
| 53 |
dup("***struct{}"), |
| 54 |
dup("*struct{a int; b float32}"), |
| 55 |
|
| 56 |
// functions |
| 57 |
dup("func()"), |
| 58 |
dup("func(x int)"), |
| 59 |
{"func(x, y int)", "func(x int, y int)"}, |
| 60 |
{"func(x, y int, z string)", "func(x int, y int, z string)"}, |
| 61 |
dup("func(int)"), |
| 62 |
{"func(int, string, byte)", "func(int, string, byte)"}, |
| 63 |
|
| 64 |
dup("func() int"), |
| 65 |
{"func() (string)", "func() string"}, |
| 66 |
dup("func() (u int)"), |
| 67 |
{"func() (u, v int, w string)", "func() (u int, v int, w string)"}, |
| 68 |
|
| 69 |
dup("func(int) string"), |
| 70 |
dup("func(x int) string"), |
| 71 |
dup("func(x int) (u string)"), |
| 72 |
{"func(x, y int) (u string)", "func(x int, y int) (u string)"}, |
| 73 |
|
| 74 |
dup("func(...int) string"), |
| 75 |
dup("func(x ...int) string"), |
| 76 |
dup("func(x ...int) (u string)"), |
| 77 |
{"func(x int, y ...int) (u string)", "func(x int, y ...int) (u string)"}, |
| 78 |
|
| 79 |
// interfaces |
| 80 |
dup("interface{}"), |
| 81 |
dup("interface{m()}"), |
| 82 |
dup(`interface{String() string; m(int) float32}`), |
| 83 |
dup("interface{int | float32 | complex128}"), |
| 84 |
dup("interface{int | ~float32 | ~complex128}"), |
| 85 |
dup("any"), |
| 86 |
dup("interface{comparable}"), |
| 87 |
// TODO(gri) adjust test for EvalCompositeTest |
| 88 |
// {"comparable", "interface{comparable}"}, |
| 89 |
// {"error", "interface{Error() string}"}, |
| 90 |
|
| 91 |
// maps |
| 92 |
dup("map[string]int"), |
| 93 |
{"map[struct{x, y int}][]byte", "map[struct{x int; y int}][]byte"}, |
| 94 |
|
| 95 |
// channels |
| 96 |
dup("chan<- chan int"), |
| 97 |
dup("chan<- <-chan int"), |
| 98 |
dup("<-chan <-chan int"), |
| 99 |
dup("chan (<-chan int)"), |
| 100 |
dup("chan<- func()"), |
| 101 |
dup("<-chan []func() int"), |
| 102 |
} |
| 103 |
|
| 104 |
// types that depend on other type declarations (src in TestTypes) |
| 105 |
var dependentTestTypes = []testEntry{ |
| 106 |
// interfaces |
| 107 |
dup(`interface{io.Reader; io.Writer}`), |
| 108 |
dup(`interface{m() int; io.Writer}`), |
| 109 |
{`interface{m() interface{T}}`, `interface{m() interface{p.T}}`}, |
| 110 |
} |
| 111 |
|
| 112 |
func TestTypeString(t *testing.T) { |
| 113 |
// The Go command is needed for the importer to determine the locations of stdlib .a files. |
| 114 |
testenv.MustHaveGoBuild(t) |
| 115 |
|
| 116 |
var tests []testEntry |
| 117 |
tests = append(tests, independentTestTypes...) |
| 118 |
tests = append(tests, dependentTestTypes...) |
| 119 |
|
| 120 |
for _, test := range tests { |
| 121 |
src := `package p; import "io"; type _ io.Writer; type T ` + test.src |
| 122 |
pkg, err := typecheck(src, nil, nil) |
| 123 |
if err != nil { |
| 124 |
t.Errorf("%s: %s", src, err) |
| 125 |
continue |
| 126 |
} |
| 127 |
obj := pkg.Scope().Lookup("T") |
| 128 |
if obj == nil { |
| 129 |
t.Errorf("%s: T not found", test.src) |
| 130 |
continue |
| 131 |
} |
| 132 |
typ := obj.Type().Underlying() |
| 133 |
if got := typ.String(); got != test.str { |
| 134 |
t.Errorf("%s: got %s, want %s", test.src, got, test.str) |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
func TestQualifiedTypeString(t *testing.T) { |
| 140 |
p := mustTypecheck("package p; type T int", nil, nil) |
| 141 |
q := mustTypecheck("package q", nil, nil) |
| 142 |
|
| 143 |
pT := p.Scope().Lookup("T").Type() |
| 144 |
for _, test := range []struct { |
| 145 |
typ Type |
| 146 |
this *Package |
| 147 |
want string |
| 148 |
}{ |
| 149 |
{nil, nil, "<nil>"}, |
| 150 |
{pT, nil, "p.T"}, |
| 151 |
{pT, p, "T"}, |
| 152 |
{pT, q, "p.T"}, |
| 153 |
{NewPointer(pT), p, "*T"}, |
| 154 |
{NewPointer(pT), q, "*p.T"}, |
| 155 |
} { |
| 156 |
qualifier := func(pkg *Package) string { |
| 157 |
if pkg != test.this { |
| 158 |
return pkg.Name() |
| 159 |
} |
| 160 |
return "" |
| 161 |
} |
| 162 |
if got := TypeString(test.typ, qualifier); got != test.want { |
| 163 |
t.Errorf("TypeString(%s, %s) = %s, want %s", |
| 164 |
test.this, test.typ, got, test.want) |
| 165 |
} |
| 166 |
} |
| 167 |
} |