2020-02-27 15:20:53 -07:00
|
|
|
package regtest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
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-03-23 15:26:05 -06:00
|
|
|
runner.Run(t, unformattedProgram, func(env *Env) {
|
2020-02-27 15:20:53 -07:00
|
|
|
env.OpenFile("main.go")
|
|
|
|
env.FormatBuffer("main.go")
|
|
|
|
got := env.E.BufferText("main.go")
|
|
|
|
want := env.ReadWorkspaceFile("main.go.golden")
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("\n## got formatted file:\n%s\n## want:\n%s", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
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-03-23 15:26:05 -06:00
|
|
|
runner.Run(t, disorganizedProgram, func(env *Env) {
|
2020-02-27 15:20:53 -07:00
|
|
|
env.OpenFile("main.go")
|
|
|
|
env.OrganizeImports("main.go")
|
|
|
|
got := env.E.BufferText("main.go")
|
|
|
|
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-03-23 15:26:05 -06:00
|
|
|
runner.Run(t, disorganizedProgram, func(env *Env) {
|
2020-02-27 15:20:53 -07:00
|
|
|
env.OpenFile("main.go")
|
|
|
|
env.SaveBuffer("main.go")
|
|
|
|
got := env.E.BufferText("main.go")
|
|
|
|
want := env.ReadWorkspaceFile("main.go.formatted")
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("\n## got formatted file:\n%s\n## want:\n%s", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|