1 // Copyright 2014 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.
15 "golang.org/x/tools/go/types/typeutil"
18 type closure map[string]*types.Package
20 func (c closure) Import(path string) (*types.Package, error) { return c[path], nil }
22 func TestDependencies(t *testing.T) {
23 packages := make(map[string]*types.Package)
25 Importer: closure(packages),
27 fset := token.NewFileSet()
29 // All edges go to the right.
33 for i, content := range []string{
35 `package c; import (_ "a")`,
36 `package b; import (_ "a")`,
37 `package e; import (_ "c")`,
38 `package d; import (_ "b"; _ "c")`,
39 `package f; import (_ "d"; _ "e")`,
41 f, err := parser.ParseFile(fset, fmt.Sprintf("%d.go", i), content, 0)
45 pkg, err := conf.Check(f.Name.Name, fset, []*ast.File{f}, nil)
49 packages[pkg.Path()] = pkg
52 for _, test := range []struct {
68 var pkgs []*types.Package
69 for _, r := range test.roots {
70 pkgs = append(pkgs, packages[string(r)])
73 for _, p := range typeutil.Dependencies(pkgs...) {
77 t.Errorf("Dependencies(%q) = %q, want %q", test.roots, got, test.want)