Debian Codegraph @

gopls/internal/test/integration/misc/addtest_test.go

raw symbols SCIP
1 // Copyright 2025 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 misc
6
7 import (
8 "testing"
9
10 "golang.org/x/tools/gopls/internal/protocol"
11 "golang.org/x/tools/gopls/internal/settings"
12 "golang.org/x/tools/gopls/internal/test/compare"
13 . "golang.org/x/tools/gopls/internal/test/integration"
14 )
15
16 // TestAddTest is a basic test of interaction with the "gopls.add_test" code action.
17 func TestAddTest(t *testing.T) {
18 const files = `
19 -- go.mod --
20 module example.com
21
22 go 1.24
23
24 -- a/a.go --
25 package a
26
27 import(
28 "context"
29 )
30
31 func Foo(ctx context.Context, in string) string {return in}
32
33 -- a/a_test.go --
34 package a_test
35
36 import(
37 "testing"
38 )
39
40 func TestExisting(t *testing.T) {}
41 `
42 const want = `package a_test
43
44 import (
45 "testing"
46
47 "example.com/a"
48 )
49
50 func TestExisting(t *testing.T) {}
51
52 func TestFoo(t *testing.T) {
53 tests := []struct {
54 name string // description of this test case
55 // Named input parameters for target function.
56 in string
57 want string
58 }{
59 // TODO: Add test cases.
60 }
61 for _, tt := range tests {
62 t.Run(tt.name, func(t *testing.T) {
63 got := a.Foo(t.Context(), tt.in)
64 // TODO: update the condition below to compare got with tt.want.
65 if true {
66 t.Errorf("Foo() = %v, want %v", got, tt.want)
67 }
68 })
69 }
70 }
71 `
72 Run(t, files, func(t *testing.T, env *Env) {
73 env.OpenFile("a/a.go")
74
75 loc := env.RegexpSearch("a/a.go", "Foo")
76 actions, err := env.Editor.CodeAction(env.Ctx, loc, nil, protocol.CodeActionUnknownTrigger)
77 if err != nil {
78 t.Fatalf("CodeAction: %v", err)
79 }
80 action, err := CodeActionByKind(actions, settings.AddTest)
81 if err != nil {
82 t.Fatal(err)
83 }
84 action, err = env.Editor.ResolveCodeAction(env.Ctx, action)
85 if err != nil {
86 t.Fatal(err)
87 }
88
89 // Execute the command.
90 // Its side effect should be a single showDocument request.
91 params := &protocol.ExecuteCommandParams{
92 Command: action.Command.Command,
93 Arguments: action.Command.Arguments,
94 }
95
96 listen := env.Awaiter.ListenToShownDocuments()
97 env.ExecuteCommand(params, nil)
98 // Wait until we finish writing to the file.
99 env.AfterChange()
100 if got := env.BufferText("a/a_test.go"); got != want {
101 t.Errorf("gopls.add_test returned unexpected diff (-want +got):\n%s", compare.Text(want, got))
102 }
103
104 got := listen()
105 if len(got) != 1 {
106 t.Errorf("gopls.add_test: got %d showDocument requests, want 1: %v", len(got), got)
107 } else {
108 if want := protocol.URI(env.Sandbox.Workdir.URI("a/a_test.go")); got[0].URI != want {
109 t.Errorf("gopls.add_test: got showDocument requests for %v, want %v", got[0].URI, want)
110 }
111
112 // Pointing to the line of test function declaration.
113 if want := (protocol.Range{
114 Start: protocol.Position{
115 Line: 10,
116 },
117 End: protocol.Position{
118 Line: 10,
119 },
120 }); *got[0].Selection != want {
121 t.Errorf("gopls.add_test: got showDocument requests selection for %v, want %v", *got[0].Selection, want)
122 }
123 }
124 })
125 }