1 // Copyright 2013 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // No testdata on Android.
16 "golang.org/x/tools/go/loader"
17 "golang.org/x/tools/go/ssa"
18 "golang.org/x/tools/go/ssa/ssautil"
21 func TestSwitches(t *testing.T) {
22 conf := loader.Config{ParserMode: parser.ParseComments}
23 f, err := conf.ParseFile("testdata/switches.go", nil)
29 conf.CreateFromFiles("main", f)
30 iprog, err := conf.Load()
36 prog := ssautil.CreateProgram(iprog, 0)
37 mainPkg := prog.Package(iprog.Created[0].Pkg)
40 for _, mem := range mainPkg.Members {
41 if fn, ok := mem.(*ssa.Function); ok {
42 if fn.Synthetic != "" {
43 continue // e.g. init()
45 // Each (multi-line) "switch" comment within
46 // this function must match the printed form
48 var wantSwitches []string
49 for _, c := range f.Comments {
50 if fn.Syntax().Pos() <= c.Pos() && c.Pos() < fn.Syntax().End() {
51 text := strings.TrimSpace(c.Text())
52 if strings.HasPrefix(text, "switch ") {
53 wantSwitches = append(wantSwitches, text)
58 switches := ssautil.Switches(fn)
59 if len(switches) != len(wantSwitches) {
60 t.Errorf("in %s, found %d switches, want %d", fn, len(switches), len(wantSwitches))
62 for i, sw := range switches {
64 if i >= len(wantSwitches) {
67 want := wantSwitches[i]
69 t.Errorf("in %s, found switch %d: got <<%s>>, want <<%s>>", fn, i, got, want)