Skip to content

Refactor srctesting to support parsing and checking multiple packages. #1274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions build/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,18 +418,17 @@ func TestOverlayAugmentation(t *testing.T) {
test.want = test.src
}

fsetSrc := token.NewFileSet()
fileSrc := srctesting.Parse(t, fsetSrc, pkgName+test.src)
f := srctesting.New(t)
fileSrc := f.Parse("test.go", pkgName+test.src)

overrides := map[string]overrideInfo{}
augmentOverlayFile(fileSrc, overrides)
pruneImports(fileSrc)

got := srctesting.Format(t, fsetSrc, fileSrc)
got := srctesting.Format(t, f.FileSet, fileSrc)

fsetWant := token.NewFileSet()
fileWant := srctesting.Parse(t, fsetWant, pkgName+test.want)
want := srctesting.Format(t, fsetWant, fileWant)
fileWant := f.Parse("test.go", pkgName+test.want)
want := srctesting.Format(t, f.FileSet, fileWant)

if got != want {
t.Errorf("augmentOverlayFile and pruneImports got unexpected code:\n"+
Expand Down Expand Up @@ -720,18 +719,17 @@ func TestOriginalAugmentation(t *testing.T) {
t.Run(test.desc, func(t *testing.T) {
pkgName := "package testpackage\n\n"
importPath := `math/rand`
fsetSrc := token.NewFileSet()
fileSrc := srctesting.Parse(t, fsetSrc, pkgName+test.src)
f := srctesting.New(t)
fileSrc := f.Parse("test.go", pkgName+test.src)

augmentOriginalImports(importPath, fileSrc)
augmentOriginalFile(fileSrc, test.info)
pruneImports(fileSrc)

got := srctesting.Format(t, fsetSrc, fileSrc)
got := srctesting.Format(t, f.FileSet, fileSrc)

fsetWant := token.NewFileSet()
fileWant := srctesting.Parse(t, fsetWant, pkgName+test.want)
want := srctesting.Format(t, fsetWant, fileWant)
fileWant := f.Parse("test.go", pkgName+test.want)
want := srctesting.Format(t, f.FileSet, fileWant)

if got != want {
t.Errorf("augmentOriginalImports, augmentOriginalFile, and pruneImports got unexpected code:\n"+
Expand Down
9 changes: 4 additions & 5 deletions compiler/analysis/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package analysis

import (
"go/ast"
"go/token"
"go/types"
"testing"

Expand Down Expand Up @@ -34,11 +33,11 @@ func notBlocking() {
func() { println() } ()
}
`
fset := token.NewFileSet()
file := srctesting.Parse(t, fset, src)
typesInfo, typesPkg := srctesting.Check(t, fset, file)
f := srctesting.New(t)
file := f.Parse("test.go", src)
typesInfo, typesPkg := f.Check("pkg/test", file)

pkgInfo := AnalyzePkg([]*ast.File{file}, fset, typesInfo, typesPkg, func(f *types.Func) bool {
pkgInfo := AnalyzePkg([]*ast.File{file}, f.FileSet, typesInfo, typesPkg, func(f *types.Func) bool {
panic("isBlocking() should be never called for imported functions in this test.")
})

Expand Down
10 changes: 3 additions & 7 deletions compiler/astutil/astutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package astutil

import (
"go/ast"
"go/token"
"strconv"
"testing"

Expand Down Expand Up @@ -44,8 +43,7 @@ func TestImportsUnsafe(t *testing.T) {
for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
src := "package testpackage\n\n" + test.imports
fset := token.NewFileSet()
file := srctesting.Parse(t, fset, src)
file := srctesting.New(t).Parse("test.go", src)
got := ImportsUnsafe(file)
if got != test.want {
t.Fatalf("ImportsUnsafe() returned %t, want %t", got, test.want)
Expand Down Expand Up @@ -81,8 +79,7 @@ func TestImportName(t *testing.T) {
for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
src := "package testpackage\n\n" + test.src
fset := token.NewFileSet()
file := srctesting.Parse(t, fset, src)
file := srctesting.New(t).Parse("test.go", src)
if len(file.Imports) != 1 {
t.Fatal(`expected one and only one import`)
}
Expand Down Expand Up @@ -399,8 +396,7 @@ func TestHasDirectiveOnFile(t *testing.T) {
for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
const action = `do-stuff`
fset := token.NewFileSet()
file := srctesting.Parse(t, fset, test.src)
file := srctesting.New(t).Parse("test.go", test.src)
if got := hasDirective(file, action); got != test.want {
t.Errorf(`hasDirective(%T, %q) returned %t, want %t`, file, action, got, test.want)
}
Expand Down
15 changes: 7 additions & 8 deletions compiler/internal/symbol/symbol_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package symbol

import (
"go/token"
"go/types"
"testing"

Expand All @@ -18,28 +17,28 @@ func TestName(t *testing.T) {
var AVariable int32
`

fset := token.NewFileSet()
_, pkg := srctesting.Check(t, fset, srctesting.Parse(t, fset, src))
f := srctesting.New(t)
_, pkg := f.Check("pkg/test", f.Parse("test.go", src))

tests := []struct {
obj types.Object
want Name
}{
{
obj: pkg.Scope().Lookup("AFunction"),
want: Name{PkgPath: "test", Name: "AFunction"},
want: Name{PkgPath: "pkg/test", Name: "AFunction"},
}, {
obj: pkg.Scope().Lookup("AType"),
want: Name{PkgPath: "test", Name: "AType"},
want: Name{PkgPath: "pkg/test", Name: "AType"},
}, {
obj: types.NewMethodSet(pkg.Scope().Lookup("AType").Type()).Lookup(pkg, "AMethod").Obj(),
want: Name{PkgPath: "test", Name: "AType.AMethod"},
want: Name{PkgPath: "pkg/test", Name: "AType.AMethod"},
}, {
obj: types.NewMethodSet(types.NewPointer(pkg.Scope().Lookup("AType").Type())).Lookup(pkg, "APointerMethod").Obj(),
want: Name{PkgPath: "test", Name: "(*AType).APointerMethod"},
want: Name{PkgPath: "pkg/test", Name: "(*AType).APointerMethod"},
}, {
obj: pkg.Scope().Lookup("AVariable"),
want: Name{PkgPath: "test", Name: "AVariable"},
want: Name{PkgPath: "pkg/test", Name: "AVariable"},
},
}

Expand Down
35 changes: 17 additions & 18 deletions compiler/internal/typeparams/collect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package typeparams

import (
"go/ast"
"go/token"
"go/types"
"testing"

Expand Down Expand Up @@ -80,9 +79,9 @@ func TestVisitor(t *testing.T) {

type entry5 = typ[int, F]
`
fset := token.NewFileSet()
file := srctesting.Parse(t, fset, src)
info, pkg := srctesting.Check(t, fset, file)
f := srctesting.New(t)
file := f.Parse("test.go", src)
info, pkg := f.Check("pkg/test", file)

lookupObj := func(name string) types.Object {
return srctesting.LookupObj(pkg, name)
Expand Down Expand Up @@ -280,9 +279,9 @@ func TestSeedVisitor(t *testing.T) {
func e() { var _ typ[int64] }
`

fset := token.NewFileSet()
file := srctesting.Parse(t, fset, src)
info, pkg := srctesting.Check(t, fset, file)
f := srctesting.New(t)
file := f.Parse("test.go", src)
info, pkg := f.Check("pkg/test", file)

sv := seedVisitor{
visitor: visitor{
Expand Down Expand Up @@ -343,9 +342,9 @@ func TestCollector(t *testing.T) {
}
`

fset := token.NewFileSet()
file := srctesting.Parse(t, fset, src)
info, pkg := srctesting.Check(t, fset, file)
f := srctesting.New(t)
file := f.Parse("test.go", src)
info, pkg := f.Check("pkg/test", file)

c := Collector{
TContext: types.NewContext(),
Expand Down Expand Up @@ -396,7 +395,7 @@ func TestResolver_SubstituteSelection(t *testing.T) {
func (_ g[T]) Method(t T) string {
return t.String()
}`,
wantObj: "func (test.x).String() string",
wantObj: "func (pkg/test.x).String() string",
wantSig: "func() string",
}, {
descr: "generic receiver type with type parameter",
Expand All @@ -407,8 +406,8 @@ func TestResolver_SubstituteSelection(t *testing.T) {
func (_ g[T]) Method(t T) string {
return g[T]{}.Method(t)
}`,
wantObj: "func (test.g[test.x]).Method(t test.x) string",
wantSig: "func(t test.x) string",
wantObj: "func (pkg/test.g[pkg/test.x]).Method(t pkg/test.x) string",
wantSig: "func(t pkg/test.x) string",
}, {
descr: "method expression",
src: `package test
Expand All @@ -418,15 +417,15 @@ func TestResolver_SubstituteSelection(t *testing.T) {
func (recv g[T]) Method(t T) string {
return g[T].Method(recv, t)
}`,
wantObj: "func (test.g[test.x]).Method(t test.x) string",
wantSig: "func(recv test.g[test.x], t test.x) string",
wantObj: "func (pkg/test.g[pkg/test.x]).Method(t pkg/test.x) string",
wantSig: "func(recv pkg/test.g[pkg/test.x], t pkg/test.x) string",
}}

for _, test := range tests {
t.Run(test.descr, func(t *testing.T) {
fset := token.NewFileSet()
file := srctesting.Parse(t, fset, test.src)
info, pkg := srctesting.Check(t, fset, file)
f := srctesting.New(t)
file := f.Parse("test.go", test.src)
info, pkg := f.Check("pkg/test", file)

method := srctesting.LookupObj(pkg, "g.Method").(*types.Func).Type().(*types.Signature)
resolver := NewResolver(nil, ToSlice(method.RecvTypeParams()), []types.Type{srctesting.LookupObj(pkg, "x").Type()})
Expand Down
25 changes: 12 additions & 13 deletions compiler/internal/typeparams/instance_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package typeparams

import (
"go/token"
"go/types"
"testing"

Expand Down Expand Up @@ -38,8 +37,8 @@ func TestInstanceString(t *testing.T) {
func Fun[U any, W any](x, y U) {}
func fun[U any, W any](x, y U) {}
`
fset := token.NewFileSet()
_, pkg := srctesting.Check(t, fset, srctesting.Parse(t, fset, src))
f := srctesting.New(t)
_, pkg := f.Check("pkg/test", f.Parse("test.go", src))
mustType := testingx.Must[types.Type](t)

tests := []struct {
Expand All @@ -53,50 +52,50 @@ func TestInstanceString(t *testing.T) {
Object: pkg.Scope().Lookup("Typ"),
TArgs: []types.Type{types.Typ[types.Int], types.Typ[types.String]},
},
wantStr: "test.Typ<int, string>",
wantStr: "pkg/test.Typ<int, string>",
wantTypeString: "testcase.Typ[int, string]",
}, {
descr: "exported method",
instance: Instance{
Object: pkg.Scope().Lookup("Typ").Type().(*types.Named).Method(0),
TArgs: []types.Type{types.Typ[types.Int], types.Typ[types.String]},
},
wantStr: "test.Typ.Method<int, string>",
wantStr: "pkg/test.Typ.Method<int, string>",
}, {
descr: "exported function",
instance: Instance{
Object: pkg.Scope().Lookup("Fun"),
TArgs: []types.Type{types.Typ[types.Int], types.Typ[types.String]},
},
wantStr: "test.Fun<int, string>",
wantStr: "pkg/test.Fun<int, string>",
}, {
descr: "unexported type",
instance: Instance{
Object: pkg.Scope().Lookup("typ"),
TArgs: []types.Type{types.Typ[types.Int], types.Typ[types.String]},
},
wantStr: "test.typ<int, string>",
wantStr: "pkg/test.typ<int, string>",
wantTypeString: "testcase.typ[int, string]",
}, {
descr: "unexported method",
instance: Instance{
Object: pkg.Scope().Lookup("typ").Type().(*types.Named).Method(0),
TArgs: []types.Type{types.Typ[types.Int], types.Typ[types.String]},
},
wantStr: "test.typ.method<int, string>",
wantStr: "pkg/test.typ.method<int, string>",
}, {
descr: "unexported function",
instance: Instance{
Object: pkg.Scope().Lookup("fun"),
TArgs: []types.Type{types.Typ[types.Int], types.Typ[types.String]},
},
wantStr: "test.fun<int, string>",
wantStr: "pkg/test.fun<int, string>",
}, {
descr: "no type params",
instance: Instance{
Object: pkg.Scope().Lookup("Ints"),
},
wantStr: "test.Ints",
wantStr: "pkg/test.Ints",
wantTypeString: "testcase.Ints",
}, {
descr: "complex parameter type",
Expand All @@ -110,7 +109,7 @@ func TestInstanceString(t *testing.T) {
}, true)),
},
},
wantStr: "test.fun<[]int, test.typ[int, string]>",
wantStr: "pkg/test.fun<[]int, pkg/test.typ[int, string]>",
}}

for _, test := range tests {
Expand All @@ -134,8 +133,8 @@ func TestInstanceQueue(t *testing.T) {
type Typ[T any, V any] []T
func Fun[U any, W any](x, y U) {}
`
fset := token.NewFileSet()
_, pkg := srctesting.Check(t, fset, srctesting.Parse(t, fset, src))
f := srctesting.New(t)
_, pkg := f.Check("pkg/test", f.Parse("test.go", src))

i1 := Instance{
Object: pkg.Scope().Lookup("Typ"),
Expand Down
5 changes: 2 additions & 3 deletions compiler/typesutil/typenames_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package typesutil

import (
"go/token"
"go/types"
"testing"

Expand All @@ -24,8 +23,8 @@ func TestTypeNames(t *testing.T) {
type B int
type C int
`
fset := token.NewFileSet()
_, pkg := srctesting.Check(t, fset, srctesting.Parse(t, fset, src))
f := srctesting.New(t)
_, pkg := f.Check("pkg/test", f.Parse("test.go", src))
A := srctesting.LookupObj(pkg, "A").(*types.TypeName)
B := srctesting.LookupObj(pkg, "B").(*types.TypeName)
C := srctesting.LookupObj(pkg, "C").(*types.TypeName)
Expand Down
Loading