-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvery_custom_struct_test.go
86 lines (72 loc) · 1.53 KB
/
very_custom_struct_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
package pb_fuzz_workshop
import (
"fmt"
"testing"
"pgregory.net/rapid"
)
type point struct {
x int
y int
}
type circle struct {
center point
radius int
}
type line struct {
a point
b point
}
type shape interface {
Print()
}
func (c circle) Print() {
fmt.Printf("center %v radius: %v\n", c.center, c.radius)
}
func (l line) Print() {
fmt.Printf("a %v b: %v\n", l.a, l.b)
}
func GeneratePoint() *rapid.Generator {
return rapid.Custom(func(t *rapid.T) point {
return point{
x: rapid.Int().Draw(t, "point_x").(int),
y: rapid.Int().Draw(t, "point_y").(int),
}
})
}
func GenerateCircle() *rapid.Generator {
return rapid.Custom(func(t *rapid.T) circle {
return circle{
center: point{
x: rapid.Int().Draw(t, "circle_x").(int),
y: rapid.Int().Draw(t, "circle_y").(int),
},
radius: rapid.Int().Draw(t, "rad").(int),
}
})
}
func GenerateLine() *rapid.Generator {
return rapid.Custom(func(t *rapid.T) line {
return line{
a: point{
x: rapid.Int().Draw(t, "line_x").(int),
y: rapid.Int().Draw(t, "line_y").(int),
},
b: GeneratePoint().Draw(t, "line_b").(point),
}
})
}
func GenerateShape() *rapid.Generator {
return rapid.Custom(func(t *rapid.T) shape {
return rapid.OneOf(GenerateCircle(), GenerateLine()).Draw(t, "shape").(shape)
})
}
func TestCustomStruct(t *testing.T) {
rapid.Check(t, func(t *rapid.T) {
sp := rapid.SliceOf(GenerateShape()).Draw(t, "slice of points").([]shape)
fmt.Println("-----------")
for i := range sp {
sp[i].Print()
}
fmt.Println("-----------")
})
}