7 "golang.org/x/tools/internal/lsp/tests"
10 const unformattedProgram = `
15 fmt.Println("Hello World.")
23 fmt.Println("Hello World.")
27 func TestFormatting(t *testing.T) {
28 runner.Run(t, unformattedProgram, func(t *testing.T, env *Env) {
29 env.OpenFile("main.go")
30 env.FormatBuffer("main.go")
31 got := env.Editor.BufferText("main.go")
32 want := env.ReadWorkspaceFile("main.go.golden")
34 t.Errorf("unexpected formatting result:\n%s", tests.Diff(want, got))
39 // Tests golang/go#36824.
40 func TestFormattingOneLine36824(t *testing.T) {
41 const onelineProgram = `
43 package main; func f() {}
50 runner.Run(t, onelineProgram, func(t *testing.T, env *Env) {
52 env.FormatBuffer("a.go")
53 got := env.Editor.BufferText("a.go")
54 want := env.ReadWorkspaceFile("a.go.formatted")
56 t.Errorf("unexpected formatting result:\n%s", tests.Diff(want, got))
61 // Tests golang/go#36824.
62 func TestFormattingOneLineImports36824(t *testing.T) {
63 const onelineProgramA = `
65 package x; func f() {fmt.Println()}
72 func f() { fmt.Println() }
74 runner.Run(t, onelineProgramA, func(t *testing.T, env *Env) {
76 env.OrganizeImports("a.go")
77 got := env.Editor.BufferText("a.go")
78 want := env.ReadWorkspaceFile("a.go.imported")
80 t.Errorf("unexpected formatting result:\n%s", tests.Diff(want, got))
85 func TestFormattingOneLineRmImports36824(t *testing.T) {
86 const onelineProgramB = `
88 package x; import "os"; func f() {}
95 runner.Run(t, onelineProgramB, func(t *testing.T, env *Env) {
97 env.OrganizeImports("a.go")
98 got := env.Editor.BufferText("a.go")
99 want := env.ReadWorkspaceFile("a.go.imported")
101 t.Errorf("unexpected formatting result:\n%s", tests.Diff(want, got))
106 const disorganizedProgram = `
115 fmt.Println(errors.New("bad"))
117 -- main.go.organized --
125 fmt.Println(errors.New("bad"))
127 -- main.go.formatted --
136 fmt.Println(errors.New("bad"))
140 func TestOrganizeImports(t *testing.T) {
141 runner.Run(t, disorganizedProgram, func(t *testing.T, env *Env) {
142 env.OpenFile("main.go")
143 env.OrganizeImports("main.go")
144 got := env.Editor.BufferText("main.go")
145 want := env.ReadWorkspaceFile("main.go.organized")
147 t.Errorf("unexpected formatting result:\n%s", tests.Diff(want, got))
152 func TestFormattingOnSave(t *testing.T) {
153 runner.Run(t, disorganizedProgram, func(t *testing.T, env *Env) {
154 env.OpenFile("main.go")
155 env.SaveBuffer("main.go")
156 got := env.Editor.BufferText("main.go")
157 want := env.ReadWorkspaceFile("main.go.formatted")
159 t.Errorf("unexpected formatting result:\n%s", tests.Diff(want, got))
164 // Reproduce golang/go#41057.
165 func TestCRLF(t *testing.T) {
166 runner.Run(t, "-- main.go --", func(t *testing.T, env *Env) {
167 want := `package main
175 crlf := strings.ReplaceAll(want, "\n", "\r\n")
176 env.CreateBuffer("main.go", crlf)
177 env.SaveBuffer("main.go")
178 got := env.Editor.BufferText("main.go")
180 t.Errorf("unexpected content after save:\n%s", tests.Diff(want, got))