Debian Codegraph @

gopls/internal/golang/identifier_test.go

raw symbols SCIP
1 // Copyright 2020 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.
4
5 package golang
6
7 import (
8 "bytes"
9 "go/ast"
10 "go/parser"
11 "go/token"
12 "go/types"
13 "testing"
14
15 "golang.org/x/tools/go/ast/inspector"
16 )
17
18 func TestSearchForEnclosing(t *testing.T) {
19 tests := []struct {
20 desc string
21 // For convenience, consider the first occurrence of the identifier "X" in
22 // src.
23 src string
24 // By convention, "" means no type found.
25 wantTypeName string
26 }{
27 {
28 // TODO(rFindley): is this correct, or do we want to resolve I2 here?
29 desc: "embedded interface in interface",
30 src: `package a; var y = i1.X; type i1 interface {I2}; type I2 interface{X()}`,
31 wantTypeName: "",
32 },
33 {
34 desc: "embedded interface in struct",
35 src: `package a; var y = t.X; type t struct {I}; type I interface{X()}`,
36 wantTypeName: "I",
37 },
38 {
39 desc: "double embedding",
40 src: `package a; var y = t1.X; type t1 struct {t2}; type t2 struct {I}; type I interface{X()}`,
41 wantTypeName: "I",
42 },
43 }
44
45 for _, test := range tests {
46 t.Run(test.desc, func(t *testing.T) {
47 fset := token.NewFileSet()
48 file, err := parser.ParseFile(fset, "a.go", test.src, parser.AllErrors|parser.SkipObjectResolution)
49 if err != nil {
50 t.Fatal(err)
51 }
52 var (
53 files = []*ast.File{file}
54 column = 1 + bytes.IndexRune([]byte(test.src), 'X')
55 pos = posAt(1, column, fset, "a.go")
56 )
57 cur, ok := inspector.New(files).Root().FindByPos(pos, pos)
58 if !ok {
59 t.Fatalf("no ident found at (1, %d)", column)
60 }
61 info := newInfo()
62 if _, err = (*types.Config)(nil).Check("p", fset, files, info); err != nil {
63 t.Fatal(err)
64 }
65 obj := searchForEnclosing(info, cur)
66 if obj == nil {
67 if test.wantTypeName != "" {
68 t.Errorf("searchForEnclosing(...) = <nil>, want %q", test.wantTypeName)
69 }
70 return
71 }
72 if got := obj.Name(); got != test.wantTypeName {
73 t.Errorf("searchForEnclosing(...) = %q, want %q", got, test.wantTypeName)
74 }
75 })
76 }
77 }
78
79 // posAt returns the token.Pos corresponding to the 1-based (line, column)
80 // coordinates in the file fname of fset.
81 func posAt(line, column int, fset *token.FileSet, fname string) token.Pos {
82 var tok *token.File
83 fset.Iterate(func(tf *token.File) bool {
84 if tf.Name() == fname {
85 tok = tf
86 return false
87 }
88 return true
89 })
90 if tok == nil {
91 return token.NoPos
92 }
93 start := tok.LineStart(line)
94 return start + token.Pos(column-1)
95 }
96
97 // newInfo returns a types.Info with all maps populated.
98 func newInfo() *types.Info {
99 info := &types.Info{
100 Types: make(map[ast.Expr]types.TypeAndValue),
101 Defs: make(map[*ast.Ident]types.Object),
102 Uses: make(map[*ast.Ident]types.Object),
103 Implicits: make(map[ast.Node]types.Object),
104 Selections: make(map[*ast.SelectorExpr]*types.Selection),
105 Scopes: make(map[ast.Node]*types.Scope),
106 FileVersions: make(map[*ast.File]string),
107 }
108 return info
109 }