2020-02-27 15:20:53 -07:00
|
|
|
package regtest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2020-05-20 22:12:56 -06:00
|
|
|
|
|
|
|
"golang.org/x/tools/internal/lsp/tests"
|
2020-02-27 15:20:53 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
const unformattedProgram = `
|
|
|
|
-- main.go --
|
|
|
|
package main
|
|
|
|
import "fmt"
|
|
|
|
func main( ) {
|
|
|
|
fmt.Println("Hello World.")
|
|
|
|
}
|
|
|
|
-- main.go.golden --
|
|
|
|
package main
|
|
|
|
|
|
|
|
import "fmt"
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
fmt.Println("Hello World.")
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
func TestFormatting(t *testing.T) {
|
2020-04-14 11:40:37 -06:00
|
|
|
runner.Run(t, unformattedProgram, func(t *testing.T, env *Env) {
|
2020-02-27 15:20:53 -07:00
|
|
|
env.OpenFile("main.go")
|
|
|
|
env.FormatBuffer("main.go")
|
2020-04-28 22:00:52 -06:00
|
|
|
got := env.Editor.BufferText("main.go")
|
2020-02-27 15:20:53 -07:00
|
|
|
want := env.ReadWorkspaceFile("main.go.golden")
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("\n## got formatted file:\n%s\n## want:\n%s", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-20 22:12:56 -06:00
|
|
|
// Tests golang/go#36824.
|
|
|
|
func TestFormattingOneLine36824(t *testing.T) {
|
|
|
|
|
|
|
|
const onelineProgram = `
|
2020-04-21 14:23:39 -06:00
|
|
|
-- a.go --
|
|
|
|
package main; func f() {}
|
|
|
|
|
|
|
|
-- a.go.formatted --
|
|
|
|
package main
|
|
|
|
|
|
|
|
func f() {}
|
|
|
|
`
|
|
|
|
runner.Run(t, onelineProgram, func(t *testing.T, env *Env) {
|
|
|
|
env.OpenFile("a.go")
|
|
|
|
env.FormatBuffer("a.go")
|
2020-04-28 22:00:52 -06:00
|
|
|
got := env.Editor.BufferText("a.go")
|
2020-04-21 14:23:39 -06:00
|
|
|
want := env.ReadWorkspaceFile("a.go.formatted")
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("got\n%q wanted\n%q", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-20 22:12:56 -06:00
|
|
|
// Tests golang/go#36824.
|
|
|
|
func TestFormattingOneLineImports36824(t *testing.T) {
|
|
|
|
|
|
|
|
const onelineProgramA = `
|
2020-04-21 14:23:39 -06:00
|
|
|
-- a.go --
|
|
|
|
package x; func f() {fmt.Println()}
|
|
|
|
|
|
|
|
-- a.go.imported --
|
|
|
|
package x
|
|
|
|
|
|
|
|
import "fmt"
|
|
|
|
|
|
|
|
func f() { fmt.Println() }
|
|
|
|
`
|
|
|
|
runner.Run(t, onelineProgramA, func(t *testing.T, env *Env) {
|
|
|
|
env.OpenFile("a.go")
|
|
|
|
env.OrganizeImports("a.go")
|
2020-04-28 22:00:52 -06:00
|
|
|
got := env.Editor.BufferText("a.go")
|
2020-04-21 14:23:39 -06:00
|
|
|
want := env.ReadWorkspaceFile("a.go.imported")
|
|
|
|
if got != want {
|
2020-05-24 15:13:47 -06:00
|
|
|
t.Errorf("OneLineImports3824:\n%s", tests.Diff(want, got))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFormattingOneLineRmImports36824(t *testing.T) {
|
|
|
|
const onelineProgramB = `
|
|
|
|
-- a.go --
|
|
|
|
package x; import "os"; func f() {}
|
|
|
|
|
|
|
|
-- a.go.imported --
|
|
|
|
package x
|
|
|
|
|
|
|
|
func f() {}
|
|
|
|
`
|
|
|
|
runner.Run(t, onelineProgramB, func(t *testing.T, env *Env) {
|
|
|
|
env.OpenFile("a.go")
|
|
|
|
env.OrganizeImports("a.go")
|
|
|
|
got := env.Editor.BufferText("a.go")
|
|
|
|
want := env.ReadWorkspaceFile("a.go.imported")
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("OneLineRmImports:\n%s", tests.Diff(want, got))
|
2020-04-21 14:23:39 -06:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-02-27 15:20:53 -07:00
|
|
|
const disorganizedProgram = `
|
|
|
|
-- main.go --
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"errors"
|
|
|
|
)
|
|
|
|
func main( ) {
|
|
|
|
fmt.Println(errors.New("bad"))
|
|
|
|
}
|
|
|
|
-- main.go.organized --
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
func main( ) {
|
|
|
|
fmt.Println(errors.New("bad"))
|
|
|
|
}
|
|
|
|
-- main.go.formatted --
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
fmt.Println(errors.New("bad"))
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
func TestOrganizeImports(t *testing.T) {
|
2020-04-14 11:40:37 -06:00
|
|
|
runner.Run(t, disorganizedProgram, func(t *testing.T, env *Env) {
|
2020-02-27 15:20:53 -07:00
|
|
|
env.OpenFile("main.go")
|
|
|
|
env.OrganizeImports("main.go")
|
2020-04-28 22:00:52 -06:00
|
|
|
got := env.Editor.BufferText("main.go")
|
2020-02-27 15:20:53 -07:00
|
|
|
want := env.ReadWorkspaceFile("main.go.organized")
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("\n## got formatted file:\n%s\n## want:\n%s", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFormattingOnSave(t *testing.T) {
|
2020-04-14 11:40:37 -06:00
|
|
|
runner.Run(t, disorganizedProgram, func(t *testing.T, env *Env) {
|
2020-02-27 15:20:53 -07:00
|
|
|
env.OpenFile("main.go")
|
|
|
|
env.SaveBuffer("main.go")
|
2020-04-28 22:00:52 -06:00
|
|
|
got := env.Editor.BufferText("main.go")
|
2020-02-27 15:20:53 -07:00
|
|
|
want := env.ReadWorkspaceFile("main.go.formatted")
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("\n## got formatted file:\n%s\n## want:\n%s", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|