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 package proto_test
33
34 import (
35 "bytes"
36 "testing"
37
38 "github.com/gogo/protobuf/proto"
39 pb "github.com/gogo/protobuf/proto/proto3_proto"
40 tpb "github.com/gogo/protobuf/proto/test_proto"
41 )
42
43 func TestProto3ZeroValues(t *testing.T) {
44 tests := []struct {
45 desc string
46 m proto.Message
47 }{
48 {"zero message", &pb.Message{}},
49 {"empty bytes field", &pb.Message{Data: []byte{}}},
50 }
51 for _, test := range tests {
52 b, err := proto.Marshal(test.m)
53 if err != nil {
54 t.Errorf("%s: proto.Marshal: %v", test.desc, err)
55 continue
56 }
57 if len(b) > 0 {
58 t.Errorf("%s: Encoding is non-empty: %q", test.desc, b)
59 }
60 }
61 }
62
63 func TestRoundTripProto3(t *testing.T) {
64 m := &pb.Message{
65 Name: "David",
66 Hilarity: pb.Message_PUNS,
67 HeightInCm: 178,
68 Data: []byte("roboto"),
69 ResultCount: 47,
70 TrueScotsman: true,
71 Score: 8.1,
72
73 Key: []uint64{1, 0xdeadbeef},
74 Nested: &pb.Nested{
75 Bunny: "Monty",
76 },
77 }
78 t.Logf(" m: %v", m)
79
80 b, err := proto.Marshal(m)
81 if err != nil {
82 t.Fatalf("proto.Marshal: %v", err)
83 }
84 t.Logf(" b: %q", b)
85
86 m2 := new(pb.Message)
87 if err := proto.Unmarshal(b, m2); err != nil {
88 t.Fatalf("proto.Unmarshal: %v", err)
89 }
90 t.Logf("m2: %v", m2)
91
92 if !proto.Equal(m, m2) {
93 t.Errorf("proto.Equal returned false:\n m: %v\nm2: %v", m, m2)
94 }
95 }
96
97 func TestGettersForBasicTypesExist(t *testing.T) {
98 var m pb.Message
99 if got := m.GetNested().GetBunny(); got != "" {
100 t.Errorf("m.GetNested().GetBunny() = %q, want empty string", got)
101 }
102 if got := m.GetNested().GetCute(); got {
103 t.Errorf("m.GetNested().GetCute() = %t, want false", got)
104 }
105 }
106
107 func TestProto3SetDefaults(t *testing.T) {
108 in := &pb.Message{
109 Terrain: map[string]*pb.Nested{
110 "meadow": new(pb.Nested),
111 },
112 Proto2Field: new(tpb.SubDefaults),
113 Proto2Value: map[string]*tpb.SubDefaults{
114 "badlands": new(tpb.SubDefaults),
115 },
116 }
117
118 got := proto.Clone(in).(*pb.Message)
119 proto.SetDefaults(got)
120
121
122
123 want := &pb.Message{
124 Terrain: map[string]*pb.Nested{
125 "meadow": new(pb.Nested),
126 },
127 Proto2Field: &tpb.SubDefaults{N: proto.Int64(7)},
128 Proto2Value: map[string]*tpb.SubDefaults{
129 "badlands": {N: proto.Int64(7)},
130 },
131 }
132
133 if !proto.Equal(got, want) {
134 t.Errorf("with in = %v\nproto.SetDefaults(in) =>\ngot %v\nwant %v", in, got, want)
135 }
136 }
137
138 func TestUnknownFieldPreservation(t *testing.T) {
139 b1 := "\x0a\x05David"
140 b2 := "\xc2\x0c\x06Google"
141 b := []byte(b1 + b2)
142
143 m := new(pb.Message)
144 if err := proto.Unmarshal(b, m); err != nil {
145 t.Fatalf("proto.Unmarshal: %v", err)
146 }
147
148 if !bytes.Equal(m.XXX_unrecognized, []byte(b2)) {
149 t.Fatalf("mismatching unknown fields:\ngot %q\nwant %q", m.XXX_unrecognized, b2)
150 }
151 }
152
View as plain text