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