| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1 | // 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 | package proto_test |
| 33 | |
| 34 | import ( |
| 35 | . "goprotobuf.googlecode.com/hg/proto" |
| 36 | . "./testdata/_obj/test_proto" |
| 37 | "reflect" |
| 38 | "testing" |
| 39 | ) |
| 40 | |
| 41 | type UnmarshalTextTest struct { |
| 42 | in string |
| 43 | error string // if "", no error expected |
| 44 | out *MyMessage |
| 45 | } |
| 46 | |
| 47 | var unMarshalTextTests = []UnmarshalTextTest{ |
| 48 | // Basic |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 49 | { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 50 | in: " count:42\n name:\"Dave\" ", |
| 51 | out: &MyMessage{ |
| 52 | Count: Int32(42), |
| Rob Pike | 9caa5b9 | 2010-05-11 16:04:57 -0700 | [diff] [blame] | 53 | Name: String("Dave"), |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 54 | }, |
| 55 | }, |
| 56 | |
| 57 | // Empty quoted string |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 58 | { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 59 | in: `count:42 name:""`, |
| 60 | out: &MyMessage{ |
| 61 | Count: Int32(42), |
| Rob Pike | 9caa5b9 | 2010-05-11 16:04:57 -0700 | [diff] [blame] | 62 | Name: String(""), |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 63 | }, |
| 64 | }, |
| 65 | |
| 66 | // Quoted string concatenation |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 67 | { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 68 | in: `count:42 name: "My name is "` + "\n" + `"elsewhere"`, |
| 69 | out: &MyMessage{ |
| 70 | Count: Int32(42), |
| Rob Pike | 9caa5b9 | 2010-05-11 16:04:57 -0700 | [diff] [blame] | 71 | Name: String("My name is elsewhere"), |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 72 | }, |
| 73 | }, |
| 74 | |
| 75 | // Bad quoted string |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 76 | { |
| Rob Pike | 9caa5b9 | 2010-05-11 16:04:57 -0700 | [diff] [blame] | 77 | in: `inner: < host: "\0" >` + "\n", |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 78 | error: `line 1.15: invalid quoted string "\0"`, |
| 79 | }, |
| 80 | |
| 81 | // Number too large for int64 |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 82 | { |
| Rob Pike | 9caa5b9 | 2010-05-11 16:04:57 -0700 | [diff] [blame] | 83 | in: "count: 123456789012345678901", |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 84 | error: "line 1.7: invalid int32: 123456789012345678901", |
| 85 | }, |
| 86 | |
| 87 | // Number too large for int32 |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 88 | { |
| Rob Pike | 9caa5b9 | 2010-05-11 16:04:57 -0700 | [diff] [blame] | 89 | in: "count: 1234567890123", |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 90 | error: "line 1.7: invalid int32: 1234567890123", |
| 91 | }, |
| 92 | |
| 93 | // Number too large for float32 |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 94 | { |
| Rob Pike | 9caa5b9 | 2010-05-11 16:04:57 -0700 | [diff] [blame] | 95 | in: "others:< weight: 12345678901234567890123456789012345678901234567890 >", |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 96 | error: "line 1.17: invalid float32: 12345678901234567890123456789012345678901234567890", |
| 97 | }, |
| 98 | |
| 99 | // Number posing as a quoted string |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 100 | { |
| Rob Pike | 9caa5b9 | 2010-05-11 16:04:57 -0700 | [diff] [blame] | 101 | in: `inner: < host: 12 >` + "\n", |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 102 | error: `line 1.15: invalid string: 12`, |
| 103 | }, |
| 104 | |
| 105 | // Quoted string posing as int32 |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 106 | { |
| Rob Pike | 9caa5b9 | 2010-05-11 16:04:57 -0700 | [diff] [blame] | 107 | in: `count: "12"`, |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 108 | error: `line 1.7: invalid int32: "12"`, |
| 109 | }, |
| 110 | |
| 111 | // Quoted string posing a float32 |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 112 | { |
| Rob Pike | 9caa5b9 | 2010-05-11 16:04:57 -0700 | [diff] [blame] | 113 | in: `others:< weight: "17.4" >`, |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 114 | error: `line 1.17: invalid float32: "17.4"`, |
| 115 | }, |
| 116 | |
| 117 | // Enum |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 118 | { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 119 | in: `count:42 bikeshed: BLUE`, |
| 120 | out: &MyMessage{ |
| Rob Pike | 9caa5b9 | 2010-05-11 16:04:57 -0700 | [diff] [blame] | 121 | Count: Int32(42), |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 122 | Bikeshed: NewMyMessage_Color(MyMessage_BLUE), |
| 123 | }, |
| 124 | }, |
| 125 | |
| 126 | // Repeated field |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 127 | { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 128 | in: `count:42 pet: "horsey" pet:"bunny"`, |
| 129 | out: &MyMessage{ |
| 130 | Count: Int32(42), |
| Rob Pike | 9caa5b9 | 2010-05-11 16:04:57 -0700 | [diff] [blame] | 131 | Pet: []string{"horsey", "bunny"}, |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 132 | }, |
| 133 | }, |
| 134 | |
| Rob Pike | aaf695a | 2010-06-22 15:51:21 -0700 | [diff] [blame] | 135 | // Repeated message with/without colon and <>/{} |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 136 | { |
| Rob Pike | aaf695a | 2010-06-22 15:51:21 -0700 | [diff] [blame] | 137 | in: `count:42 others:{} others{} others:<> others:{}`, |
| 138 | out: &MyMessage{ |
| 139 | Count: Int32(42), |
| 140 | Others: []*OtherMessage{ |
| 141 | &OtherMessage{}, |
| 142 | &OtherMessage{}, |
| 143 | &OtherMessage{}, |
| 144 | &OtherMessage{}, |
| 145 | }, |
| 146 | }, |
| 147 | }, |
| 148 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 149 | // Missing colon for inner message |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 150 | { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 151 | in: `count:42 inner < host: "cauchy.syd" >`, |
| 152 | out: &MyMessage{ |
| 153 | Count: Int32(42), |
| 154 | Inner: &InnerMessage{ |
| 155 | Host: String("cauchy.syd"), |
| 156 | }, |
| 157 | }, |
| 158 | }, |
| 159 | |
| 160 | // Missing colon for string field |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 161 | { |
| Rob Pike | 9caa5b9 | 2010-05-11 16:04:57 -0700 | [diff] [blame] | 162 | in: `name "Dave"`, |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 163 | error: `line 1.5: expected ':', found "\"Dave\""`, |
| 164 | }, |
| 165 | |
| 166 | // Missing colon for int32 field |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 167 | { |
| Rob Pike | 9caa5b9 | 2010-05-11 16:04:57 -0700 | [diff] [blame] | 168 | in: `count 42`, |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 169 | error: `line 1.6: expected ':', found "42"`, |
| 170 | }, |
| 171 | |
| 172 | // Missing required field |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 173 | { |
| Rob Pike | 9caa5b9 | 2010-05-11 16:04:57 -0700 | [diff] [blame] | 174 | in: ``, |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 175 | error: `line 1.0: message test_proto.MyMessage missing required field "count"`, |
| 176 | }, |
| 177 | |
| 178 | // Repeated non-repeated field |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 179 | { |
| Rob Pike | 9caa5b9 | 2010-05-11 16:04:57 -0700 | [diff] [blame] | 180 | in: `name: "Rob" name: "Russ"`, |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 181 | error: `line 1.12: non-repeated field "name" was repeated`, |
| 182 | }, |
| 183 | |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 184 | // Group |
| 185 | { |
| 186 | in: `count: 17 SomeGroup { group_field: 12 }`, |
| 187 | out: &MyMessage{ |
| 188 | Count: Int32(17), |
| 189 | Somegroup: &MyMessage_SomeGroup{ |
| 190 | GroupField: Int32(12), |
| 191 | }, |
| 192 | }, |
| 193 | }, |
| 194 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 195 | // Big all-in-one |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 196 | { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 197 | in: "count:42 # Meaning\n" + |
| 198 | `name:"Dave" ` + |
| 199 | `quote:"\"I didn't want to go.\"" ` + |
| 200 | `pet:"bunny" ` + |
| 201 | `pet:"kitty" ` + |
| 202 | `pet:"horsey" ` + |
| 203 | `inner:<` + |
| 204 | ` host:"footrest.syd" ` + |
| 205 | ` port:7001 ` + |
| 206 | ` connected:true ` + |
| 207 | `> ` + |
| 208 | `others:<` + |
| 209 | ` key:3735928559 ` + |
| 210 | ` value:"\x01A\a\f" ` + |
| 211 | `> ` + |
| 212 | `others:<` + |
| 213 | " weight:58.9 # Atomic weight of Co\n" + |
| 214 | ` inner:<` + |
| 215 | ` host:"lesha.mtv" ` + |
| 216 | ` port:8002 ` + |
| 217 | ` >` + |
| 218 | `>`, |
| 219 | out: &MyMessage{ |
| 220 | Count: Int32(42), |
| Rob Pike | 9caa5b9 | 2010-05-11 16:04:57 -0700 | [diff] [blame] | 221 | Name: String("Dave"), |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 222 | Quote: String(`"I didn't want to go."`), |
| Rob Pike | 9caa5b9 | 2010-05-11 16:04:57 -0700 | [diff] [blame] | 223 | Pet: []string{"bunny", "kitty", "horsey"}, |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 224 | Inner: &InnerMessage{ |
| Rob Pike | 9caa5b9 | 2010-05-11 16:04:57 -0700 | [diff] [blame] | 225 | Host: String("footrest.syd"), |
| 226 | Port: Int32(7001), |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 227 | Connected: Bool(true), |
| 228 | }, |
| 229 | Others: []*OtherMessage{ |
| 230 | &OtherMessage{ |
| Rob Pike | 9caa5b9 | 2010-05-11 16:04:57 -0700 | [diff] [blame] | 231 | Key: Int64(3735928559), |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 232 | Value: []byte{0x1, 'A', '\a', '\f'}, |
| 233 | }, |
| 234 | &OtherMessage{ |
| 235 | Weight: Float32(58.9), |
| 236 | Inner: &InnerMessage{ |
| 237 | Host: String("lesha.mtv"), |
| 238 | Port: Int32(8002), |
| 239 | }, |
| 240 | }, |
| 241 | }, |
| 242 | }, |
| 243 | }, |
| 244 | } |
| 245 | |
| 246 | func TestUnmarshalText(t *testing.T) { |
| 247 | for i, test := range unMarshalTextTests { |
| 248 | pb := new(MyMessage) |
| 249 | err := UnmarshalText(test.in, pb) |
| 250 | if test.error == "" { |
| 251 | // We don't expect failure. |
| 252 | if err != nil { |
| 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)) |
| 258 | } |
| 259 | } else { |
| 260 | // We do expect failure. |
| 261 | if err == nil { |
| 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) |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | } |
| David Symonds | 79eae33 | 2010-10-16 11:33:20 +1100 | [diff] [blame] | 270 | |
| 271 | var benchInput string |
| 272 | |
| 273 | func init() { |
| 274 | benchInput = "count: 4\n" |
| 275 | for i := 0; i < 1000; i++ { |
| 276 | benchInput += "pet: \"fido\"\n" |
| 277 | } |
| 278 | |
| 279 | // Check it is valid input. |
| 280 | pb := new(MyMessage) |
| 281 | err := UnmarshalText(benchInput, pb) |
| 282 | if err != nil { |
| 283 | panic("Bad benchmark input: " + err.String()) |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | func BenchmarkUnmarshalText(b *testing.B) { |
| 288 | pb := new(MyMessage) |
| 289 | for i := 0; i < b.N; i++ { |
| 290 | UnmarshalText(benchInput, pb) |
| 291 | } |
| 292 | b.SetBytes(int64(len(benchInput))) |
| 293 | } |