Skip to content

Commit

Permalink
Fix: handling of *time.Time
Browse files Browse the repository at this point in the history
  • Loading branch information
untoldwind committed Jan 24, 2020
1 parent f24d09d commit f08e9cd
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 22 deletions.
9 changes: 2 additions & 7 deletions arbitrary/arbitraries.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,8 @@ type Arbitraries struct {
func DefaultArbitraries() *Arbitraries {
return &Arbitraries{
generators: map[reflect.Type]gopter.Gen{
reflect.TypeOf(time.Time{}): gen.Time(),
reflect.TypeOf(&time.Time{}): gen.Time().
Map(func(time time.Time) *time.Time { return &time }).
WithShrinker(func(v interface{}) gopter.Shrink {
t := v.(*time.Time)
return gen.TimeShrinker(*t)
}),
reflect.TypeOf(time.Time{}): gen.Time(),
reflect.TypeOf(&time.Time{}): gen.PtrOf(gen.Time()),
},
}
}
Expand Down
41 changes: 29 additions & 12 deletions arbitrary/example_arbitrary_struct_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package arbitrary_test

import (
"fmt"
"time"

"github.com/leanovate/gopter"
"github.com/leanovate/gopter/arbitrary"
)
Expand All @@ -16,18 +19,25 @@ type MyUInt32Type uint32
type MyUInt64Type uint64

type Foo struct {
Name MyStringType
Id1 MyInt8Type
Id2 MyInt16Type
Id3 MyInt32Type
Id4 MyInt64Type
Id5 MyUInt8Type
Id6 MyUInt16Type
Id7 MyUInt32Type
Id8 MyUInt64Type
Name MyStringType
Id1 MyInt8Type
Id2 MyInt16Type
Id3 MyInt32Type
Id4 MyInt64Type
Id5 MyUInt8Type
Id6 MyUInt16Type
Id7 MyUInt32Type
Id8 MyUInt64Type
ATime time.Time
ATimePtr *time.Time
}

func (f Foo) ToString() string {
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)
}

func Example_arbitrary_structs() {
time.Local = time.UTC
parameters := gopter.DefaultTestParametersWithSeed(1234) // Example should generate reproducible results, otherwise DefaultTestParameters() will suffice

arbitraries := arbitrary.DefaultArbitraries()
Expand All @@ -44,11 +54,11 @@ func Example_arbitrary_structs() {
}))
properties.Property("Foo", arbitraries.ForAll(
func(foo *Foo) bool {
return true
return foo.ATime.After(time.Unix(0, 0))
}))
properties.Property("Foo2", arbitraries.ForAll(
func(foo Foo) bool {
return true
return foo.ATimePtr == nil || foo.ATimePtr.Before(time.Unix(20000, 0))
}))

properties.Run(gopter.ConsoleReporter(false))
Expand All @@ -60,5 +70,12 @@ func Example_arbitrary_structs() {
// ARG_0: 2000
// ARG_0_ORIGINAL (23 shrinks): 2161922319
// + Foo: OK, passed 100 tests.
// + Foo2: OK, passed 100 tests.
// ! Foo2: Falsified after 1 passed tests.
// ARG_0: {Name: Id1:0 Id2:0 Id3:0 Id4:0 Id5:0 Id6:0 Id7:0 Id8:0
// ATime:1970-01-01 00:00:00 +0000 UTC ATimePtr:1970-01-01 05:33:20 +0000
// UTC}
// ARG_0_ORIGINAL (40 shrinks): {Name: Id1:-67 Id2:27301 Id3:-1350752892
// Id4:7128486677722156226 Id5:208 Id6:28663 Id7:4178604448
// Id8:16360504079646654692 ATime:2239-08-20 23:46:28.063412239 +0000 UTC
// ATimePtr:5468-08-19 13:09:39.171622464 +0000 UTC}
}
2 changes: 1 addition & 1 deletion example_libraries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,5 @@ func Example_libraries2() {
properties.Run(gopter.ConsoleReporter(false))
// Output:
// ! libraries always empty: Falsified after 2 passed tests.
// ARG_0: &{map[z:[]]}
// ARG_0: &{Libraries:map[z:[]]}
}
4 changes: 2 additions & 2 deletions formated_reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ func (r *FormatedReporter) reportPropArg(idx int, propArg *PropArg) string {
if label == "" {
label = fmt.Sprintf("ARG_%d", idx)
}
result := fmt.Sprintf("%s: %v", label, propArg.Arg)
result := fmt.Sprintf("%s: %+v", label, propArg.Arg)
if propArg.Shrinks > 0 {
result += fmt.Sprintf("\n%s_ORIGINAL (%d shrinks): %v", label, propArg.Shrinks, propArg.OrigArg)
result += fmt.Sprintf("\n%s_ORIGINAL (%d shrinks): %+v", label, propArg.Shrinks, propArg.OrigArg)
}

return result
Expand Down

0 comments on commit f08e9cd

Please sign in to comment.