github.com/kekek/gb@v0.4.5-0.20170222120241-d4ba64b0b297/test/testmain17.go (about)

     1  // +build !go1.8
     2  
     3  // Copyright 2011 The Go Authors.  All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  package test
     8  
     9  import "text/template"
    10  
    11  // imported from $GOROOT/src/cmd/go/test.go
    12  
    13  var testmainTmpl = template.Must(template.New("main").Parse(`
    14  package main
    15  
    16  import (
    17  {{if not .TestMain}}
    18  	"os"
    19  {{end}}
    20  	"regexp"
    21  	"testing"
    22  
    23  {{if .ImportTest}}
    24  	{{if .NeedTest}}_test{{else}}_{{end}} {{.Package.ImportPath | printf "%q"}}
    25  {{end}}
    26  {{if .ImportXtest}}
    27  	{{if .NeedXtest}}_xtest{{else}}_{{end}} {{.Package.ImportPath | printf "%s_test" | printf "%q"}}
    28  {{end}}
    29  {{range $i, $p := .Cover}}
    30  	_cover{{$i}} {{$p.Package.ImportPath | printf "%q"}}
    31  {{end}}
    32  
    33  {{if .NeedCgo}}
    34  	_ "runtime/cgo"
    35  {{end}}
    36  )
    37  
    38  var tests = []testing.InternalTest{
    39  {{range .Tests}}
    40  	{"{{.Name}}", {{.Package}}.{{.Name}}},
    41  {{end}}
    42  }
    43  
    44  var benchmarks = []testing.InternalBenchmark{
    45  {{range .Benchmarks}}
    46  	{"{{.Name}}", {{.Package}}.{{.Name}}},
    47  {{end}}
    48  }
    49  
    50  var examples = []testing.InternalExample{
    51  {{range .Examples}}
    52  	{Name: "{{.Name}}", F: {{.Package}}.{{.Name}}, Output: {{.Output | printf "%q"}}},
    53  {{end}}
    54  }
    55  
    56  var matchPat string
    57  var matchRe *regexp.Regexp
    58  
    59  func matchString(pat, str string) (result bool, err error) {
    60  	if matchRe == nil || matchPat != pat {
    61  		matchPat = pat
    62  		matchRe, err = regexp.Compile(matchPat)
    63  		if err != nil {
    64  			return
    65  		}
    66  	}
    67  	return matchRe.MatchString(str), nil
    68  }
    69  
    70  {{if .CoverEnabled}}
    71  
    72  // Only updated by init functions, so no need for atomicity.
    73  var (
    74  	coverCounters = make(map[string][]uint32)
    75  	coverBlocks = make(map[string][]testing.CoverBlock)
    76  )
    77  
    78  func init() {
    79  	{{range $i, $p := .Cover}}
    80  	{{range $file, $cover := $p.Vars}}
    81  	coverRegisterFile({{printf "%q" $cover.File}}, _cover{{$i}}.{{$cover.Var}}.Count[:], _cover{{$i}}.{{$cover.Var}}.Pos[:], _cover{{$i}}.{{$cover.Var}}.NumStmt[:])
    82  	{{end}}
    83  	{{end}}
    84  }
    85  
    86  func coverRegisterFile(fileName string, counter []uint32, pos []uint32, numStmts []uint16) {
    87  	if 3*len(counter) != len(pos) || len(counter) != len(numStmts) {
    88  		panic("coverage: mismatched sizes")
    89  	}
    90  	if coverCounters[fileName] != nil {
    91  		// Already registered.
    92  		return
    93  	}
    94  	coverCounters[fileName] = counter
    95  	block := make([]testing.CoverBlock, len(counter))
    96  	for i := range counter {
    97  		block[i] = testing.CoverBlock{
    98  			Line0: pos[3*i+0],
    99  			Col0: uint16(pos[3*i+2]),
   100  			Line1: pos[3*i+1],
   101  			Col1: uint16(pos[3*i+2]>>16),
   102  			Stmts: numStmts[i],
   103  		}
   104  	}
   105  	coverBlocks[fileName] = block
   106  }
   107  {{end}}
   108  
   109  func main() {
   110  {{if .CoverEnabled}}
   111  	testing.RegisterCover(testing.Cover{
   112  		Mode: {{printf "%q" .CoverMode}},
   113  		Counters: coverCounters,
   114  		Blocks: coverBlocks,
   115  		CoveredPackages: {{printf "%q" .Covered}},
   116  	})
   117  {{end}}
   118  	m := testing.MainStart(matchString, tests, benchmarks, examples)
   119  {{with .TestMain}}
   120  	{{.Package}}.{{.Name}}(m)
   121  {{else}}
   122  	os.Exit(m.Run())
   123  {{end}}
   124  }
   125  
   126  `))