@@ -187,40 +187,122 @@ limitations under the License.`))
187
187
}
188
188
189
189
func TestFix (t * testing.T ) {
190
- const src = `// mycompany.net
191
- // SPDX-License-Identifier: Foo
190
+ const pkg = `
192
191
193
192
// Package foo
194
193
package foo
195
194
196
195
func Foo() { println("Foo") }
197
196
`
198
197
199
- a := goheader .New (
200
- goheader .WithTemplate (`{{ MY COMPANY }}
198
+ analyze := func (header string ) goheader.Issue {
199
+ a := goheader .New (
200
+ goheader .WithTemplate (`{{ MY COMPANY }}
201
201
SPDX-License-Identifier: Foo` ),
202
- goheader .WithValues (map [string ]goheader.Value {
203
- "MY COMPANY" : & goheader.ConstValue {
204
- RawValue : "mycompany.com" ,
205
- },
206
- }))
202
+ goheader .WithValues (map [string ]goheader.Value {
203
+ "MY COMPANY" : & goheader.ConstValue {
204
+ RawValue : "mycompany.com" ,
205
+ },
206
+ }))
207
207
208
- fset := token .NewFileSet ()
209
- file , err := parser .ParseFile (fset , "foo.go" , src , parser .ParseComments )
210
- require .NoError (t , err )
208
+ fset := token .NewFileSet ()
209
+ file , err := parser .ParseFile (fset , "foo.go" , header + pkg , parser .ParseComments )
210
+ require .NoError (t , err )
211
+
212
+ issue := a .Analyze (& goheader.Target {
213
+ File : file ,
214
+ Path : t .TempDir (),
215
+ })
216
+ require .NotNil (t , issue )
217
+ require .NotNil (t , issue .Fix ())
218
+ return issue
219
+ }
211
220
212
- issue := a .Analyze (& goheader.Target {
213
- File : file ,
214
- Path : t .TempDir (),
221
+ t .Run ("Line comment" , func (t * testing.T ) {
222
+ issue := analyze (`// mycompany.net
223
+ // SPDX-License-Identifier: Foo` )
224
+
225
+ require .Equal (t , []string {
226
+ "// mycompany.net" ,
227
+ "// SPDX-License-Identifier: Foo" ,
228
+ }, issue .Fix ().Actual )
229
+ require .Equal (t , []string {
230
+ "// mycompany.com" ,
231
+ "// SPDX-License-Identifier: Foo" ,
232
+ }, issue .Fix ().Expected )
233
+ })
234
+
235
+ t .Run ("Block comment 1" , func (t * testing.T ) {
236
+ issue := analyze (`/* mycompany.net
237
+ SPDX-License-Identifier: Foo */` )
238
+
239
+ require .Equal (t , []string {
240
+ "/* mycompany.net" ,
241
+ "SPDX-License-Identifier: Foo */" ,
242
+ }, issue .Fix ().Actual )
243
+ require .Equal (t , []string {
244
+ "/* mycompany.com" ,
245
+ "SPDX-License-Identifier: Foo */" ,
246
+ }, issue .Fix ().Expected )
247
+ })
248
+
249
+ t .Run ("Block comment 2" , func (t * testing.T ) {
250
+ issue := analyze (`/*
251
+ mycompany.net
252
+ SPDX-License-Identifier: Foo */` )
253
+
254
+ require .Equal (t , []string {
255
+ "/*" ,
256
+ "mycompany.net" ,
257
+ "SPDX-License-Identifier: Foo */" ,
258
+ }, issue .Fix ().Actual )
259
+ require .Equal (t , []string {
260
+ "/*" ,
261
+ "mycompany.com" ,
262
+ "SPDX-License-Identifier: Foo */" ,
263
+ }, issue .Fix ().Expected )
264
+ })
265
+
266
+ t .Run ("Block comment 3" , func (t * testing.T ) {
267
+ issue := analyze (`/* mycompany.net
268
+ SPDX-License-Identifier: Foo
269
+ */` )
270
+
271
+ require .Equal (t , []string {
272
+ "/* mycompany.net" ,
273
+ "SPDX-License-Identifier: Foo" ,
274
+ "*/" ,
275
+ }, issue .Fix ().Actual )
276
+ require .Equal (t , []string {
277
+ "/* mycompany.com" ,
278
+ "SPDX-License-Identifier: Foo" ,
279
+ "*/" ,
280
+ }, issue .Fix ().Expected )
281
+ })
282
+
283
+ t .Run ("Block comment 4" , func (t * testing.T ) {
284
+ issue := analyze (`/*
285
+
286
+ mycompany.net
287
+ SPDX-License-Identifier: Foo
288
+
289
+ */` )
290
+
291
+ require .Equal (t , []string {
292
+ "/*" ,
293
+ "" ,
294
+ "mycompany.net" ,
295
+ "SPDX-License-Identifier: Foo" ,
296
+ "" ,
297
+ "*/" ,
298
+ }, issue .Fix ().Actual )
299
+ require .Equal (t , []string {
300
+ "/*" ,
301
+ "" ,
302
+ "mycompany.com" ,
303
+ "SPDX-License-Identifier: Foo" ,
304
+ "" ,
305
+ "*/" ,
306
+ }, issue .Fix ().Expected )
215
307
})
216
- require .NotNil (t , issue )
217
- require .NotNil (t , issue .Fix ())
218
- require .Equal (t , []string {
219
- "// mycompany.net" ,
220
- "// SPDX-License-Identifier: Foo" ,
221
- }, issue .Fix ().Actual )
222
- require .Equal (t , []string {
223
- "// mycompany.com" ,
224
- "// SPDX-License-Identifier: Foo" ,
225
- }, issue .Fix ().Expected )
226
308
}
0 commit comments