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