github.com/leanovate/gopter@v0.2.9/prop/example_time_test.go (about)

     1  package prop_test
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/leanovate/gopter"
     7  	"github.com/leanovate/gopter/gen"
     8  	"github.com/leanovate/gopter/prop"
     9  )
    10  
    11  func Example_timeGen() {
    12  	parameters := gopter.DefaultTestParametersWithSeed(1234) // Example should generate reproducible results, otherwise DefaultTestParameters() will suffice
    13  	time.Local = time.UTC                                    // Just for this example to generate reproducible results
    14  
    15  	properties := gopter.NewProperties(parameters)
    16  
    17  	properties.Property("time in range format parsable", prop.ForAll(
    18  		func(actual time.Time) (bool, error) {
    19  			str := actual.Format(time.RFC3339Nano)
    20  			parsed, err := time.Parse(time.RFC3339Nano, str)
    21  			return actual.Equal(parsed), err
    22  		},
    23  		gen.TimeRange(time.Now(), time.Duration(100*24*365)*time.Hour),
    24  	))
    25  
    26  	properties.Property("regular time format parsable", prop.ForAll(
    27  		func(actual time.Time) (bool, error) {
    28  			str := actual.Format(time.RFC3339Nano)
    29  			parsed, err := time.Parse(time.RFC3339Nano, str)
    30  			return actual.Equal(parsed), err
    31  		},
    32  		gen.Time(),
    33  	))
    34  
    35  	properties.Property("any time format parsable", prop.ForAll(
    36  		func(actual time.Time) (bool, error) {
    37  			str := actual.Format(time.RFC3339Nano)
    38  			parsed, err := time.Parse(time.RFC3339Nano, str)
    39  			return actual.Equal(parsed), err
    40  		},
    41  		gen.AnyTime(),
    42  	))
    43  
    44  	properties.Run(gopter.ConsoleReporter(false))
    45  	// Output:
    46  	// + time in range format parsable: OK, passed 100 tests.
    47  	// + regular time format parsable: OK, passed 100 tests.
    48  	// ! any time format parsable: Error on property evaluation after 0 passed
    49  	//    tests: parsing time "10000-01-01T00:00:00Z" as
    50  	//    "2006-01-02T15:04:05.999999999Z07:00": cannot parse "0-01-01T00:00:00Z"
    51  	//    as "-"
    52  	// ARG_0: 10000-01-01 00:00:00 +0000 UTC
    53  	// ARG_0_ORIGINAL (45 shrinks): 237903042092-02-10 19:15:18.148265469 +0000
    54  	//    UTC
    55  }