| 1 |
|
| 2 |
|
| 3 |
|
| 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 |
|
| 19 |
func TestAssembly(t *testing.T) { |
| 20 |
testenv.NeedsGoCommand1Point(t, 22) |
| 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 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 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 |
|
| 75 |
|
| 76 |
|
| 77 |
checkMatch(t, true, report, `example.com/a.f.deferwrap`) |
| 78 |
|
| 79 |
|
| 80 |
checkMatch(t, false, report, `TEXT.*example.com/a.g`) |
| 81 |
} |
| 82 |
|
| 83 |
|
| 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 |
|
| 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 |
|
| 111 |
func TestTestAssembly(t *testing.T) { |
| 112 |
testenv.NeedsGoCommand1Point(t, 22) |
| 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 |
|
| 152 |
|
| 153 |
func asmFor(t *testing.T, env *Env, loc protocol.Location) []byte { |
| 154 |
_, content := codeActionWebPage(t, env, settings.GoAssembly, loc) |
| 155 |
return content |
| 156 |
} |