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.
17 "golang.org/x/tools/go/loader"
18 "golang.org/x/tools/go/ssa"
19 "golang.org/x/tools/go/ssa/ssautil"
22 func TestSwitches(t *testing.T) {
23 conf := loader.Config{ParserMode: parser.ParseComments}
24 f, err := conf.ParseFile("testdata/switches.go", nil)
30 conf.CreateFromFiles("main", f)
31 iprog, err := conf.Load()
37 prog := ssautil.CreateProgram(iprog, 0)
38 mainPkg := prog.Package(iprog.Created[0].Pkg)
41 for _, mem := range mainPkg.Members {
42 if fn, ok := mem.(*ssa.Function); ok {
43 if fn.Synthetic != "" {
44 continue // e.g. init()
46 // Each (multi-line) "switch" comment within
47 // this function must match the printed form
49 var wantSwitches []string
50 for _, c := range f.Comments {
51 if fn.Syntax().Pos() <= c.Pos() && c.Pos() < fn.Syntax().End() {
52 text := strings.TrimSpace(c.Text())
53 if strings.HasPrefix(text, "switch ") {
54 wantSwitches = append(wantSwitches, text)
59 switches := ssautil.Switches(fn)
60 if len(switches) != len(wantSwitches) {
61 t.Errorf("in %s, found %d switches, want %d", fn, len(switches), len(wantSwitches))
63 for i, sw := range switches {
65 if i >= len(wantSwitches) {
68 want := wantSwitches[i]
70 t.Errorf("in %s, found switch %d: got <<%s>>, want <<%s>>", fn, i, got, want)