| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package web_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"testing" |
| 9 |
|
| 10 |
"golang.org/x/tools/gopls/internal/protocol" |
| 11 |
"golang.org/x/tools/gopls/internal/protocol/command" |
| 12 |
"golang.org/x/tools/gopls/internal/settings" |
| 13 |
. "golang.org/x/tools/gopls/internal/test/integration" |
| 14 |
) |
| 15 |
|
| 16 |
|
| 17 |
func TestFreeSymbols(t *testing.T) { |
| 18 |
const files = ` |
| 19 |
-- go.mod -- |
| 20 |
module example.com |
| 21 |
|
| 22 |
-- a/a.go -- |
| 23 |
package a |
| 24 |
|
| 25 |
import "fmt" |
| 26 |
import "bytes" |
| 27 |
|
| 28 |
func f(buf bytes.Buffer, greeting string) { |
| 29 |
/* « */ |
| 30 |
fmt.Fprintf(&buf, "%s", greeting) |
| 31 |
buf.WriteString(fmt.Sprint("foo")) |
| 32 |
buf.WriteByte(0) |
| 33 |
/* » */ |
| 34 |
buf.Write(nil) |
| 35 |
} |
| 36 |
` |
| 37 |
Run(t, files, func(t *testing.T, env *Env) { |
| 38 |
env.OpenFile("a/a.go") |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
loc := env.RegexpSearch("a/a.go", "«((?:.|\n)*)»") |
| 43 |
actions, err := env.Editor.CodeAction(env.Ctx, loc, nil, protocol.CodeActionUnknownTrigger) |
| 44 |
if err != nil { |
| 45 |
t.Fatalf("CodeAction: %v", err) |
| 46 |
} |
| 47 |
action, err := CodeActionByKind(actions, settings.GoFreeSymbols) |
| 48 |
if err != nil { |
| 49 |
t.Fatal(err) |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
params := &protocol.ExecuteCommandParams{ |
| 55 |
Command: action.Command.Command, |
| 56 |
Arguments: action.Command.Arguments, |
| 57 |
} |
| 58 |
var result command.DebuggingResult |
| 59 |
collectDocs := env.Awaiter.ListenToShownDocuments() |
| 60 |
env.ExecuteCommand(params, &result) |
| 61 |
doc := shownDocument(t, collectDocs(), "http:") |
| 62 |
if doc == nil { |
| 63 |
t.Fatalf("no showDocument call had 'file:' prefix") |
| 64 |
} |
| 65 |
t.Log("showDocument(package doc) URL:", doc.URI) |
| 66 |
|
| 67 |
|
| 68 |
report := get(t, doc.URI) |
| 69 |
checkMatch(t, true, report, `<li>import "<a .*'>fmt</a>" // for Fprintf, Sprint</li>`) |
| 70 |
checkMatch(t, true, report, `<li>var <a .*>buf</a> bytes.Buffer</li>`) |
| 71 |
checkMatch(t, true, report, `<li>func <a .*>WriteByte</a> func\(c byte\) error</li>`) |
| 72 |
checkMatch(t, true, report, `<li>func <a .*>WriteString</a> func\(s string\) \(n int, err error\)</li>`) |
| 73 |
checkMatch(t, false, report, `<li>func <a .*>Write</a>`) |
| 74 |
checkMatch(t, true, report, `<li>var <a .*>greeting</a> string</li>`) |
| 75 |
}) |
| 76 |
} |