1 // Go support for Protocol Buffers - Google's data interchange format
3 // Copyright 2010 Google Inc. All rights reserved.
4 // http://code.google.com/p/goprotobuf/
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are
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
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.
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.
35 . "goprotobuf.googlecode.com/hg/proto"
36 . "./testdata/_obj/test_proto"
41 type UnmarshalTextTest struct {
43 error string // if "", no error expected
47 var unMarshalTextTests = []UnmarshalTextTest{
50 in: " count:42\n name:\"Dave\" ",
57 // Empty quoted string
59 in: `count:42 name:""`,
66 // Quoted string concatenation
68 in: `count:42 name: "My name is "` + "\n" + `"elsewhere"`,
71 Name: String("My name is elsewhere"),
77 in: `inner: < host: "\0" >` + "\n",
78 error: `line 1.15: invalid quoted string "\0"`,
81 // Number too large for int64
83 in: "count: 123456789012345678901",
84 error: "line 1.7: invalid int32: 123456789012345678901",
87 // Number too large for int32
89 in: "count: 1234567890123",
90 error: "line 1.7: invalid int32: 1234567890123",
93 // Number too large for float32
95 in: "others:< weight: 12345678901234567890123456789012345678901234567890 >",
96 error: "line 1.17: invalid float32: 12345678901234567890123456789012345678901234567890",
99 // Number posing as a quoted string
101 in: `inner: < host: 12 >` + "\n",
102 error: `line 1.15: invalid string: 12`,
105 // Quoted string posing as int32
108 error: `line 1.7: invalid int32: "12"`,
111 // Quoted string posing a float32
113 in: `others:< weight: "17.4" >`,
114 error: `line 1.17: invalid float32: "17.4"`,
119 in: `count:42 bikeshed: BLUE`,
122 Bikeshed: NewMyMessage_Color(MyMessage_BLUE),
128 in: `count:42 pet: "horsey" pet:"bunny"`,
131 Pet: []string{"horsey", "bunny"},
135 // Repeated message with/without colon and <>/{}
137 in: `count:42 others:{} others{} others:<> others:{}`,
140 Others: []*OtherMessage{
149 // Missing colon for inner message
151 in: `count:42 inner < host: "cauchy.syd" >`,
154 Inner: &InnerMessage{
155 Host: String("cauchy.syd"),
160 // Missing colon for string field
163 error: `line 1.5: expected ':', found "\"Dave\""`,
166 // Missing colon for int32 field
169 error: `line 1.6: expected ':', found "42"`,
172 // Missing required field
175 error: `line 1.0: message test_proto.MyMessage missing required field "count"`,
178 // Repeated non-repeated field
180 in: `name: "Rob" name: "Russ"`,
181 error: `line 1.12: non-repeated field "name" was repeated`,
186 in: `count: 17 SomeGroup { group_field: 12 }`,
189 Somegroup: &MyMessage_SomeGroup{
190 GroupField: Int32(12),
197 in: "count:42 # Meaning\n" +
199 `quote:"\"I didn't want to go.\"" ` +
204 ` host:"footrest.syd" ` +
210 ` value:"\x01A\a\f" ` +
213 " weight:58.9 # Atomic weight of Co\n" +
215 ` host:"lesha.mtv" ` +
221 Name: String("Dave"),
222 Quote: String(`"I didn't want to go."`),
223 Pet: []string{"bunny", "kitty", "horsey"},
224 Inner: &InnerMessage{
225 Host: String("footrest.syd"),
227 Connected: Bool(true),
229 Others: []*OtherMessage{
231 Key: Int64(3735928559),
232 Value: []byte{0x1, 'A', '\a', '\f'},
235 Weight: Float32(58.9),
236 Inner: &InnerMessage{
237 Host: String("lesha.mtv"),
246 func TestUnmarshalText(t *testing.T) {
247 for i, test := range unMarshalTextTests {
249 err := UnmarshalText(test.in, pb)
250 if test.error == "" {
251 // We don't expect failure.
253 t.Errorf("Test %d: Unexpected error: %v", i, err)
254 } else if !reflect.DeepEqual(pb, test.out) {
255 t.Errorf("Test %d: Incorrect populated \n"+
256 "Have: %v\nWant: %v",
257 i, CompactTextString(pb), CompactTextString(test.out))
260 // We do expect failure.
262 t.Errorf("Test %d: Didn't get expected error: %v", i, test.error)
263 } else if err.String() != test.error {
264 t.Errorf("Test %d: Incorrect error.\nHave: %v\nWant: %v",
265 i, err.String(), test.error)
271 var benchInput string
274 benchInput = "count: 4\n"
275 for i := 0; i < 1000; i++ {
276 benchInput += "pet: \"fido\"\n"
279 // Check it is valid input.
281 err := UnmarshalText(benchInput, pb)
283 panic("Bad benchmark input: " + err.String())
287 func BenchmarkUnmarshalText(b *testing.B) {
289 for i := 0; i < b.N; i++ {
290 UnmarshalText(benchInput, pb)
292 b.SetBytes(int64(len(benchInput)))