github.com/leanovate/gopter@v0.2.9/arbitrary/example_arbitrary_struct_test.go (about) 1 package arbitrary_test 2 3 import ( 4 "fmt" 5 "time" 6 7 "github.com/leanovate/gopter" 8 "github.com/leanovate/gopter/arbitrary" 9 ) 10 11 type MyStringType string 12 type MyInt8Type int8 13 type MyInt16Type int16 14 type MyInt32Type int32 15 type MyInt64Type int64 16 type MyUInt8Type uint8 17 type MyUInt16Type uint16 18 type MyUInt32Type uint32 19 type MyUInt64Type uint64 20 21 type Foo struct { 22 Name MyStringType 23 Id1 MyInt8Type 24 Id2 MyInt16Type 25 Id3 MyInt32Type 26 Id4 MyInt64Type 27 Id5 MyUInt8Type 28 Id6 MyUInt16Type 29 Id7 MyUInt32Type 30 Id8 MyUInt64Type 31 ATime time.Time 32 ATimePtr *time.Time 33 } 34 35 func (f Foo) ToString() string { 36 return fmt.Sprintf("For(%s, %d, %d, %d, %d, %d, %d, %d, %d, %v, %v)", f.Name, f.Id1, f.Id2, f.Id3, f.Id4, f.Id5, f.Id6, f.Id7, f.Id8, f.ATime, f.ATimePtr) 37 } 38 39 func Example_arbitrary_structs() { 40 time.Local = time.UTC 41 parameters := gopter.DefaultTestParametersWithSeed(1234) // Example should generate reproducible results, otherwise DefaultTestParameters() will suffice 42 43 arbitraries := arbitrary.DefaultArbitraries() 44 45 properties := gopter.NewProperties(parameters) 46 47 properties.Property("MyInt64", arbitraries.ForAll( 48 func(id MyInt64Type) bool { 49 return id > -1000 50 })) 51 properties.Property("MyUInt32Type", arbitraries.ForAll( 52 func(id MyUInt32Type) bool { 53 return id < 2000 54 })) 55 properties.Property("Foo", arbitraries.ForAll( 56 func(foo *Foo) bool { 57 return foo.ATime.After(time.Unix(0, 0)) 58 })) 59 properties.Property("Foo2", arbitraries.ForAll( 60 func(foo Foo) bool { 61 return foo.ATimePtr == nil || foo.ATimePtr.Before(time.Unix(20000, 0)) 62 })) 63 64 properties.Run(gopter.ConsoleReporter(false)) 65 // Output: 66 // ! MyInt64: Falsified after 6 passed tests. 67 // ARG_0: -1000 68 // ARG_0_ORIGINAL (54 shrinks): -1601066829744837253 69 // ! MyUInt32Type: Falsified after 0 passed tests. 70 // ARG_0: 2000 71 // ARG_0_ORIGINAL (23 shrinks): 2161922319 72 // + Foo: OK, passed 100 tests. 73 // ! Foo2: Falsified after 1 passed tests. 74 // ARG_0: {Name: Id1:0 Id2:0 Id3:0 Id4:0 Id5:0 Id6:0 Id7:0 Id8:0 75 // ATime:1970-01-01 00:00:00 +0000 UTC ATimePtr:1970-01-01 05:33:20 +0000 76 // UTC} 77 // ARG_0_ORIGINAL (40 shrinks): {Name: Id1:-67 Id2:27301 Id3:-1350752892 78 // Id4:7128486677722156226 Id5:208 Id6:28663 Id7:4178604448 79 // Id8:16360504079646654692 ATime:2239-08-20 23:46:28.063412239 +0000 UTC 80 // ATimePtr:5468-08-19 13:09:39.171622464 +0000 UTC} 81 }