blob: e31e21cf0358af98c83e5b1604699a4ed907f198 [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"
36 . "goprotobuf.googlecode.com/hg/proto"
37 . "./testdata/_obj/test_proto"
38 "testing"
39)
40
41func newTestMessage() *MyMessage {
42 return &MyMessage{
43 Count: Int32(42),
Rob Pike9caa5b92010-05-11 16:04:57 -070044 Name: String("Dave"),
Rob Pikeaaa3a622010-03-20 22:32:34 -070045 Quote: String(`"I didn't want to go."`),
Rob Pike9caa5b92010-05-11 16:04:57 -070046 Pet: []string{"bunny", "kitty", "horsey"},
Rob Pikeaaa3a622010-03-20 22:32:34 -070047 Inner: &InnerMessage{
Rob Pike9caa5b92010-05-11 16:04:57 -070048 Host: String("footrest.syd"),
49 Port: Int32(7001),
Rob Pikeaaa3a622010-03-20 22:32:34 -070050 Connected: Bool(true),
51 },
52 Others: []*OtherMessage{
53 &OtherMessage{
Rob Pike9caa5b92010-05-11 16:04:57 -070054 Key: Int64(0xdeadbeef),
Rob Pikeaaa3a622010-03-20 22:32:34 -070055 Value: []byte{1, 65, 7, 12},
56 },
57 &OtherMessage{
58 Weight: Float32(6.022),
59 Inner: &InnerMessage{
60 Host: String("lesha.mtv"),
61 Port: Int32(8002),
62 },
63 },
64 },
65 Bikeshed: NewMyMessage_Color(MyMessage_BLUE),
David Symonds9f402812011-04-28 18:08:44 +100066 Somegroup: &MyMessage_SomeGroup{
67 GroupField: Int32(8),
68 },
Rob Pikeaaa3a622010-03-20 22:32:34 -070069 }
70}
71
72const text = `count: 42
73name: "Dave"
74quote: "\"I didn't want to go.\""
75pet: "bunny"
76pet: "kitty"
77pet: "horsey"
78inner: <
79 host: "footrest.syd"
80 port: 7001
81 connected: true
82>
83others: <
84 key: 3735928559
85 value: "\x01A\a\f"
86>
87others: <
88 weight: 6.022
89 inner: <
90 host: "lesha.mtv"
91 port: 8002
92 >
93>
94bikeshed: BLUE
David Symonds9f402812011-04-28 18:08:44 +100095SomeGroup {
96 group_field: 8
97}
Rob Pikeaaa3a622010-03-20 22:32:34 -070098`
99
100func TestMarshalTextFull(t *testing.T) {
101 buf := new(bytes.Buffer)
102 MarshalText(buf, newTestMessage())
103 s := buf.String()
104 if s != text {
105 t.Errorf("Got:\n===\n%v===\nExpected:\n===\n%v===\n", s, text)
106 }
107}
108
109func compact(src string) string {
110 // ,s/[ \n]+/ /g; s/ $//;
111 dst := make([]byte, len(src))
112 space := false
113 j := 0
114 for i := 0; i < len(src); i++ {
115 c := src[i]
116 if c == ' ' || c == '\n' {
117 space = true
118 continue
119 }
David Symonds9f402812011-04-28 18:08:44 +1000120 if j > 0 && (dst[j-1] == ':' || dst[j-1] == '<' || dst[j-1] == '{') {
121 space = false
122 }
123 if c == '{' {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700124 space = false
125 }
126 if space {
127 dst[j] = ' '
128 j++
129 space = false
130 }
131 dst[j] = c
132 j++
133 }
134 if space {
135 dst[j] = ' '
136 j++
137 }
138 return string(dst[0:j])
139}
140
141var compactText = compact(text)
142
143func TestCompactText(t *testing.T) {
144 s := CompactTextString(newTestMessage())
145 if s != compactText {
146 t.Errorf("Got:\n===\n%v===\nExpected:\n===\n%v===\n", s, compactText)
147 }
148}