Name | Date | Size | #Lines | LOC | ||
---|---|---|---|---|---|---|
.. | 03-May-2022 | - | ||||
.travis.yml | H A D | 28-Oct-2019 | 187 | 14 | 10 | |
CONTRIBUTING.md | H A D | 28-Oct-2019 | 3.1 KiB | 68 | 50 | |
LICENSE | H A D | 28-Oct-2019 | 11.1 KiB | 203 | 169 | |
README.md | H A D | 28-Oct-2019 | 1.7 KiB | 72 | 58 | |
doc.go | H A D | 28-Oct-2019 | 668 | 19 | 1 | |
example_test.go | H A D | 28-Oct-2019 | 4.5 KiB | 226 | 142 | |
fuzz.go | H A D | 28-Oct-2019 | 13.8 KiB | 507 | 357 | |
fuzz_test.go | H A D | 28-Oct-2019 | 9.2 KiB | 507 | 433 | |
go.mod | H A D | 28-Oct-2019 | 41 | 4 | 2 |
README.md
1gofuzz 2====== 3 4gofuzz is a library for populating go objects with random values. 5 6[![GoDoc](https://godoc.org/github.com/google/gofuzz?status.svg)](https://godoc.org/github.com/google/gofuzz) 7[![Travis](https://travis-ci.org/google/gofuzz.svg?branch=master)](https://travis-ci.org/google/gofuzz) 8 9This is useful for testing: 10 11* Do your project's objects really serialize/unserialize correctly in all cases? 12* Is there an incorrectly formatted object that will cause your project to panic? 13 14Import with ```import "github.com/google/gofuzz"``` 15 16You can use it on single variables: 17```go 18f := fuzz.New() 19var myInt int 20f.Fuzz(&myInt) // myInt gets a random value. 21``` 22 23You can use it on maps: 24```go 25f := fuzz.New().NilChance(0).NumElements(1, 1) 26var myMap map[ComplexKeyType]string 27f.Fuzz(&myMap) // myMap will have exactly one element. 28``` 29 30Customize the chance of getting a nil pointer: 31```go 32f := fuzz.New().NilChance(.5) 33var fancyStruct struct { 34 A, B, C, D *string 35} 36f.Fuzz(&fancyStruct) // About half the pointers should be set. 37``` 38 39You can even customize the randomization completely if needed: 40```go 41type MyEnum string 42const ( 43 A MyEnum = "A" 44 B MyEnum = "B" 45) 46type MyInfo struct { 47 Type MyEnum 48 AInfo *string 49 BInfo *string 50} 51 52f := fuzz.New().NilChance(0).Funcs( 53 func(e *MyInfo, c fuzz.Continue) { 54 switch c.Intn(2) { 55 case 0: 56 e.Type = A 57 c.Fuzz(&e.AInfo) 58 case 1: 59 e.Type = B 60 c.Fuzz(&e.BInfo) 61 } 62 }, 63) 64 65var myObject MyInfo 66f.Fuzz(&myObject) // Type will correspond to whether A or B info is set. 67``` 68 69See more examples in ```example_test.go```. 70 71Happy testing! 72