github.com/leanovate/gopter@v0.2.9/gen/ptr_of_test.go (about) 1 package gen_test 2 3 import ( 4 "testing" 5 6 "github.com/leanovate/gopter" 7 "github.com/leanovate/gopter/gen" 8 "github.com/leanovate/gopter/prop" 9 ) 10 11 func TestPtrOf(t *testing.T) { 12 genParams := gopter.DefaultGenParameters() 13 elementGen := gen.Const("element") 14 ptrGen := gen.PtrOf(elementGen) 15 16 for i := 0; i < 100; i++ { 17 sample, ok := ptrGen(genParams).Retrieve() 18 19 if !ok { 20 t.Error("Sample was not ok") 21 } 22 if sample == nil { 23 continue 24 } 25 stringPtr, ok := sample.(*string) 26 if !ok { 27 t.Errorf("Sample not pointer to string: %#v", sample) 28 } else if *stringPtr != "element" { 29 t.Errorf("Sample contains invalid value: %#v %#v", sample, *stringPtr) 30 } 31 } 32 } 33 34 type Foo string 35 36 func TestPtrOfFoo(t *testing.T) { 37 parameters := gopter.DefaultTestParameters() 38 properties := gopter.NewProperties(parameters) 39 40 properties.Property("PtrOf", prop.ForAll( 41 func(foo *Foo, 42 ) bool { 43 return true 44 }, 45 gen.PtrOf(GenFoo()), 46 )) 47 properties.TestingRun(t) 48 } 49 50 func GenFoo() gopter.Gen { 51 return gen.SliceOfN(16, gen.Rune()).Map(func(v []rune) Foo { 52 return Foo(v) 53 }) 54 }