.gitignore added
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / golang.org / x / tools@v0.1.1-0.20210319172145-bda8f5cee399 / go / ssa / ssautil / 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 //go:build !android
8 // +build !android
9
10 package ssautil_test
11
12 import (
13         "go/parser"
14         "strings"
15         "testing"
16
17         "golang.org/x/tools/go/loader"
18         "golang.org/x/tools/go/ssa"
19         "golang.org/x/tools/go/ssa/ssautil"
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 := ssautil.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.(*ssa.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.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)
55                                         }
56                                 }
57                         }
58
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))
62                         }
63                         for i, sw := range switches {
64                                 got := sw.String()
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 }