aboutsummaryrefslogtreecommitdiff
path: root/gopls/internal/regtest/misc/formatting_test.go
blob: 75d8f622458e4f32042c912b723ffd0813a681f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package misc

import (
	"strings"
	"testing"

	. "golang.org/x/tools/internal/lsp/regtest"

	"golang.org/x/tools/internal/lsp/tests"
)

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) {
	Run(t, unformattedProgram, func(t *testing.T, env *Env) {
		env.OpenFile("main.go")
		env.FormatBuffer("main.go")
		got := env.Editor.BufferText("main.go")
		want := env.ReadWorkspaceFile("main.go.golden")
		if got != want {
			t.Errorf("unexpected formatting result:\n%s", tests.Diff(t, want, got))
		}
	})
}

// Tests golang/go#36824.
func TestFormattingOneLine36824(t *testing.T) {
	const onelineProgram = `
-- a.go --
package main; func f() {}

-- a.go.formatted --
package main

func f() {}
`
	Run(t, onelineProgram, func(t *testing.T, env *Env) {
		env.OpenFile("a.go")
		env.FormatBuffer("a.go")
		got := env.Editor.BufferText("a.go")
		want := env.ReadWorkspaceFile("a.go.formatted")
		if got != want {
			t.Errorf("unexpected formatting result:\n%s", tests.Diff(t, want, got))
		}
	})
}

// Tests golang/go#36824.
func TestFormattingOneLineImports36824(t *testing.T) {
	const onelineProgramA = `
-- a.go --
package x; func f() {fmt.Println()}

-- a.go.imported --
package x

import "fmt"

func f() { fmt.Println() }
`
	Run(t, onelineProgramA, 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("unexpected formatting result:\n%s", tests.Diff(t, want, got))
		}
	})
}

func TestFormattingOneLineRmImports36824(t *testing.T) {
	const onelineProgramB = `
-- a.go --
package x; import "os"; func f() {}

-- a.go.imported --
package x

func f() {}
`
	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("unexpected formatting result:\n%s", tests.Diff(t, want, got))
		}
	})
}

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) {
	Run(t, disorganizedProgram, func(t *testing.T, env *Env) {
		env.OpenFile("main.go")
		env.OrganizeImports("main.go")
		got := env.Editor.BufferText("main.go")
		want := env.ReadWorkspaceFile("main.go.organized")
		if got != want {
			t.Errorf("unexpected formatting result:\n%s", tests.Diff(t, want, got))
		}
	})
}

func TestFormattingOnSave(t *testing.T) {
	Run(t, disorganizedProgram, func(t *testing.T, env *Env) {
		env.OpenFile("main.go")
		env.SaveBuffer("main.go")
		got := env.Editor.BufferText("main.go")
		want := env.ReadWorkspaceFile("main.go.formatted")
		if got != want {
			t.Errorf("unexpected formatting result:\n%s", tests.Diff(t, want, got))
		}
	})
}

// Tests various possibilities for comments in files with CRLF line endings.
// Import organization in these files has historically been a source of bugs.
func TestCRLFLineEndings(t *testing.T) {
	for _, tt := range []struct {
		issue, input, want string
	}{
		{
			issue: "41057",
			want: `package main

/*
Hi description
*/
func Hi() {
}
`,
		},
		{
			issue: "42646",
			want: `package main

import (
	"fmt"
)

/*
func upload(c echo.Context) error {
	if err := r.ParseForm(); err != nil {
		fmt.Fprintf(w, "ParseForm() err: %v", err)
		return
	}
	fmt.Fprintf(w, "POST request successful")
	path_ver := r.FormValue("path_ver")
	ukclin_ver := r.FormValue("ukclin_ver")

	fmt.Fprintf(w, "Name = %s\n", path_ver)
	fmt.Fprintf(w, "Address = %s\n", ukclin_ver)
}
*/

func main() {
	const server_port = 8080
	fmt.Printf("port: %d\n", server_port)
}
`,
		},
		{
			issue: "42923",
			want: `package main

// Line 1.
// aa
type Tree struct {
	arr []string
}
`,
		},
		{
			issue: "47200",
			input: `package main

import "fmt"

func main() {
	math.Sqrt(9)
	fmt.Println("hello")
}
`,
			want: `package main

import (
	"fmt"
	"math"
)

func main() {
	math.Sqrt(9)
	fmt.Println("hello")
}
`,
		},
	} {
		t.Run(tt.issue, func(t *testing.T) {
			Run(t, "-- main.go --", func(t *testing.T, env *Env) {
				input := tt.input
				if input == "" {
					input = tt.want
				}
				crlf := strings.ReplaceAll(input, "\n", "\r\n")
				env.CreateBuffer("main.go", crlf)
				env.Await(env.DoneWithOpen())
				env.OrganizeImports("main.go")
				got := env.Editor.BufferText("main.go")
				got = strings.ReplaceAll(got, "\r\n", "\n") // convert everything to LF for simplicity
				if tt.want != got {
					t.Errorf("unexpected content after save:\n%s", tests.Diff(t, tt.want, got))
				}
			})
		})
	}
}

func TestFormattingOfGeneratedFile_Issue49555(t *testing.T) {
	const input = `
-- main.go --
// Code generated by generator.go. DO NOT EDIT.

package main

import "fmt"

func main() {




	fmt.Print("hello")
}
`

	Run(t, input, func(t *testing.T, env *Env) {
		wantErrSuffix := "file is generated"

		env.OpenFile("main.go")
		err := env.Editor.FormatBuffer(env.Ctx, "main.go")
		if err == nil {
			t.Fatal("expected error, got nil")
		}
		// Check only the suffix because an error contains a dynamic path to main.go
		if !strings.HasSuffix(err.Error(), wantErrSuffix) {
			t.Fatalf("unexpected error %q, want suffix %q", err.Error(), wantErrSuffix)
		}
	})
}

func TestGofumptFormatting(t *testing.T) {

	// Exercise some gofumpt formatting rules:
	//  - No empty lines following an assignment operator
	//  - Octal integer literals should use the 0o prefix on modules using Go
	//    1.13 and later. Requires LangVersion to be correctly resolved.
	//  - std imports must be in a separate group at the top. Requires ModulePath
	//    to be correctly resolved.
	const input = `
-- go.mod --
module foo

go 1.17
-- foo.go --
package foo

import (
	"foo/bar"
	"fmt"
)

const perm = 0755

func foo() {
	foo :=
		"bar"
	fmt.Println(foo, bar.Bar)
}
-- foo.go.formatted --
package foo

import (
	"fmt"

	"foo/bar"
)

const perm = 0o755

func foo() {
	foo := "bar"
	fmt.Println(foo, bar.Bar)
}
-- bar/bar.go --
package bar

const Bar = 42
`

	WithOptions(
		EditorConfig{
			Settings: map[string]interface{}{
				"gofumpt": true,
			},
		},
	).Run(t, input, func(t *testing.T, env *Env) {
		env.OpenFile("foo.go")
		env.FormatBuffer("foo.go")
		got := env.Editor.BufferText("foo.go")
		want := env.ReadWorkspaceFile("foo.go.formatted")
		if got != want {
			t.Errorf("unexpected formatting result:\n%s", tests.Diff(t, want, got))
		}
	})
}