Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / golang.org / x / tools@v0.0.0-20201105173854-bc9fc8d8c4bc / cmd / callgraph / main_test.go
1 // Copyright 2014 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 // +build go1.11
9
10 package main
11
12 import (
13         "bytes"
14         "fmt"
15         "log"
16         "os"
17         "path/filepath"
18         "strings"
19         "testing"
20
21         "golang.org/x/tools/internal/testenv"
22 )
23
24 func init() {
25         // This test currently requires GOPATH mode.
26         // Explicitly disabling module mode should suffix, but
27         // we'll also turn off GOPROXY just for good measure.
28         if err := os.Setenv("GO111MODULE", "off"); err != nil {
29                 log.Fatal(err)
30         }
31         if err := os.Setenv("GOPROXY", "off"); err != nil {
32                 log.Fatal(err)
33         }
34 }
35
36 func TestCallgraph(t *testing.T) {
37         testenv.NeedsTool(t, "go")
38
39         gopath, err := filepath.Abs("testdata")
40         if err != nil {
41                 t.Fatal(err)
42         }
43
44         for _, test := range []struct {
45                 algo  string
46                 tests bool
47                 want  []string
48         }{
49                 {"rta", false, []string{
50                         // rta imprecisely shows cross product of {main,main2} x {C,D}
51                         `pkg.main --> (pkg.C).f`,
52                         `pkg.main --> (pkg.D).f`,
53                         `pkg.main --> pkg.main2`,
54                         `pkg.main2 --> (pkg.C).f`,
55                         `pkg.main2 --> (pkg.D).f`,
56                 }},
57                 {"pta", false, []string{
58                         // pta distinguishes main->C, main2->D.  Also has a root node.
59                         `<root> --> pkg.init`,
60                         `<root> --> pkg.main`,
61                         `pkg.main --> (pkg.C).f`,
62                         `pkg.main --> pkg.main2`,
63                         `pkg.main2 --> (pkg.D).f`,
64                 }},
65                 // tests: both the package's main and the test's main are called.
66                 // The callgraph includes all the guts of the "testing" package.
67                 {"rta", true, []string{
68                         `pkg.test.main --> testing.MainStart`,
69                         `testing.runExample --> pkg.Example`,
70                         `pkg.Example --> (pkg.C).f`,
71                         `pkg.main --> (pkg.C).f`,
72                 }},
73                 {"pta", true, []string{
74                         `<root> --> pkg.test.main`,
75                         `<root> --> pkg.main`,
76                         `pkg.test.main --> testing.MainStart`,
77                         `testing.runExample --> pkg.Example`,
78                         `pkg.Example --> (pkg.C).f`,
79                         `pkg.main --> (pkg.C).f`,
80                 }},
81         } {
82                 const format = "{{.Caller}} --> {{.Callee}}"
83                 stdout = new(bytes.Buffer)
84                 if err := doCallgraph("testdata/src", gopath, test.algo, format, test.tests, []string{"pkg"}); err != nil {
85                         t.Error(err)
86                         continue
87                 }
88
89                 edges := make(map[string]bool)
90                 for _, line := range strings.Split(fmt.Sprint(stdout), "\n") {
91                         edges[line] = true
92                 }
93                 for _, edge := range test.want {
94                         if !edges[edge] {
95                                 t.Errorf("callgraph(%q, %t): missing edge: %s",
96                                         test.algo, test.tests, edge)
97                         }
98                 }
99                 if t.Failed() {
100                         t.Log("got:\n", stdout)
101                 }
102         }
103 }