Exemplo n.º 1
0
func TestFindTests(t *testing.T) {
	test := `
package foo

import "testing"

type T int

// Tests:
func Test(t *testing.T) {}
func TestA(t *testing.T) {}
func TestB(t *testing.T) {}

// Not tests:
func testC(t *testing.T) {}
func TestD() {}
func testE(t *testing.T) int { return 0 }
func (T) Test(t *testing.T) {}

// Benchmarks:
func Benchmark(*testing.B) {}
func BenchmarkA(b *testing.B) {}
func BenchmarkB(*testing.B) {}

// Not benchmarks:
func benchmarkC(t *testing.T) {}
func BenchmarkD() {}
func benchmarkE(t *testing.T) int { return 0 }
func (T) Benchmark(t *testing.T) {}

// Examples:
func Example() {}
func ExampleA() {}

// Not examples:
func exampleC() {}
func ExampleD(t *testing.T) {}
func exampleE() int { return 0 }
func (T) Example() {}
`
	pkgs := create(t, test)
	_, tests, benchmarks, examples := ssa.FindTests(pkgs)

	sort.Sort(funcsByPos(tests))
	if got, want := fmt.Sprint(tests), "[foo.Test foo.TestA foo.TestB]"; got != want {
		t.Errorf("FindTests.tests = %s, want %s", got, want)
	}

	sort.Sort(funcsByPos(benchmarks))
	if got, want := fmt.Sprint(benchmarks), "[foo.Benchmark foo.BenchmarkA foo.BenchmarkB]"; got != want {
		t.Errorf("FindTests.benchmarks = %s, want %s", got, want)
	}

	sort.Sort(funcsByPos(examples))
	if got, want := fmt.Sprint(examples), "[foo.Example foo.ExampleA]"; got != want {
		t.Errorf("FindTests examples = %s, want %s", got, want)
	}
}
Exemplo n.º 2
0
func TestFindTestsTesting(t *testing.T) {
	test := `
package foo

// foo does not import "testing", but defines Examples.

func Example() {}
func ExampleA() {}
`
	pkgs := create(t, test)
	_, tests, benchmarks, examples := ssa.FindTests(pkgs)
	if len(tests) > 0 {
		t.Errorf("FindTests.tests = %s, want none", tests)
	}
	if len(benchmarks) > 0 {
		t.Errorf("FindTests.benchmarks = %s, want none", benchmarks)
	}
	sort.Sort(funcsByPos(examples))
	if got, want := fmt.Sprint(examples), "[foo.Example foo.ExampleA]"; got != want {
		t.Errorf("FindTests examples = %s, want %s", got, want)
	}
}