golang.org/x/tools@v0.21.0/go/analysis/passes/tests/testdata/src/a/go118_test.go (about)

     1  package a
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func Fuzzfoo(*testing.F) {} // want "first letter after 'Fuzz' must not be lowercase"
     8  
     9  func FuzzBoo(*testing.F) {} // OK because first letter after 'Fuzz' is Uppercase.
    10  
    11  func FuzzCallDifferentFunc(f *testing.F) {
    12  	f.Name() //OK
    13  }
    14  
    15  func FuzzFunc(f *testing.F) {
    16  	f.Fuzz(func(t *testing.T) {}) // OK "first argument is of type *testing.T"
    17  }
    18  
    19  func FuzzFuncWithArgs(f *testing.F) {
    20  	f.Add()                                      // want `wrong number of values in call to \(\*testing.F\)\.Add: 0, fuzz target expects 2`
    21  	f.Add(1, 2, 3, 4)                            // want `wrong number of values in call to \(\*testing.F\)\.Add: 4, fuzz target expects 2`
    22  	f.Add(5, 5)                                  // want `mismatched type in call to \(\*testing.F\)\.Add: int, fuzz target expects \[\]byte`
    23  	f.Add([]byte("hello"), 5)                    // want `mismatched types in call to \(\*testing.F\)\.Add: \[\[\]byte int\], fuzz target expects \[int \[\]byte\]`
    24  	f.Add(5, []byte("hello"))                    // OK
    25  	f.Fuzz(func(t *testing.T, i int, b []byte) { // OK "arguments in func are allowed"
    26  		f.Add(5, []byte("hello"))     // want `fuzz target must not call any \*F methods`
    27  		f.Name()                      // OK "calls to (*F).Failed and (*F).Name are allowed"
    28  		f.Failed()                    // OK "calls to (*F).Failed and (*F).Name are allowed"
    29  		f.Fuzz(func(t *testing.T) {}) // want `fuzz target must not call any \*F methods`
    30  	})
    31  }
    32  
    33  func FuzzArgFunc(f *testing.F) {
    34  	f.Fuzz(0) // want "argument to Fuzz must be a function"
    35  }
    36  
    37  func FuzzFuncWithReturn(f *testing.F) {
    38  	f.Fuzz(func(t *testing.T) bool { return true }) // want "fuzz target must not return any value"
    39  }
    40  
    41  func FuzzFuncNoArg(f *testing.F) {
    42  	f.Fuzz(func() {}) // want "fuzz target must have 1 or more argument"
    43  }
    44  
    45  func FuzzFuncFirstArgNotTesting(f *testing.F) {
    46  	f.Fuzz(func(i int64) {}) // want "the first parameter of a fuzz target must be \\*testing.T"
    47  }
    48  
    49  func FuzzFuncFirstArgTestingNotT(f *testing.F) {
    50  	f.Fuzz(func(t *testing.F) {}) // want "the first parameter of a fuzz target must be \\*testing.T"
    51  }
    52  
    53  func FuzzFuncSecondArgNotAllowed(f *testing.F) {
    54  	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"
    55  }
    56  
    57  func FuzzFuncSecondArgArrNotAllowed(f *testing.F) {
    58  	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"
    59  }
    60  
    61  func FuzzFuncConsecutiveArgNotAllowed(f *testing.F) {
    62  	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"
    63  }
    64  
    65  func FuzzFuncInner(f *testing.F) {
    66  	innerFunc := func(t *testing.T, i float32) {}
    67  	f.Fuzz(innerFunc) // ok
    68  }
    69  
    70  func FuzzArrayOfFunc(f *testing.F) {
    71  	var funcs = []func(t *testing.T, i int){func(t *testing.T, i int) {}}
    72  	f.Fuzz(funcs[0]) // ok
    73  }
    74  
    75  type GenericSlice[T any] []T
    76  
    77  func FuzzGenericFunc(f *testing.F) {
    78  	g := GenericSlice[func(t *testing.T, i int)]{func(t *testing.T, i int) {}}
    79  	f.Fuzz(g[0]) // ok
    80  }
    81  
    82  type F func(t *testing.T, i int32)
    83  
    84  type myType struct {
    85  	myVar F
    86  }
    87  
    88  func FuzzObjectMethod(f *testing.F) {
    89  	obj := myType{
    90  		myVar: func(t *testing.T, i int32) {},
    91  	}
    92  	f.Fuzz(obj.myVar) // ok
    93  }
    94  
    95  // Test for golang/go#56505: checking fuzz arguments should not panic on *error.
    96  func FuzzIssue56505(f *testing.F) {
    97  	f.Fuzz(func(e *error) {}) // want "the first parameter of a fuzz target must be \\*testing.T"
    98  }