forked from cuonglm/gocmt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_test.go
310 lines (244 loc) · 5.66 KB
/
parse_test.go
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
package main
import (
"bytes"
"go/format"
"go/token"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
const baseSrc = `package p
var i = 0
// I ...
var I = 1
var c = "constant un-exported"
// C ...
const C = "constant exported"
type t struct{}
// T ...
type T struct{}
func main() {
}
func unexport(s string) {
}
// Export ...
func Export(s string) {
}
// ExportWithComment ...
func ExportWithComment(s string) {
}
// ExistedComment ...
func ExistedComment() {}
`
const parenSrc = `package p
// Summon ...
type Summon string
// DarkOmega ...
const (
DarkOmega Summon = "celeste"
// LightOmega best summon
LightOmega Summon = "luminineria"
// WindOmega
WindOmega Summon = "tiamat"
)
// FireUtility ...
const FireUtility Summon = "the sun"
// Light ...
const (
// Light best summon
Light Summon = "lucifer"
)
// Light2 best summon
const (
Light2 Summon = "lucifer"
)
`
const parenSrc2 = `package p
// Summon ...
type Summon string
const (
// DarkOmega ...
DarkOmega Summon = "celeste"
// LightOmega best summon
LightOmega Summon = "luminineria"
// WindOmega ...
WindOmega Summon = "tiamat"
)
// FireUtility ...
const FireUtility Summon = "the sun"
const (
// Light best summon
Light Summon = "lucifer"
)
// Light2 best summon
const (
// Light2 ...
Light2 Summon = "lucifer"
)
`
const issue7 = `package p
import "log"
// I ...
var I = 1
func a() {
// LogAll ...
var LogAll map[string]struct{}
log.Println(LogAll)
}`
const existed = `package p
// global comments should never be deleted
// something
import "embed"
// global comment 1
// global comment 2
// global comment 3
// ============= function =============
// FuncWithExistedComment1 ...
func FuncWithExistedComment1() {
}
// FuncWithExistedComment2 ...
func FuncWithExistedComment2() {
// this comments should never be deleted
}
// FuncWithExistedComment3 something
func FuncWithExistedComment3() {
}
// FuncWithExistedComment4 multi-line comments
// something
func FuncWithExistedComment4() {
}
/*
something
*/
func FuncWithExistedComment5() {
}
// ============= value =============
// ValueWithExistedComment1 existed comment
var ValueWithExistedComment1 = 1
// ValueWithExistedComment2 existed comment with spaces
var ValueWithExistedComment2 = 1
// ValueWithExistedComment3 multi-line comments
// something
var ValueWithExistedComment3 = 1
/*
should't change C style comment
*/
var ValueWithExistedComment4 = 1
// ============= paren value =============
// ParenValueWithExistedComment1 existed comment
const (
ParenValueWithExistedComment1 = 1
// ParenValueWithExistedComment2 something
ParenValueWithExistedComment2 = 1
)
// ParenValueWithExistedComment3 multi-line comments
// something
const (
ParenValueWithExistedComment3 = 1
// ParenValueWithExistedComment2 something
ParenValueWithExistedComment4 = 1
)
// ============= type =============
// TypeWithExistedComment1 existed comment
type TypeWithExistedComment1 int
// TypeWithExistedComment2 existed comment with spaces
type TypeWithExistedComment2 int
// TypeWithExistedComment3 multi-line comments
// something
type TypeWithExistedComment3 int
/*
should't change C style comment
*/
type TypeWithExistedComment4 int
// ============= marker comment =============
// Embed ...
//go:embed dont_modify_this_comment.txt
//go:embed image/*
var Embed embed.FS
// Embed something
//go:embed dont_modify_this_comment.txt
var Embed embed.FS
// Generate ...
//go:generate goyacc -o gopher.go -p parser gopher.y
func Generate() {
}
// ============= end =============
`
func Test_parseFile(t *testing.T) {
parseFileTests := []struct {
path string
expectedSrc string
modified bool
wantErr bool
}{
{"testdata/main.go", baseSrc, true, false},
{"testdata/parenthesis.go", parenSrc, true, false},
{"testdata/invalid_file.go", "", false, true},
{"testdata/issue7.go", issue7, false, false},
{"testdata/existed.go", existed, true, false},
}
for _, tc := range parseFileTests {
tc := tc
t.Run(tc.path, func(t *testing.T) {
t.Parallel()
fset := token.NewFileSet()
af, modified, err := parseFile(fset, tc.path, "...")
assert.True(t, tc.wantErr == (err != nil))
assert.Equal(t, tc.modified, modified)
if tc.modified {
buf := new(bytes.Buffer)
assert.NoError(t, format.Node(buf, fset, af))
newBuf := buf.Bytes()
newBuf = tralingWsRegex.ReplaceAll(newBuf, []byte(""))
newBuf = newlinesRegex.ReplaceAll(newBuf, []byte("\n\n"))
assert.Equal(t, tc.expectedSrc, string(newBuf))
}
})
}
}
func Test_parseFileWithParenComment(t *testing.T) {
*parenComment = true
parseFileTests := []struct {
path string
expectedSrc string
modified bool
wantErr bool
}{
{"testdata/parenthesis.go", parenSrc2, true, false},
}
for _, tc := range parseFileTests {
tc := tc
t.Run(tc.path, func(t *testing.T) {
t.Parallel()
fset := token.NewFileSet()
af, modified, err := parseFile(fset, tc.path, "...")
assert.True(t, tc.wantErr == (err != nil))
assert.Equal(t, tc.modified, modified)
if tc.modified {
buf := new(bytes.Buffer)
assert.NoError(t, format.Node(buf, fset, af))
newBuf := buf.Bytes()
newBuf = tralingWsRegex.ReplaceAll(newBuf, []byte(""))
newBuf = newlinesRegex.ReplaceAll(newBuf, []byte("\n\n"))
assert.Equal(t, tc.expectedSrc, string(newBuf))
}
})
}
}
func TestSkipVendor(t *testing.T) {
filePath := "testdata/vendor/main.go"
origBuf, err := os.ReadFile(filePath)
if err != nil {
t.Fatal(err)
}
if err := processFile(filePath, "...", true); err != nil {
t.Fatal(err)
}
buf, err := os.ReadFile(filePath)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(buf, origBuf) {
t.Fatal("file in vendor/ directory was edited")
}
}