github.com/jhump/golang-x-tools@v0.0.0-20220218190644-4958d6d39439/go/analysis/passes/tests/testdata/src/a/go118_test.go (about)

     1  //go:build go1.18
     2  // +build go1.18
     3  
     4  package a
     5  
     6  import (
     7  	"testing"
     8  )
     9  
    10  func Fuzzfoo(*testing.F) {} // want "first letter after 'Fuzz' must not be lowercase"
    11  
    12  func FuzzBoo(*testing.F) {} // OK because first letter after 'Fuzz' is Uppercase.
    13  
    14  func FuzzCallDifferentFunc(f *testing.F) {
    15  	f.Name() //OK
    16  }
    17  
    18  func FuzzFunc(f *testing.F) {
    19  	f.Fuzz(func(t *testing.T) {}) // OK "first argument is of type *testing.T"
    20  }
    21  
    22  func FuzzFuncWithArgs(f *testing.F) {
    23  	f.Fuzz(func(t *testing.T, i int, b []byte) {}) // OK "arguments in func are allowed"
    24  }
    25  
    26  func FuzzArgFunc(f *testing.F) {
    27  	f.Fuzz(0) // want "argument to Fuzz must be a function"
    28  }
    29  
    30  func FuzzFuncWithReturn(f *testing.F) {
    31  	f.Fuzz(func(t *testing.T) bool { return true }) // want "fuzz target must not return any value"
    32  }
    33  
    34  func FuzzFuncNoArg(f *testing.F) {
    35  	f.Fuzz(func() {}) // want "fuzz target must have 1 or more argument"
    36  }
    37  
    38  func FuzzFuncFirstArgNotTesting(f *testing.F) {
    39  	f.Fuzz(func(i int64) {}) // want "the first parameter of a fuzz target must be \\*testing.T"
    40  }
    41  
    42  func FuzzFuncFirstArgTestingNotT(f *testing.F) {
    43  	f.Fuzz(func(t *testing.F) {}) // want "the first parameter of a fuzz target must be \\*testing.T"
    44  }
    45  
    46  func FuzzFuncSecondArgNotAllowed(f *testing.F) {
    47  	f.Fuzz(func(t *testing.T, i complex64) {}) // want "fuzzing arguments can only have the following types: string, bool, float32, float64, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, \\[\\]byte"
    48  }
    49  
    50  func FuzzFuncSecondArgArrNotAllowed(f *testing.F) {
    51  	f.Fuzz(func(t *testing.T, i []int) {}) // want "fuzzing arguments can only have the following types: string, bool, float32, float64, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, \\[\\]byte"
    52  }
    53  
    54  func FuzzFuncInner(f *testing.F) {
    55  	innerFunc := func(t *testing.T, i float32) {}
    56  	f.Fuzz(innerFunc) // ok
    57  }
    58  
    59  func FuzzArrayOfFunc(f *testing.F) {
    60  	var funcs = []func(t *testing.T, i int){func(t *testing.T, i int) {}}
    61  	f.Fuzz(funcs[0]) // ok
    62  }
    63  
    64  type GenericSlice[T any] []T
    65  
    66  func FuzzGenericFunc(f *testing.F) {
    67  	g := GenericSlice[func(t *testing.T, i int)]{func(t *testing.T, i int) {}}
    68  	f.Fuzz(g[0]) // ok
    69  }
    70  
    71  type F func(t *testing.T, i int32)
    72  
    73  type myType struct {
    74  	myVar F
    75  }
    76  
    77  func FuzzObjectMethod(f *testing.F) {
    78  	obj := myType{
    79  		myVar: func(t *testing.T, i int32) {},
    80  	}
    81  	f.Fuzz(obj.myVar) // ok
    82  }