Skip to content

Commit

Permalink
Add default arbitraries for maps
Browse files Browse the repository at this point in the history
  • Loading branch information
untoldwind committed Jan 15, 2019
1 parent 2e0ff0c commit c8df375
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
4 changes: 4 additions & 0 deletions arbitrary/gen_for_kind.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,10 @@ func (a *Arbitraries) genForKind(rt reflect.Type) gopter.Gen {
}
}
return gen.Struct(rt, gens)
case reflect.Map:
keyGen := a.GenForType(rt.Key())
valueGen := a.GenForType(rt.Elem())
return gen.MapOf(keyGen, valueGen)
}
return nil
}
25 changes: 24 additions & 1 deletion example_libraries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func genTestLibrary() gopter.Gen {

type CityName = string
type TestCities struct {
Libraries map[string][]TestLibrary
Libraries map[CityName][]TestLibrary
}

func genTestCities() gopter.Gen {
Expand Down Expand Up @@ -71,3 +71,26 @@ func Example_libraries() {
// Output:
// + no unsupervised libraries: OK, passed 100 tests.
}

func Example_libraries2() {
parameters := gopter.DefaultTestParameters()
parameters.Rng.Seed(1234) // Just for this example to generate reproducable results

arbitraries := arbitrary.DefaultArbitraries()
// All string are alphanumeric
arbitraries.RegisterGen(gen.AlphaString())

properties := gopter.NewProperties(parameters)

properties.Property("libraries always empty", arbitraries.ForAll(
func(tc *TestCities) bool {
return len(tc.Libraries) == 0
},
))

// When using testing.T you might just use: properties.TestingRun(t)
properties.Run(gopter.ConsoleReporter(false))
// Output:
// ! libraries always empty: Falsified after 2 passed tests.
// ARG_0: &{map[z:[]]}
}

0 comments on commit c8df375

Please sign in to comment.