| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package misc |
| 6 |
|
| 7 |
import ( |
| 8 |
"testing" |
| 9 |
|
| 10 |
"golang.org/x/tools/gopls/internal/settings" |
| 11 |
"golang.org/x/tools/gopls/internal/test/compare" |
| 12 |
. "golang.org/x/tools/gopls/internal/test/integration" |
| 13 |
|
| 14 |
"golang.org/x/tools/gopls/internal/protocol" |
| 15 |
) |
| 16 |
|
| 17 |
func TestExtractFunction(t *testing.T) { |
| 18 |
const files = ` |
| 19 |
-- go.mod -- |
| 20 |
module mod.com |
| 21 |
|
| 22 |
go 1.12 |
| 23 |
-- main.go -- |
| 24 |
package main |
| 25 |
|
| 26 |
func Foo() int { |
| 27 |
a := 5 |
| 28 |
return a |
| 29 |
} |
| 30 |
` |
| 31 |
Run(t, files, func(t *testing.T, env *Env) { |
| 32 |
env.OpenFile("main.go") |
| 33 |
loc := env.RegexpSearch("main.go", `a := 5\n.*return a`) |
| 34 |
actions, err := env.Editor.CodeAction(env.Ctx, loc, nil, protocol.CodeActionUnknownTrigger) |
| 35 |
if err != nil { |
| 36 |
t.Fatal(err) |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
var extractFunc *protocol.CodeAction |
| 41 |
for _, action := range actions { |
| 42 |
if action.Kind == settings.RefactorExtractFunction { |
| 43 |
extractFunc = &action |
| 44 |
break |
| 45 |
} |
| 46 |
} |
| 47 |
if extractFunc == nil { |
| 48 |
t.Fatal("could not find extract function action") |
| 49 |
} |
| 50 |
|
| 51 |
env.ApplyCodeAction(*extractFunc) |
| 52 |
want := `package main |
| 53 |
|
| 54 |
func Foo() int { |
| 55 |
return newFunction() |
| 56 |
} |
| 57 |
|
| 58 |
func newFunction() int { |
| 59 |
a := 5 |
| 60 |
return a |
| 61 |
} |
| 62 |
` |
| 63 |
if got := env.BufferText("main.go"); got != want { |
| 64 |
t.Fatalf("TestExtractFunction failed:\n%s", compare.Text(want, got)) |
| 65 |
} |
| 66 |
}) |
| 67 |
} |
| 68 |
|
| 69 |
func TestExtractLabel(t *testing.T) { |
| 70 |
const files = ` |
| 71 |
-- go.mod -- |
| 72 |
module mod.com |
| 73 |
|
| 74 |
go 1.12 |
| 75 |
-- main.go -- |
| 76 |
package main |
| 77 |
|
| 78 |
func Foo(x []int) int { |
| 79 |
var i int |
| 80 |
for i = range x { |
| 81 |
if x[i] == 10 { |
| 82 |
goto found |
| 83 |
} |
| 84 |
} |
| 85 |
panic("not found") |
| 86 |
found: |
| 87 |
|
| 88 |
if i < 2 { |
| 89 |
} |
| 90 |
} |
| 91 |
` |
| 92 |
Run(t, files, func(t *testing.T, env *Env) { |
| 93 |
env.OpenFile("main.go") |
| 94 |
loc := env.RegexpSearch("main.go", `(?s)for.*found:`) |
| 95 |
actions, err := env.Editor.CodeAction(env.Ctx, loc, nil, protocol.CodeActionUnknownTrigger) |
| 96 |
if err != nil { |
| 97 |
t.Fatal(err) |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
var extractFunc *protocol.CodeAction |
| 102 |
for _, action := range actions { |
| 103 |
if action.Kind == settings.RefactorExtractFunction { |
| 104 |
extractFunc = &action |
| 105 |
break |
| 106 |
} |
| 107 |
} |
| 108 |
if extractFunc == nil { |
| 109 |
t.Fatal("could not find extract function action") |
| 110 |
} |
| 111 |
|
| 112 |
env.ApplyCodeAction(*extractFunc) |
| 113 |
want := `package main |
| 114 |
|
| 115 |
func Foo(x []int) int { |
| 116 |
var i int |
| 117 |
newFunction(i, x) |
| 118 |
|
| 119 |
if i < 2 { |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
func newFunction(i int, x []int) { |
| 124 |
for i = range x { |
| 125 |
if x[i] == 10 { |
| 126 |
goto found |
| 127 |
} |
| 128 |
} |
| 129 |
panic("not found") |
| 130 |
found: |
| 131 |
} |
| 132 |
` |
| 133 |
if got := env.BufferText("main.go"); got != want { |
| 134 |
t.Fatalf("TestExtractlabel failed:\n%s", compare.Text(want, got)) |
| 135 |
} |
| 136 |
}) |
| 137 |
} |