-
Notifications
You must be signed in to change notification settings - Fork 5
/
rand_test.go
106 lines (97 loc) · 2.41 KB
/
rand_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
100
101
102
103
104
105
106
package protorand
//go:generate go run github.com/bufbuild/buf/cmd/[email protected] generate
import (
"testing"
"google.golang.org/protobuf/proto"
testpb "github.com/sryoya/protorand/testdata"
)
func TestEmbedValues(t *testing.T) {
p := New()
p.Seed(0)
input := &testpb.TestMessage{}
res, err := p.Gen(input)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
// check if the input is not mutated.
if !proto.Equal(input, &testpb.TestMessage{}) {
t.Errorf("The input was unexpectedly mutated.")
}
got := res.(*testpb.TestMessage)
// assert all the fields got some value
if got.SomeInt32 == 0 {
t.Errorf("Field SomeInt32 is not set")
}
if got.SomeSint32 == 0 {
t.Errorf("Field SomeSint32 is not set")
}
if got.SomeUint32 == 0 {
t.Errorf("Field SomeUint32 is not set")
}
if got.SomeFloat32 == 0 {
t.Errorf("Field SomeFloat32 is not set")
}
if got.SomeFixed32 == 0 {
t.Errorf("Field SomeFixed32 is not set")
}
if got.SomeSfixed32 == 0 {
t.Errorf("Field SomeSfixed32 is not set")
}
if got.SomeInt64 == 0 {
t.Errorf("Field SomeInt64 is not set")
}
if got.SomeSint64 == 0 {
t.Errorf("Field SomeSint64 is not set")
}
if got.SomeUint64 == 0 {
t.Errorf("Field SomeUint64 is not set")
}
if got.SomeFloat64 == 0 {
t.Errorf("Field SomeFloat64 is not set")
}
if got.SomeFixed64 == 0 {
t.Errorf("Field SomeFixed64 is not set")
}
if got.SomeSfixed64 == 0 {
t.Errorf("Field SomeSfixed64 is not set")
}
if got.SomeStr == "" {
t.Errorf("Field SomeStr is not set")
}
if got.SomeMsg == nil {
t.Errorf("Field SomeMsg is not set")
}
if got.SomeMsg.SomeInt == 0 {
t.Errorf("Field SomeMsg.SomeInt is not set")
}
if got.SomeMsg.SubChild == nil {
t.Errorf("Field SomeMsg.SubChild is not set")
}
if got.SomeMsg.SubChild.SomeInt == 0 {
t.Errorf("Field SomeMsg.SubChild.SomeInt is not set")
}
if len(got.SomeSlice) == 0 {
t.Errorf("Field SomeSlice is not set")
}
if len(got.SomeMsgs) == 0 {
t.Errorf("Field SomeMsgs is not set")
}
if len(got.SomeMap) == 0 {
t.Errorf("Field SomeMap is not set")
}
if got.SomeEnum > 3 { // undeclared enum value
t.Errorf("Field SomeEnum is not set")
}
if got.SomeEnum2 > 1 { // undeclared enum value
t.Errorf("Field SomeEnum2 is not set")
}
if got.SomeOneOf == nil {
t.Errorf("Field SomeOneOf is not set")
}
if got.Timestamp == nil {
t.Errorf("Field Timestamp is not set")
}
if got.Duration == nil {
t.Errorf("Field Duration is not set")
}
}