github.com/powerman/golang-tools@v0.1.11-0.20220410185822-5ad214d8d803/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.Add()                                      // want `wrong number of values in call to \(\*testing.F\)\.Add: 0, fuzz target expects 2`
    24  	f.Add(1, 2, 3, 4)                            // want `wrong number of values in call to \(\*testing.F\)\.Add: 4, fuzz target expects 2`
    25  	f.Add(5, 5)                                  // want `mismatched type in call to \(\*testing.F\)\.Add: int, fuzz target expects \[\]byte`
    26  	f.Add([]byte("hello"), 5)                    // want `mismatched types in call to \(\*testing.F\)\.Add: \[\[\]byte int\], fuzz target expects \[int \[\]byte\]`
    27  	f.Add(5, []byte("hello"))                    // OK
    28  	f.Fuzz(func(t *testing.T, i int, b []byte) { // OK "arguments in func are allowed"
    29  		f.Add(5, []byte("hello"))     // want `fuzz target must not call any \*F methods`
    30  		f.Name()                      // OK "calls to (*F).Failed and (*F).Name are allowed"
    31  		f.Failed()                    // OK "calls to (*F).Failed and (*F).Name are allowed"
    32  		f.Fuzz(func(t *testing.T) {}) // want `fuzz target must not call any \*F methods`
    33  	})
    34  }
    35  
    36  func FuzzArgFunc(f *testing.F) {
    37  	f.Fuzz(0) // want "argument to Fuzz must be a function"
    38  }
    39  
    40  func FuzzFuncWithReturn(f *testing.F) {
    41  	f.Fuzz(func(t *testing.T) bool { return true }) // want "fuzz target must not return any value"
    42  }
    43  
    44  func FuzzFuncNoArg(f *testing.F) {
    45  	f.Fuzz(func() {}) // want "fuzz target must have 1 or more argument"
    46  }
    47  
    48  func FuzzFuncFirstArgNotTesting(f *testing.F) {
    49  	f.Fuzz(func(i int64) {}) // want "the first parameter of a fuzz target must be \\*testing.T"
    50  }
    51  
    52  func FuzzFuncFirstArgTestingNotT(f *testing.F) {
    53  	f.Fuzz(func(t *testing.F) {}) // want "the first parameter of a fuzz target must be \\*testing.T"
    54  }
    55  
    56  func FuzzFuncSecondArgNotAllowed(f *testing.F) {
    57  	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"
    58  }
    59  
    60  func FuzzFuncSecondArgArrNotAllowed(f *testing.F) {
    61  	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"
    62  }
    63  
    64  func FuzzFuncConsecutiveArgNotAllowed(f *testing.F) {
    65  	f.Fuzz(func(t *testing.T, i, j string, k 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"
    66  }
    67  
    68  func FuzzFuncInner(f *testing.F) {
    69  	innerFunc := func(t *testing.T, i float32) {}
    70  	f.Fuzz(innerFunc) // ok
    71  }
    72  
    73  func FuzzArrayOfFunc(f *testing.F) {
    74  	var funcs = []func(t *testing.T, i int){func(t *testing.T, i int) {}}
    75  	f.Fuzz(funcs[0]) // ok
    76  }
    77  
    78  type GenericSlice[T any] []T
    79  
    80  func FuzzGenericFunc(f *testing.F) {
    81  	g := GenericSlice[func(t *testing.T, i int)]{func(t *testing.T, i int) {}}
    82  	f.Fuzz(g[0]) // ok
    83  }
    84  
    85  type F func(t *testing.T, i int32)
    86  
    87  type myType struct {
    88  	myVar F
    89  }
    90  
    91  func FuzzObjectMethod(f *testing.F) {
    92  	obj := myType{
    93  		myVar: func(t *testing.T, i int32) {},
    94  	}
    95  	f.Fuzz(obj.myVar) // ok
    96  }