github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/tools/cmd/callgraph/main_test.go (about) 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 9 package main 10 11 import ( 12 "bytes" 13 "fmt" 14 "go/build" 15 "reflect" 16 "sort" 17 "strings" 18 "testing" 19 ) 20 21 func TestCallgraph(t *testing.T) { 22 ctxt := build.Default // copy 23 ctxt.GOPATH = "testdata" 24 25 const format = "{{.Caller}} --> {{.Callee}}" 26 27 for _, test := range []struct { 28 algo, format string 29 tests bool 30 want []string 31 }{ 32 {"rta", format, false, []string{ 33 // rta imprecisely shows cross product of {main,main2} x {C,D} 34 `pkg.main --> (pkg.C).f`, 35 `pkg.main --> (pkg.D).f`, 36 `pkg.main --> pkg.main2`, 37 `pkg.main2 --> (pkg.C).f`, 38 `pkg.main2 --> (pkg.D).f`, 39 }}, 40 {"pta", format, false, []string{ 41 // pta distinguishes main->C, main2->D. Also has a root node. 42 `<root> --> pkg.init`, 43 `<root> --> pkg.main`, 44 `pkg.main --> (pkg.C).f`, 45 `pkg.main --> pkg.main2`, 46 `pkg.main2 --> (pkg.D).f`, 47 }}, 48 // tests: main is not called. 49 {"rta", format, true, []string{ 50 `pkg.Example --> (pkg.C).f`, 51 `test$main.init --> pkg.init`, 52 }}, 53 {"pta", format, true, []string{ 54 `<root> --> pkg.Example`, 55 `<root> --> test$main.init`, 56 `pkg.Example --> (pkg.C).f`, 57 `test$main.init --> pkg.init`, 58 }}, 59 } { 60 stdout = new(bytes.Buffer) 61 if err := doCallgraph(&ctxt, test.algo, test.format, test.tests, []string{"pkg"}); err != nil { 62 t.Error(err) 63 continue 64 } 65 66 got := sortedLines(fmt.Sprint(stdout)) 67 if !reflect.DeepEqual(got, test.want) { 68 t.Errorf("callgraph(%q, %q, %t):\ngot:\n%s\nwant:\n%s", 69 test.algo, test.format, test.tests, 70 strings.Join(got, "\n"), 71 strings.Join(test.want, "\n")) 72 } 73 } 74 } 75 76 func sortedLines(s string) []string { 77 s = strings.TrimSpace(s) 78 lines := strings.Split(s, "\n") 79 sort.Strings(lines) 80 return lines 81 }