Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / golang.org / x / tools / gopls@v0.5.2 / internal / regtest / formatting_test.go
1 package regtest
2
3 import (
4         "strings"
5         "testing"
6
7         "golang.org/x/tools/internal/lsp/tests"
8 )
9
10 const unformattedProgram = `
11 -- main.go --
12 package main
13 import "fmt"
14 func main(  ) {
15         fmt.Println("Hello World.")
16 }
17 -- main.go.golden --
18 package main
19
20 import "fmt"
21
22 func main() {
23         fmt.Println("Hello World.")
24 }
25 `
26
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")
33                 if got != want {
34                         t.Errorf("unexpected formatting result:\n%s", tests.Diff(want, got))
35                 }
36         })
37 }
38
39 // Tests golang/go#36824.
40 func TestFormattingOneLine36824(t *testing.T) {
41         const onelineProgram = `
42 -- a.go --
43 package main; func f() {}
44
45 -- a.go.formatted --
46 package main
47
48 func f() {}
49 `
50         runner.Run(t, onelineProgram, func(t *testing.T, env *Env) {
51                 env.OpenFile("a.go")
52                 env.FormatBuffer("a.go")
53                 got := env.Editor.BufferText("a.go")
54                 want := env.ReadWorkspaceFile("a.go.formatted")
55                 if got != want {
56                         t.Errorf("unexpected formatting result:\n%s", tests.Diff(want, got))
57                 }
58         })
59 }
60
61 // Tests golang/go#36824.
62 func TestFormattingOneLineImports36824(t *testing.T) {
63         const onelineProgramA = `
64 -- a.go --
65 package x; func f() {fmt.Println()}
66
67 -- a.go.imported --
68 package x
69
70 import "fmt"
71
72 func f() { fmt.Println() }
73 `
74         runner.Run(t, onelineProgramA, func(t *testing.T, env *Env) {
75                 env.OpenFile("a.go")
76                 env.OrganizeImports("a.go")
77                 got := env.Editor.BufferText("a.go")
78                 want := env.ReadWorkspaceFile("a.go.imported")
79                 if got != want {
80                         t.Errorf("unexpected formatting result:\n%s", tests.Diff(want, got))
81                 }
82         })
83 }
84
85 func TestFormattingOneLineRmImports36824(t *testing.T) {
86         const onelineProgramB = `
87 -- a.go --
88 package x; import "os"; func f() {}
89
90 -- a.go.imported --
91 package x
92
93 func f() {}
94 `
95         runner.Run(t, onelineProgramB, func(t *testing.T, env *Env) {
96                 env.OpenFile("a.go")
97                 env.OrganizeImports("a.go")
98                 got := env.Editor.BufferText("a.go")
99                 want := env.ReadWorkspaceFile("a.go.imported")
100                 if got != want {
101                         t.Errorf("unexpected formatting result:\n%s", tests.Diff(want, got))
102                 }
103         })
104 }
105
106 const disorganizedProgram = `
107 -- main.go --
108 package main
109
110 import (
111         "fmt"
112         "errors"
113 )
114 func main(  ) {
115         fmt.Println(errors.New("bad"))
116 }
117 -- main.go.organized --
118 package main
119
120 import (
121         "errors"
122         "fmt"
123 )
124 func main(  ) {
125         fmt.Println(errors.New("bad"))
126 }
127 -- main.go.formatted --
128 package main
129
130 import (
131         "errors"
132         "fmt"
133 )
134
135 func main() {
136         fmt.Println(errors.New("bad"))
137 }
138 `
139
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")
146                 if got != want {
147                         t.Errorf("unexpected formatting result:\n%s", tests.Diff(want, got))
148                 }
149         })
150 }
151
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")
158                 if got != want {
159                         t.Errorf("unexpected formatting result:\n%s", tests.Diff(want, got))
160                 }
161         })
162 }
163
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
168
169 /*
170 Hi description
171 */
172 func Hi() {
173 }
174 `
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")
179                 if want != got {
180                         t.Errorf("unexpected content after save:\n%s", tests.Diff(want, got))
181                 }
182         })
183 }