github.com/goplus/igop@v0.25.0/load/test_go117.go (about) 1 //go:build !go1.18 2 // +build !go1.18 3 4 /* 5 * Copyright (c) 2022 The GoPlus Authors (goplus.org). All rights reserved. 6 * 7 * Licensed under the Apache License, Version 2.0 (the "License"); 8 * you may not use this file except in compliance with the License. 9 * You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, software 14 * distributed under the License is distributed on an "AS IS" BASIS, 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 * See the License for the specific language governing permissions and 17 * limitations under the License. 18 */ 19 20 package load 21 22 import ( 23 "fmt" 24 "go/ast" 25 "strings" 26 ) 27 28 func checkTestFunc(fn *ast.FuncDecl, arg string) error { 29 if !isTestFunc(fn, arg) { 30 name := fn.Name.String() 31 pos := testFileSet.Position(fn.Pos()) 32 return fmt.Errorf("%s: wrong signature for %s, must be: func %s(%s *testing.%s)", pos, name, name, strings.ToLower(arg), arg) 33 } 34 return nil 35 } 36 37 var testmainData = ` 38 // Code generated by 'go test'. DO NOT EDIT. 39 40 package main 41 42 import ( 43 "os" 44 {{if .TestMain}} 45 "reflect" 46 {{end}} 47 "testing" 48 "testing/internal/testdeps" 49 50 {{if .ImportTest}} 51 {{if .NeedTest}}_test{{else}}_{{end}} {{.Package.ImportPath | printf "%q"}} 52 {{end}} 53 {{if .ImportXtest}} 54 {{if .NeedXtest}}_xtest{{else}}_{{end}} {{.Package.ImportPath | printf "%s_test" | printf "%q"}} 55 {{end}} 56 {{if .Cover}} 57 {{range $i, $p := .Cover.Vars}} 58 _cover{{$i}} {{$p.Package.ImportPath | printf "%q"}} 59 {{end}} 60 {{end}} 61 ) 62 63 var tests = []testing.InternalTest{ 64 {{range .Tests}} 65 {"{{.Name}}", {{.Package}}.{{.Name}}}, 66 {{end}} 67 } 68 69 var benchmarks = []testing.InternalBenchmark{ 70 {{range .Benchmarks}} 71 {"{{.Name}}", {{.Package}}.{{.Name}}}, 72 {{end}} 73 } 74 75 var examples = []testing.InternalExample{ 76 {{range .Examples}} 77 {"{{.Name}}", {{.Package}}.{{.Name}}, {{.Output | printf "%q"}}, {{.Unordered}}}, 78 {{end}} 79 } 80 81 func init() { 82 testdeps.ImportPath = {{.ImportPath | printf "%q"}} 83 } 84 85 {{if .Cover}} 86 87 // Only updated by init functions, so no need for atomicity. 88 var ( 89 coverCounters = make(map[string][]uint32) 90 coverBlocks = make(map[string][]testing.CoverBlock) 91 ) 92 93 func init() { 94 {{range $i, $p := .Cover.Vars}} 95 {{range $file, $cover := $p.Vars}} 96 coverRegisterFile({{printf "%q" $cover.File}}, _cover{{$i}}.{{$cover.Var}}.Count[:], _cover{{$i}}.{{$cover.Var}}.Pos[:], _cover{{$i}}.{{$cover.Var}}.NumStmt[:]) 97 {{end}} 98 {{end}} 99 } 100 101 func coverRegisterFile(fileName string, counter []uint32, pos []uint32, numStmts []uint16) { 102 if 3*len(counter) != len(pos) || len(counter) != len(numStmts) { 103 panic("coverage: mismatched sizes") 104 } 105 if coverCounters[fileName] != nil { 106 // Already registered. 107 return 108 } 109 coverCounters[fileName] = counter 110 block := make([]testing.CoverBlock, len(counter)) 111 for i := range counter { 112 block[i] = testing.CoverBlock{ 113 Line0: pos[3*i+0], 114 Col0: uint16(pos[3*i+2]), 115 Line1: pos[3*i+1], 116 Col1: uint16(pos[3*i+2]>>16), 117 Stmts: numStmts[i], 118 } 119 } 120 coverBlocks[fileName] = block 121 } 122 {{end}} 123 124 func main() { 125 {{if .Cover}} 126 testing.RegisterCover(testing.Cover{ 127 Mode: {{printf "%q" .Cover.Mode}}, 128 Counters: coverCounters, 129 Blocks: coverBlocks, 130 CoveredPackages: {{printf "%q" .Covered}}, 131 }) 132 {{end}} 133 m := testing.MainStart(testdeps.TestDeps{}, tests, benchmarks, examples) 134 {{with .TestMain}} 135 {{.Package}}.{{.Name}}(m) 136 os.Exit(int(reflect.ValueOf(m).Elem().FieldByName("exitCode").Int())) 137 {{else}} 138 os.Exit(m.Run()) 139 {{end}} 140 } 141 142 `