1package gopter_test
2
3import (
4	"reflect"
5
6	"github.com/leanovate/gopter"
7	"github.com/leanovate/gopter/arbitrary"
8	"github.com/leanovate/gopter/gen"
9)
10
11type TestBook struct {
12	Title   string
13	Content string
14}
15
16func genTestBook() gopter.Gen {
17	return gen.Struct(reflect.TypeOf(&TestBook{}), map[string]gopter.Gen{
18		"Title":   gen.AlphaString(),
19		"Content": gen.AlphaString(),
20	})
21}
22
23type TestLibrary struct {
24	Name       string
25	Librarians uint8
26	Books      []TestBook
27}
28
29func genTestLibrary() gopter.Gen {
30	return gen.Struct(reflect.TypeOf(&TestLibrary{}), map[string]gopter.Gen{
31		"Name":       gen.AlphaString(),
32		"Librarians": gen.UInt8Range(1, 255),
33		"Books":      gen.SliceOf(genTestBook()),
34	})
35}
36
37type CityName = string
38type TestCities struct {
39	Libraries map[CityName][]TestLibrary
40}
41
42func genTestCities() gopter.Gen {
43	return gen.StructPtr(reflect.TypeOf(&TestCities{}), map[string]gopter.Gen{
44		"Libraries": (gen.MapOf(gen.AlphaString(), gen.SliceOf(genTestLibrary()))),
45	})
46}
47func Example_libraries() {
48	parameters := gopter.DefaultTestParameters()
49	parameters.Rng.Seed(1234) // Just for this example to generate reproducable results
50	parameters.MaxSize = 5
51	arbitraries := arbitrary.DefaultArbitraries()
52	arbitraries.RegisterGen(genTestCities())
53
54	properties := gopter.NewProperties(parameters)
55
56	properties.Property("no unsupervised libraries", arbitraries.ForAll(
57		func(tc *TestCities) bool {
58			for _, libraries := range tc.Libraries {
59				for _, library := range libraries {
60					if library.Librarians == 0 {
61						return false
62					}
63				}
64			}
65			return true
66		},
67	))
68
69	// When using testing.T you might just use: properties.TestingRun(t)
70	properties.Run(gopter.ConsoleReporter(false))
71	// Output:
72	// + no unsupervised libraries: OK, passed 100 tests.
73}
74
75func Example_libraries2() {
76	parameters := gopter.DefaultTestParameters()
77	parameters.Rng.Seed(1234) // Just for this example to generate reproducable results
78
79	arbitraries := arbitrary.DefaultArbitraries()
80	// All string are alphanumeric
81	arbitraries.RegisterGen(gen.AlphaString())
82
83	properties := gopter.NewProperties(parameters)
84
85	properties.Property("libraries always empty", arbitraries.ForAll(
86		func(tc *TestCities) bool {
87			return len(tc.Libraries) == 0
88		},
89	))
90
91	// When using testing.T you might just use: properties.TestingRun(t)
92	properties.Run(gopter.ConsoleReporter(false))
93	// Output:
94	// ! libraries always empty: Falsified after 2 passed tests.
95	// ARG_0: &{map[z:[]]}
96}
97