| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package misc |
| 6 |
|
| 7 |
import ( |
| 8 |
"strings" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"golang.org/x/tools/gopls/internal/test/compare" |
| 12 |
. "golang.org/x/tools/gopls/internal/test/integration" |
| 13 |
) |
| 14 |
|
| 15 |
const unformattedProgram = ` |
| 16 |
-- main.go -- |
| 17 |
package main |
| 18 |
import "fmt" |
| 19 |
func main( ) { |
| 20 |
fmt.Println("Hello World.") |
| 21 |
} |
| 22 |
-- main.go.golden -- |
| 23 |
package main |
| 24 |
|
| 25 |
import "fmt" |
| 26 |
|
| 27 |
func main() { |
| 28 |
fmt.Println("Hello World.") |
| 29 |
} |
| 30 |
` |
| 31 |
|
| 32 |
func TestFormatting(t *testing.T) { |
| 33 |
Run(t, unformattedProgram, func(t *testing.T, env *Env) { |
| 34 |
env.OpenFile("main.go") |
| 35 |
env.FormatBuffer("main.go") |
| 36 |
got := env.BufferText("main.go") |
| 37 |
want := env.ReadWorkspaceFile("main.go.golden") |
| 38 |
if got != want { |
| 39 |
t.Errorf("unexpected formatting result:\n%s", compare.Text(want, got)) |
| 40 |
} |
| 41 |
}) |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
func TestFormattingOneLine36824(t *testing.T) { |
| 46 |
const onelineProgram = ` |
| 47 |
-- a.go -- |
| 48 |
package main; func f() {} |
| 49 |
|
| 50 |
-- a.go.formatted -- |
| 51 |
package main |
| 52 |
|
| 53 |
func f() {} |
| 54 |
` |
| 55 |
Run(t, onelineProgram, func(t *testing.T, env *Env) { |
| 56 |
env.OpenFile("a.go") |
| 57 |
env.FormatBuffer("a.go") |
| 58 |
got := env.BufferText("a.go") |
| 59 |
want := env.ReadWorkspaceFile("a.go.formatted") |
| 60 |
if got != want { |
| 61 |
t.Errorf("unexpected formatting result:\n%s", compare.Text(want, got)) |
| 62 |
} |
| 63 |
}) |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
func TestFormattingOneLineImports36824(t *testing.T) { |
| 68 |
const onelineProgramA = ` |
| 69 |
-- a.go -- |
| 70 |
package x; func f() {fmt.Println()} |
| 71 |
|
| 72 |
-- a.go.imported -- |
| 73 |
package x |
| 74 |
|
| 75 |
import "fmt" |
| 76 |
|
| 77 |
func f() { fmt.Println() } |
| 78 |
` |
| 79 |
Run(t, onelineProgramA, func(t *testing.T, env *Env) { |
| 80 |
env.OpenFile("a.go") |
| 81 |
env.OrganizeImports("a.go") |
| 82 |
got := env.BufferText("a.go") |
| 83 |
want := env.ReadWorkspaceFile("a.go.imported") |
| 84 |
if got != want { |
| 85 |
t.Errorf("unexpected formatting result:\n%s", compare.Text(want, got)) |
| 86 |
} |
| 87 |
}) |
| 88 |
} |
| 89 |
|
| 90 |
func TestFormattingOneLineRmImports36824(t *testing.T) { |
| 91 |
const onelineProgramB = ` |
| 92 |
-- a.go -- |
| 93 |
package x; import "os"; func f() {} |
| 94 |
|
| 95 |
-- a.go.imported -- |
| 96 |
package x |
| 97 |
|
| 98 |
func f() {} |
| 99 |
` |
| 100 |
Run(t, onelineProgramB, func(t *testing.T, env *Env) { |
| 101 |
env.OpenFile("a.go") |
| 102 |
env.OrganizeImports("a.go") |
| 103 |
got := env.BufferText("a.go") |
| 104 |
want := env.ReadWorkspaceFile("a.go.imported") |
| 105 |
if got != want { |
| 106 |
t.Errorf("unexpected formatting result:\n%s", compare.Text(want, got)) |
| 107 |
} |
| 108 |
}) |
| 109 |
} |
| 110 |
|
| 111 |
const disorganizedProgram = ` |
| 112 |
-- main.go -- |
| 113 |
package main |
| 114 |
|
| 115 |
import ( |
| 116 |
"fmt" |
| 117 |
"errors" |
| 118 |
) |
| 119 |
func main( ) { |
| 120 |
fmt.Println(errors.New("bad")) |
| 121 |
} |
| 122 |
-- main.go.organized -- |
| 123 |
package main |
| 124 |
|
| 125 |
import ( |
| 126 |
"errors" |
| 127 |
"fmt" |
| 128 |
) |
| 129 |
func main( ) { |
| 130 |
fmt.Println(errors.New("bad")) |
| 131 |
} |
| 132 |
-- main.go.formatted -- |
| 133 |
package main |
| 134 |
|
| 135 |
import ( |
| 136 |
"errors" |
| 137 |
"fmt" |
| 138 |
) |
| 139 |
|
| 140 |
func main() { |
| 141 |
fmt.Println(errors.New("bad")) |
| 142 |
} |
| 143 |
` |
| 144 |
|
| 145 |
func TestOrganizeImports(t *testing.T) { |
| 146 |
Run(t, disorganizedProgram, func(t *testing.T, env *Env) { |
| 147 |
env.OpenFile("main.go") |
| 148 |
env.OrganizeImports("main.go") |
| 149 |
got := env.BufferText("main.go") |
| 150 |
want := env.ReadWorkspaceFile("main.go.organized") |
| 151 |
if got != want { |
| 152 |
t.Errorf("unexpected formatting result:\n%s", compare.Text(want, got)) |
| 153 |
} |
| 154 |
}) |
| 155 |
} |
| 156 |
|
| 157 |
func TestFormattingOnSave(t *testing.T) { |
| 158 |
Run(t, disorganizedProgram, func(t *testing.T, env *Env) { |
| 159 |
env.OpenFile("main.go") |
| 160 |
env.SaveBuffer("main.go") |
| 161 |
got := env.BufferText("main.go") |
| 162 |
want := env.ReadWorkspaceFile("main.go.formatted") |
| 163 |
if got != want { |
| 164 |
t.Errorf("unexpected formatting result:\n%s", compare.Text(want, got)) |
| 165 |
} |
| 166 |
}) |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
func TestCRLFLineEndings(t *testing.T) { |
| 172 |
for _, tt := range []struct { |
| 173 |
issue, input, want string |
| 174 |
}{ |
| 175 |
{ |
| 176 |
issue: "41057", |
| 177 |
want: `package main |
| 178 |
|
| 179 |
/* |
| 180 |
Hi description |
| 181 |
*/ |
| 182 |
func Hi() { |
| 183 |
} |
| 184 |
`, |
| 185 |
}, |
| 186 |
{ |
| 187 |
issue: "42646", |
| 188 |
want: `package main |
| 189 |
|
| 190 |
import ( |
| 191 |
"fmt" |
| 192 |
) |
| 193 |
|
| 194 |
/* |
| 195 |
func upload(c echo.Context) error { |
| 196 |
if err := r.ParseForm(); err != nil { |
| 197 |
fmt.Fprintf(w, "ParseForm() err: %v", err) |
| 198 |
return |
| 199 |
} |
| 200 |
fmt.Fprintf(w, "POST request successful") |
| 201 |
path_ver := r.FormValue("path_ver") |
| 202 |
ukclin_ver := r.FormValue("ukclin_ver") |
| 203 |
|
| 204 |
fmt.Fprintf(w, "Name = %s\n", path_ver) |
| 205 |
fmt.Fprintf(w, "Address = %s\n", ukclin_ver) |
| 206 |
} |
| 207 |
*/ |
| 208 |
|
| 209 |
func main() { |
| 210 |
const server_port = 8080 |
| 211 |
fmt.Printf("port: %d\n", server_port) |
| 212 |
} |
| 213 |
`, |
| 214 |
}, |
| 215 |
{ |
| 216 |
issue: "42923", |
| 217 |
want: `package main |
| 218 |
|
| 219 |
// Line 1. |
| 220 |
// aa |
| 221 |
type Tree struct { |
| 222 |
arr []string |
| 223 |
} |
| 224 |
`, |
| 225 |
}, |
| 226 |
{ |
| 227 |
issue: "47200", |
| 228 |
input: `package main |
| 229 |
|
| 230 |
import "fmt" |
| 231 |
|
| 232 |
func main() { |
| 233 |
math.Sqrt(9) |
| 234 |
fmt.Println("hello") |
| 235 |
} |
| 236 |
`, |
| 237 |
want: `package main |
| 238 |
|
| 239 |
import ( |
| 240 |
"fmt" |
| 241 |
"math" |
| 242 |
) |
| 243 |
|
| 244 |
func main() { |
| 245 |
math.Sqrt(9) |
| 246 |
fmt.Println("hello") |
| 247 |
} |
| 248 |
`, |
| 249 |
}, |
| 250 |
} { |
| 251 |
t.Run(tt.issue, func(t *testing.T) { |
| 252 |
Run(t, "-- main.go --", func(t *testing.T, env *Env) { |
| 253 |
input := tt.input |
| 254 |
if input == "" { |
| 255 |
input = tt.want |
| 256 |
} |
| 257 |
crlf := strings.ReplaceAll(input, "\n", "\r\n") |
| 258 |
env.CreateBuffer("main.go", crlf) |
| 259 |
env.Await(env.DoneWithOpen()) |
| 260 |
env.OrganizeImports("main.go") |
| 261 |
got := env.BufferText("main.go") |
| 262 |
got = strings.ReplaceAll(got, "\r\n", "\n") |
| 263 |
if tt.want != got { |
| 264 |
t.Errorf("unexpected content after save:\n%s", compare.Text(tt.want, got)) |
| 265 |
} |
| 266 |
}) |
| 267 |
}) |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
func TestGofumptFormatting(t *testing.T) { |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
const input = ` |
| 279 |
-- go.mod -- |
| 280 |
module foo |
| 281 |
|
| 282 |
go 1.17 |
| 283 |
-- foo.go -- |
| 284 |
package foo |
| 285 |
|
| 286 |
import ( |
| 287 |
"foo/bar" |
| 288 |
"fmt" |
| 289 |
) |
| 290 |
|
| 291 |
const perm = 0755 |
| 292 |
|
| 293 |
func foo() { |
| 294 |
foo := |
| 295 |
"bar" |
| 296 |
fmt.Println(foo, bar.Bar) |
| 297 |
} |
| 298 |
-- foo.go.formatted -- |
| 299 |
package foo |
| 300 |
|
| 301 |
import ( |
| 302 |
"fmt" |
| 303 |
|
| 304 |
"foo/bar" |
| 305 |
) |
| 306 |
|
| 307 |
const perm = 0o755 |
| 308 |
|
| 309 |
func foo() { |
| 310 |
foo := "bar" |
| 311 |
fmt.Println(foo, bar.Bar) |
| 312 |
} |
| 313 |
-- bar/bar.go -- |
| 314 |
package bar |
| 315 |
|
| 316 |
const Bar = 42 |
| 317 |
` |
| 318 |
|
| 319 |
WithOptions( |
| 320 |
Settings{ |
| 321 |
"gofumpt": true, |
| 322 |
}, |
| 323 |
).Run(t, input, func(t *testing.T, env *Env) { |
| 324 |
env.OpenFile("foo.go") |
| 325 |
env.FormatBuffer("foo.go") |
| 326 |
got := env.BufferText("foo.go") |
| 327 |
want := env.ReadWorkspaceFile("foo.go.formatted") |
| 328 |
if got != want { |
| 329 |
t.Errorf("unexpected formatting result:\n%s", compare.Text(want, got)) |
| 330 |
} |
| 331 |
}) |
| 332 |
} |
| 333 |
|
| 334 |
func TestGofumpt_Issue61692(t *testing.T) { |
| 335 |
const input = ` |
| 336 |
-- go.mod -- |
| 337 |
module foo |
| 338 |
|
| 339 |
go 1.21rc3 |
| 340 |
-- foo.go -- |
| 341 |
package foo |
| 342 |
|
| 343 |
func _() { |
| 344 |
foo := |
| 345 |
"bar" |
| 346 |
} |
| 347 |
` |
| 348 |
|
| 349 |
WithOptions( |
| 350 |
Settings{ |
| 351 |
"gofumpt": true, |
| 352 |
}, |
| 353 |
).Run(t, input, func(t *testing.T, env *Env) { |
| 354 |
env.OpenFile("foo.go") |
| 355 |
env.FormatBuffer("foo.go") |
| 356 |
}) |
| 357 |
} |