Skip to content

Commit

Permalink
go/ssa: cleanup TestGenericBodies to pickup package name
Browse files Browse the repository at this point in the history
Change-Id: Ied570c45b491c375ca6f790a7d571251e9411eb5
Reviewed-on: https://go-review.googlesource.com/c/tools/+/493096
Run-TryBot: Tim King <taking@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
Reviewed-by: Zvonimir Pavlinovic <zpavlinovic@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
  • Loading branch information
timothy-king committed May 8, 2023
1 parent 3d99ebe commit f4d143e
Showing 1 changed file with 41 additions and 103 deletions.
144 changes: 41 additions & 103 deletions go/ssa/builder_generic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,8 @@ func TestGenericBodies(t *testing.T) {
if !typeparams.Enabled {
t.Skip("TestGenericBodies requires type parameters")
}
for _, test := range []struct {
pkg string // name of the package.
contents string // contents of the Go package.
}{
{
pkg: "p",
contents: `
for _, contents := range []string{
`
package p
func f(x int) {
Expand All @@ -50,31 +45,22 @@ func TestGenericBodies(t *testing.T) {
print(x) //@ types(int)
}
`,
},
{
pkg: "q",
contents: `
`
package q
func f[T any](x T) {
print(x) //@ types(T)
}
`,
},
{
pkg: "r",
contents: `
`
package r
func f[T ~int]() {
var x T
print(x) //@ types(T)
}
`,
},
{
pkg: "s",
contents: `
`
package s
func a[T ~[4]byte](x T) {
Expand Down Expand Up @@ -128,10 +114,7 @@ func TestGenericBodies(t *testing.T) {
print(f[F]) //@ types("func(x s.F)")
}
`,
},
{
pkg: "t",
contents: `
`
package t
func f[S any, T ~chan S](x T) {
Expand All @@ -145,10 +128,7 @@ func TestGenericBodies(t *testing.T) {
print(f[string, F]) //@ types("func(x t.F)")
}
`,
},
{
pkg: "u",
contents: `
`
package u
func fibonacci[T ~chan int](c, quit T) {
Expand Down Expand Up @@ -179,10 +159,7 @@ func TestGenericBodies(t *testing.T) {
print(fibonacci[F], c, quit) //@ types("func(c u.F, quit u.F)", "u.F", "u.F")
}
`,
},
{
pkg: "v",
contents: `
`
package v
func f[T ~struct{ x int; y string }](i int) T {
Expand All @@ -194,10 +171,7 @@ func TestGenericBodies(t *testing.T) {
print(f[S]) //@ types("func(i int) v.S")
}
`,
},
{
pkg: "w",
contents: `
`
package w
func f[T ~[4]int8](x T, l, h int) []int8 {
Expand All @@ -218,10 +192,7 @@ func TestGenericBodies(t *testing.T) {
print(h[H](nil, 0, 0)) //@ types("w.H")
}
`,
},
{
pkg: "x",
contents: `
`
package x
func h[E any, T ~[]E](x T, l, h int) []E {
Expand All @@ -234,10 +205,7 @@ func TestGenericBodies(t *testing.T) {
print(h[int32, H](nil, 0, 0)) //@ types("[]int32")
}
`,
},
{
pkg: "y",
contents: `
`
package y
// Test "make" builtin with different forms on core types and
Expand Down Expand Up @@ -267,10 +235,7 @@ func TestGenericBodies(t *testing.T) {
j[I, J](6)
}
`,
},
{
pkg: "z",
contents: `
`
package z
func h[T ~[4]int](x T) {
Expand All @@ -287,10 +252,7 @@ func TestGenericBodies(t *testing.T) {
panic(x)
}
`,
},
{
pkg: "a",
contents: `
`
package a
func f[E any, F ~func() E](x F) {
Expand All @@ -302,10 +264,7 @@ func TestGenericBodies(t *testing.T) {
f[int, func() int](func() int { return 1 })
}
`,
},
{
pkg: "b",
contents: `
`
package b
func f[E any, M ~map[string]E](m M) {
Expand All @@ -317,10 +276,7 @@ func TestGenericBodies(t *testing.T) {
f(O{"lorem": []int{0, 1, 2, 3}})
}
`,
},
{
pkg: "c",
contents: `
`
package c
func a[T interface{ []int64 | [5]int64 }](x T) int64 {
Expand Down Expand Up @@ -361,10 +317,7 @@ func TestGenericBodies(t *testing.T) {
print(t, &t[0]) //@ types(T, "*int")
}
`,
},
{
pkg: "d",
contents: `
`
package d
type MyInt int
Expand Down Expand Up @@ -392,37 +345,25 @@ func TestGenericBodies(t *testing.T) {
func sl0[T *[4]int | *[2]int](x []int) { v := T(x); print(x, v) /*@ types("[]int", T)*/ }
func sl1[T *[4]int | *[2]int, S []int](x S) { v := T(x); print(x, v) /*@ types(S, T)*/ }
`,
},
{
pkg: "e",
contents: `
`
package e
func c[T interface{ foo() string }](x T) {
print(x, x.foo, x.foo()) /*@ types(T, "func() string", string)*/
}
`,
},
{
pkg: "f",
contents: `package f
`package f
func eq[T comparable](t T, i interface{}) bool {
return t == i
}
`,
},
{
// TODO(59983): investigate why writing g.c panics in (*FieldAddr).String.
pkg: "g",
contents: `package g
// TODO(59983): investigate why writing g.c panics in (*FieldAddr).String.
`package g
type S struct{ f int }
func c[P *S]() []P { return []P{{f: 1}} }
`,
},
{
pkg: "h",
contents: `package h
`package h
func sign[bytes []byte | string](s bytes) (bool, bool) {
neg := false
if len(s) > 0 && (s[0] == '-' || s[0] == '+') {
Expand All @@ -431,10 +372,7 @@ func TestGenericBodies(t *testing.T) {
}
return !neg, len(s) > 0
}`,
},
{
pkg: "i",
contents: `package i
`package i
func digits[bytes []byte | string](s bytes) bool {
for _, c := range []byte(s) {
if c < '0' || '9' < c {
Expand All @@ -443,10 +381,7 @@ func TestGenericBodies(t *testing.T) {
}
return true
}`,
},
{
pkg: "j",
contents: `
`
package j
type E interface{}
Expand All @@ -458,10 +393,7 @@ func TestGenericBodies(t *testing.T) {
return x
}
`,
},
{
pkg: "k",
contents: `
`
package k
func f[M any, PM *M](p PM) {
Expand All @@ -471,10 +403,7 @@ func TestGenericBodies(t *testing.T) {
print(p) /*@ types(PM)*/
}
`,
},
{
pkg: "l",
contents: `
`
package l
type A struct{int}
Expand All @@ -497,18 +426,17 @@ func TestGenericBodies(t *testing.T) {
b := *(any(v).(*B)); print(b) /*@ types("l.B")*/
c := *(any(v).(*C)); print(c) /*@ types("l.C")*/
}`,
},
} {
test := test
t.Run(test.pkg, func(t *testing.T) {
contents := contents
pkgname := packageName(t, contents)
t.Run(pkgname, func(t *testing.T) {
// Parse
conf := loader.Config{ParserMode: parser.ParseComments}
fname := test.pkg + ".go"
f, err := conf.ParseFile(fname, test.contents)
f, err := conf.ParseFile("file.go", contents)
if err != nil {
t.Fatalf("parse: %v", err)
}
conf.CreateFromFiles(test.pkg, f)
conf.CreateFromFiles(pkgname, f)

// Load
lprog, err := conf.Load()
Expand All @@ -523,7 +451,7 @@ func TestGenericBodies(t *testing.T) {
prog.CreatePackage(info.Pkg, info.Files, &info.Info, info.Importable)
}
}
p := prog.Package(lprog.Package(test.pkg).Pkg)
p := prog.Package(lprog.Package(pkgname).Pkg)
p.Build()

// Collect calls to the builtin print function.
Expand Down Expand Up @@ -717,3 +645,13 @@ func TestInstructionString(t *testing.T) {
}
}
}

// packageName is a test helper to extract the package name from a string
// containing the content of a go file.
func packageName(t testing.TB, content string) string {
f, err := parser.ParseFile(token.NewFileSet(), "", content, parser.PackageClauseOnly)
if err != nil {
t.Fatalf("parsing the file %q failed with error: %s", content, err)
}
return f.Name.Name
}

0 comments on commit f4d143e

Please sign in to comment.