X-Git-Url: http://www.git.cypherpunks.ru/?a=blobdiff_plain;f=src%2Fgo%2Ftypes%2Fapi_test.go;h=3050b930b5d692c551d6bd881bd311347a656b61;hb=8da6405e0db80fa0a4136fb816c7ca2db716c2b2;hp=0769890101caf02c40303ae66c6d319fa020839f;hpb=3188758653fc7d2b229e234273d41878ddfdd5f2;p=gostls13.git diff --git a/src/go/types/api_test.go b/src/go/types/api_test.go index 0769890101..3050b930b5 100644 --- a/src/go/types/api_test.go +++ b/src/go/types/api_test.go @@ -960,6 +960,81 @@ func TestImplicitsInfo(t *testing.T) { } } +func TestPkgNameOf(t *testing.T) { + testenv.MustHaveGoBuild(t) + + const src = ` +package p + +import ( + . "os" + _ "io" + "math" + "path/filepath" + snort "sort" +) + +// avoid imported and not used errors +var ( + _ = Open // os.Open + _ = math.Sin + _ = filepath.Abs + _ = snort.Ints +) +` + + var tests = []struct { + path string // path string enclosed in "'s + want string + }{ + {`"os"`, "."}, + {`"io"`, "_"}, + {`"math"`, "math"}, + {`"path/filepath"`, "filepath"}, + {`"sort"`, "snort"}, + } + + fset := token.NewFileSet() + f := mustParse(fset, src) + info := Info{ + Defs: make(map[*ast.Ident]Object), + Implicits: make(map[ast.Node]Object), + } + var conf Config + conf.Importer = importer.Default() + _, err := conf.Check("p", fset, []*ast.File{f}, &info) + if err != nil { + t.Fatal(err) + } + + // map import paths to importDecl + imports := make(map[string]*ast.ImportSpec) + for _, s := range f.Decls[0].(*ast.GenDecl).Specs { + if imp, _ := s.(*ast.ImportSpec); imp != nil { + imports[imp.Path.Value] = imp + } + } + + for _, test := range tests { + imp := imports[test.path] + if imp == nil { + t.Fatalf("invalid test case: import path %s not found", test.path) + } + got := info.PkgNameOf(imp) + if got == nil { + t.Fatalf("import %s: package name not found", test.path) + } + if got.Name() != test.want { + t.Errorf("import %s: got %s; want %s", test.path, got.Name(), test.want) + } + } + + // test non-existing importDecl + if got := info.PkgNameOf(new(ast.ImportSpec)); got != nil { + t.Errorf("got %s for non-existing import declaration", got.Name()) + } +} + func predString(tv TypeAndValue) string { var buf strings.Builder pred := func(b bool, s string) {