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 package issue449
30
31 import (
32 "testing"
33
34 "github.com/gogo/protobuf/proto"
35 )
36
37 func TestCodeGenMsgMarshalUnmarshal(t *testing.T) {
38 src := &CodeGenMsg{
39 Int64ReqPtr: proto.Int64(111),
40 Int32OptPtr: proto.Int32(222),
41 Int64Req: 333,
42 Int32Opt: 444,
43 }
44 buf, err := proto.Marshal(src)
45 if err != nil {
46 t.Fatal(err)
47 }
48 dst := &CodeGenMsg{}
49 if err := proto.Unmarshal(buf, dst); err != nil {
50 t.Fatal(err)
51 }
52 if !src.Equal(dst) {
53 t.Fatal("message is not equals")
54 }
55 }
56
57 func TestNonCodeGenMsgMarshalUnmarshal(t *testing.T) {
58 src := &NonCodeGenMsg{
59 Int64ReqPtr: proto.Int64(111),
60 Int32OptPtr: proto.Int32(222),
61 Int64Req: 333,
62 Int32Opt: 444,
63 }
64 buf, err := proto.Marshal(src)
65 if err != nil {
66 t.Fatal(err)
67 }
68 dst := &NonCodeGenMsg{}
69 if err := proto.Unmarshal(buf, dst); err != nil {
70 t.Fatal(err)
71 }
72 if !src.Equal(dst) {
73 t.Fatal("message is not equals")
74 }
75 }
76
77 func TestRequiredFieldCheck(t *testing.T) {
78 tbl := []struct {
79 In proto.Message
80 Success bool
81 }{
82
83 {
84
85 In: &CodeGenMsg{
86 Int64ReqPtr: proto.Int64(111),
87 Int32OptPtr: proto.Int32(222),
88 Int64Req: 333,
89 Int32Opt: 444,
90 },
91 Success: true,
92 },
93 {
94
95 In: &CodeGenMsg{
96 Int64ReqPtr: proto.Int64(0),
97 Int32OptPtr: proto.Int32(0),
98 Int64Req: 0,
99 Int32Opt: 0,
100 },
101 Success: true,
102 },
103 {
104
105 In: &CodeGenMsg{
106 Int64Req: 333,
107 },
108 Success: false,
109 },
110 {
111
112 In: &CodeGenMsg{
113 Int64ReqPtr: proto.Int64(111),
114 },
115 Success: true,
116 },
117
118
119 {
120
121 In: &NonCodeGenMsg{
122 Int64ReqPtr: proto.Int64(111),
123 Int32OptPtr: proto.Int32(222),
124 Int64Req: 333,
125 Int32Opt: 444,
126 },
127 Success: true,
128 },
129 {
130
131 In: &NonCodeGenMsg{
132 Int64ReqPtr: proto.Int64(0),
133 Int32OptPtr: proto.Int32(0),
134 Int64Req: 0,
135 Int32Opt: 0,
136 },
137 Success: true,
138 },
139 {
140
141 In: &NonCodeGenMsg{
142 Int64Req: 333,
143 },
144 Success: false,
145 },
146 {
147
148 In: &NonCodeGenMsg{
149 Int64ReqPtr: proto.Int64(111),
150 },
151 Success: true,
152 },
153 }
154 for i, v := range tbl {
155 _, err := proto.Marshal(v.In)
156 switch v.Success {
157 case true:
158 if err != nil {
159 t.Fatalf("[%d] failed to proto.Marshal(%v), %s", i, v.In, err)
160 }
161 case false:
162 if err == nil {
163 t.Fatalf("[%d] required field check is not working", i)
164 }
165 if _, ok := err.(*proto.RequiredNotSetError); !ok {
166 t.Fatalf("[%d] failed to proto.Marshal(%v), %s", i, v.In, err)
167 }
168 }
169 }
170 }
171
View as plain text