Debian Codegraph @

gopls/internal/test/integration/web/assembly_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 web_test
6
7 import (
8 "regexp"
9 "runtime"
10 "testing"
11
12 "golang.org/x/tools/gopls/internal/protocol"
13 "golang.org/x/tools/gopls/internal/settings"
14 . "golang.org/x/tools/gopls/internal/test/integration"
15 "golang.org/x/tools/internal/testenv"
16 )
17
18 // TestAssembly is a basic test of the web-based assembly listing.
19 func TestAssembly(t *testing.T) {
20 testenv.NeedsGoCommand1Point(t, 22) // for up-to-date assembly listing
21
22 const files = `
23 -- go.mod --
24 module example.com
25
26 -- a/a.go --
27 package a
28
29 func f(x int) int {
30 println("hello")
31 defer println("world")
32 return x
33 }
34
35 func g() {
36 println("goodbye")
37 }
38
39 var v = [...]int{
40 f(123),
41 f(456),
42 }
43
44 func init() {
45 f(789)
46 }
47 `
48 Run(t, files, func(t *testing.T, env *Env) {
49 env.OpenFile("a/a.go")
50
51 // Get the report and do some minimal checks for sensible results.
52 //
53 // Use only portable instructions below! Remember that
54 // this is a test of plumbing, not compilation, so
55 // it's better to skip the tests, rather than refine
56 // them, on any architecture that gives us trouble
57 // (e.g. uses JAL for CALL, or BL<cc> for RET).
58 // We conservatively test only on the two most popular
59 // architectures.
60 {
61 loc := env.RegexpSearch("a/a.go", "println")
62 report := asmFor(t, env, loc)
63 checkMatch(t, true, report, `TEXT.*example.com/a.f`)
64 switch runtime.GOARCH {
65 case "amd64", "arm64":
66 checkMatch(t, true, report, `CALL runtime.printlock`)
67 checkMatch(t, true, report, `CALL runtime.printstring`)
68 checkMatch(t, true, report, `CALL runtime.printunlock`)
69 checkMatch(t, true, report, `CALL example.com/a.f.deferwrap`)
70 checkMatch(t, true, report, `RET`)
71 checkMatch(t, true, report, `CALL runtime.morestack_noctxt`)
72 }
73
74 // Nested functions are also shown.
75 //
76 // The condition here was relaxed to unblock go.dev/cl/639515.
77 checkMatch(t, true, report, `example.com/a.f.deferwrap`)
78
79 // But other functions are not.
80 checkMatch(t, false, report, `TEXT.*example.com/a.g`)
81 }
82
83 // Check that code in a package-level var initializer is found too.
84 {
85 loc := env.RegexpSearch("a/a.go", `f\(123\)`)
86 report := asmFor(t, env, loc)
87 switch runtime.GOARCH {
88 case "amd64", "arm64":
89 checkMatch(t, true, report, `TEXT.*example.com/a.init`)
90 checkMatch(t, true, report, `MOV.? \$123`)
91 checkMatch(t, true, report, `MOV.? \$456`)
92 checkMatch(t, true, report, `CALL example.com/a.f`)
93 }
94 }
95
96 // And code in a source-level init function.
97 {
98 loc := env.RegexpSearch("a/a.go", `f\(789\)`)
99 report := asmFor(t, env, loc)
100 switch runtime.GOARCH {
101 case "amd64", "arm64":
102 checkMatch(t, true, report, `TEXT.*example.com/a.init`)
103 checkMatch(t, true, report, `MOV.? \$789`)
104 checkMatch(t, true, report, `CALL example.com/a.f`)
105 }
106 }
107 })
108 }
109
110 // TestTestAssembly exercises assembly listing of tests.
111 func TestTestAssembly(t *testing.T) {
112 testenv.NeedsGoCommand1Point(t, 22) // for up-to-date assembly listing
113
114 const files = `
115 -- go.mod --
116 module example.com
117
118 -- a/a_test.go --
119 package a
120
121 import "testing"
122
123 func Test1(*testing.T) { println(0) }
124
125 -- a/a_x_test.go --
126 package a_test
127
128 import "testing"
129
130 func Test2(*testing.T) { println(0) }
131 `
132 Run(t, files, func(t *testing.T, env *Env) {
133 for _, test := range []struct {
134 filename, symbol string
135 }{
136 {"a/a_test.go", "example.com/a.Test1"},
137 {"a/a_x_test.go", "example.com/a_test.Test2"},
138 } {
139 env.OpenFile(test.filename)
140 loc := env.RegexpSearch(test.filename, `println`)
141 report := asmFor(t, env, loc)
142 checkMatch(t, true, report, `TEXT.*`+regexp.QuoteMeta(test.symbol))
143 switch runtime.GOARCH {
144 case "amd64", "arm64":
145 checkMatch(t, true, report, `CALL runtime.printint`)
146 }
147 }
148 })
149 }
150
151 // asmFor returns the HTML document served by gopls for a "Browse assembly"
152 // command at the specified location in an open file.
153 func asmFor(t *testing.T, env *Env, loc protocol.Location) []byte {
154 _, content := codeActionWebPage(t, env, settings.GoAssembly, loc)
155 return content
156 }