github.com/llvm-mirror/llgo@v0.0.0-20190322182713-bf6f0a60fce1/third_party/gotools/go/ssa/ssautil/switch_test.go (about) 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 package ssautil_test 6 7 import ( 8 "go/parser" 9 "strings" 10 "testing" 11 12 "llvm.org/llgo/third_party/gotools/go/loader" 13 "llvm.org/llgo/third_party/gotools/go/ssa" 14 "llvm.org/llgo/third_party/gotools/go/ssa/ssautil" 15 ) 16 17 func TestSwitches(t *testing.T) { 18 conf := loader.Config{ParserMode: parser.ParseComments} 19 f, err := conf.ParseFile("testdata/switches.go", nil) 20 if err != nil { 21 t.Error(err) 22 return 23 } 24 25 conf.CreateFromFiles("main", f) 26 iprog, err := conf.Load() 27 if err != nil { 28 t.Error(err) 29 return 30 } 31 32 prog := ssa.Create(iprog, 0) 33 mainPkg := prog.Package(iprog.Created[0].Pkg) 34 mainPkg.Build() 35 36 for _, mem := range mainPkg.Members { 37 if fn, ok := mem.(*ssa.Function); ok { 38 if fn.Synthetic != "" { 39 continue // e.g. init() 40 } 41 // Each (multi-line) "switch" comment within 42 // this function must match the printed form 43 // of a ConstSwitch. 44 var wantSwitches []string 45 for _, c := range f.Comments { 46 if fn.Syntax().Pos() <= c.Pos() && c.Pos() < fn.Syntax().End() { 47 text := strings.TrimSpace(c.Text()) 48 if strings.HasPrefix(text, "switch ") { 49 wantSwitches = append(wantSwitches, text) 50 } 51 } 52 } 53 54 switches := ssautil.Switches(fn) 55 if len(switches) != len(wantSwitches) { 56 t.Errorf("in %s, found %d switches, want %d", fn, len(switches), len(wantSwitches)) 57 } 58 for i, sw := range switches { 59 got := sw.String() 60 if i >= len(wantSwitches) { 61 continue 62 } 63 want := wantSwitches[i] 64 if got != want { 65 t.Errorf("in %s, found switch %d: got <<%s>>, want <<%s>>", fn, i, got, want) 66 } 67 } 68 } 69 } 70 }