-
Notifications
You must be signed in to change notification settings - Fork 40
/
properties.go
59 lines (51 loc) · 1.51 KB
/
properties.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
package gopter
import "testing"
// Properties is a collection of properties that should be checked in a test
type Properties struct {
parameters *TestParameters
props map[string]Prop
propNames []string
}
// NewProperties create new Properties with given test parameters.
// If parameters is nil default test parameters will be used
func NewProperties(parameters *TestParameters) *Properties {
if parameters == nil {
parameters = DefaultTestParameters()
}
return &Properties{
parameters: parameters,
props: make(map[string]Prop, 0),
propNames: make([]string, 0),
}
}
// Property add/defines a property in a test.
func (p *Properties) Property(name string, prop Prop) {
p.propNames = append(p.propNames, name)
p.props[name] = prop
}
// Run checks all definied propertiesand reports the result
func (p *Properties) Run(reporter Reporter) bool {
success := true
for _, propName := range p.propNames {
prop := p.props[propName]
result := prop.Check(p.parameters)
reporter.ReportTestResult(propName, result)
if !result.Passed() {
success = false
}
}
return success
}
// TestingRun checks all definied properties with a testing.T context.
// This the preferred wait to run property tests as part of a go unit test.
func (p *Properties) TestingRun(t *testing.T, opts ...interface{}) {
reporter := ConsoleReporter(true)
for _, opt := range opts {
if r, ok := opt.(Reporter); ok {
reporter = r
}
}
if !p.Run(reporter) {
t.Errorf("failed with initial seed: %d", p.parameters.Seed())
}
}