blob: 6c3f546c2978282dc50dd1d5a7de41d29bc2b694 [file] [log] [blame]
Rob Pikeaaa3a622010-03-20 22:32:34 -07001// 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
32package proto_test
33
34import (
35 "bytes"
Rob Pikeaaa3a622010-03-20 22:32:34 -070036 "testing"
David Symondsaa922ff2011-07-19 14:58:06 +100037
38 "goprotobuf.googlecode.com/hg/proto"
39
40 pb "./testdata/_obj/test_proto"
Rob Pikeaaa3a622010-03-20 22:32:34 -070041)
42
David Symondsaa922ff2011-07-19 14:58:06 +100043func newTestMessage() *pb.MyMessage {
44 msg := &pb.MyMessage{
45 Count: proto.Int32(42),
46 Name: proto.String("Dave"),
47 Quote: proto.String(`"I didn't want to go."`),
Rob Pike9caa5b92010-05-11 16:04:57 -070048 Pet: []string{"bunny", "kitty", "horsey"},
David Symondsaa922ff2011-07-19 14:58:06 +100049 Inner: &pb.InnerMessage{
50 Host: proto.String("footrest.syd"),
51 Port: proto.Int32(7001),
52 Connected: proto.Bool(true),
Rob Pikeaaa3a622010-03-20 22:32:34 -070053 },
David Symondsaa922ff2011-07-19 14:58:06 +100054 Others: []*pb.OtherMessage{
55 &pb.OtherMessage{
56 Key: proto.Int64(0xdeadbeef),
Rob Pikeaaa3a622010-03-20 22:32:34 -070057 Value: []byte{1, 65, 7, 12},
58 },
David Symondsaa922ff2011-07-19 14:58:06 +100059 &pb.OtherMessage{
60 Weight: proto.Float32(6.022),
61 Inner: &pb.InnerMessage{
62 Host: proto.String("lesha.mtv"),
63 Port: proto.Int32(8002),
Rob Pikeaaa3a622010-03-20 22:32:34 -070064 },
65 },
66 },
David Symondsaa922ff2011-07-19 14:58:06 +100067 Bikeshed: pb.NewMyMessage_Color(pb.MyMessage_BLUE),
68 Somegroup: &pb.MyMessage_SomeGroup{
69 GroupField: proto.Int32(8),
David Symonds9f402812011-04-28 18:08:44 +100070 },
Rob Pikeaaa3a622010-03-20 22:32:34 -070071 }
David Symondsaa922ff2011-07-19 14:58:06 +100072 ext := &pb.Ext{
73 Data: proto.String("Big gobs for big rats"),
David Symondse37856c2011-06-22 12:52:53 +100074 }
David Symondsaa922ff2011-07-19 14:58:06 +100075 if err := proto.SetExtension(msg, pb.E_Ext_More, ext); err != nil {
David Symondse37856c2011-06-22 12:52:53 +100076 panic(err)
77 }
78 return msg
Rob Pikeaaa3a622010-03-20 22:32:34 -070079}
80
81const text = `count: 42
82name: "Dave"
83quote: "\"I didn't want to go.\""
84pet: "bunny"
85pet: "kitty"
86pet: "horsey"
87inner: <
88 host: "footrest.syd"
89 port: 7001
90 connected: true
91>
92others: <
93 key: 3735928559
94 value: "\x01A\a\f"
95>
96others: <
97 weight: 6.022
98 inner: <
99 host: "lesha.mtv"
100 port: 8002
101 >
102>
103bikeshed: BLUE
David Symonds9f402812011-04-28 18:08:44 +1000104SomeGroup {
105 group_field: 8
106}
David Symondse37856c2011-06-22 12:52:53 +1000107[test_proto.more]: <
108 data: "Big gobs for big rats"
109>
Rob Pikeaaa3a622010-03-20 22:32:34 -0700110`
111
112func TestMarshalTextFull(t *testing.T) {
113 buf := new(bytes.Buffer)
David Symondsaa922ff2011-07-19 14:58:06 +1000114 proto.MarshalText(buf, newTestMessage())
Rob Pikeaaa3a622010-03-20 22:32:34 -0700115 s := buf.String()
116 if s != text {
117 t.Errorf("Got:\n===\n%v===\nExpected:\n===\n%v===\n", s, text)
118 }
119}
120
121func compact(src string) string {
David Symondse37856c2011-06-22 12:52:53 +1000122 // s/[ \n]+/ /g; s/ $//;
Rob Pikeaaa3a622010-03-20 22:32:34 -0700123 dst := make([]byte, len(src))
124 space := false
125 j := 0
126 for i := 0; i < len(src); i++ {
127 c := src[i]
128 if c == ' ' || c == '\n' {
129 space = true
130 continue
131 }
David Symonds9f402812011-04-28 18:08:44 +1000132 if j > 0 && (dst[j-1] == ':' || dst[j-1] == '<' || dst[j-1] == '{') {
133 space = false
134 }
135 if c == '{' {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700136 space = false
137 }
138 if space {
139 dst[j] = ' '
140 j++
141 space = false
142 }
143 dst[j] = c
144 j++
145 }
146 if space {
147 dst[j] = ' '
148 j++
149 }
150 return string(dst[0:j])
151}
152
153var compactText = compact(text)
154
155func TestCompactText(t *testing.T) {
David Symondsaa922ff2011-07-19 14:58:06 +1000156 s := proto.CompactTextString(newTestMessage())
Rob Pikeaaa3a622010-03-20 22:32:34 -0700157 if s != compactText {
158 t.Errorf("Got:\n===\n%v===\nExpected:\n===\n%v===\n", s, compactText)
159 }
160}