Debian Codegraph @

gopls/internal/golang/freesymbols_test.go

raw symbols SCIP
1 // Copyright 2024 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 "fmt"
9 "go/ast"
10 "go/importer"
11 "go/parser"
12 "go/token"
13 "go/types"
14 "reflect"
15 "runtime"
16 "strings"
17 "testing"
18
19 "github.com/google/go-cmp/cmp"
20 )
21
22 // TestFreeRefs is a unit test of the free-references algorithm.
23 func TestFreeRefs(t *testing.T) {
24 if runtime.GOOS == "js" || runtime.GOARCH == "wasm" {
25 t.Skip("some test imports are unsupported on js or wasm")
26 }
27
28 for i, test := range []struct {
29 src string
30 want []string // expected list of "scope kind dotted-path" triples
31 }{
32 {
33 // basic example (has a "cannot infer" type error)
34 `package p; func f[T ~int](x any) { var y T; « f(x.(T) + y) » }`,
35 []string{"pkg func f", "local var x", "local typename T", "local var y"},
36 },
37 {
38 // selection need not be tree-aligned
39 `package p; type T int; type U « T; func _(x U) »`,
40 []string{"pkg typename T", "pkg typename U"},
41 },
42 {
43 // imported symbols
44 `package p; import "fmt"; func f() { « var x fmt.Stringer » }`,
45 []string{"file pkgname fmt.Stringer"},
46 },
47 {
48 // unsafe and error, our old nemeses
49 `package p; import "unsafe"; var ( « _ unsafe.Pointer; _ = error(nil).Error »; )`,
50 []string{"file pkgname unsafe.Pointer"},
51 },
52 {
53 // two attributes of a var, but not the var itself
54 `package p; import "bytes"; func _(buf bytes.Buffer) { « buf.WriteByte(0); buf.WriteString(""); » }`,
55 []string{"local var buf.WriteByte", "local var buf.WriteString"},
56 },
57 {
58 // dot imports (an edge case)
59 `package p; import . "errors"; var _ = « New»`,
60 []string{"file pkgname errors.New"},
61 },
62 {
63 // struct field (regression test for overzealous dot import logic)
64 `package p; import "net/url"; var _ = «url.URL{Host: ""}»`,
65 []string{"file pkgname url.URL"},
66 },
67 {
68 // dot imports (another regression test of same)
69 `package p; import . "net/url"; var _ = «URL{Host: ""}»`,
70 []string{"file pkgname url.URL"},
71 },
72 {
73 // dot import of unsafe (a corner case)
74 `package p; import . "unsafe"; var _ « Pointer»`,
75 []string{"file pkgname unsafe.Pointer"},
76 },
77 {
78 // dotted path
79 `package p; import "go/build"; var _ = « build.Default.GOOS »`,
80 []string{"file pkgname build.Default.GOOS"},
81 },
82 {
83 // type error
84 `package p; import "nope"; var _ = « nope.nope.nope »`,
85 []string{"file pkgname nope"},
86 },
87 } {
88 name := fmt.Sprintf("file%d.go", i)
89 t.Run(name, func(t *testing.T) {
90 fset := token.NewFileSet()
91 startOffset := strings.Index(test.src, "«")
92 endOffset := strings.Index(test.src, "»")
93 if startOffset < 0 || endOffset < startOffset {
94 t.Fatalf("invalid «...» selection (%d:%d)", startOffset, endOffset)
95 }
96 src := test.src[:startOffset] +
97 " " +
98 test.src[startOffset+len("«"):endOffset] +
99 " " +
100 test.src[endOffset+len("»"):]
101 f, err := parser.ParseFile(fset, name, src, parser.SkipObjectResolution)
102 if err != nil {
103 t.Fatal(err)
104 }
105 conf := &types.Config{
106 Importer: importer.Default(),
107 Error: func(err error) { t.Log(err) }, // not fatal
108 }
109 info := &types.Info{
110 Uses: make(map[*ast.Ident]types.Object),
111 Scopes: make(map[ast.Node]*types.Scope),
112 Types: make(map[ast.Expr]types.TypeAndValue),
113 }
114 pkg, _ := conf.Check(f.Name.Name, fset, []*ast.File{f}, info) // ignore errors
115 tf := fset.File(f.Package)
116 refs := freeRefs(pkg, info, f, tf.Pos(startOffset), tf.Pos(endOffset))
117
118 kind := func(obj types.Object) string { // e.g. "var", "const"
119 return strings.ToLower(reflect.TypeOf(obj).Elem().Name())
120 }
121
122 var got []string
123 for _, ref := range refs {
124 msg := ref.scope + " " + kind(ref.objects[0]) + " " + ref.dotted
125 got = append(got, msg)
126 }
127 if diff := cmp.Diff(test.want, got); diff != "" {
128 t.Errorf("(-want +got)\n%s", diff)
129 }
130 })
131 }
132 }