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.
18 "honnef.co/go/tools/go/ir"
20 "golang.org/x/tools/go/loader"
23 func TestSwitches(t *testing.T) {
24 conf := loader.Config{ParserMode: parser.ParseComments}
25 f, err := conf.ParseFile("testdata/switches.go", nil)
31 conf.CreateFromFiles("main", f)
32 iprog, err := conf.Load()
38 prog := CreateProgram(iprog, 0)
39 mainPkg := prog.Package(iprog.Created[0].Pkg)
42 for _, mem := range mainPkg.Members {
43 if fn, ok := mem.(*ir.Function); ok {
44 if fn.Synthetic != 0 {
45 continue // e.g. init()
47 // Each (multi-line) "switch" comment within
48 // this function must match the printed form
50 var wantSwitches []string
51 for _, c := range f.Comments {
52 if fn.Source().Pos() <= c.Pos() && c.Pos() < fn.Source().End() {
53 text := strings.TrimSpace(c.Text())
54 if strings.HasPrefix(text, "switch ") {
55 wantSwitches = append(wantSwitches, text)
60 switches := Switches(fn)
61 if len(switches) != len(wantSwitches) {
62 t.Errorf("in %s, found %d switches, want %d", fn, len(switches), len(wantSwitches))
64 for i, sw := range switches {
65 got := sw.testString()
66 if i >= len(wantSwitches) {
69 want := wantSwitches[i]
71 t.Errorf("in %s, found switch %d: got <<%s>>, want <<%s>>", fn, i, got, want)
78 func (sw *Switch) testString() string {
79 // same as the actual String method, but use the second to last
80 // instruction instead, to skip over all the phi and sigma nodes
83 if sw.ConstCases != nil {
84 fmt.Fprintf(&buf, "switch %s {\n", sw.X.Name())
85 for _, c := range sw.ConstCases {
86 n := len(c.Body.Instrs) - 2
90 fmt.Fprintf(&buf, "case %s: %s\n", c.Value.Name(), c.Body.Instrs[n])
93 fmt.Fprintf(&buf, "switch %s.(type) {\n", sw.X.Name())
94 for _, c := range sw.TypeCases {
95 n := len(c.Body.Instrs) - 2
99 fmt.Fprintf(&buf, "case %s %s: %s\n",
100 c.Binding.Name(), c.Type, c.Body.Instrs[n])
103 if sw.Default != nil {
104 n := len(sw.Default.Instrs) - 2
108 fmt.Fprintf(&buf, "default: %s\n", sw.Default.Instrs[n])
110 fmt.Fprintf(&buf, "}")