Debian Codegraph @

v2/parser/parse_test.go

raw
1 /*
2 Copyright 2015 The Kubernetes Authors.
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
17 package parser
18
19 import (
20 "encoding/json"
21 "fmt"
22 "path/filepath"
23 "reflect"
24 "sort"
25 "testing"
26
27 "github.com/google/go-cmp/cmp"
28 "github.com/google/go-cmp/cmp/cmpopts"
29 "golang.org/x/tools/go/packages"
30 "k8s.io/gengo/v2/types"
31 )
32
33 func TestNew(t *testing.T) {
34 parser := New()
35 if parser.goPkgs == nil {
36 t.Errorf("expected .goPkgs to be initialized")
37 }
38 if parser.userRequested == nil {
39 t.Errorf("expected .userRequested to be initialized")
40 }
41 if parser.fullyProcessed == nil {
42 t.Errorf("expected .fullyProcessed to be initialized")
43 }
44 if parser.fset == nil {
45 t.Errorf("expected .fset to be initialized")
46 }
47 if parser.endLineToCommentGroup == nil {
48 t.Errorf("expected .endLineToCommentGroup to be initialized")
49 }
50 }
51
52 func sorted(in ...string) []string {
53 out := make([]string, len(in))
54 copy(out, in)
55 sort.Strings(out)
56 return out
57 }
58
59 func sliceEq(a, b []string) bool {
60 if len(a) != len(b) {
61 return false
62 }
63 for i := range a {
64 if a[i] != b[i] {
65 return false
66 }
67 }
68 return true
69 }
70
71 func pkgPathsFromSlice(pkgs []*packages.Package) []string {
72 paths := []string{}
73 for _, pkg := range pkgs {
74 paths = append(paths, pkg.PkgPath)
75 }
76 return sorted(paths...)
77 }
78
79 func pkgPathsFromMap(pkgs map[string]*packages.Package) []string {
80 paths := []string{}
81 for _, pkg := range pkgs {
82 paths = append(paths, pkg.PkgPath)
83 }
84 return sorted(paths...)
85 }
86
87 func pkgPathsFromUniverse(u types.Universe) []string {
88 paths := []string{}
89 for path := range u {
90 paths = append(paths, path)
91 }
92 return sorted(paths...)
93 }
94
95 func pretty(in []string) string {
96 size := 0
97 oneline := true
98 for _, s := range in {
99 size += len(s)
100 if size > 60 {
101 oneline = false
102 break
103 }
104 }
105 var jb []byte
106 var err error
107 if oneline {
108 jb, err = json.Marshal(in)
109 } else {
110 jb, err = json.MarshalIndent(in, "", " ")
111 }
112 if err != nil {
113 panic(fmt.Sprintf("JSON marshal failed: %v", err))
114 }
115 return string(jb)
116 }
117
118 func keys[T any](m map[string]T) []string {
119 ret := []string{}
120 for k := range m {
121 ret = append(ret, k)
122 }
123 return sorted(ret...)
124 }
125
126 func TestAddBuildTags(t *testing.T) {
127 testTags := []string{"foo", "bar", "qux"}
128
129 parser := New()
130 if len(parser.buildTags) != 0 {
131 t.Errorf("expected no default build tags, got %v", parser.buildTags)
132 }
133 parser = NewWithOptions(Options{BuildTags: testTags[0:1]})
134 if want, got := testTags[0:1], parser.buildTags; !sliceEq(want, got) {
135 t.Errorf("wrong build tags:\nwant: %v\ngot: %v", pretty(want), pretty(got))
136 }
137 parser = NewWithOptions(Options{BuildTags: testTags})
138 if want, got := testTags, parser.buildTags; !sliceEq(want, got) {
139 t.Errorf("wrong build tags:\nwant: %v\ngot: %v", pretty(want), pretty(got))
140 }
141 }
142
143 func TestFindPackages(t *testing.T) {
144 parser := New()
145
146 // Proper packages with deps.
147 if pkgs, err := parser.FindPackages("./testdata/root1", "./testdata/root2", "./testdata/roots345/..."); err != nil {
148 t.Errorf("unexpected error: %v", err)
149 } else {
150 expected := sorted(
151 "k8s.io/gengo/v2/parser/testdata/root1",
152 "k8s.io/gengo/v2/parser/testdata/root2",
153 "k8s.io/gengo/v2/parser/testdata/roots345/root3",
154 "k8s.io/gengo/v2/parser/testdata/roots345/root3/lib3",
155 "k8s.io/gengo/v2/parser/testdata/roots345/root4",
156 "k8s.io/gengo/v2/parser/testdata/roots345/root4/lib4",
157 "k8s.io/gengo/v2/parser/testdata/roots345/root5",
158 "k8s.io/gengo/v2/parser/testdata/roots345/root5/lib5",
159 )
160 if want, got := expected, sorted(pkgs...); !sliceEq(want, got) {
161 t.Errorf("wrong pkgs:\nwant: %v\ngot: %v", pretty(want), pretty(got))
162 }
163 if len(parser.goPkgs) != 0 {
164 t.Errorf("expected no added .goPkgs, got %v", pretty(pkgPathsFromMap(parser.goPkgs)))
165 }
166 }
167
168 // Non-existent packages should be an error.
169 if pkgs, err := parser.FindPackages("./testdata/does-not-exist"); err == nil {
170 t.Errorf("unexpected success: %v", pkgs)
171 }
172
173 // Packages without .go files should be an error.
174 if pkgs, err := parser.FindPackages("./testdata/has-no-gofiles"); err == nil {
175 t.Errorf("unexpected success: %v", pkgs)
176 }
177
178 // Invalid go files are not an error.
179 if pkgs, err := parser.FindPackages("./testdata/does-not-parse"); err != nil {
180 t.Errorf("unexpected error: %v", err)
181 } else {
182 expected := []string{
183 "k8s.io/gengo/v2/parser/testdata/does-not-parse",
184 }
185 if want, got := expected, pkgs; !sliceEq(want, got) {
186 t.Errorf("wrong pkgs:\nwant: %v\ngot: %v", pretty(want), pretty(got))
187 }
188 }
189 }
190
191 func TestAlreadyLoaded(t *testing.T) {
192 newPkg := func(path string) *packages.Package {
193 return &packages.Package{
194 ID: path,
195 PkgPath: path,
196 Name: filepath.Base(path),
197 }
198 }
199
200 parser := New()
201
202 // Test loading something we don't have.
203 if existing, netNew, err := parser.alreadyLoaded(nil, "./testdata/root1"); err != nil {
204 t.Errorf("unexpected error: %v", err)
205 } else {
206 if len(existing) > 0 {
207 t.Errorf("unexpected existing pkg(s): %v", existing)
208 }
209 if len(netNew) != 1 {
210 t.Errorf("expected 1 net-new, got: %v", netNew)
211 } else if want, got := "k8s.io/gengo/v2/parser/testdata/root1", netNew[0]; want != got {
212 t.Errorf("wrong net-new, want %v, got %v", want, got)
213 }
214 }
215
216 // Test loading something already present.
217 parser.goPkgs["k8s.io/gengo/v2/parser/testdata/root1"] = newPkg("k8s.io/gengo/v2/parser/testdata/root1")
218 if existing, netNew, err := parser.alreadyLoaded(nil, "./testdata/root1"); err != nil {
219 t.Errorf("unexpected error: %v", err)
220 } else {
221 if len(existing) != 1 {
222 t.Errorf("expected 1 existing, got: %v", existing)
223 } else if want, got := "k8s.io/gengo/v2/parser/testdata/root1", existing[0].PkgPath; want != got {
224 t.Errorf("wrong existing, want %v, got %v", want, got)
225 }
226 if len(netNew) > 0 {
227 t.Errorf("unexpected net-new pkg(s): %v", netNew)
228 }
229 }
230
231 // Test loading something partly present.
232 if existing, netNew, err := parser.alreadyLoaded(nil, "./testdata/root1/..."); err != nil {
233 t.Errorf("unexpected error: %v", err)
234 } else {
235 if len(existing) != 1 {
236 t.Errorf("expected 1 existing, got: %v", existing)
237 } else if want, got := "k8s.io/gengo/v2/parser/testdata/root1", existing[0].PkgPath; want != got {
238 t.Errorf("wrong existing, want %v, got %v", want, got)
239 }
240 if len(netNew) != 1 {
241 t.Errorf("expected 1 net-new, got: %v", netNew)
242 } else if want, got := "k8s.io/gengo/v2/parser/testdata/root1/lib1", netNew[0]; want != got {
243 t.Errorf("wrong net-new, want %v, got %v", want, got)
244 }
245 }
246 }
247
248 func TestLoadPackagesInternal(t *testing.T) {
249 parser := New()
250
251 // Proper packages with deps.
252 if pkgs, err := parser.loadPackages("./testdata/root1", "./testdata/root2", "./testdata/roots345/..."); err != nil {
253 t.Errorf("unexpected error: %v", err)
254 } else {
255 expectedDirect := sorted(
256 "k8s.io/gengo/v2/parser/testdata/root1",
257 "k8s.io/gengo/v2/parser/testdata/root2",
258 "k8s.io/gengo/v2/parser/testdata/roots345/root3",
259 "k8s.io/gengo/v2/parser/testdata/roots345/root3/lib3",
260 "k8s.io/gengo/v2/parser/testdata/roots345/root4",
261 "k8s.io/gengo/v2/parser/testdata/roots345/root4/lib4",
262 "k8s.io/gengo/v2/parser/testdata/roots345/root5",
263 "k8s.io/gengo/v2/parser/testdata/roots345/root5/lib5",
264 )
265 expectedIndirect := sorted(
266 "k8s.io/gengo/v2/parser/testdata/root1/lib1",
267 "k8s.io/gengo/v2/parser/testdata/root2/lib2",
268 "k8s.io/gengo/v2/parser/testdata/rootpeer",
269 "k8s.io/gengo/v2/parser/testdata/rootpeer/sub1",
270 "k8s.io/gengo/v2/parser/testdata/rootpeer/sub2",
271 )
272 expectedAll := sorted(append(expectedDirect, expectedIndirect...)...)
273
274 if want, got := expectedDirect, pkgPathsFromSlice(pkgs); !sliceEq(want, got) {
275 t.Errorf("wrong pkgs returned:\nwant: %v\ngot: %v", pretty(want), pretty(got))
276 }
277 if want, got := expectedAll, pkgPathsFromMap(parser.goPkgs); !sliceEq(want, got) {
278 t.Errorf("wrong pkgs in .goPkgs:\nwant: %v\ngot: %v", pretty(want), pretty(got))
279 }
280
281 for _, path := range expectedDirect {
282 if !parser.userRequested[path] {
283 t.Errorf("expected .userRequested[%q] to be set", path)
284 }
285 if parser.fullyProcessed[path] {
286 t.Errorf("expected .fullyProcessed[%q] to be unset", path)
287 }
288 }
289 for _, path := range expectedIndirect {
290 if parser.userRequested[path] {
291 t.Errorf("expected .userRequested[%q] to be unset", path)
292 }
293 if parser.fullyProcessed[path] {
294 t.Errorf("expected .fullyProcessed[%q] to be unset", path)
295 }
296 }
297
298 // There is a comment is at this fixed location.
299 pos := fileLine{parser.goPkgs["k8s.io/gengo/v2/parser/testdata/root1"].GoFiles[0], 9}
300 if parser.endLineToCommentGroup[pos] == nil {
301 t.Errorf("expected a comment-group ending at %v", pos)
302 t.Errorf("%v", parser.endLineToCommentGroup)
303 }
304 }
305
306 // Non-existent packages should be an error.
307 if pkgs, err := parser.loadPackages("./testdata/does-not-exist"); err == nil {
308 t.Errorf("unexpected success: %v", pkgs)
309 }
310
311 // Packages without .go files should be an error.
312 if pkgs, err := parser.loadPackages("./testdata/has-no-gofiles"); err == nil {
313 t.Errorf("unexpected success: %v", pkgs)
314 }
315
316 // Invalid go files are an error.
317 if pkgs, err := parser.loadPackages("./testdata/does-not-parse"); err == nil {
318 t.Errorf("unexpected success: %v", pkgs)
319 }
320
321 // Packages which parse but do not compile are NOT an error.
322 if pkgs, err := parser.loadPackages("./testdata/does-not-compile"); err != nil {
323 t.Errorf("unexpected error: %v", err)
324 } else {
325 expected := []string{
326 "k8s.io/gengo/v2/parser/testdata/does-not-compile",
327 }
328 if want, got := expected, pkgPathsFromSlice(pkgs); !sliceEq(want, got) {
329 t.Errorf("wrong pkgs:\nwant: %v\ngot: %v", pretty(want), pretty(got))
330 }
331 if parser.goPkgs[expected[0]] == nil {
332 t.Errorf("package not found in .goPkgs: %v", expected[0])
333 }
334 }
335
336 // Packages with only test files are not an error.
337 if pkgs, err := parser.loadPackages("./testdata/only-test-files"); err != nil {
338 t.Errorf("unexpected error: %v", err)
339 } else if len(pkgs[0].GoFiles) > 0 {
340 t.Errorf("expected 0 GoFiles, got %q", pkgs[0].GoFiles)
341 }
342 }
343
344 func TestLoadPackagesTo(t *testing.T) {
345 parser := New()
346 u := types.Universe{}
347
348 // Proper packages with deps.
349 if pkgs, err := parser.LoadPackagesTo(&u, "./testdata/root1", "./testdata/root2", "./testdata/roots345/..."); err != nil {
350 t.Errorf("unexpected error: %v", err)
351 } else {
352 expectedDirect := sorted(
353 "k8s.io/gengo/v2/parser/testdata/root1",
354 "k8s.io/gengo/v2/parser/testdata/root2",
355 "k8s.io/gengo/v2/parser/testdata/roots345/root3",
356 "k8s.io/gengo/v2/parser/testdata/roots345/root3/lib3",
357 "k8s.io/gengo/v2/parser/testdata/roots345/root4",
358 "k8s.io/gengo/v2/parser/testdata/roots345/root4/lib4",
359 "k8s.io/gengo/v2/parser/testdata/roots345/root5",
360 "k8s.io/gengo/v2/parser/testdata/roots345/root5/lib5",
361 )
362 expectedIndirect := sorted(
363 "k8s.io/gengo/v2/parser/testdata/root1/lib1",
364 "k8s.io/gengo/v2/parser/testdata/root2/lib2",
365 "k8s.io/gengo/v2/parser/testdata/rootpeer",
366 "k8s.io/gengo/v2/parser/testdata/rootpeer/sub1",
367 "k8s.io/gengo/v2/parser/testdata/rootpeer/sub2",
368 )
369 expectedAll := expectedDirect
370 expectedAll = append(expectedAll, expectedIndirect...)
371 expectedAll = sorted(append(expectedAll, "")...) // This is the "builtin" pkg
372
373 if want, got := len(expectedDirect), len(pkgs); want != got {
374 t.Errorf("wrong number of pkgs returned: want: %d got: %d", want, got)
375 }
376 if want, got := expectedAll, pkgPathsFromUniverse(u); !sliceEq(want, got) {
377 t.Errorf("wrong pkgs in universe:\nwant: %v\ngot: %v", pretty(want), pretty(got))
378 }
379 }
380 }
381
382 func TestForEachPackageRecursive(t *testing.T) {
383 newPkg := func(path string, imports ...*packages.Package) *packages.Package {
384 pkg := &packages.Package{
385 ID: path,
386 PkgPath: path,
387 Name: filepath.Base(path),
388 Imports: map[string]*packages.Package{},
389 }
390 for _, imp := range imports {
391 pkg.Imports[imp.PkgPath] = imp
392 }
393 return pkg
394 }
395
396 pkgs := []*packages.Package{
397 newPkg("example.com/root1",
398 newPkg("example.com/dep1", newPkg("example.com/dep11"), newPkg("example.com/dep12")),
399 newPkg("example.com/dep2", newPkg("example.com/dep21")),
400 ),
401 newPkg("example.com/root2",
402 newPkg("example.com/dep3"),
403 ),
404 newPkg("example.com/root3"),
405 }
406
407 // Test success.
408 expect := sorted(
409 "example.com/root1",
410 "example.com/dep1",
411 "example.com/dep11",
412 "example.com/dep12",
413 "example.com/dep2",
414 "example.com/dep21",
415 "example.com/root2",
416 "example.com/dep3",
417 "example.com/root3",
418 )
419 visited := []string{}
420 visit := func(pkg *packages.Package) error {
421 visited = append(visited, pkg.PkgPath)
422 return nil
423 }
424 if err := forEachPackageRecursive(pkgs, visit); err != nil {
425 t.Errorf("unexpected error: %v", err)
426 } else if want, got := expect, sorted(visited...); !sliceEq(want, got) {
427 t.Errorf("want: %v\ngot: %v", pretty(want), pretty(got))
428 }
429
430 // Test errors.
431 fail := func(pkg *packages.Package) error {
432 return fmt.Errorf("%s", pkg.PkgPath)
433 }
434 if err := forEachPackageRecursive(pkgs, fail); err == nil {
435 t.Errorf("unexpected success")
436 } else if wrapped, ok := err.(interface{ Unwrap() []error }); !ok {
437 t.Errorf("expected unwrappable error, got %v", err)
438 } else {
439 visited := []string{}
440 for _, err := range wrapped.Unwrap() {
441 visited = append(visited, err.Error())
442 }
443 if want, got := expect, sorted(visited...); !sliceEq(want, got) {
444 t.Errorf("wrong errors:\nwant: %v\ngot: %v", pretty(want), pretty(got))
445 }
446 }
447 }
448
449 func TestUserRequestedPackages(t *testing.T) {
450 parser := New()
451
452 // Proper packages with deps.
453 if err := parser.LoadPackages("./testdata/root1", "./testdata/root2", "./testdata/roots345/..."); err != nil {
454 t.Errorf("unexpected error: %v", err)
455 } else {
456 want := sorted(
457 "k8s.io/gengo/v2/parser/testdata/root1",
458 "k8s.io/gengo/v2/parser/testdata/root2",
459 "k8s.io/gengo/v2/parser/testdata/roots345/root3",
460 "k8s.io/gengo/v2/parser/testdata/roots345/root3/lib3",
461 "k8s.io/gengo/v2/parser/testdata/roots345/root4",
462 "k8s.io/gengo/v2/parser/testdata/roots345/root4/lib4",
463 "k8s.io/gengo/v2/parser/testdata/roots345/root5",
464 "k8s.io/gengo/v2/parser/testdata/roots345/root5/lib5",
465 )
466 got := parser.UserRequestedPackages() // should be sorted!
467
468 if !sliceEq(want, got) {
469 t.Errorf("wrong pkgs returned:\nwant: %v\ngot: %v", pretty(want), pretty(got))
470 }
471 }
472 }
473
474 func TestAddOnePkgToUniverse(t *testing.T) {
475 parser := New()
476
477 // Proper packages with deps.
478 if pkgs, err := parser.loadPackages("./testdata/root2"); err != nil {
479 t.Errorf("unexpected error: %v", err)
480 } else {
481 direct := "k8s.io/gengo/v2/parser/testdata/root2"
482 indirect := "k8s.io/gengo/v2/parser/testdata/root2/lib2"
483
484 if want, got := []string{direct}, pkgPathsFromSlice(pkgs); !sliceEq(want, got) {
485 t.Errorf("wrong pkgs returned:\nwant: %v\ngot: %v", pretty(want), pretty(got))
486 }
487
488 u := types.Universe{}
489 if err := parser.addPkgToUniverse(pkgs[0], &u); err != nil {
490 t.Errorf("unexpected error: %v", err)
491 }
492
493 // TODO: This is not an exhaustive test. There are lots of types
494 // and combinations of things that are not covered.
495
496 // verify the depth of processing
497 if !parser.fullyProcessed[direct] {
498 t.Errorf("expected .fullyProcessed[%q] to be set", direct)
499 }
500 if parser.fullyProcessed[indirect] {
501 t.Errorf("expected .fullyProcessed[%q] to be unset", indirect)
502 }
503
504 // verify their existence
505 pd := parser.goPkgs[direct]
506 if pd == nil {
507 t.Fatalf("expected non-nil from .goPkgs")
508 }
509 pi := parser.goPkgs[indirect]
510 if pi == nil {
511 t.Fatalf("expected non-nil from .goPkgs")
512 }
513 ud := u[direct]
514 if ud == nil {
515 t.Fatalf("expected non-nil from universe")
516 }
517 ui := u[indirect]
518 if ui == nil {
519 t.Fatalf("expected non-nil from universe")
520 }
521
522 // verify metadata
523 if want, got := pd.PkgPath, ud.Path; want != got {
524 t.Errorf("expected .Path %q, got %q", want, got)
525 }
526 if want, got := filepath.Dir(pd.GoFiles[0]), ud.Dir; want != got {
527 t.Errorf("expected .Dir %q, got %q", want, got)
528 }
529 if want, got := pi.PkgPath, ui.Path; want != got {
530 t.Errorf("expected .Path %q, got %q", want, got)
531 }
532 if want, got := filepath.Dir(pi.GoFiles[0]), ui.Dir; want != got {
533 t.Errorf("expected .Dir %q, got %q", want, got)
534 }
535
536 // verify doc.go handling
537 if len(ud.DocComments) != 2 { // Fixed value from the testdata
538 t.Errorf("expected 2 doc-comment lines, got: %v", pretty(ud.DocComments))
539 }
540 if len(ud.Comments) != 6 { // Fixed value from the testdata
541 t.Errorf("expected 3 comments, 2 lines each, got: %v", pretty(ud.Comments))
542 }
543
544 // verify types
545 if len(ui.Types) != 0 {
546 t.Errorf("expected zero types in indirect package, got %d", len(ui.Types))
547 }
548 if len(ud.Types) == 0 {
549 t.Errorf("expected non-zero types in direct package")
550 } else {
551 type testcase struct {
552 kind types.Kind
553 elem string // just the type name
554 key string // just the type name
555 underlying *testcase
556 }
557 cases := map[string]testcase{
558 "Int": {
559 kind: types.Alias,
560 underlying: &testcase{
561 kind: types.Builtin,
562 },
563 },
564 "String": {
565 kind: types.Alias,
566 underlying: &testcase{
567 kind: types.Builtin,
568 },
569 },
570 "EmptyStruct": {
571 kind: types.Struct,
572 },
573 "Struct": {
574 kind: types.Struct,
575 },
576 "M": {
577 kind: types.Alias,
578 underlying: &testcase{
579 kind: types.Map,
580 elem: "*k8s.io/gengo/v2/parser/testdata/root2.Struct",
581 key: types.String.String(),
582 },
583 },
584 }
585
586 want := keys(cases)
587 got := keys(ud.Types)
588
589 if !sliceEq(want, got) {
590 t.Errorf("wrong types found:\nwant: %v\ngot: %v", pretty(want), pretty(got))
591 } else {
592 for name, obj := range ud.Types {
593 n := types.Name{Package: ud.Path, Name: name}
594 if obj.Name != n {
595 t.Errorf("wrong name for type %s: %v", name, obj.Name)
596 }
597 comment1 := fmt.Sprintf("%s comment", name)
598 if want, got := []string{comment1}, obj.CommentLines; !sliceEq(want, got) {
599 t.Errorf("wrong comments for type %s:\nwant: %v\ngot: %v", name, want, got)
600 }
601 comment2 := fmt.Sprintf("SecondClosest %s comment", name)
602 if want, got := []string{comment2}, obj.SecondClosestCommentLines; !sliceEq(want, got) {
603 t.Errorf("wrong comments for type %s:\nwant: %v\ngot: %v", name, want, got)
604 }
605 }
606
607 // Declare, then define because it is recursive.
608 var vrfy func(name string, tc *testcase, obj *types.Type)
609 vrfy = func(name string, tc *testcase, obj *types.Type) {
610 if want, got := tc.kind, obj.Kind; want != got {
611 t.Errorf("wrong .Kind for type %s:\nwant: %v, got: %v", name, want, got)
612 } else if obj.Kind == types.Alias {
613 vrfy(name+"^", tc.underlying, obj.Underlying)
614 }
615 if want, got := tc.elem, obj.Elem.String(); want != got {
616 t.Errorf("wrong .Elem for type %s:\nwant: %v, got: %v", name, want, got)
617 }
618 if want, got := tc.key, obj.Key.String(); want != got {
619 t.Errorf("wrong .Key for type %s:\nwant: %v, got: %v", name, want, got)
620 }
621 // TODO: Members, Methods, Len
622 }
623 for name, tc := range cases {
624 obj := ud.Types[name]
625 vrfy(name, &tc, obj)
626 }
627 }
628 }
629
630 // verify functions
631 if len(ui.Functions) != 0 {
632 t.Errorf("expected zero functions in indirect package, got %d", len(ui.Functions))
633 }
634 if len(ud.Functions) == 0 {
635 t.Errorf("expected non-zero functions in direct package")
636 } else {
637 type testcase struct {
638 kind types.Kind
639 }
640 cases := map[string]testcase{
641 "PublicFunc": {
642 kind: types.DeclarationOf,
643 },
644 "privateFunc": {
645 kind: types.DeclarationOf,
646 },
647 }
648
649 want := keys(cases)
650 got := keys(ud.Functions)
651
652 if !sliceEq(want, got) {
653 t.Errorf("wrong functions found:\nwant: %v\ngot: %v", pretty(want), pretty(got))
654 } else {
655 for name, obj := range ud.Functions {
656 n := types.Name{Package: ud.Path, Name: name}
657 if obj.Name != n {
658 t.Errorf("wrong name for function %s: %v", name, obj.Name)
659 }
660 comment1 := fmt.Sprintf("%s comment", name)
661 if want, got := []string{comment1}, obj.CommentLines; !sliceEq(want, got) {
662 t.Errorf("wrong comments for function %s:\nwant: %v\ngot: %v", name, want, got)
663 }
664 comment2 := fmt.Sprintf("SecondClosest %s comment", name)
665 if want, got := []string{comment2}, obj.SecondClosestCommentLines; !sliceEq(want, got) {
666 t.Errorf("wrong comments for function %s:\nwant: %v\ngot: %v", name, want, got)
667 }
668 }
669
670 for name, tc := range cases {
671 obj := ud.Functions[name]
672 if want, got := tc.kind, obj.Kind; want != got {
673 t.Errorf("wrong .Kind for function %s:\nwant: %v, got: %v", name, want, got)
674 }
675 // TODO: Signature
676 }
677 }
678 }
679
680 // verify variables
681 if len(ui.Variables) != 0 {
682 t.Errorf("expected zero variables in indirect package, got %d", len(ui.Variables))
683 }
684 if len(ud.Variables) == 0 {
685 t.Errorf("expected non-zero variables in direct package")
686 } else {
687 type testcase struct {
688 kind types.Kind
689 }
690 cases := map[string]testcase{
691 "X": {
692 kind: types.DeclarationOf,
693 },
694 }
695
696 want := keys(cases)
697 got := keys(ud.Variables)
698
699 if !sliceEq(want, got) {
700 t.Errorf("wrong variables found:\nwant: %v\ngot: %v", pretty(want), pretty(got))
701 } else {
702 for name, obj := range ud.Variables {
703 n := types.Name{Package: ud.Path, Name: name}
704 if obj.Name != n {
705 t.Errorf("wrong name for variable %s: %v", name, obj.Name)
706 }
707 comment1 := fmt.Sprintf("%s comment", name)
708 if want, got := []string{comment1}, obj.CommentLines; !sliceEq(want, got) {
709 t.Errorf("wrong comments for variable %s:\nwant: %v\ngot: %v", name, want, got)
710 }
711 comment2 := fmt.Sprintf("SecondClosest %s comment", name)
712 if want, got := []string{comment2}, obj.SecondClosestCommentLines; !sliceEq(want, got) {
713 t.Errorf("wrong comments for variable %s:\nwant: %v\ngot: %v", name, want, got)
714 }
715 }
716
717 for name, tc := range cases {
718 obj := ud.Variables[name]
719 if want, got := tc.kind, obj.Kind; want != got {
720 t.Errorf("wrong .Kind for variable %s:\nwant: %v, got: %v", name, want, got)
721 }
722 // TODO: Underlying
723 }
724 }
725 }
726
727 // verify constants
728 if len(ui.Constants) != 0 {
729 t.Errorf("expected zero constants in indirect package, got %d", len(ui.Constants))
730 }
731 if len(ud.Constants) == 0 {
732 t.Errorf("expected non-zero constants in direct package")
733 } else {
734 type testcase struct {
735 kind types.Kind
736 }
737 cases := map[string]testcase{
738 "Y": {
739 kind: types.DeclarationOf,
740 },
741 }
742
743 want := keys(cases)
744 got := keys(ud.Constants)
745
746 if !sliceEq(want, got) {
747 t.Errorf("wrong constants found:\nwant: %v\ngot: %v", pretty(want), pretty(got))
748 } else {
749 for name, obj := range ud.Constants {
750 n := types.Name{Package: ud.Path, Name: name}
751 if obj.Name != n {
752 t.Errorf("wrong name for constant %s: %v", name, obj.Name)
753 }
754 comment1 := fmt.Sprintf("%s comment", name)
755 if want, got := []string{comment1}, obj.CommentLines; !sliceEq(want, got) {
756 t.Errorf("wrong comments for constant %s:\nwant: %v\ngot: %v", name, want, got)
757 }
758 comment2 := fmt.Sprintf("SecondClosest %s comment", name)
759 if want, got := []string{comment2}, obj.SecondClosestCommentLines; !sliceEq(want, got) {
760 t.Errorf("wrong comments for constant %s:\nwant: %v\ngot: %v", name, want, got)
761 }
762 }
763
764 for name, tc := range cases {
765 obj := ud.Constants[name]
766 if want, got := tc.kind, obj.Kind; want != got {
767 t.Errorf("wrong .Kind for constant %s:\nwant: %v, got: %v", name, want, got)
768 }
769 // TODO: Underlying
770 }
771 }
772 }
773
774 // verify imports
775 if len(ui.Imports) != 0 {
776 t.Errorf("expected zero imports in indirect package, got %d", len(ui.Imports))
777 }
778 if len(ud.Imports) == 0 {
779 t.Errorf("expected non-zero imports in direct package")
780 } else {
781 want := sorted(
782 "k8s.io/gengo/v2/parser/testdata/root2/lib2",
783 "k8s.io/gengo/v2/parser/testdata/rootpeer",
784 )
785 got := keys(ud.Imports)
786
787 if !sliceEq(want, got) {
788 t.Errorf("wrong imports found:\nwant: %v\ngot: %v", pretty(want), pretty(got))
789 }
790 }
791
792 // verify types in the "" pkg
793 if builtins := u[""]; len(builtins.Types) == 0 {
794 t.Errorf("expected non-zero types in the \"\" package")
795 } else {
796 // NOTE: this captures how the code behaved at the time of this
797 // test's creation, but does not speak to why it does this or
798 // whether it is correct or optimal.
799 want := sorted(
800 "int",
801 "*int",
802 "*string",
803 "string",
804 "untyped string",
805 "func()",
806 "*k8s.io/gengo/v2/parser/testdata/root2.Int",
807 "*k8s.io/gengo/v2/parser/testdata/root2.String",
808 "*k8s.io/gengo/v2/parser/testdata/root2.EmptyStruct",
809 "*k8s.io/gengo/v2/parser/testdata/root2.Struct",
810 "func (k8s.io/gengo/v2/parser/testdata/root2.Struct).PublicMethod()",
811 "func (k8s.io/gengo/v2/parser/testdata/root2.Struct).privateMethod()",
812 "map[string]*k8s.io/gengo/v2/parser/testdata/root2.Struct",
813 )
814 got := keys(builtins.Types)
815
816 if !sliceEq(want, got) {
817 t.Errorf("wrong types found:\nwant: %v\ngot: %v", pretty(want), pretty(got))
818 }
819 }
820 }
821 }
822
823 func TestStructParse(t *testing.T) {
824 testCases := []struct {
825 description string
826 testFile string
827 expected func() *types.Type
828 }{
829 {
830 description: "basic comments",
831 testFile: "k8s.io/gengo/v2/parser/testdata/basic",
832 expected: func() *types.Type {
833 return &types.Type{
834 Name: types.Name{
835 Package: "k8s.io/gengo/v2/parser/testdata/basic",
836 Name: "Blah",
837 },
838 Kind: types.Struct,
839 CommentLines: []string{"Blah is a test.", "A test, I tell you."},
840 SecondClosestCommentLines: []string{""},
841 Members: []types.Member{
842 {
843 Name: "A",
844 Embedded: false,
845 CommentLines: []string{"A is the first field."},
846 Tags: `json:"a"`,
847 Type: types.Int64,
848 },
849 {
850 Name: "B",
851 Embedded: false,
852 CommentLines: []string{"B is the second field.", "Multiline comments work."},
853 Tags: `json:"b"`,
854 Type: types.String,
855 },
856 },
857 TypeParams: map[string]*types.Type{},
858 }
859 },
860 },
861 {
862 description: "generic",
863 testFile: "./testdata/generic",
864 expected: func() *types.Type {
865 return &types.Type{
866 Name: types.Name{
867 Package: "k8s.io/gengo/v2/parser/testdata/generic",
868 Name: "Blah[T]",
869 },
870 Kind: types.Struct,
871 CommentLines: []string{""},
872 SecondClosestCommentLines: []string{""},
873 Members: []types.Member{
874 {
875 Name: "V",
876 Embedded: false,
877 CommentLines: []string{"V is the first field."},
878 Tags: `json:"v"`,
879 Type: &types.Type{
880 Kind: types.TypeParam,
881 Name: types.Name{
882 Name: "T",
883 },
884 },
885 },
886 },
887 TypeParams: map[string]*types.Type{
888 "T": {
889 Name: types.Name{
890 Name: "any",
891 },
892 Kind: types.Interface,
893 },
894 },
895 }
896 },
897 },
898 {
899 description: "generic on field",
900 testFile: "./testdata/generic-field",
901 expected: func() *types.Type {
902 fieldType := &types.Type{
903 Name: types.Name{
904 Package: "k8s.io/gengo/v2/parser/testdata/generic-field",
905 Name: "Blah[T]",
906 },
907 Kind: types.Struct,
908 CommentLines: []string{""},
909 SecondClosestCommentLines: []string{""},
910 Members: []types.Member{
911 {
912 Name: "V",
913 Embedded: false,
914 CommentLines: []string{"V is the first field."},
915 Tags: `json:"v"`,
916 Type: &types.Type{
917 Kind: types.TypeParam,
918 Name: types.Name{
919 Name: "T",
920 },
921 },
922 },
923 },
924 TypeParams: map[string]*types.Type{
925 "T": {
926 Name: types.Name{
927 Name: "any",
928 },
929 Kind: types.Interface,
930 },
931 },
932 }
933 return &types.Type{
934 Name: types.Name{
935 Package: "k8s.io/gengo/v2/parser/testdata/generic-field",
936 Name: "Foo",
937 },
938 Kind: types.Struct,
939 CommentLines: []string{""},
940 SecondClosestCommentLines: []string{""},
941 Members: []types.Member{
942 {
943 Name: "B",
944 Embedded: false,
945 CommentLines: []string{""},
946 Tags: `json:"b"`,
947 Type: fieldType,
948 },
949 },
950 TypeParams: map[string]*types.Type{},
951 }
952 },
953 },
954 {
955 description: "generic multiple",
956 testFile: "./testdata/generic-multi",
957 expected: func() *types.Type {
958 return &types.Type{
959 Name: types.Name{
960 Package: "k8s.io/gengo/v2/parser/testdata/generic-multi",
961 Name: "Blah[T,U,V]",
962 },
963 Kind: types.Struct,
964 CommentLines: []string{""},
965 SecondClosestCommentLines: []string{""},
966 Members: []types.Member{
967 {
968 Name: "V1",
969 Embedded: false,
970 CommentLines: []string{"V1 is the first field."},
971 Tags: `json:"v1"`,
972 Type: &types.Type{
973 Kind: types.TypeParam,
974 Name: types.Name{
975 Name: "T",
976 },
977 },
978 },
979 {
980 Name: "V2",
981 Embedded: false,
982 CommentLines: []string{"V2 is the second field."},
983 Tags: `json:"v2"`,
984 Type: &types.Type{
985 Kind: types.TypeParam,
986 Name: types.Name{
987 Name: "U",
988 },
989 },
990 },
991 {
992 Name: "V3",
993 Embedded: false,
994 CommentLines: []string{"V3 is the third field."},
995 Tags: `json:"v3"`,
996 Type: &types.Type{
997 Kind: types.TypeParam,
998 Name: types.Name{
999 Name: "V",
1000 },
1001 },
1002 },
1003 },
1004 TypeParams: map[string]*types.Type{
1005 "T": {
1006 Name: types.Name{
1007 Name: "any",
1008 },
1009 Kind: types.Interface,
1010 },
1011 "U": {
1012 Name: types.Name{
1013 Name: "any",
1014 },
1015 Kind: types.Interface,
1016 },
1017 "V": {
1018 Name: types.Name{
1019 Name: "any",
1020 },
1021 Kind: types.Interface,
1022 },
1023 },
1024 }
1025 },
1026 },
1027 {
1028 description: "generic recursive",
1029 testFile: "./testdata/generic-recursive",
1030 expected: func() *types.Type {
1031 recursiveT := &types.Type{
1032 Name: types.Name{
1033 Package: "k8s.io/gengo/v2/parser/testdata/generic-recursive",
1034 Name: "DeepCopyable[T]",
1035 },
1036 Kind: types.Interface,
1037 CommentLines: []string{""},
1038 SecondClosestCommentLines: []string{""},
1039 Methods: map[string]*types.Type{},
1040 TypeParams: map[string]*types.Type{
1041 "T": {
1042 Name: types.Name{
1043 Name: "any",
1044 },
1045 Kind: types.Interface,
1046 },
1047 },
1048 }
1049 recursiveT.Methods["DeepCopy"] = &types.Type{
1050 Name: types.Name{
1051 Name: "func (k8s.io/gengo/v2/parser/testdata/generic-recursive.DeepCopyable[T]).DeepCopy() T",
1052 },
1053 Kind: types.Func,
1054 CommentLines: []string{""},
1055 Signature: &types.Signature{
1056 Receiver: recursiveT,
1057 Results: []*types.ParamResult{
1058 {
1059 Name: "",
1060 Type: &types.Type{
1061 Name: types.Name{
1062 Name: "T",
1063 },
1064 Kind: types.TypeParam,
1065 },
1066 },
1067 },
1068 },
1069 }
1070 return &types.Type{
1071 Name: types.Name{
1072 Package: "k8s.io/gengo/v2/parser/testdata/generic-recursive",
1073 Name: "Blah[T]",
1074 },
1075 Kind: types.Struct,
1076 CommentLines: []string{""},
1077 SecondClosestCommentLines: []string{""},
1078 Members: []types.Member{
1079 {
1080 Name: "V",
1081 Embedded: false,
1082 CommentLines: []string{"V is the first field."},
1083 Tags: `json:"v"`,
1084 Type: &types.Type{
1085 Name: types.Name{
1086 Name: "T",
1087 },
1088 Kind: types.TypeParam,
1089 },
1090 },
1091 },
1092 TypeParams: map[string]*types.Type{
1093 "T": recursiveT,
1094 },
1095 }
1096 },
1097 },
1098 }
1099
1100 for _, tc := range testCases {
1101 t.Run(tc.description, func(t *testing.T) {
1102 parser := New()
1103
1104 pkgs, err := parser.loadPackages(tc.testFile)
1105 if err != nil {
1106 t.Errorf("unexpected error: %v", err)
1107 }
1108 u := types.Universe{}
1109 if err := parser.addPkgToUniverse(pkgs[0], &u); err != nil {
1110 t.Errorf("unexpected error: %v", err)
1111 }
1112
1113 expected := tc.expected()
1114 pkg, ok := u[expected.Name.Package]
1115 if !ok {
1116 t.Fatalf("package %s not found", expected.Name.Package)
1117 }
1118 st := pkg.Type(expected.Name.Name)
1119 if st == nil || st.Kind == types.Unknown {
1120 t.Fatalf("type %s not found", expected.Name.Name)
1121 }
1122 if st.GoType == nil {
1123 t.Errorf("type %s did not have GoType", expected.Name.Name)
1124 }
1125 opts := []cmp.Option{
1126 cmpopts.IgnoreTypes(types.Type{}, "GoType"),
1127 }
1128 if e, a := expected, st; !cmp.Equal(e, a, opts...) {
1129 t.Errorf("wanted, got:\n%#v\n%#v\n%s", e, a, cmp.Diff(e, a, opts...))
1130 }
1131 })
1132 }
1133 }
1134
1135 func TestGoNameToName(t *testing.T) {
1136 testCases := []struct {
1137 input string
1138 expect types.Name
1139 }{
1140 {input: "foo", expect: types.Name{Name: "foo"}},
1141 {input: "foo.bar", expect: types.Name{Package: "foo", Name: "bar"}},
1142 {input: "foo.bar.baz", expect: types.Name{Package: "foo.bar", Name: "baz"}},
1143 {input: "Foo[T]", expect: types.Name{Package: "", Name: "Foo[T]"}},
1144 {input: "Foo[T any]", expect: types.Name{Package: "", Name: "Foo[T any]"}},
1145 {input: "pkg.Foo[T]", expect: types.Name{Package: "pkg", Name: "Foo[T]"}},
1146 {input: "pkg.Foo[T any]", expect: types.Name{Package: "pkg", Name: "Foo[T any]"}},
1147 }
1148 for _, tc := range testCases {
1149 t.Run(tc.input, func(t *testing.T) {
1150 if got, want := goNameToName(tc.input), tc.expect; !reflect.DeepEqual(got, want) {
1151 t.Errorf("\nwant: %#v\ngot: %#v", want, got)
1152 }
1153 })
1154 }
1155 }