github.com/goplus/igop@v0.25.0/load/test_go118.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  	var why string
    30  	if !isTestFunc(fn, arg) {
    31  		why = fmt.Sprintf("must be: func %s(%s *testing.%s)", fn.Name.String(), strings.ToLower(arg), arg)
    32  	}
    33  	if fn.Type.TypeParams.NumFields() > 0 {
    34  		why = "test functions cannot have type parameters"
    35  	}
    36  	if why != "" {
    37  		pos := testFileSet.Position(fn.Pos())
    38  		return fmt.Errorf("%s: wrong signature for %s, %s", pos, fn.Name.String(), why)
    39  	}
    40  	return nil
    41  }
    42  
    43  var testmainData = `
    44  // Code generated by 'go test'. DO NOT EDIT.
    45  
    46  package main
    47  
    48  import (
    49  	"os"
    50  {{if .TestMain}}
    51  	"reflect"
    52  {{end}}
    53  	"testing"
    54  	"testing/internal/testdeps"
    55  
    56  {{if .ImportTest}}
    57  	{{if .NeedTest}}_test{{else}}_{{end}} {{.Package.ImportPath | printf "%q"}}
    58  {{end}}
    59  {{if .ImportXtest}}
    60  	{{if .NeedXtest}}_xtest{{else}}_{{end}} {{.Package.ImportPath | printf "%s_test" | printf "%q"}}
    61  {{end}}
    62  {{if .Cover}}
    63  {{range $i, $p := .Cover.Vars}}
    64  	_cover{{$i}} {{$p.Package.ImportPath | printf "%q"}}
    65  {{end}}
    66  {{end}}
    67  )
    68  
    69  var tests = []testing.InternalTest{
    70  {{range .Tests}}
    71  	{"{{.Name}}", {{.Package}}.{{.Name}}},
    72  {{end}}
    73  }
    74  
    75  var benchmarks = []testing.InternalBenchmark{
    76  {{range .Benchmarks}}
    77  	{"{{.Name}}", {{.Package}}.{{.Name}}},
    78  {{end}}
    79  }
    80  
    81  var fuzzTargets = []testing.InternalFuzzTarget{
    82  {{range .FuzzTargets}}
    83  	{"{{.Name}}", {{.Package}}.{{.Name}}},
    84  {{end}}
    85  }
    86  
    87  var examples = []testing.InternalExample{
    88  {{range .Examples}}
    89  	{"{{.Name}}", {{.Package}}.{{.Name}}, {{.Output | printf "%q"}}, {{.Unordered}}},
    90  {{end}}
    91  }
    92  
    93  func init() {
    94  	testdeps.ImportPath = {{.ImportPath | printf "%q"}}
    95  }
    96  
    97  {{if .Cover}}
    98  
    99  // Only updated by init functions, so no need for atomicity.
   100  var (
   101  	coverCounters = make(map[string][]uint32)
   102  	coverBlocks = make(map[string][]testing.CoverBlock)
   103  )
   104  
   105  func init() {
   106  	{{range $i, $p := .Cover.Vars}}
   107  	{{range $file, $cover := $p.Vars}}
   108  	coverRegisterFile({{printf "%q" $cover.File}}, _cover{{$i}}.{{$cover.Var}}.Count[:], _cover{{$i}}.{{$cover.Var}}.Pos[:], _cover{{$i}}.{{$cover.Var}}.NumStmt[:])
   109  	{{end}}
   110  	{{end}}
   111  }
   112  
   113  func coverRegisterFile(fileName string, counter []uint32, pos []uint32, numStmts []uint16) {
   114  	if 3*len(counter) != len(pos) || len(counter) != len(numStmts) {
   115  		panic("coverage: mismatched sizes")
   116  	}
   117  	if coverCounters[fileName] != nil {
   118  		// Already registered.
   119  		return
   120  	}
   121  	coverCounters[fileName] = counter
   122  	block := make([]testing.CoverBlock, len(counter))
   123  	for i := range counter {
   124  		block[i] = testing.CoverBlock{
   125  			Line0: pos[3*i+0],
   126  			Col0: uint16(pos[3*i+2]),
   127  			Line1: pos[3*i+1],
   128  			Col1: uint16(pos[3*i+2]>>16),
   129  			Stmts: numStmts[i],
   130  		}
   131  	}
   132  	coverBlocks[fileName] = block
   133  }
   134  {{end}}
   135  
   136  func main() {
   137  {{if .Cover}}
   138  	testing.RegisterCover(testing.Cover{
   139  		Mode: {{printf "%q" .Cover.Mode}},
   140  		Counters: coverCounters,
   141  		Blocks: coverBlocks,
   142  		CoveredPackages: {{printf "%q" .Covered}},
   143  	})
   144  {{end}}
   145  	m := testing.MainStart(testdeps.TestDeps{}, tests, benchmarks, fuzzTargets, examples)
   146  {{with .TestMain}}
   147  	{{.Package}}.{{.Name}}(m)
   148  	os.Exit(int(reflect.ValueOf(m).Elem().FieldByName("exitCode").Int()))
   149  {{else}}
   150  	os.Exit(m.Run())
   151  {{end}}
   152  }
   153  
   154  `