1 // Copyright 2020 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
11 . "golang.org/x/tools/gopls/internal/regtest"
13 "golang.org/x/tools/internal/lsp/tests"
16 const unformattedProgram = `
21 fmt.Println("Hello World.")
29 fmt.Println("Hello World.")
33 func TestFormatting(t *testing.T) {
34 Run(t, unformattedProgram, func(t *testing.T, env *Env) {
35 env.OpenFile("main.go")
36 env.FormatBuffer("main.go")
37 got := env.Editor.BufferText("main.go")
38 want := env.ReadWorkspaceFile("main.go.golden")
40 t.Errorf("unexpected formatting result:\n%s", tests.Diff(t, want, got))
45 // Tests golang/go#36824.
46 func TestFormattingOneLine36824(t *testing.T) {
47 const onelineProgram = `
49 package main; func f() {}
56 Run(t, onelineProgram, func(t *testing.T, env *Env) {
58 env.FormatBuffer("a.go")
59 got := env.Editor.BufferText("a.go")
60 want := env.ReadWorkspaceFile("a.go.formatted")
62 t.Errorf("unexpected formatting result:\n%s", tests.Diff(t, want, got))
67 // Tests golang/go#36824.
68 func TestFormattingOneLineImports36824(t *testing.T) {
69 const onelineProgramA = `
71 package x; func f() {fmt.Println()}
78 func f() { fmt.Println() }
80 Run(t, onelineProgramA, func(t *testing.T, env *Env) {
82 env.OrganizeImports("a.go")
83 got := env.Editor.BufferText("a.go")
84 want := env.ReadWorkspaceFile("a.go.imported")
86 t.Errorf("unexpected formatting result:\n%s", tests.Diff(t, want, got))
91 func TestFormattingOneLineRmImports36824(t *testing.T) {
92 const onelineProgramB = `
94 package x; import "os"; func f() {}
101 Run(t, onelineProgramB, func(t *testing.T, env *Env) {
103 env.OrganizeImports("a.go")
104 got := env.Editor.BufferText("a.go")
105 want := env.ReadWorkspaceFile("a.go.imported")
107 t.Errorf("unexpected formatting result:\n%s", tests.Diff(t, want, got))
112 const disorganizedProgram = `
121 fmt.Println(errors.New("bad"))
123 -- main.go.organized --
131 fmt.Println(errors.New("bad"))
133 -- main.go.formatted --
142 fmt.Println(errors.New("bad"))
146 func TestOrganizeImports(t *testing.T) {
147 Run(t, disorganizedProgram, func(t *testing.T, env *Env) {
148 env.OpenFile("main.go")
149 env.OrganizeImports("main.go")
150 got := env.Editor.BufferText("main.go")
151 want := env.ReadWorkspaceFile("main.go.organized")
153 t.Errorf("unexpected formatting result:\n%s", tests.Diff(t, want, got))
158 func TestFormattingOnSave(t *testing.T) {
159 Run(t, disorganizedProgram, func(t *testing.T, env *Env) {
160 env.OpenFile("main.go")
161 env.SaveBuffer("main.go")
162 got := env.Editor.BufferText("main.go")
163 want := env.ReadWorkspaceFile("main.go.formatted")
165 t.Errorf("unexpected formatting result:\n%s", tests.Diff(t, want, got))
170 // Tests various possibilities for comments in files with CRLF line endings.
171 // Import organization in these files has historically been a source of bugs.
172 func TestCRLFLineEndings(t *testing.T) {
173 for _, tt := range []struct {
196 func upload(c echo.Context) error {
197 if err := r.ParseForm(); err != nil {
198 fmt.Fprintf(w, "ParseForm() err: %v", err)
201 fmt.Fprintf(w, "POST request successful")
202 path_ver := r.FormValue("path_ver")
203 ukclin_ver := r.FormValue("ukclin_ver")
205 fmt.Fprintf(w, "Name = %s\n", path_ver)
206 fmt.Fprintf(w, "Address = %s\n", ukclin_ver)
211 const server_port = 8080
212 fmt.Printf("port: %d\n", server_port)
228 t.Run(tt.issue, func(t *testing.T) {
229 Run(t, "-- main.go --", func(t *testing.T, env *Env) {
230 crlf := strings.ReplaceAll(tt.want, "\n", "\r\n")
231 env.CreateBuffer("main.go", crlf)
232 env.Await(env.DoneWithOpen())
233 env.OrganizeImports("main.go")
234 got := env.Editor.BufferText("main.go")
235 got = strings.ReplaceAll(got, "\r\n", "\n") // convert everything to LF for simplicity
237 t.Errorf("unexpected content after save:\n%s", tests.Diff(t, tt.want, got))