blob: 96ec9653c35c23a2264dc66bde8fba40b2f1808a [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 . "goprotobuf.googlecode.com/hg/proto"
36 . "./testdata/_obj/test_proto"
37 "reflect"
38 "testing"
39)
40
41type UnmarshalTextTest struct {
42 in string
43 error string // if "", no error expected
44 out *MyMessage
45}
46
47var unMarshalTextTests = []UnmarshalTextTest{
48 // Basic
49 UnmarshalTextTest{
50 in: " count:42\n name:\"Dave\" ",
51 out: &MyMessage{
52 Count: Int32(42),
Rob Pike9caa5b92010-05-11 16:04:57 -070053 Name: String("Dave"),
Rob Pikeaaa3a622010-03-20 22:32:34 -070054 },
55 },
56
57 // Empty quoted string
58 UnmarshalTextTest{
59 in: `count:42 name:""`,
60 out: &MyMessage{
61 Count: Int32(42),
Rob Pike9caa5b92010-05-11 16:04:57 -070062 Name: String(""),
Rob Pikeaaa3a622010-03-20 22:32:34 -070063 },
64 },
65
66 // Quoted string concatenation
67 UnmarshalTextTest{
68 in: `count:42 name: "My name is "` + "\n" + `"elsewhere"`,
69 out: &MyMessage{
70 Count: Int32(42),
Rob Pike9caa5b92010-05-11 16:04:57 -070071 Name: String("My name is elsewhere"),
Rob Pikeaaa3a622010-03-20 22:32:34 -070072 },
73 },
74
75 // Bad quoted string
76 UnmarshalTextTest{
Rob Pike9caa5b92010-05-11 16:04:57 -070077 in: `inner: < host: "\0" >` + "\n",
Rob Pikeaaa3a622010-03-20 22:32:34 -070078 error: `line 1.15: invalid quoted string "\0"`,
79 },
80
81 // Number too large for int64
82 UnmarshalTextTest{
Rob Pike9caa5b92010-05-11 16:04:57 -070083 in: "count: 123456789012345678901",
Rob Pikeaaa3a622010-03-20 22:32:34 -070084 error: "line 1.7: invalid int32: 123456789012345678901",
85 },
86
87 // Number too large for int32
88 UnmarshalTextTest{
Rob Pike9caa5b92010-05-11 16:04:57 -070089 in: "count: 1234567890123",
Rob Pikeaaa3a622010-03-20 22:32:34 -070090 error: "line 1.7: invalid int32: 1234567890123",
91 },
92
93 // Number too large for float32
94 UnmarshalTextTest{
Rob Pike9caa5b92010-05-11 16:04:57 -070095 in: "others:< weight: 12345678901234567890123456789012345678901234567890 >",
Rob Pikeaaa3a622010-03-20 22:32:34 -070096 error: "line 1.17: invalid float32: 12345678901234567890123456789012345678901234567890",
97 },
98
99 // Number posing as a quoted string
100 UnmarshalTextTest{
Rob Pike9caa5b92010-05-11 16:04:57 -0700101 in: `inner: < host: 12 >` + "\n",
Rob Pikeaaa3a622010-03-20 22:32:34 -0700102 error: `line 1.15: invalid string: 12`,
103 },
104
105 // Quoted string posing as int32
106 UnmarshalTextTest{
Rob Pike9caa5b92010-05-11 16:04:57 -0700107 in: `count: "12"`,
Rob Pikeaaa3a622010-03-20 22:32:34 -0700108 error: `line 1.7: invalid int32: "12"`,
109 },
110
111 // Quoted string posing a float32
112 UnmarshalTextTest{
Rob Pike9caa5b92010-05-11 16:04:57 -0700113 in: `others:< weight: "17.4" >`,
Rob Pikeaaa3a622010-03-20 22:32:34 -0700114 error: `line 1.17: invalid float32: "17.4"`,
115 },
116
117 // Enum
118 UnmarshalTextTest{
119 in: `count:42 bikeshed: BLUE`,
120 out: &MyMessage{
Rob Pike9caa5b92010-05-11 16:04:57 -0700121 Count: Int32(42),
Rob Pikeaaa3a622010-03-20 22:32:34 -0700122 Bikeshed: NewMyMessage_Color(MyMessage_BLUE),
123 },
124 },
125
126 // Repeated field
127 UnmarshalTextTest{
128 in: `count:42 pet: "horsey" pet:"bunny"`,
129 out: &MyMessage{
130 Count: Int32(42),
Rob Pike9caa5b92010-05-11 16:04:57 -0700131 Pet: []string{"horsey", "bunny"},
Rob Pikeaaa3a622010-03-20 22:32:34 -0700132 },
133 },
134
135 // Missing colon for inner message
136 UnmarshalTextTest{
137 in: `count:42 inner < host: "cauchy.syd" >`,
138 out: &MyMessage{
139 Count: Int32(42),
140 Inner: &InnerMessage{
141 Host: String("cauchy.syd"),
142 },
143 },
144 },
145
146 // Missing colon for string field
147 UnmarshalTextTest{
Rob Pike9caa5b92010-05-11 16:04:57 -0700148 in: `name "Dave"`,
Rob Pikeaaa3a622010-03-20 22:32:34 -0700149 error: `line 1.5: expected ':', found "\"Dave\""`,
150 },
151
152 // Missing colon for int32 field
153 UnmarshalTextTest{
Rob Pike9caa5b92010-05-11 16:04:57 -0700154 in: `count 42`,
Rob Pikeaaa3a622010-03-20 22:32:34 -0700155 error: `line 1.6: expected ':', found "42"`,
156 },
157
158 // Missing required field
159 UnmarshalTextTest{
Rob Pike9caa5b92010-05-11 16:04:57 -0700160 in: ``,
Rob Pikeaaa3a622010-03-20 22:32:34 -0700161 error: `line 1.0: message test_proto.MyMessage missing required field "count"`,
162 },
163
164 // Repeated non-repeated field
165 UnmarshalTextTest{
Rob Pike9caa5b92010-05-11 16:04:57 -0700166 in: `name: "Rob" name: "Russ"`,
Rob Pikeaaa3a622010-03-20 22:32:34 -0700167 error: `line 1.12: non-repeated field "name" was repeated`,
168 },
169
170 // Big all-in-one
171 UnmarshalTextTest{
172 in: "count:42 # Meaning\n" +
173 `name:"Dave" ` +
174 `quote:"\"I didn't want to go.\"" ` +
175 `pet:"bunny" ` +
176 `pet:"kitty" ` +
177 `pet:"horsey" ` +
178 `inner:<` +
179 ` host:"footrest.syd" ` +
180 ` port:7001 ` +
181 ` connected:true ` +
182 `> ` +
183 `others:<` +
184 ` key:3735928559 ` +
185 ` value:"\x01A\a\f" ` +
186 `> ` +
187 `others:<` +
188 " weight:58.9 # Atomic weight of Co\n" +
189 ` inner:<` +
190 ` host:"lesha.mtv" ` +
191 ` port:8002 ` +
192 ` >` +
193 `>`,
194 out: &MyMessage{
195 Count: Int32(42),
Rob Pike9caa5b92010-05-11 16:04:57 -0700196 Name: String("Dave"),
Rob Pikeaaa3a622010-03-20 22:32:34 -0700197 Quote: String(`"I didn't want to go."`),
Rob Pike9caa5b92010-05-11 16:04:57 -0700198 Pet: []string{"bunny", "kitty", "horsey"},
Rob Pikeaaa3a622010-03-20 22:32:34 -0700199 Inner: &InnerMessage{
Rob Pike9caa5b92010-05-11 16:04:57 -0700200 Host: String("footrest.syd"),
201 Port: Int32(7001),
Rob Pikeaaa3a622010-03-20 22:32:34 -0700202 Connected: Bool(true),
203 },
204 Others: []*OtherMessage{
205 &OtherMessage{
Rob Pike9caa5b92010-05-11 16:04:57 -0700206 Key: Int64(3735928559),
Rob Pikeaaa3a622010-03-20 22:32:34 -0700207 Value: []byte{0x1, 'A', '\a', '\f'},
208 },
209 &OtherMessage{
210 Weight: Float32(58.9),
211 Inner: &InnerMessage{
212 Host: String("lesha.mtv"),
213 Port: Int32(8002),
214 },
215 },
216 },
217 },
218 },
219}
220
221func TestUnmarshalText(t *testing.T) {
222 for i, test := range unMarshalTextTests {
223 pb := new(MyMessage)
224 err := UnmarshalText(test.in, pb)
225 if test.error == "" {
226 // We don't expect failure.
227 if err != nil {
228 t.Errorf("Test %d: Unexpected error: %v", i, err)
229 } else if !reflect.DeepEqual(pb, test.out) {
230 t.Errorf("Test %d: Incorrect populated \n"+
231 "Have: %v\nWant: %v",
232 i, CompactTextString(pb), CompactTextString(test.out))
233 }
234 } else {
235 // We do expect failure.
236 if err == nil {
237 t.Errorf("Test %d: Didn't get expected error: %v", i, test.error)
238 } else if err.String() != test.error {
239 t.Errorf("Test %d: Incorrect error.\nHave: %v\nWant: %v",
240 i, err.String(), test.error)
241 }
242 }
243 }
244}