GoPLS Viewer

Home|gopls/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
10package ssautil_test
11
12import (
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
22func TestSwitches(t *testing.T) {
23    conf := loader.Config{ParserModeparser.ParseComments}
24    ferr := conf.ParseFile("testdata/switches.go"nil)
25    if err != nil {
26        t.Error(err)
27        return
28    }
29
30    conf.CreateFromFiles("main"f)
31    iprogerr := conf.Load()
32    if err != nil {
33        t.Error(err)
34        return
35    }
36
37    prog := ssautil.CreateProgram(iprogssa.BuilderMode(0))
38    mainPkg := prog.Package(iprog.Created[0].Pkg)
39    mainPkg.Build()
40
41    for _mem := range mainPkg.Members {
42        if fnok := 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(wantSwitchestext)
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"fnlen(switches), len(wantSwitches))
62            }
63            for isw := 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>>"fnigotwant)
71                }
72            }
73        }
74    }
75}
76
MembersX
TestSwitches.iprog
TestSwitches.RangeStmt_817.BlockStmt.BlockStmt.RangeStmt_1536.BlockStmt.got
TestSwitches.prog
TestSwitches.RangeStmt_817.mem
TestSwitches.RangeStmt_817.BlockStmt.BlockStmt.wantSwitches
TestSwitches.t
TestSwitches.conf
TestSwitches.f
TestSwitches.RangeStmt_817.BlockStmt.BlockStmt.switches
TestSwitches
TestSwitches.err
TestSwitches.RangeStmt_817.BlockStmt.BlockStmt.RangeStmt_1108.BlockStmt.BlockStmt.text
TestSwitches.RangeStmt_817.BlockStmt.BlockStmt.RangeStmt_1536.sw
TestSwitches.mainPkg
TestSwitches.RangeStmt_817.BlockStmt.BlockStmt.RangeStmt_1108.c
TestSwitches.RangeStmt_817.BlockStmt.BlockStmt.RangeStmt_1536.i
Members
X