github.com/leanovate/gopter@v0.2.9/gen/one_of.go (about) 1 package gen 2 3 import ( 4 "reflect" 5 6 "github.com/leanovate/gopter" 7 ) 8 9 // OneConstOf generate one of a list of constant values 10 func OneConstOf(consts ...interface{}) gopter.Gen { 11 if len(consts) == 0 { 12 return Fail(reflect.TypeOf(nil)) 13 } 14 return func(genParams *gopter.GenParameters) *gopter.GenResult { 15 idx := genParams.Rng.Intn(len(consts)) 16 return gopter.NewGenResult(consts[idx], gopter.NoShrinker) 17 } 18 } 19 20 // OneGenOf generate one value from a a list of generators 21 func OneGenOf(gens ...gopter.Gen) gopter.Gen { 22 if len(gens) == 0 { 23 return Fail(reflect.TypeOf(nil)) 24 } 25 return func(genParams *gopter.GenParameters) *gopter.GenResult { 26 idx := genParams.Rng.Intn(len(gens)) 27 return gens[idx](genParams) 28 } 29 }