github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/tools/go/ssa/testmain_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 package ssa_test 6 7 // Tests of FindTests. CreateTestMainPackage is tested via the interpreter. 8 // TODO(adonovan): test the 'pkgs' result from FindTests. 9 10 import ( 11 "fmt" 12 "sort" 13 "testing" 14 15 "golang.org/x/tools/go/loader" 16 "golang.org/x/tools/go/ssa" 17 "golang.org/x/tools/go/ssa/ssautil" 18 ) 19 20 func create(t *testing.T, content string) []*ssa.Package { 21 var conf loader.Config 22 f, err := conf.ParseFile("foo_test.go", content) 23 if err != nil { 24 t.Fatal(err) 25 } 26 conf.CreateFromFiles("foo", f) 27 28 iprog, err := conf.Load() 29 if err != nil { 30 t.Fatal(err) 31 } 32 33 // We needn't call Build. 34 return ssautil.CreateProgram(iprog, ssa.SanityCheckFunctions).AllPackages() 35 } 36 37 func TestFindTests(t *testing.T) { 38 test := ` 39 package foo 40 41 import "testing" 42 43 type T int 44 45 // Tests: 46 func Test(t *testing.T) {} 47 func TestA(t *testing.T) {} 48 func TestB(t *testing.T) {} 49 50 // Not tests: 51 func testC(t *testing.T) {} 52 func TestD() {} 53 func testE(t *testing.T) int { return 0 } 54 func (T) Test(t *testing.T) {} 55 56 // Benchmarks: 57 func Benchmark(*testing.B) {} 58 func BenchmarkA(b *testing.B) {} 59 func BenchmarkB(*testing.B) {} 60 61 // Not benchmarks: 62 func benchmarkC(t *testing.T) {} 63 func BenchmarkD() {} 64 func benchmarkE(t *testing.T) int { return 0 } 65 func (T) Benchmark(t *testing.T) {} 66 67 // Examples: 68 func Example() {} 69 func ExampleA() {} 70 71 // Not examples: 72 func exampleC() {} 73 func ExampleD(t *testing.T) {} 74 func exampleE() int { return 0 } 75 func (T) Example() {} 76 ` 77 pkgs := create(t, test) 78 _, tests, benchmarks, examples := ssa.FindTests(pkgs) 79 80 sort.Sort(funcsByPos(tests)) 81 if got, want := fmt.Sprint(tests), "[foo.Test foo.TestA foo.TestB]"; got != want { 82 t.Errorf("FindTests.tests = %s, want %s", got, want) 83 } 84 85 sort.Sort(funcsByPos(benchmarks)) 86 if got, want := fmt.Sprint(benchmarks), "[foo.Benchmark foo.BenchmarkA foo.BenchmarkB]"; got != want { 87 t.Errorf("FindTests.benchmarks = %s, want %s", got, want) 88 } 89 90 sort.Sort(funcsByPos(examples)) 91 if got, want := fmt.Sprint(examples), "[foo.Example foo.ExampleA]"; got != want { 92 t.Errorf("FindTests examples = %s, want %s", got, want) 93 } 94 } 95 96 func TestFindTestsTesting(t *testing.T) { 97 test := ` 98 package foo 99 100 // foo does not import "testing", but defines Examples. 101 102 func Example() {} 103 func ExampleA() {} 104 ` 105 pkgs := create(t, test) 106 _, tests, benchmarks, examples := ssa.FindTests(pkgs) 107 if len(tests) > 0 { 108 t.Errorf("FindTests.tests = %s, want none", tests) 109 } 110 if len(benchmarks) > 0 { 111 t.Errorf("FindTests.benchmarks = %s, want none", benchmarks) 112 } 113 sort.Sort(funcsByPos(examples)) 114 if got, want := fmt.Sprint(examples), "[foo.Example foo.ExampleA]"; got != want { 115 t.Errorf("FindTests examples = %s, want %s", got, want) 116 } 117 } 118 119 type funcsByPos []*ssa.Function 120 121 func (p funcsByPos) Len() int { return len(p) } 122 func (p funcsByPos) Less(i, j int) bool { return p[i].Pos() < p[j].Pos() } 123 func (p funcsByPos) Swap(i, j int) { p[i], p[j] = p[j], p[i] }