Debian Codegraph @

generate/type_string_test.go

raw symbols SCIP
1 // Copyright 2015 Aaron Jacobs. All Rights Reserved.
2 // Author: [email protected] (Aaron Jacobs)
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15
16 package generate
17
18 import (
19 "io"
20 "reflect"
21 "testing"
22 "unsafe"
23
24 "github.com/jacobsa/oglemock/createmock/testdata/gcs"
25 . "github.com/jacobsa/ogletest"
26 )
27
28 func TestTypeString(t *testing.T) { RunTests(t) }
29
30 ////////////////////////////////////////////////////////////////////////
31 // Boilerplate
32 ////////////////////////////////////////////////////////////////////////
33
34 type TypeStringTest struct {
35 }
36
37 func init() { RegisterTestSuite(&TypeStringTest{}) }
38
39 ////////////////////////////////////////////////////////////////////////
40 // Test functions
41 ////////////////////////////////////////////////////////////////////////
42
43 func (t *TypeStringTest) TestCases() {
44 const gcsPkgPath = "github.com/jacobsa/oglemock/createmock/testdata/gcs"
45 to := reflect.TypeOf
46
47 testCases := []struct {
48 t reflect.Type
49 pkgPath string
50 expected string
51 }{
52 /////////////////////////
53 // Scalar types
54 /////////////////////////
55
56 0: {to(true), "", "bool"},
57 1: {to(true), "some/pkg", "bool"},
58 2: {to(int(17)), "some/pkg", "int"},
59 3: {to(int32(17)), "some/pkg", "int32"},
60 4: {to(uint(17)), "some/pkg", "uint"},
61 5: {to(uint32(17)), "some/pkg", "uint32"},
62 6: {to(uintptr(17)), "some/pkg", "uintptr"},
63 7: {to(float32(17)), "some/pkg", "float32"},
64 8: {to(complex64(17)), "some/pkg", "complex64"},
65
66 /////////////////////////
67 // Structs
68 /////////////////////////
69
70 9: {to(gcs.Object{}), "some/pkg", "gcs.Object"},
71 10: {to(gcs.Object{}), gcsPkgPath, "Object"},
72
73 11: {
74 to(struct {
75 a int
76 b gcs.Object
77 }{}),
78 "some/pkg",
79 "struct { a int; b gcs.Object }",
80 },
81
82 12: {
83 to(struct {
84 a int
85 b gcs.Object
86 }{}),
87 gcsPkgPath,
88 "struct { a int; b Object }",
89 },
90
91 /////////////////////////
92 // Pointers
93 /////////////////////////
94
95 13: {to((*int)(nil)), gcsPkgPath, "*int"},
96 14: {to((*gcs.Object)(nil)), "some/pkg", "*gcs.Object"},
97 15: {to((*gcs.Object)(nil)), gcsPkgPath, "*Object"},
98
99 /////////////////////////
100 // Arrays
101 /////////////////////////
102
103 16: {to([3]int{}), "some/pkg", "[3]int"},
104 17: {to([3]gcs.Object{}), gcsPkgPath, "[3]Object"},
105
106 /////////////////////////
107 // Channels
108 /////////////////////////
109
110 18: {to((chan int)(nil)), "some/pkg", "chan int"},
111 19: {to((<-chan int)(nil)), "some/pkg", "<-chan int"},
112 20: {to((chan<- int)(nil)), "some/pkg", "chan<- int"},
113 21: {to((<-chan gcs.Object)(nil)), gcsPkgPath, "<-chan Object"},
114
115 /////////////////////////
116 // Functions
117 /////////////////////////
118
119 22: {
120 to(func(int, gcs.Object) {}),
121 gcsPkgPath,
122 "func(int, Object) ()",
123 },
124
125 23: {
126 to(func() (*gcs.Object, error) { return nil, nil }),
127 gcsPkgPath,
128 "func() (*Object, error)",
129 },
130
131 24: {
132 to(func(int, gcs.Object) (*gcs.Object, error) { return nil, nil }),
133 gcsPkgPath,
134 "func(int, Object) (*Object, error)",
135 },
136
137 /////////////////////////
138 // Interfaces
139 /////////////////////////
140
141 25: {to((*error)(nil)).Elem(), "some/pkg", "error"},
142 26: {to((*io.Reader)(nil)).Elem(), "some/pkg", "io.Reader"},
143 27: {to((*io.Reader)(nil)).Elem(), "io", "Reader"},
144
145 28: {
146 to((*interface{})(nil)).Elem(),
147 "some/pkg",
148 "interface { }",
149 },
150
151 29: {
152 to((*interface {
153 Foo(int)
154 Bar(gcs.Object)
155 })(nil)).Elem(),
156 "some/pkg",
157 "interface { Bar(gcs.Object) (); Foo(int) () }",
158 },
159
160 30: {
161 to((*interface {
162 Foo(int)
163 Bar(gcs.Object)
164 })(nil)).Elem(),
165 gcsPkgPath,
166 "interface { Bar(Object) (); Foo(int) () }",
167 },
168
169 /////////////////////////
170 // Maps
171 /////////////////////////
172
173 31: {to(map[*gcs.Object]gcs.Object{}), gcsPkgPath, "map[*Object]Object"},
174
175 /////////////////////////
176 // Slices
177 /////////////////////////
178
179 32: {to([]int{}), "some/pkg", "[]int"},
180 33: {to([]gcs.Object{}), gcsPkgPath, "[]Object"},
181
182 /////////////////////////
183 // Strings
184 /////////////////////////
185
186 34: {to(""), gcsPkgPath, "string"},
187
188 /////////////////////////
189 // Unsafe pointer
190 /////////////////////////
191
192 35: {to(unsafe.Pointer(nil)), gcsPkgPath, "unsafe.Pointer"},
193
194 /////////////////////////
195 // Other named types
196 /////////////////////////
197
198 36: {to(gcs.Int(17)), "some/pkg", "gcs.Int"},
199 37: {to(gcs.Int(17)), gcsPkgPath, "Int"},
200
201 38: {to(gcs.Array{}), "some/pkg", "gcs.Array"},
202 39: {to(gcs.Array{}), gcsPkgPath, "Array"},
203
204 40: {to(gcs.Chan(nil)), "some/pkg", "gcs.Chan"},
205 41: {to(gcs.Chan(nil)), gcsPkgPath, "Chan"},
206
207 42: {to(gcs.Ptr(nil)), "some/pkg", "gcs.Ptr"},
208 43: {to(gcs.Ptr(nil)), gcsPkgPath, "Ptr"},
209
210 44: {to((*gcs.Int)(nil)), "some/pkg", "*gcs.Int"},
211 45: {to((*gcs.Int)(nil)), gcsPkgPath, "*Int"},
212 }
213
214 for i, tc := range testCases {
215 ExpectEq(
216 tc.expected,
217 typeString(tc.t, tc.pkgPath),
218 "Case %d: %v, %q", i, tc.t, tc.pkgPath)
219 }
220 }