OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / golang / protobuf / proto / proto3_test.go
1 // Go support for Protocol Buffers - Google's data interchange format
2 //
3 // Copyright 2014 The Go Authors.  All rights reserved.
4 // https://github.com/golang/protobuf
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are
8 // met:
9 //
10 //     * Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 //     * Redistributions in binary form must reproduce the above
13 // copyright notice, this list of conditions and the following disclaimer
14 // in the documentation and/or other materials provided with the
15 // distribution.
16 //     * Neither the name of Google Inc. nor the names of its
17 // contributors may be used to endorse or promote products derived from
18 // this software without specific prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32 package proto_test
33
34 import (
35         "testing"
36
37         "github.com/golang/protobuf/proto"
38         pb "github.com/golang/protobuf/proto/proto3_proto"
39         tpb "github.com/golang/protobuf/proto/testdata"
40 )
41
42 func TestProto3ZeroValues(t *testing.T) {
43         tests := []struct {
44                 desc string
45                 m    proto.Message
46         }{
47                 {"zero message", &pb.Message{}},
48                 {"empty bytes field", &pb.Message{Data: []byte{}}},
49         }
50         for _, test := range tests {
51                 b, err := proto.Marshal(test.m)
52                 if err != nil {
53                         t.Errorf("%s: proto.Marshal: %v", test.desc, err)
54                         continue
55                 }
56                 if len(b) > 0 {
57                         t.Errorf("%s: Encoding is non-empty: %q", test.desc, b)
58                 }
59         }
60 }
61
62 func TestRoundTripProto3(t *testing.T) {
63         m := &pb.Message{
64                 Name:         "David",          // (2 | 1<<3): 0x0a 0x05 "David"
65                 Hilarity:     pb.Message_PUNS,  // (0 | 2<<3): 0x10 0x01
66                 HeightInCm:   178,              // (0 | 3<<3): 0x18 0xb2 0x01
67                 Data:         []byte("roboto"), // (2 | 4<<3): 0x20 0x06 "roboto"
68                 ResultCount:  47,               // (0 | 7<<3): 0x38 0x2f
69                 TrueScotsman: true,             // (0 | 8<<3): 0x40 0x01
70                 Score:        8.1,              // (5 | 9<<3): 0x4d <8.1>
71
72                 Key: []uint64{1, 0xdeadbeef},
73                 Nested: &pb.Nested{
74                         Bunny: "Monty",
75                 },
76         }
77         t.Logf(" m: %v", m)
78
79         b, err := proto.Marshal(m)
80         if err != nil {
81                 t.Fatalf("proto.Marshal: %v", err)
82         }
83         t.Logf(" b: %q", b)
84
85         m2 := new(pb.Message)
86         if err := proto.Unmarshal(b, m2); err != nil {
87                 t.Fatalf("proto.Unmarshal: %v", err)
88         }
89         t.Logf("m2: %v", m2)
90
91         if !proto.Equal(m, m2) {
92                 t.Errorf("proto.Equal returned false:\n m: %v\nm2: %v", m, m2)
93         }
94 }
95
96 func TestGettersForBasicTypesExist(t *testing.T) {
97         var m pb.Message
98         if got := m.GetNested().GetBunny(); got != "" {
99                 t.Errorf("m.GetNested().GetBunny() = %q, want empty string", got)
100         }
101         if got := m.GetNested().GetCute(); got {
102                 t.Errorf("m.GetNested().GetCute() = %t, want false", got)
103         }
104 }
105
106 func TestProto3SetDefaults(t *testing.T) {
107         in := &pb.Message{
108                 Terrain: map[string]*pb.Nested{
109                         "meadow": new(pb.Nested),
110                 },
111                 Proto2Field: new(tpb.SubDefaults),
112                 Proto2Value: map[string]*tpb.SubDefaults{
113                         "badlands": new(tpb.SubDefaults),
114                 },
115         }
116
117         got := proto.Clone(in).(*pb.Message)
118         proto.SetDefaults(got)
119
120         // There are no defaults in proto3.  Everything should be the zero value, but
121         // we need to remember to set defaults for nested proto2 messages.
122         want := &pb.Message{
123                 Terrain: map[string]*pb.Nested{
124                         "meadow": new(pb.Nested),
125                 },
126                 Proto2Field: &tpb.SubDefaults{N: proto.Int64(7)},
127                 Proto2Value: map[string]*tpb.SubDefaults{
128                         "badlands": &tpb.SubDefaults{N: proto.Int64(7)},
129                 },
130         }
131
132         if !proto.Equal(got, want) {
133                 t.Errorf("with in = %v\nproto.SetDefaults(in) =>\ngot %v\nwant %v", in, got, want)
134         }
135 }