Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / honnef.co / go / tools@v0.0.1-2020.1.5 / 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         "golang.org/x/tools/go/loader"
19         "honnef.co/go/tools/ir"
20 )
21
22 func TestSwitches(t *testing.T) {
23         conf := loader.Config{ParserMode: parser.ParseComments}
24         f, err := conf.ParseFile("testdata/switches.go", nil)
25         if err != nil {
26                 t.Error(err)
27                 return
28         }
29
30         conf.CreateFromFiles("main", f)
31         iprog, err := conf.Load()
32         if err != nil {
33                 t.Error(err)
34                 return
35         }
36
37         prog := CreateProgram(iprog, 0)
38         mainPkg := prog.Package(iprog.Created[0].Pkg)
39         mainPkg.Build()
40
41         for _, mem := range mainPkg.Members {
42                 if fn, ok := mem.(*ir.Function); ok {
43                         if fn.Synthetic != "" {
44                                 continue // e.g. init()
45                         }
46                         // Each (multi-line) "switch" comment within
47                         // this function must match the printed form
48                         // of a ConstSwitch.
49                         var wantSwitches []string
50                         for _, c := range f.Comments {
51                                 if fn.Source().Pos() <= c.Pos() && c.Pos() < fn.Source().End() {
52                                         text := strings.TrimSpace(c.Text())
53                                         if strings.HasPrefix(text, "switch ") {
54                                                 wantSwitches = append(wantSwitches, text)
55                                         }
56                                 }
57                         }
58
59                         switches := Switches(fn)
60                         if len(switches) != len(wantSwitches) {
61                                 t.Errorf("in %s, found %d switches, want %d", fn, len(switches), len(wantSwitches))
62                         }
63                         for i, sw := range switches {
64                                 got := sw.testString()
65                                 if i >= len(wantSwitches) {
66                                         continue
67                                 }
68                                 want := wantSwitches[i]
69                                 if got != want {
70                                         t.Errorf("in %s, found switch %d: got <<%s>>, want <<%s>>", fn, i, got, want)
71                                 }
72                         }
73                 }
74         }
75 }
76
77 func (sw *Switch) testString() string {
78         // same as the actual String method, but use the second to last
79         // instruction instead, to skip over all the phi and sigma nodes
80         // that SSI produces.
81         var buf bytes.Buffer
82         if sw.ConstCases != nil {
83                 fmt.Fprintf(&buf, "switch %s {\n", sw.X.Name())
84                 for _, c := range sw.ConstCases {
85                         n := len(c.Body.Instrs) - 2
86                         if n < 0 {
87                                 n = 0
88                         }
89                         fmt.Fprintf(&buf, "case %s: %s\n", c.Value.Name(), c.Body.Instrs[n])
90                 }
91         } else {
92                 fmt.Fprintf(&buf, "switch %s.(type) {\n", sw.X.Name())
93                 for _, c := range sw.TypeCases {
94                         n := len(c.Body.Instrs) - 2
95                         if n < 0 {
96                                 n = 0
97                         }
98                         fmt.Fprintf(&buf, "case %s %s: %s\n",
99                                 c.Binding.Name(), c.Type, c.Body.Instrs[n])
100                 }
101         }
102         if sw.Default != nil {
103                 n := len(sw.Default.Instrs) - 2
104                 if n < 0 {
105                         n = 0
106                 }
107                 fmt.Fprintf(&buf, "default: %s\n", sw.Default.Instrs[n])
108         }
109         fmt.Fprintf(&buf, "}")
110         return buf.String()
111 }