blob: 85c7d56106f1c28c768effe695da057fcd16eda4 [file] [log] [blame]
David Symondsc37ad662010-04-07 09:25:13 +10001// Go support for Protocol Buffers - Google's data interchange format
2//
3// Copyright 2010 Google Inc. All rights reserved.
4// http://code.google.com/p/goprotobuf/
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
34package main
35
36import (
37 "testing"
38
39 "goprotobuf.googlecode.com/hg/proto"
40 base "extension_base.pb"
41 user "extension_user.pb"
42)
43
44func TestSingleFieldExtension(t *testing.T) {
Rob Pikec6d8e4a2010-07-28 15:34:32 -070045 bm := &base.BaseMessage{}
David Symondsc37ad662010-04-07 09:25:13 +100046 bm.Height = proto.Int32(178)
47
48 // Use extension within scope of another type.
49 vol := proto.Uint32(11)
David Symonds9bd0a832010-04-11 10:02:32 +100050 err := proto.SetExtension(bm, user.E_LoudMessage_Volume, vol)
David Symondsc37ad662010-04-07 09:25:13 +100051 if err != nil {
52 t.Fatal("Failed setting extension:", err)
53 }
54 buf, err := proto.Marshal(bm)
55 if err != nil {
56 t.Fatal("Failed encoding message with extension:", err)
57 }
Rob Pikec6d8e4a2010-07-28 15:34:32 -070058 bm_new := &base.BaseMessage{}
David Symondsc37ad662010-04-07 09:25:13 +100059 if err := proto.Unmarshal(buf, bm_new); err != nil {
60 t.Fatal("Failed decoding message with extension:", err)
61 }
David Symonds9bd0a832010-04-11 10:02:32 +100062 if !proto.HasExtension(bm_new, user.E_LoudMessage_Volume) {
David Symondsc37ad662010-04-07 09:25:13 +100063 t.Fatal("Decoded message didn't contain extension.")
64 }
David Symonds9bd0a832010-04-11 10:02:32 +100065 vol_out, err := proto.GetExtension(bm_new, user.E_LoudMessage_Volume)
David Symondsc37ad662010-04-07 09:25:13 +100066 if err != nil {
67 t.Fatal("Failed getting extension:", err)
68 }
69 if v := vol_out.(*uint32); *v != *vol {
70 t.Errorf("vol_out = %v, expected %v", *v, *vol)
71 }
David Symonds9bd0a832010-04-11 10:02:32 +100072 proto.ClearExtension(bm_new, user.E_LoudMessage_Volume)
73 if proto.HasExtension(bm_new, user.E_LoudMessage_Volume) {
David Symondsc37ad662010-04-07 09:25:13 +100074 t.Fatal("Failed clearing extension.")
75 }
76}
77
78func TestMessageExtension(t *testing.T) {
Rob Pikec6d8e4a2010-07-28 15:34:32 -070079 bm := &base.BaseMessage{}
David Symondsc37ad662010-04-07 09:25:13 +100080 bm.Height = proto.Int32(179)
81
82 // Use extension that is itself a message.
83 um := &user.UserMessage{
84 Name: proto.String("Dave"),
85 Rank: proto.String("Major"),
86 }
David Symonds9bd0a832010-04-11 10:02:32 +100087 err := proto.SetExtension(bm, user.E_LoginMessage_UserMessage, um)
David Symondsc37ad662010-04-07 09:25:13 +100088 if err != nil {
89 t.Fatal("Failed setting extension:", err)
90 }
91 buf, err := proto.Marshal(bm)
92 if err != nil {
93 t.Fatal("Failed encoding message with extension:", err)
94 }
Rob Pikec6d8e4a2010-07-28 15:34:32 -070095 bm_new := &base.BaseMessage{}
David Symondsc37ad662010-04-07 09:25:13 +100096 if err := proto.Unmarshal(buf, bm_new); err != nil {
97 t.Fatal("Failed decoding message with extension:", err)
98 }
David Symonds9bd0a832010-04-11 10:02:32 +100099 if !proto.HasExtension(bm_new, user.E_LoginMessage_UserMessage) {
David Symondsc37ad662010-04-07 09:25:13 +1000100 t.Fatal("Decoded message didn't contain extension.")
101 }
David Symonds9bd0a832010-04-11 10:02:32 +1000102 um_out, err := proto.GetExtension(bm_new, user.E_LoginMessage_UserMessage)
David Symondsc37ad662010-04-07 09:25:13 +1000103 if err != nil {
104 t.Fatal("Failed getting extension:", err)
105 }
106 if n := um_out.(*user.UserMessage).Name; *n != *um.Name {
107 t.Errorf("um_out.Name = %q, expected %q", *n, *um.Name)
108 }
109 if r := um_out.(*user.UserMessage).Rank; *r != *um.Rank {
110 t.Errorf("um_out.Rank = %q, expected %q", *r, *um.Rank)
111 }
David Symonds9bd0a832010-04-11 10:02:32 +1000112 proto.ClearExtension(bm_new, user.E_LoginMessage_UserMessage)
113 if proto.HasExtension(bm_new, user.E_LoginMessage_UserMessage) {
David Symondsc37ad662010-04-07 09:25:13 +1000114 t.Fatal("Failed clearing extension.")
115 }
116}
117
118func TestTopLevelExtension(t *testing.T) {
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700119 bm := &base.BaseMessage{}
David Symondsc37ad662010-04-07 09:25:13 +1000120 bm.Height = proto.Int32(179)
121
122 width := proto.Int32(17)
123 err := proto.SetExtension(bm, user.E_Width, width)
124 if err != nil {
125 t.Fatal("Failed setting extension:", err)
126 }
127 buf, err := proto.Marshal(bm)
128 if err != nil {
129 t.Fatal("Failed encoding message with extension:", err)
130 }
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700131 bm_new := &base.BaseMessage{}
David Symondsc37ad662010-04-07 09:25:13 +1000132 if err := proto.Unmarshal(buf, bm_new); err != nil {
133 t.Fatal("Failed decoding message with extension:", err)
134 }
135 if !proto.HasExtension(bm_new, user.E_Width) {
136 t.Fatal("Decoded message didn't contain extension.")
137 }
138 width_out, err := proto.GetExtension(bm_new, user.E_Width)
139 if err != nil {
140 t.Fatal("Failed getting extension:", err)
141 }
142 if w := width_out.(*int32); *w != *width {
143 t.Errorf("width_out = %v, expected %v", *w, *width)
144 }
145 proto.ClearExtension(bm_new, user.E_Width)
146 if proto.HasExtension(bm_new, user.E_Width) {
147 t.Fatal("Failed clearing extension.")
148 }
149}
150
151func main() {
152 // simpler than rigging up gotest
153 testing.Main([]testing.Test{
154 testing.Test{"TestSingleFieldExtension", TestSingleFieldExtension},
155 testing.Test{"TestMessageExtension", TestMessageExtension},
156 testing.Test{"TestTopLevelExtension", TestTopLevelExtension},
157 })
158}