Debian Codegraph @

gopls/internal/test/integration/completion/completion18_test.go

raw symbols SCIP
1 // Copyright 2021 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 completion
6
7 // This file is misnamed; it has no version constraints.
8 // TODO(adonovan): fold into completion_test.go
9
10 import (
11 "testing"
12
13 "golang.org/x/tools/gopls/internal/protocol"
14 . "golang.org/x/tools/gopls/internal/test/integration"
15 "golang.org/x/tools/internal/testenv"
16 )
17
18 // test generic receivers
19 func TestGenericReceiver(t *testing.T) {
20 const files = `
21 -- go.mod --
22 module mod.com
23
24 go 1.18
25 -- main.go --
26 package main
27 type SyncMap[K any, V comparable] struct {}
28 func (s *SyncMap[K,V]) f() {}
29 type XX[T any] struct {}
30 type UU[T any] struct {}
31 func (s SyncMap[XX,string]) g(v UU) {}
32 `
33
34 tests := []struct {
35 pat string
36 want []string
37 }{
38 {"s .Syn", []string{"SyncMap[K, V]"}},
39 {"Map.X", []string{}}, // This is probably wrong, Maybe "XX"?
40 {"v U", []string{"UU", "uint", "uint16", "uint32", "uint64", "uint8", "uintptr"}}, // not U[T]
41 }
42 Run(t, files, func(t *testing.T, env *Env) {
43 env.OpenFile("main.go")
44 env.Await(env.DoneWithOpen())
45 for _, tst := range tests {
46 loc := env.RegexpSearch("main.go", tst.pat)
47 loc.Range.Start.Character += uint32(protocol.UTF16Len([]byte(tst.pat)))
48 completions := env.Completion(loc)
49 result := compareCompletionLabels(tst.want, completions.Items)
50 if result != "" {
51 t.Errorf("%s: wanted %v", result, tst.want)
52 for i, g := range completions.Items {
53 t.Errorf("got %d %s %s", i, g.Label, g.Detail)
54 }
55 }
56 }
57 })
58 }
59
60 func TestFuzzFunc(t *testing.T) {
61 // The behavior under test is derived from the std module,
62 // not the x/tools/internal/stdlib linked into gopls.
63 testenv.NeedsGoCommand1Point(t, 25) // go1.25 added TBF.Attr
64
65 // use the example from the package documentation
66 modfile := `
67 -- go.mod --
68 module mod.com
69
70 go 1.25
71 `
72 part0 := `package foo
73 import "testing"
74 func FuzzNone(f *testing.F) {
75 f.Add(12) // better not find this f.Add
76 }
77 func FuzzHex(f *testing.F) {
78 for _, seed := range [][]byte{{}, {0}, {9}, {0xa}, {0xf}, {1, 2, 3, 4}} {
79 f.Ad`
80 part1 := `d(seed)
81 }
82 f.F`
83 part2 := `uzz(func(t *testing.T, in []byte) {
84 enc := hex.EncodeToString(in)
85 out, err := hex.DecodeString(enc)
86 if err != nil {
87 f.Failed()
88 }
89 if !bytes.Equal(in, out) {
90 t.Fatalf("%v: round trip: %v, %s", in, out, f.Name())
91 }
92 })
93 }
94 `
95 data := modfile + `-- a_test.go --
96 ` + part0 + `
97 -- b_test.go --
98 ` + part0 + part1 + `
99 -- c_test.go --
100 ` + part0 + part1 + part2
101
102 tests := []struct {
103 file string
104 pat string
105 want []string
106 }{
107 // To avoid breaking these assertions as the "testing" package evolves,
108 // use an optional (?) suffix for newer symbols.
109 {"a_test.go", "f.A()d", []string{"Add", "ArtifactDir?", "Attr"}}, // Attr is 1.25, Artifact is 1.26
110 {"c_test.go", " f.F()", []string{"Failed"}},
111 {"c_test.go", "f.N()", []string{"Name"}},
112 {"b_test.go", "f.F()", []string{"Fuzz(func(t *testing.T, a []byte)", "Fail", "FailNow",
113 "Failed", "Fatal", "Fatalf"}},
114 }
115 Run(t, data, func(t *testing.T, env *Env) {
116 for _, test := range tests {
117 env.OpenFile(test.file)
118 env.Await(env.DoneWithOpen())
119 loc := env.RegexpSearch(test.file, test.pat)
120 completions := env.Completion(loc)
121 result := compareCompletionLabels(test.want, completions.Items)
122 if result != "" {
123 t.Errorf("pat=%q <<%s>>", test.pat, result)
124 for i, it := range completions.Items {
125 t.Errorf("%d got %q %q", i, it.Label, it.Detail)
126 }
127 }
128 }
129 })
130 }