1
2
3
4
5 package types2_test
6
7 import (
8 "internal/testenv"
9 "testing"
10
11 "cmd/compile/internal/syntax"
12 . "cmd/compile/internal/types2"
13 )
14
15 const filename = "<src>"
16
17 func makePkg(src string) (*Package, error) {
18 file, err := parseSrc(filename, src)
19 if err != nil {
20 return nil, err
21 }
22
23 conf := Config{Importer: defaultImporter()}
24 return conf.Check(file.PkgName.Value, []*syntax.File{file}, nil)
25 }
26
27 type testEntry struct {
28 src, str string
29 }
30
31
32 func dup(s string) testEntry {
33 return testEntry{s, s}
34 }
35
36
37 var independentTestTypes = []testEntry{
38
39 dup("int"),
40 dup("float32"),
41 dup("string"),
42
43
44 dup("[10]int"),
45
46
47 dup("[]int"),
48 dup("[][]int"),
49
50
51 dup("struct{}"),
52 dup("struct{x int}"),
53 {`struct {
54 x, y int
55 z float32 "foo"
56 }`, `struct{x int; y int; z float32 "foo"}`},
57 {`struct {
58 string
59 elems []complex128
60 }`, `struct{string; elems []complex128}`},
61
62
63 dup("*int"),
64 dup("***struct{}"),
65 dup("*struct{a int; b float32}"),
66
67
68 dup("func()"),
69 dup("func(x int)"),
70 {"func(x, y int)", "func(x int, y int)"},
71 {"func(x, y int, z string)", "func(x int, y int, z string)"},
72 dup("func(int)"),
73 {"func(int, string, byte)", "func(int, string, byte)"},
74
75 dup("func() int"),
76 {"func() (string)", "func() string"},
77 dup("func() (u int)"),
78 {"func() (u, v int, w string)", "func() (u int, v int, w string)"},
79
80 dup("func(int) string"),
81 dup("func(x int) string"),
82 dup("func(x int) (u string)"),
83 {"func(x, y int) (u string)", "func(x int, y int) (u string)"},
84
85 dup("func(...int) string"),
86 dup("func(x ...int) string"),
87 dup("func(x ...int) (u string)"),
88 {"func(x int, y ...int) (u string)", "func(x int, y ...int) (u string)"},
89
90
91 dup("interface{}"),
92 dup("interface{m()}"),
93 dup(`interface{String() string; m(int) float32}`),
94 dup("interface{int|float32|complex128}"),
95 dup("interface{int|~float32|~complex128}"),
96 dup("any"),
97 dup("interface{comparable}"),
98 {"comparable", "interface{comparable}"},
99 {"error", "interface{Error() string}"},
100
101
102 dup("map[string]int"),
103 {"map[struct{x, y int}][]byte", "map[struct{x int; y int}][]byte"},
104
105
106 dup("chan<- chan int"),
107 dup("chan<- <-chan int"),
108 dup("<-chan <-chan int"),
109 dup("chan (<-chan int)"),
110 dup("chan<- func()"),
111 dup("<-chan []func() int"),
112 }
113
114
115 var dependentTestTypes = []testEntry{
116
117 dup(`interface{io.Reader; io.Writer}`),
118 dup(`interface{m() int; io.Writer}`),
119 {`interface{m() interface{T}}`, `interface{m() interface{generic_p.T}}`},
120 }
121
122 func TestTypeString(t *testing.T) {
123 testenv.MustHaveGoBuild(t)
124
125 var tests []testEntry
126 tests = append(tests, independentTestTypes...)
127 tests = append(tests, dependentTestTypes...)
128
129 for _, test := range tests {
130 src := `package generic_p; import "io"; type _ io.Writer; type T ` + test.src
131 pkg, err := makePkg(src)
132 if err != nil {
133 t.Errorf("%s: %s", src, err)
134 continue
135 }
136 obj := pkg.Scope().Lookup("T")
137 if obj == nil {
138 t.Errorf("%s: T not found", test.src)
139 continue
140 }
141 typ := obj.Type().Underlying()
142 if got := typ.String(); got != test.str {
143 t.Errorf("%s: got %s, want %s", test.src, got, test.str)
144 }
145 }
146 }
147
148 func TestQualifiedTypeString(t *testing.T) {
149 p, _ := pkgFor("p.go", "package p; type T int", nil)
150 q, _ := pkgFor("q.go", "package q", nil)
151
152 pT := p.Scope().Lookup("T").Type()
153 for _, test := range []struct {
154 typ Type
155 this *Package
156 want string
157 }{
158 {nil, nil, "<nil>"},
159 {pT, nil, "p.T"},
160 {pT, p, "T"},
161 {pT, q, "p.T"},
162 {NewPointer(pT), p, "*T"},
163 {NewPointer(pT), q, "*p.T"},
164 } {
165 qualifier := func(pkg *Package) string {
166 if pkg != test.this {
167 return pkg.Name()
168 }
169 return ""
170 }
171 if got := TypeString(test.typ, qualifier); got != test.want {
172 t.Errorf("TypeString(%s, %s) = %s, want %s",
173 test.this, test.typ, got, test.want)
174 }
175 }
176 }
177
View as plain text