github.com/kekek/gb@v0.4.5-0.20170222120241-d4ba64b0b297/test/testmain18.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 "testing" 21 "testing/internal/testdeps" 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}}", {{.Package}}.{{.Name}}, {{.Output | printf "%q"}}, {{.Unordered}}}, 53 {{end}} 54 } 55 56 {{if .CoverEnabled}} 57 58 // Only updated by init functions, so no need for atomicity. 59 var ( 60 coverCounters = make(map[string][]uint32) 61 coverBlocks = make(map[string][]testing.CoverBlock) 62 ) 63 64 func init() { 65 {{range $i, $p := .Cover}} 66 {{range $file, $cover := $p.Vars}} 67 coverRegisterFile({{printf "%q" $cover.File}}, _cover{{$i}}.{{$cover.Var}}.Count[:], _cover{{$i}}.{{$cover.Var}}.Pos[:], _cover{{$i}}.{{$cover.Var}}.NumStmt[:]) 68 {{end}} 69 {{end}} 70 } 71 72 func coverRegisterFile(fileName string, counter []uint32, pos []uint32, numStmts []uint16) { 73 if 3*len(counter) != len(pos) || len(counter) != len(numStmts) { 74 panic("coverage: mismatched sizes") 75 } 76 if coverCounters[fileName] != nil { 77 // Already registered. 78 return 79 } 80 coverCounters[fileName] = counter 81 block := make([]testing.CoverBlock, len(counter)) 82 for i := range counter { 83 block[i] = testing.CoverBlock{ 84 Line0: pos[3*i+0], 85 Col0: uint16(pos[3*i+2]), 86 Line1: pos[3*i+1], 87 Col1: uint16(pos[3*i+2]>>16), 88 Stmts: numStmts[i], 89 } 90 } 91 coverBlocks[fileName] = block 92 } 93 {{end}} 94 95 func main() { 96 {{if .CoverEnabled}} 97 testing.RegisterCover(testing.Cover{ 98 Mode: {{printf "%q" .CoverMode}}, 99 Counters: coverCounters, 100 Blocks: coverBlocks, 101 CoveredPackages: {{printf "%q" .Covered}}, 102 }) 103 {{end}} 104 m := testing.MainStart(testdeps.TestDeps{}, tests, benchmarks, examples) 105 {{with .TestMain}} 106 {{.Package}}.{{.Name}}(m) 107 {{else}} 108 os.Exit(m.Run()) 109 {{end}} 110 } 111 112 `))