-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathmarshal_test.go
108 lines (98 loc) · 2.09 KB
/
marshal_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
107
108
package protoutils_test
import (
"encoding/json"
"fmt"
"github.com/gogo/protobuf/proto"
"github.com/gogo/protobuf/types"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/solo-io/go-utils/protoutils"
)
type testType struct {
A string
B b
}
func (t testType) Reset() {}
func (t testType) String() string {
byt, _ := json.Marshal(t)
return string(byt)
}
func (t testType) ProtoMessage() {}
type b struct {
C string
D string
}
var tests = []struct {
in interface{}
expected proto.Message
}{
{
in: testType{
A: "a",
B: b{
C: "c",
D: "d",
},
},
expected: &types.Struct{
Fields: map[string]*types.Value{
"A": {
Kind: &types.Value_StringValue{StringValue: "a"},
},
"B": {
Kind: &types.Value_StructValue{
StructValue: &types.Struct{
Fields: map[string]*types.Value{
"C": {
Kind: &types.Value_StringValue{StringValue: "c"},
},
"D": {
Kind: &types.Value_StringValue{StringValue: "d"},
},
},
},
},
},
},
},
},
{
in: map[string]interface{}{
"A": "a",
"B": b{
C: "c",
D: "d",
},
},
expected: &types.Struct{
Fields: map[string]*types.Value{
"a": {
Kind: &types.Value_StringValue{StringValue: "b"},
},
"c": {
Kind: &types.Value_StringValue{StringValue: "d"},
},
},
},
},
}
// TODO: Fix this text
// This test is temporarily out of order because it assumed an old version of MarshalStruct which took random data
// However, it now takes a proto.message to begin with. So the setup is no longer valid
var _ = PDescribe("Protoutil Funcs", func() {
Describe("MarshalStruct", func() {
for _, test := range tests {
It("returns a pb struct for object of the given type", func() {
var tt testType
byt, err := json.Marshal(test.in)
fmt.Printf("byte: %s", byt)
Expect(err).NotTo(HaveOccurred())
err = UnmarshalBytes(byt, &tt)
Expect(err).NotTo(HaveOccurred())
pb, err := MarshalStruct(&tt)
Expect(err).NotTo(HaveOccurred())
Expect(pb).To(Equal(test.expected))
})
}
})
})