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