Skip to content

Commit a859e27

Browse files
committed
merge two test cases and check possible errors
1 parent deda7b7 commit a859e27

File tree

3 files changed

+46
-140
lines changed

3 files changed

+46
-140
lines changed

go/callgraph/rta/rta_test.go

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,52 @@ import (
2727
"golang.org/x/tools/txtar"
2828
)
2929

30-
// TestRTASingleFile runs RTA on each testdata/*.txtar file containing a single
31-
// go file and compares the results with the expectations expressed in the WANT
30+
// TestRTA runs RTA on each testdata/*.txtar file containing a single
31+
// go file in a single package or multiple files in different packages,
32+
// and compares the results with the expectations expressed in the WANT
3233
// comment.
33-
func TestRTASingleFile(t *testing.T) {
34+
func TestRTA(t *testing.T) {
3435
archivePaths := []string{
3536
"testdata/func.txtar",
36-
"testdata/pkgmaingenerics.txtar",
3737
"testdata/generics.txtar",
3838
"testdata/iface.txtar",
3939
"testdata/reflectcall.txtar",
4040
"testdata/rtype.txtar",
41+
"testdata/multipkgs.txtar",
4142
}
4243
for _, archive := range archivePaths {
4344
t.Run(archive, func(t *testing.T) {
4445
pkgs := loadPackages(t, archive)
4546

46-
f := pkgs[0].Syntax[0]
47+
// find the file which contains the expected result
48+
var f *ast.File
49+
for _, p := range pkgs {
50+
// We assume the packages have a single file or
51+
// the wanted result is in the first file of the main package.
52+
if p.Name == "main" {
53+
f = p.Syntax[0]
54+
}
55+
}
56+
if f == nil {
57+
t.Fatalf("failed to find the file with expected result within main package %s", archive)
58+
}
59+
60+
prog, spkgs := ssautil.Packages(pkgs, ssa.SanityCheckFunctions|ssa.InstantiateGenerics)
61+
62+
// find the main package to get functions for rta analysis
63+
var mainPkg *ssa.Package
64+
for _, sp := range spkgs {
65+
if sp.Pkg.Name() == "main" {
66+
mainPkg = sp
67+
break
68+
}
69+
}
70+
if mainPkg == nil {
71+
t.Fatalf("failed to find main ssa package %s", archive)
72+
}
4773

48-
prog, spkg := ssautil.Packages(pkgs, ssa.SanityCheckFunctions|ssa.InstantiateGenerics)
4974
prog.Build()
50-
mainPkg := spkg[0]
75+
5176
res := rta.Analyze([]*ssa.Function{
5277
mainPkg.Func("main"),
5378
mainPkg.Func("init"),
@@ -58,38 +83,6 @@ func TestRTASingleFile(t *testing.T) {
5883
}
5984
}
6085

61-
// TestRTAOnPackages runs RTA on a go module which contains multiple packages to test the case
62-
// when an interface has implementations across different packages.
63-
func TestRTAOnPackages(t *testing.T) {
64-
pkgs := loadPackages(t, "testdata/multipkgs.txtar")
65-
66-
var f *ast.File
67-
for _, p := range pkgs {
68-
// We assume the packages have a single file or
69-
// the wanted result is in the first file of the main package.
70-
if p.Name == "main" {
71-
f = p.Syntax[0]
72-
}
73-
}
74-
75-
prog, spkgs := ssautil.Packages(pkgs, ssa.SanityCheckFunctions|ssa.InstantiateGenerics)
76-
prog.Build()
77-
var mainPkg *ssa.Package
78-
for _, sp := range spkgs {
79-
if sp.Pkg.Name() == "main" {
80-
mainPkg = sp
81-
break
82-
}
83-
}
84-
85-
res := rta.Analyze([]*ssa.Function{
86-
mainPkg.Func("main"),
87-
mainPkg.Func("init"),
88-
}, true)
89-
90-
check(t, f, mainPkg, res)
91-
}
92-
9386
// loadPackages unpacks the archive to a temporary directory and loads all packages within it.
9487
func loadPackages(t *testing.T, archive string) []*packages.Package {
9588
ar, err := txtar.ParseFile(archive)

go/callgraph/rta/testdata/generics.txtar

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
-- go.mod --
2-
module example.com/generic
2+
module example.com
33
go 1.18
44

55
-- generics.go --
6-
package generic
6+
package main
77

88
// Test of generic function calls.
99

@@ -51,34 +51,31 @@ func lambda[X I]() func() func() {
5151
}
5252
}
5353

54-
// Note: example.com/generic is used here as we load a single file by packages.Load,
55-
// it will use example.com/generic instead of the package name for ImportedPath
56-
5754
// WANT:
5855
//
5956
// edge (*C).Foo --static method call--> (C).Foo
6057
// edge (A).Foo$bound --static method call--> (A).Foo
61-
// edge instantiated[example.com/generic.A] --static method call--> (A).Foo
62-
// edge instantiated[example.com/generic.B] --static method call--> (B).Foo
58+
// edge instantiated[example.com.A] --static method call--> (A).Foo
59+
// edge instantiated[example.com.B] --static method call--> (B).Foo
6360
// edge main --dynamic method call--> (*C).Foo
6461
// edge main --dynamic function call--> (A).Foo$bound
6562
// edge main --dynamic method call--> (C).Foo
66-
// edge main --static function call--> instantiated[example.com/generic.A]
67-
// edge main --static function call--> instantiated[example.com/generic.B]
68-
// edge main --static function call--> lambda[example.com/generic.A]
69-
// edge main --dynamic function call--> lambda[example.com/generic.A]$1
70-
// edge main --static function call--> local[example.com/generic.C]
63+
// edge main --static function call--> instantiated[example.com.A]
64+
// edge main --static function call--> instantiated[example.com.B]
65+
// edge main --static function call--> lambda[example.com.A]
66+
// edge main --dynamic function call--> lambda[example.com.A]$1
67+
// edge main --static function call--> local[example.com.C]
7168
//
7269
// reachable (*C).Foo
7370
// reachable (A).Foo
7471
// reachable (A).Foo$bound
7572
// reachable (B).Foo
7673
// reachable (C).Foo
77-
// reachable instantiated[example.com/generic.A]
78-
// reachable instantiated[example.com/generic.B]
79-
// reachable lambda[example.com/generic.A]
80-
// reachable lambda[example.com/generic.A]$1
81-
// reachable local[example.com/generic.C]
74+
// reachable instantiated[example.com.A]
75+
// reachable instantiated[example.com.B]
76+
// reachable lambda[example.com.A]
77+
// reachable lambda[example.com.A]$1
78+
// reachable local[example.com.C]
8279
//
8380
// rtype *C
8481
// rtype C

go/callgraph/rta/testdata/pkgmaingenerics.txtar

Lines changed: 0 additions & 84 deletions
This file was deleted.

0 commit comments

Comments
 (0)