10
10
package ssautil_test
11
11
12
12
import (
13
+ "go/ast"
13
14
"go/parser"
15
+ "path/filepath"
14
16
"strings"
15
17
"testing"
16
18
17
19
"golang.org/x/tools/go/loader"
18
20
"golang.org/x/tools/go/ssa"
19
21
"golang.org/x/tools/go/ssa/ssautil"
22
+ "golang.org/x/tools/internal/testfiles"
23
+ "golang.org/x/tools/txtar"
20
24
)
21
25
22
26
func TestSwitches (t * testing.T ) {
27
+ archive , err := txtar .ParseFile ("testdata/switches.txtar" )
28
+ if err != nil {
29
+ t .Fatal (err )
30
+ }
31
+ ppkgs := testfiles .LoadPackages (t , archive , "." )
32
+ if len (ppkgs ) != 1 {
33
+ t .Fatalf ("Expected to load one package but got %d" , len (ppkgs ))
34
+ }
35
+
36
+ prog , _ := ssautil .Packages (ppkgs , ssa .BuilderMode (0 ))
37
+ mainPkg := prog .Package (ppkgs [0 ].Types )
38
+ mainPkg .Build ()
39
+ testSwitches (t , ppkgs [0 ].Syntax [0 ], mainPkg )
40
+ }
41
+
42
+ // TestCreateProgram uses loader and ssautil.CreateProgram to create an *ssa.Program.
43
+ // It has the same testing logic with TestSwitches.
44
+ // CreateProgram is deprecated, but it is a part of the public API.
45
+ // For now keep a test that exercises it.
46
+ func TestCreateProgram (t * testing.T ) {
47
+ dir := testfiles .ExtractTxtarFileToTmp (t , "testdata/switches.txtar" )
23
48
conf := loader.Config {ParserMode : parser .ParseComments }
24
- f , err := conf .ParseFile ("testdata/ switches.go" , nil )
49
+ f , err := conf .ParseFile (filepath . Join ( dir , " switches.go") , nil )
25
50
if err != nil {
26
51
t .Error (err )
27
52
return
@@ -33,11 +58,13 @@ func TestSwitches(t *testing.T) {
33
58
t .Error (err )
34
59
return
35
60
}
36
-
37
61
prog := ssautil .CreateProgram (iprog , ssa .BuilderMode (0 ))
38
62
mainPkg := prog .Package (iprog .Created [0 ].Pkg )
39
63
mainPkg .Build ()
64
+ testSwitches (t , f , mainPkg )
65
+ }
40
66
67
+ func testSwitches (t * testing.T , f * ast.File , mainPkg * ssa.Package ) {
41
68
for _ , mem := range mainPkg .Members {
42
69
if fn , ok := mem .(* ssa.Function ); ok {
43
70
if fn .Synthetic != "" {
0 commit comments