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.
7 // Tests of FindTests. CreateTestMainPackage is tested via the interpreter.
8 // TODO(adonovan): test the 'pkgs' result from FindTests.
15 "golang.org/x/tools/go/loader"
16 "golang.org/x/tools/go/ssa"
17 "golang.org/x/tools/go/ssa/ssautil"
20 func create(t *testing.T, content string) *ssa.Package {
21 var conf loader.Config
22 f, err := conf.ParseFile("foo_test.go", content)
26 conf.CreateFromFiles("foo", f)
28 lprog, err := conf.Load()
33 // We needn't call Build.
34 foo := lprog.Package("foo").Pkg
35 return ssautil.CreateProgram(lprog, ssa.SanityCheckFunctions).Package(foo)
38 func TestFindTests(t *testing.T) {
47 func Test(t *testing.T) {}
48 func TestA(t *testing.T) {}
49 func TestB(t *testing.T) {}
52 func testC(t *testing.T) {}
54 func testE(t *testing.T) int { return 0 }
55 func (T) Test(t *testing.T) {}
58 func Benchmark(*testing.B) {}
59 func BenchmarkA(b *testing.B) {}
60 func BenchmarkB(*testing.B) {}
63 func benchmarkC(t *testing.T) {}
65 func benchmarkE(t *testing.T) int { return 0 }
66 func (T) Benchmark(t *testing.T) {}
74 func ExampleD(t *testing.T) {}
75 func exampleE() int { return 0 }
78 pkg := create(t, test)
79 tests, benchmarks, examples, _ := ssa.FindTests(pkg)
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)
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)
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)
97 func TestFindTestsTesting(t *testing.T) {
101 // foo does not import "testing", but defines Examples.
106 pkg := create(t, test)
107 tests, benchmarks, examples, _ := ssa.FindTests(pkg)
109 t.Errorf("FindTests.tests = %s, want none", tests)
111 if len(benchmarks) > 0 {
112 t.Errorf("FindTests.benchmarks = %s, want none", benchmarks)
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)
120 type funcsByPos []*ssa.Function
122 func (p funcsByPos) Len() int { return len(p) }
123 func (p funcsByPos) Less(i, j int) bool { return p[i].Pos() < p[j].Pos() }
124 func (p funcsByPos) Swap(i, j int) { p[i], p[j] = p[j], p[i] }