1
2
3
4
5 package types2_test
6
7 import (
8 "internal/testenv"
9 "testing"
10
11 . "cmd/compile/internal/types2"
12 )
13
14 const filename = "<src>"
15
16 type testEntry struct {
17 src, str string
18 }
19
20
21 func dup(s string) testEntry {
22 return testEntry{s, s}
23 }
24
25
26 var independentTestTypes = []testEntry{
27
28 dup("int"),
29 dup("float32"),
30 dup("string"),
31
32
33 dup("[10]int"),
34
35
36 dup("[]int"),
37 dup("[][]int"),
38
39
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
52 dup("*int"),
53 dup("***struct{}"),
54 dup("*struct{a int; b float32}"),
55
56
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
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 {"comparable", "interface{comparable}"},
88 {"error", "interface{Error() string}"},
89
90
91 dup("map[string]int"),
92 {"map[struct{x, y int}][]byte", "map[struct{x int; y int}][]byte"},
93
94
95 dup("chan<- chan int"),
96 dup("chan<- <-chan int"),
97 dup("<-chan <-chan int"),
98 dup("chan (<-chan int)"),
99 dup("chan<- func()"),
100 dup("<-chan []func() int"),
101 }
102
103
104 var dependentTestTypes = []testEntry{
105
106 dup(`interface{io.Reader; io.Writer}`),
107 dup(`interface{m() int; io.Writer}`),
108 {`interface{m() interface{T}}`, `interface{m() interface{generic_p.T}}`},
109 }
110
111 func TestTypeString(t *testing.T) {
112
113 testenv.MustHaveGoBuild(t)
114
115 var tests []testEntry
116 tests = append(tests, independentTestTypes...)
117 tests = append(tests, dependentTestTypes...)
118
119 for _, test := range tests {
120 src := `package generic_p; import "io"; type _ io.Writer; type T ` + test.src
121 pkg, err := typecheck(src, nil, nil)
122 if err != nil {
123 t.Errorf("%s: %s", src, err)
124 continue
125 }
126 obj := pkg.Scope().Lookup("T")
127 if obj == nil {
128 t.Errorf("%s: T not found", test.src)
129 continue
130 }
131 typ := obj.Type().Underlying()
132 if got := typ.String(); got != test.str {
133 t.Errorf("%s: got %s, want %s", test.src, got, test.str)
134 }
135 }
136 }
137
138 func TestQualifiedTypeString(t *testing.T) {
139 p := mustTypecheck("package p; type T int", nil, nil)
140 q := mustTypecheck("package q", nil, nil)
141
142 pT := p.Scope().Lookup("T").Type()
143 for _, test := range []struct {
144 typ Type
145 this *Package
146 want string
147 }{
148 {nil, nil, "<nil>"},
149 {pT, nil, "p.T"},
150 {pT, p, "T"},
151 {pT, q, "p.T"},
152 {NewPointer(pT), p, "*T"},
153 {NewPointer(pT), q, "*p.T"},
154 } {
155 qualifier := func(pkg *Package) string {
156 if pkg != test.this {
157 return pkg.Name()
158 }
159 return ""
160 }
161 if got := TypeString(test.typ, qualifier); got != test.want {
162 t.Errorf("TypeString(%s, %s) = %s, want %s",
163 test.this, test.typ, got, test.want)
164 }
165 }
166 }
167
View as plain text