@@ -34,13 +34,8 @@ func TestGenericBodies(t *testing.T) {
34
34
if ! typeparams .Enabled {
35
35
t .Skip ("TestGenericBodies requires type parameters" )
36
36
}
37
- for _ , test := range []struct {
38
- pkg string // name of the package.
39
- contents string // contents of the Go package.
40
- }{
41
- {
42
- pkg : "p" ,
43
- contents : `
37
+ for _ , contents := range []string {
38
+ `
44
39
package p
45
40
46
41
func f(x int) {
@@ -50,31 +45,22 @@ func TestGenericBodies(t *testing.T) {
50
45
print(x) //@ types(int)
51
46
}
52
47
` ,
53
- },
54
- {
55
- pkg : "q" ,
56
- contents : `
48
+ `
57
49
package q
58
50
59
51
func f[T any](x T) {
60
52
print(x) //@ types(T)
61
53
}
62
54
` ,
63
- },
64
- {
65
- pkg : "r" ,
66
- contents : `
55
+ `
67
56
package r
68
57
69
58
func f[T ~int]() {
70
59
var x T
71
60
print(x) //@ types(T)
72
61
}
73
62
` ,
74
- },
75
- {
76
- pkg : "s" ,
77
- contents : `
63
+ `
78
64
package s
79
65
80
66
func a[T ~[4]byte](x T) {
@@ -128,10 +114,7 @@ func TestGenericBodies(t *testing.T) {
128
114
print(f[F]) //@ types("func(x s.F)")
129
115
}
130
116
` ,
131
- },
132
- {
133
- pkg : "t" ,
134
- contents : `
117
+ `
135
118
package t
136
119
137
120
func f[S any, T ~chan S](x T) {
@@ -145,10 +128,7 @@ func TestGenericBodies(t *testing.T) {
145
128
print(f[string, F]) //@ types("func(x t.F)")
146
129
}
147
130
` ,
148
- },
149
- {
150
- pkg : "u" ,
151
- contents : `
131
+ `
152
132
package u
153
133
154
134
func fibonacci[T ~chan int](c, quit T) {
@@ -179,10 +159,7 @@ func TestGenericBodies(t *testing.T) {
179
159
print(fibonacci[F], c, quit) //@ types("func(c u.F, quit u.F)", "u.F", "u.F")
180
160
}
181
161
` ,
182
- },
183
- {
184
- pkg : "v" ,
185
- contents : `
162
+ `
186
163
package v
187
164
188
165
func f[T ~struct{ x int; y string }](i int) T {
@@ -194,10 +171,7 @@ func TestGenericBodies(t *testing.T) {
194
171
print(f[S]) //@ types("func(i int) v.S")
195
172
}
196
173
` ,
197
- },
198
- {
199
- pkg : "w" ,
200
- contents : `
174
+ `
201
175
package w
202
176
203
177
func f[T ~[4]int8](x T, l, h int) []int8 {
@@ -218,10 +192,7 @@ func TestGenericBodies(t *testing.T) {
218
192
print(h[H](nil, 0, 0)) //@ types("w.H")
219
193
}
220
194
` ,
221
- },
222
- {
223
- pkg : "x" ,
224
- contents : `
195
+ `
225
196
package x
226
197
227
198
func h[E any, T ~[]E](x T, l, h int) []E {
@@ -234,10 +205,7 @@ func TestGenericBodies(t *testing.T) {
234
205
print(h[int32, H](nil, 0, 0)) //@ types("[]int32")
235
206
}
236
207
` ,
237
- },
238
- {
239
- pkg : "y" ,
240
- contents : `
208
+ `
241
209
package y
242
210
243
211
// Test "make" builtin with different forms on core types and
@@ -267,10 +235,7 @@ func TestGenericBodies(t *testing.T) {
267
235
j[I, J](6)
268
236
}
269
237
` ,
270
- },
271
- {
272
- pkg : "z" ,
273
- contents : `
238
+ `
274
239
package z
275
240
276
241
func h[T ~[4]int](x T) {
@@ -287,10 +252,7 @@ func TestGenericBodies(t *testing.T) {
287
252
panic(x)
288
253
}
289
254
` ,
290
- },
291
- {
292
- pkg : "a" ,
293
- contents : `
255
+ `
294
256
package a
295
257
296
258
func f[E any, F ~func() E](x F) {
@@ -302,10 +264,7 @@ func TestGenericBodies(t *testing.T) {
302
264
f[int, func() int](func() int { return 1 })
303
265
}
304
266
` ,
305
- },
306
- {
307
- pkg : "b" ,
308
- contents : `
267
+ `
309
268
package b
310
269
311
270
func f[E any, M ~map[string]E](m M) {
@@ -317,10 +276,7 @@ func TestGenericBodies(t *testing.T) {
317
276
f(O{"lorem": []int{0, 1, 2, 3}})
318
277
}
319
278
` ,
320
- },
321
- {
322
- pkg : "c" ,
323
- contents : `
279
+ `
324
280
package c
325
281
326
282
func a[T interface{ []int64 | [5]int64 }](x T) int64 {
@@ -361,10 +317,7 @@ func TestGenericBodies(t *testing.T) {
361
317
print(t, &t[0]) //@ types(T, "*int")
362
318
}
363
319
` ,
364
- },
365
- {
366
- pkg : "d" ,
367
- contents : `
320
+ `
368
321
package d
369
322
370
323
type MyInt int
@@ -392,37 +345,25 @@ func TestGenericBodies(t *testing.T) {
392
345
func sl0[T *[4]int | *[2]int](x []int) { v := T(x); print(x, v) /*@ types("[]int", T)*/ }
393
346
func sl1[T *[4]int | *[2]int, S []int](x S) { v := T(x); print(x, v) /*@ types(S, T)*/ }
394
347
` ,
395
- },
396
- {
397
- pkg : "e" ,
398
- contents : `
348
+ `
399
349
package e
400
350
401
351
func c[T interface{ foo() string }](x T) {
402
352
print(x, x.foo, x.foo()) /*@ types(T, "func() string", string)*/
403
353
}
404
354
` ,
405
- },
406
- {
407
- pkg : "f" ,
408
- contents : `package f
355
+ `package f
409
356
410
357
func eq[T comparable](t T, i interface{}) bool {
411
358
return t == i
412
359
}
413
360
` ,
414
- },
415
- {
416
- // TODO(59983): investigate why writing g.c panics in (*FieldAddr).String.
417
- pkg : "g" ,
418
- contents : `package g
361
+ // TODO(59983): investigate why writing g.c panics in (*FieldAddr).String.
362
+ `package g
419
363
type S struct{ f int }
420
364
func c[P *S]() []P { return []P{{f: 1}} }
421
365
` ,
422
- },
423
- {
424
- pkg : "h" ,
425
- contents : `package h
366
+ `package h
426
367
func sign[bytes []byte | string](s bytes) (bool, bool) {
427
368
neg := false
428
369
if len(s) > 0 && (s[0] == '-' || s[0] == '+') {
@@ -431,10 +372,7 @@ func TestGenericBodies(t *testing.T) {
431
372
}
432
373
return !neg, len(s) > 0
433
374
}` ,
434
- },
435
- {
436
- pkg : "i" ,
437
- contents : `package i
375
+ `package i
438
376
func digits[bytes []byte | string](s bytes) bool {
439
377
for _, c := range []byte(s) {
440
378
if c < '0' || '9' < c {
@@ -443,10 +381,7 @@ func TestGenericBodies(t *testing.T) {
443
381
}
444
382
return true
445
383
}` ,
446
- },
447
- {
448
- pkg : "j" ,
449
- contents : `
384
+ `
450
385
package j
451
386
452
387
type E interface{}
@@ -458,10 +393,7 @@ func TestGenericBodies(t *testing.T) {
458
393
return x
459
394
}
460
395
` ,
461
- },
462
- {
463
- pkg : "k" ,
464
- contents : `
396
+ `
465
397
package k
466
398
467
399
func f[M any, PM *M](p PM) {
@@ -471,10 +403,7 @@ func TestGenericBodies(t *testing.T) {
471
403
print(p) /*@ types(PM)*/
472
404
}
473
405
` ,
474
- },
475
- {
476
- pkg : "l" ,
477
- contents : `
406
+ `
478
407
package l
479
408
480
409
type A struct{int}
@@ -497,18 +426,17 @@ func TestGenericBodies(t *testing.T) {
497
426
b := *(any(v).(*B)); print(b) /*@ types("l.B")*/
498
427
c := *(any(v).(*C)); print(c) /*@ types("l.C")*/
499
428
}` ,
500
- },
501
429
} {
502
- test := test
503
- t .Run (test .pkg , func (t * testing.T ) {
430
+ contents := contents
431
+ pkgname := packageName (t , contents )
432
+ t .Run (pkgname , func (t * testing.T ) {
504
433
// Parse
505
434
conf := loader.Config {ParserMode : parser .ParseComments }
506
- fname := test .pkg + ".go"
507
- f , err := conf .ParseFile (fname , test .contents )
435
+ f , err := conf .ParseFile ("file.go" , contents )
508
436
if err != nil {
509
437
t .Fatalf ("parse: %v" , err )
510
438
}
511
- conf .CreateFromFiles (test . pkg , f )
439
+ conf .CreateFromFiles (pkgname , f )
512
440
513
441
// Load
514
442
lprog , err := conf .Load ()
@@ -523,7 +451,7 @@ func TestGenericBodies(t *testing.T) {
523
451
prog .CreatePackage (info .Pkg , info .Files , & info .Info , info .Importable )
524
452
}
525
453
}
526
- p := prog .Package (lprog .Package (test . pkg ).Pkg )
454
+ p := prog .Package (lprog .Package (pkgname ).Pkg )
527
455
p .Build ()
528
456
529
457
// Collect calls to the builtin print function.
@@ -717,3 +645,13 @@ func TestInstructionString(t *testing.T) {
717
645
}
718
646
}
719
647
}
648
+
649
+ // packageName is a test helper to extract the package name from a string
650
+ // containing the content of a go file.
651
+ func packageName (t testing.TB , content string ) string {
652
+ f , err := parser .ParseFile (token .NewFileSet (), "" , content , parser .PackageClauseOnly )
653
+ if err != nil {
654
+ t .Fatalf ("parsing the file %q failed with error: %s" , content , err )
655
+ }
656
+ return f .Name .Name
657
+ }
0 commit comments