| 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 | |
| 9 | package ssautil_testscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil_test`/ |
| 10 | |
| 11 | import ( |
| 12 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 13 | "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/" |
| 14 | |
| 15 | "golang.org/x/tools/go/ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/" |
| 16 | "golang.org/x/tools/go/ssa/ssautilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/" |
| 17 | "golang.org/x/tools/internal/testfilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testfiles`/" |
| 18 | "golang.org/x/tools/txtarscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/" |
| 19 | ) |
| 20 | |
| 21 | func TestSwitchesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil_test`/TestSwitches().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 22 | archive, err := txtarscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/.ParseFilescip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/ParseFile().("testdata/switches.txtar") |
| 23 | if err != nil { |
| 24 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 25 | } |
| 26 | ppkgs := testfilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testfiles`/.LoadPackagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testfiles`/LoadPackages().(t, archive, ".") |
| 27 | if len(ppkgs) != 1 { |
| 28 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Expected to load one package but got %d", len(ppkgs)) |
| 29 | } |
| 30 | f := ppkgs[0].Syntaxscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#Syntax.[0] |
| 31 | |
| 32 | prog, _ := ssautilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/.Packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/Packages().(ppkgs, ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.BuilderModescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BuilderMode#(0)) |
| 33 | mainPkg := prog.Packagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Program#Package().(ppkgs[0].Typesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#Types.) |
| 34 | mainPkg.Buildscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#Build().() |
| 35 | |
| 36 | for _, mem := range mainPkg.Membersscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#Members. { |
| 37 | if fn, ok := mem.(*ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#); ok { |
| 38 | if fn.Syntheticscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#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.Commentsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Comments. { |
| 46 | if fn.Syntaxscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Syntax().().Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().() <= c.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CommentGroup#Pos().() && c.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CommentGroup#Pos().() < fn.Syntaxscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Syntax().().Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#End().() { |
| 47 | text := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.TrimSpacescip-go gomod github.com/golang/go/src go1.25.0 strings/TrimSpace().(c.Textscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CommentGroup#Text().()) |
| 48 | if stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasPrefix().(text, "switch ") { |
| 49 | wantSwitches = append(wantSwitches, text) |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | switches := ssautilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/.Switchesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/Switches().(fn) |
| 55 | if len(switches) != len(wantSwitches) { |
| 56 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("in %s, found %d switches, want %d", fn, len(switches), len(wantSwitches)) |
| 57 | } |
| 58 | for i, sw := range switches { |
| 59 | got := sw.Stringscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/Switch#String().() |
| 60 | if i >= len(wantSwitches) { |
| 61 | continue |
| 62 | } |
| 63 | want := wantSwitches[i] |
| 64 | if got != want { |
| 65 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("in %s, found switch %d: got <<%s>>, want <<%s>>", fn, i, got, want) |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | } |