.gitignore added
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / honnef.co / go / tools@v0.1.1 / go / ir / irutil / switch_test.go
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.
4
5 // No testdata on Android.
6
7 // +build !android
8
9 package irutil
10
11 import (
12         "bytes"
13         "fmt"
14         "go/parser"
15         "strings"
16         "testing"
17
18         "honnef.co/go/tools/go/ir"
19
20         "golang.org/x/tools/go/loader"
21 )
22
23 func TestSwitches(t *testing.T) {
24         conf := loader.Config{ParserMode: parser.ParseComments}
25         f, err := conf.ParseFile("testdata/switches.go", nil)
26         if err != nil {
27                 t.Error(err)
28                 return
29         }
30
31         conf.CreateFromFiles("main", f)
32         iprog, err := conf.Load()
33         if err != nil {
34                 t.Error(err)
35                 return
36         }
37
38         prog := CreateProgram(iprog, 0)
39         mainPkg := prog.Package(iprog.Created[0].Pkg)
40         mainPkg.Build()
41
42         for _, mem := range mainPkg.Members {
43                 if fn, ok := mem.(*ir.Function); ok {
44                         if fn.Synthetic != 0 {
45                                 continue // e.g. init()
46                         }
47                         // Each (multi-line) "switch" comment within
48                         // this function must match the printed form
49                         // of a ConstSwitch.
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)
56                                         }
57                                 }
58                         }
59
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))
63                         }
64                         for i, sw := range switches {
65                                 got := sw.testString()
66                                 if i >= len(wantSwitches) {
67                                         continue
68                                 }
69                                 want := wantSwitches[i]
70                                 if got != want {
71                                         t.Errorf("in %s, found switch %d: got <<%s>>, want <<%s>>", fn, i, got, want)
72                                 }
73                         }
74                 }
75         }
76 }
77
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
81         // that SSI produces.
82         var buf bytes.Buffer
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
87                         if n < 0 {
88                                 n = 0
89                         }
90                         fmt.Fprintf(&buf, "case %s: %s\n", c.Value.Name(), c.Body.Instrs[n])
91                 }
92         } else {
93                 fmt.Fprintf(&buf, "switch %s.(type) {\n", sw.X.Name())
94                 for _, c := range sw.TypeCases {
95                         n := len(c.Body.Instrs) - 2
96                         if n < 0 {
97                                 n = 0
98                         }
99                         fmt.Fprintf(&buf, "case %s %s: %s\n",
100                                 c.Binding.Name(), c.Type, c.Body.Instrs[n])
101                 }
102         }
103         if sw.Default != nil {
104                 n := len(sw.Default.Instrs) - 2
105                 if n < 0 {
106                         n = 0
107                 }
108                 fmt.Fprintf(&buf, "default: %s\n", sw.Default.Instrs[n])
109         }
110         fmt.Fprintf(&buf, "}")
111         return buf.String()
112 }