OSDN Git Service

Merge pull request #41 from Bytom/dev
[bytom/vapor.git] / vendor / github.com / golang / protobuf / protoc-gen-go / testdata / extension_test.go
1 // Go support for Protocol Buffers - Google's data interchange format
2 //
3 // Copyright 2010 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 // Test that we can use protocol buffers that use extensions.
33
34 package testdata
35
36 /*
37
38 import (
39         "bytes"
40         "regexp"
41         "testing"
42
43         "github.com/golang/protobuf/proto"
44         base "extension_base.pb"
45         user "extension_user.pb"
46 )
47
48 func TestSingleFieldExtension(t *testing.T) {
49         bm := &base.BaseMessage{
50                 Height: proto.Int32(178),
51         }
52
53         // Use extension within scope of another type.
54         vol := proto.Uint32(11)
55         err := proto.SetExtension(bm, user.E_LoudMessage_Volume, vol)
56         if err != nil {
57                 t.Fatal("Failed setting extension:", err)
58         }
59         buf, err := proto.Marshal(bm)
60         if err != nil {
61                 t.Fatal("Failed encoding message with extension:", err)
62         }
63         bm_new := new(base.BaseMessage)
64         if err := proto.Unmarshal(buf, bm_new); err != nil {
65                 t.Fatal("Failed decoding message with extension:", err)
66         }
67         if !proto.HasExtension(bm_new, user.E_LoudMessage_Volume) {
68                 t.Fatal("Decoded message didn't contain extension.")
69         }
70         vol_out, err := proto.GetExtension(bm_new, user.E_LoudMessage_Volume)
71         if err != nil {
72                 t.Fatal("Failed getting extension:", err)
73         }
74         if v := vol_out.(*uint32); *v != *vol {
75                 t.Errorf("vol_out = %v, expected %v", *v, *vol)
76         }
77         proto.ClearExtension(bm_new, user.E_LoudMessage_Volume)
78         if proto.HasExtension(bm_new, user.E_LoudMessage_Volume) {
79                 t.Fatal("Failed clearing extension.")
80         }
81 }
82
83 func TestMessageExtension(t *testing.T) {
84         bm := &base.BaseMessage{
85                 Height: proto.Int32(179),
86         }
87
88         // Use extension that is itself a message.
89         um := &user.UserMessage{
90                 Name: proto.String("Dave"),
91                 Rank: proto.String("Major"),
92         }
93         err := proto.SetExtension(bm, user.E_LoginMessage_UserMessage, um)
94         if err != nil {
95                 t.Fatal("Failed setting extension:", err)
96         }
97         buf, err := proto.Marshal(bm)
98         if err != nil {
99                 t.Fatal("Failed encoding message with extension:", err)
100         }
101         bm_new := new(base.BaseMessage)
102         if err := proto.Unmarshal(buf, bm_new); err != nil {
103                 t.Fatal("Failed decoding message with extension:", err)
104         }
105         if !proto.HasExtension(bm_new, user.E_LoginMessage_UserMessage) {
106                 t.Fatal("Decoded message didn't contain extension.")
107         }
108         um_out, err := proto.GetExtension(bm_new, user.E_LoginMessage_UserMessage)
109         if err != nil {
110                 t.Fatal("Failed getting extension:", err)
111         }
112         if n := um_out.(*user.UserMessage).Name; *n != *um.Name {
113                 t.Errorf("um_out.Name = %q, expected %q", *n, *um.Name)
114         }
115         if r := um_out.(*user.UserMessage).Rank; *r != *um.Rank {
116                 t.Errorf("um_out.Rank = %q, expected %q", *r, *um.Rank)
117         }
118         proto.ClearExtension(bm_new, user.E_LoginMessage_UserMessage)
119         if proto.HasExtension(bm_new, user.E_LoginMessage_UserMessage) {
120                 t.Fatal("Failed clearing extension.")
121         }
122 }
123
124 func TestTopLevelExtension(t *testing.T) {
125         bm := &base.BaseMessage{
126                 Height: proto.Int32(179),
127         }
128
129         width := proto.Int32(17)
130         err := proto.SetExtension(bm, user.E_Width, width)
131         if err != nil {
132                 t.Fatal("Failed setting extension:", err)
133         }
134         buf, err := proto.Marshal(bm)
135         if err != nil {
136                 t.Fatal("Failed encoding message with extension:", err)
137         }
138         bm_new := new(base.BaseMessage)
139         if err := proto.Unmarshal(buf, bm_new); err != nil {
140                 t.Fatal("Failed decoding message with extension:", err)
141         }
142         if !proto.HasExtension(bm_new, user.E_Width) {
143                 t.Fatal("Decoded message didn't contain extension.")
144         }
145         width_out, err := proto.GetExtension(bm_new, user.E_Width)
146         if err != nil {
147                 t.Fatal("Failed getting extension:", err)
148         }
149         if w := width_out.(*int32); *w != *width {
150                 t.Errorf("width_out = %v, expected %v", *w, *width)
151         }
152         proto.ClearExtension(bm_new, user.E_Width)
153         if proto.HasExtension(bm_new, user.E_Width) {
154                 t.Fatal("Failed clearing extension.")
155         }
156 }
157
158 func TestMessageSetWireFormat(t *testing.T) {
159         osm := new(base.OldStyleMessage)
160         osp := &user.OldStyleParcel{
161                 Name:   proto.String("Dave"),
162                 Height: proto.Int32(178),
163         }
164
165         err := proto.SetExtension(osm, user.E_OldStyleParcel_MessageSetExtension, osp)
166         if err != nil {
167                 t.Fatal("Failed setting extension:", err)
168         }
169
170         buf, err := proto.Marshal(osm)
171         if err != nil {
172                 t.Fatal("Failed encoding message:", err)
173         }
174
175         // Data generated from Python implementation.
176         expected := []byte{
177                 11, 16, 209, 15, 26, 9, 10, 4, 68, 97, 118, 101, 16, 178, 1, 12,
178         }
179
180         if !bytes.Equal(expected, buf) {
181                 t.Errorf("Encoding mismatch.\nwant %+v\n got %+v", expected, buf)
182         }
183
184         // Check that it is restored correctly.
185         osm = new(base.OldStyleMessage)
186         if err := proto.Unmarshal(buf, osm); err != nil {
187                 t.Fatal("Failed decoding message:", err)
188         }
189         osp_out, err := proto.GetExtension(osm, user.E_OldStyleParcel_MessageSetExtension)
190         if err != nil {
191                 t.Fatal("Failed getting extension:", err)
192         }
193         osp = osp_out.(*user.OldStyleParcel)
194         if *osp.Name != "Dave" || *osp.Height != 178 {
195                 t.Errorf("Retrieved extension from decoded message is not correct: %+v", osp)
196         }
197 }
198
199 func main() {
200         // simpler than rigging up gotest
201         testing.Main(regexp.MatchString, []testing.InternalTest{
202                 {"TestSingleFieldExtension", TestSingleFieldExtension},
203                 {"TestMessageExtension", TestMessageExtension},
204                 {"TestTopLevelExtension", TestTopLevelExtension},
205         },
206                 []testing.InternalBenchmark{},
207                 []testing.InternalExample{})
208 }
209
210 */