github.com/golangci/go-tools@v0.0.0-20190318060251-af6baa5dc196/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  	"github.com/golangci/go-tools/ssa"
    17  	"github.com/golangci/go-tools/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  	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  
    38  func TestFindTests(t *testing.T) {
    39  	test := `
    40  package foo
    41  
    42  import "testing"
    43  
    44  type T int
    45  
    46  // Tests:
    47  func Test(t *testing.T) {}
    48  func TestA(t *testing.T) {}
    49  func TestB(t *testing.T) {}
    50  
    51  // Not tests:
    52  func testC(t *testing.T) {}
    53  func TestD() {}
    54  func testE(t *testing.T) int { return 0 }
    55  func (T) Test(t *testing.T) {}
    56  
    57  // Benchmarks:
    58  func Benchmark(*testing.B) {}
    59  func BenchmarkA(b *testing.B) {}
    60  func BenchmarkB(*testing.B) {}
    61  
    62  // Not benchmarks:
    63  func benchmarkC(t *testing.T) {}
    64  func BenchmarkD() {}
    65  func benchmarkE(t *testing.T) int { return 0 }
    66  func (T) Benchmark(t *testing.T) {}
    67  
    68  // Examples:
    69  func Example() {}
    70  func ExampleA() {}
    71  
    72  // Not examples:
    73  func exampleC() {}
    74  func ExampleD(t *testing.T) {}
    75  func exampleE() int { return 0 }
    76  func (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  
    97  func TestFindTestsTesting(t *testing.T) {
    98  	test := `
    99  package foo
   100  
   101  // foo does not import "testing", but defines Examples.
   102  
   103  func Example() {}
   104  func 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  
   120  type funcsByPos []*ssa.Function
   121  
   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] }