| 1 |
// Copyright 2012 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
/** |
| 16 |
* @fileoverview Unit tests for goog.proto2.TextFormatSerializer. |
| 17 |
* |
| 18 |
*/ |
| 19 |
|
| 20 |
/** @suppress {extraProvide} */ |
| 21 |
goog.provide('goog.proto2.TextFormatSerializerTest'); |
| 22 |
|
| 23 |
goog.require('goog.proto2.ObjectSerializer'); |
| 24 |
goog.require('goog.proto2.TextFormatSerializer'); |
| 25 |
goog.require('goog.testing.jsunit'); |
| 26 |
goog.require('proto2.TestAllTypes'); |
| 27 |
|
| 28 |
goog.setTestOnly('goog.proto2.TextFormatSerializerTest'); |
| 29 |
|
| 30 |
function testSerialization() { |
| 31 |
var message = new proto2.TestAllTypes(); |
| 32 |
|
| 33 |
// Set the fields. |
| 34 |
// Singular. |
| 35 |
message.setOptionalInt32(101); |
| 36 |
message.setOptionalUint32(103); |
| 37 |
message.setOptionalSint32(105); |
| 38 |
message.setOptionalFixed32(107); |
| 39 |
message.setOptionalSfixed32(109); |
| 40 |
message.setOptionalInt64('102'); |
| 41 |
message.setOptionalFloat(111.5); |
| 42 |
message.setOptionalDouble(112.5); |
| 43 |
message.setOptionalBool(true); |
| 44 |
message.setOptionalString('test'); |
| 45 |
message.setOptionalBytes('abcd'); |
| 46 |
|
| 47 |
var group = new proto2.TestAllTypes.OptionalGroup(); |
| 48 |
group.setA(111); |
| 49 |
|
| 50 |
message.setOptionalgroup(group); |
| 51 |
|
| 52 |
var nestedMessage = new proto2.TestAllTypes.NestedMessage(); |
| 53 |
nestedMessage.setB(112); |
| 54 |
|
| 55 |
message.setOptionalNestedMessage(nestedMessage); |
| 56 |
|
| 57 |
message.setOptionalNestedEnum(proto2.TestAllTypes.NestedEnum.FOO); |
| 58 |
|
| 59 |
// Repeated. |
| 60 |
message.addRepeatedInt32(201); |
| 61 |
message.addRepeatedInt32(202); |
| 62 |
|
| 63 |
// Serialize to a simplified text format. |
| 64 |
var simplified = new goog.proto2.TextFormatSerializer().serialize(message); |
| 65 |
var expected = 'optional_int32: 101\n' + |
| 66 |
'optional_int64: 102\n' + |
| 67 |
'optional_uint32: 103\n' + |
| 68 |
'optional_sint32: 105\n' + |
| 69 |
'optional_fixed32: 107\n' + |
| 70 |
'optional_sfixed32: 109\n' + |
| 71 |
'optional_float: 111.5\n' + |
| 72 |
'optional_double: 112.5\n' + |
| 73 |
'optional_bool: true\n' + |
| 74 |
'optional_string: "test"\n' + |
| 75 |
'optional_bytes: "abcd"\n' + |
| 76 |
'optionalgroup {\n' + |
| 77 |
' a: 111\n' + |
| 78 |
'}\n' + |
| 79 |
'optional_nested_message {\n' + |
| 80 |
' b: 112\n' + |
| 81 |
'}\n' + |
| 82 |
'optional_nested_enum: FOO\n' + |
| 83 |
'repeated_int32: 201\n' + |
| 84 |
'repeated_int32: 202\n'; |
| 85 |
|
| 86 |
assertEquals(expected, simplified); |
| 87 |
} |
| 88 |
|
| 89 |
function testSerializationOfUnknown() { |
| 90 |
var nestedUnknown = new proto2.TestAllTypes(); |
| 91 |
var message = new proto2.TestAllTypes(); |
| 92 |
|
| 93 |
// Set the fields. |
| 94 |
// Known. |
| 95 |
message.setOptionalInt32(101); |
| 96 |
message.addRepeatedInt32(201); |
| 97 |
message.addRepeatedInt32(202); |
| 98 |
|
| 99 |
nestedUnknown.addRepeatedInt32(301); |
| 100 |
nestedUnknown.addRepeatedInt32(302); |
| 101 |
|
| 102 |
// Unknown. |
| 103 |
message.setUnknown(1000, 301); |
| 104 |
message.setUnknown(1001, 302); |
| 105 |
message.setUnknown(1002, 'hello world'); |
| 106 |
message.setUnknown(1002, nestedUnknown); |
| 107 |
|
| 108 |
nestedUnknown.setUnknown(2000, 401); |
| 109 |
|
| 110 |
// Serialize. |
| 111 |
var simplified = new goog.proto2.TextFormatSerializer().serialize(message); |
| 112 |
var expected = 'optional_int32: 101\n' + |
| 113 |
'repeated_int32: 201\n' + |
| 114 |
'repeated_int32: 202\n' + |
| 115 |
'1000: 301\n' + |
| 116 |
'1001: 302\n' + |
| 117 |
'1002 {\n' + |
| 118 |
' repeated_int32: 301\n' + |
| 119 |
' repeated_int32: 302\n' + |
| 120 |
' 2000: 401\n' + |
| 121 |
'}\n'; |
| 122 |
|
| 123 |
assertEquals(expected, simplified); |
| 124 |
} |
| 125 |
|
| 126 |
function testSerializationOfUnknownParsedFromObject() { |
| 127 |
// Construct the object-serialized representation of the message constructed |
| 128 |
// programmatically in the test above. |
| 129 |
var serialized = { |
| 130 |
1: 101, |
| 131 |
31: [201, 202], |
| 132 |
1000: 301, |
| 133 |
1001: 302, |
| 134 |
1002: {31: [301, 302], 2000: 401} |
| 135 |
}; |
| 136 |
|
| 137 |
// Deserialize that representation into a TestAllTypes message. |
| 138 |
var objectSerializer = new goog.proto2.ObjectSerializer(); |
| 139 |
var message = new proto2.TestAllTypes(); |
| 140 |
objectSerializer.deserializeTo(message, serialized); |
| 141 |
|
| 142 |
// Check that the text format matches what we expect. |
| 143 |
var simplified = new goog.proto2.TextFormatSerializer().serialize(message); |
| 144 |
var expected = |
| 145 |
('optional_int32: 101\n' + |
| 146 |
'repeated_int32: 201\n' + |
| 147 |
'repeated_int32: 202\n' + |
| 148 |
'1000: 301\n' + |
| 149 |
'1001: 302\n' + |
| 150 |
'1002 {\n' + |
| 151 |
' 31: 301\n' + |
| 152 |
' 31: 302\n' + |
| 153 |
' 2000: 401\n' + |
| 154 |
'}\n'); |
| 155 |
assertEquals(expected, simplified); |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
/** |
| 160 |
* Asserts that the given string value parses into the given set of tokens. |
| 161 |
* @param {string} value The string value to parse. |
| 162 |
* @param {Array<Object> | Object} tokens The tokens to check against. If not |
| 163 |
* an array, a single token is expected. |
| 164 |
* @param {boolean=} opt_ignoreWhitespace Whether whitespace tokens should be |
| 165 |
* skipped by the tokenizer. |
| 166 |
*/ |
| 167 |
function assertTokens(value, tokens, opt_ignoreWhitespace) { |
| 168 |
var tokenizer = new goog.proto2.TextFormatSerializer.Tokenizer_( |
| 169 |
value, opt_ignoreWhitespace); |
| 170 |
var tokensFound = []; |
| 171 |
|
| 172 |
while (tokenizer.next()) { |
| 173 |
tokensFound.push(tokenizer.getCurrent()); |
| 174 |
} |
| 175 |
|
| 176 |
if (goog.typeOf(tokens) != 'array') { |
| 177 |
tokens = [tokens]; |
| 178 |
} |
| 179 |
|
| 180 |
assertEquals(tokens.length, tokensFound.length); |
| 181 |
for (var i = 0; i < tokens.length; ++i) { |
| 182 |
assertToken(tokens[i], tokensFound[i]); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
function assertToken(expected, found) { |
| 187 |
assertEquals(expected.type, found.type); |
| 188 |
if (expected.value) { |
| 189 |
assertEquals(expected.value, found.value); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
function testTokenizer() { |
| 194 |
var types = goog.proto2.TextFormatSerializer.Tokenizer_.TokenTypes; |
| 195 |
assertTokens('{ 123 }', [ |
| 196 |
{type: types.OPEN_BRACE}, {type: types.WHITESPACE, value: ' '}, |
| 197 |
{type: types.NUMBER, value: '123'}, {type: types.WHITESPACE, value: ' '}, |
| 198 |
{type: types.CLOSE_BRACE} |
| 199 |
]); |
| 200 |
// The c++ proto serializer might represent a float in exponential |
| 201 |
// notation: |
| 202 |
assertTokens('{ 1.2345e+3 }', [ |
| 203 |
{type: types.OPEN_BRACE}, {type: types.WHITESPACE, value: ' '}, |
| 204 |
{type: types.NUMBER, value: '1.2345e+3'}, |
| 205 |
{type: types.WHITESPACE, value: ' '}, {type: types.CLOSE_BRACE} |
| 206 |
]); |
| 207 |
} |
| 208 |
|
| 209 |
function testTokenizerExponentialFloatProblem() { |
| 210 |
var input = 'merchant: { # blah blah\n' + |
| 211 |
' total_price: 3.2186e+06 # 3_218_600; 3.07Mi\n' + |
| 212 |
' taxes : 2.17199e+06\n' + |
| 213 |
'}'; |
| 214 |
var types = goog.proto2.TextFormatSerializer.Tokenizer_.TokenTypes; |
| 215 |
assertTokens( |
| 216 |
input, |
| 217 |
[ |
| 218 |
{type: types.IDENTIFIER, value: 'merchant'}, |
| 219 |
{type: types.COLON, value: ':'}, {type: types.OPEN_BRACE, value: '{'}, |
| 220 |
{type: types.COMMENT, value: '# blah blah'}, |
| 221 |
{type: types.IDENTIFIER, value: 'total_price'}, |
| 222 |
{type: types.COLON, value: ':'}, |
| 223 |
{type: types.NUMBER, value: '3.2186e+06'}, |
| 224 |
{type: types.COMMENT, value: '# 3_218_600; 3.07Mi'}, |
| 225 |
{type: types.IDENTIFIER, value: 'taxes'}, |
| 226 |
{type: types.COLON, value: ':'}, |
| 227 |
{type: types.NUMBER, value: '2.17199e+06'}, |
| 228 |
{type: types.CLOSE_BRACE, value: '}'} |
| 229 |
], |
| 230 |
true); |
| 231 |
} |
| 232 |
|
| 233 |
function testTokenizerNoWhitespace() { |
| 234 |
var types = goog.proto2.TextFormatSerializer.Tokenizer_.TokenTypes; |
| 235 |
assertTokens( |
| 236 |
'{ "hello world" }', |
| 237 |
[ |
| 238 |
{type: types.OPEN_BRACE}, {type: types.STRING, value: '"hello world"'}, |
| 239 |
{type: types.CLOSE_BRACE} |
| 240 |
], |
| 241 |
true); |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
function assertIdentifier(identifier) { |
| 246 |
var types = goog.proto2.TextFormatSerializer.Tokenizer_.TokenTypes; |
| 247 |
assertTokens(identifier, {type: types.IDENTIFIER, value: identifier}); |
| 248 |
} |
| 249 |
|
| 250 |
function assertComment(comment) { |
| 251 |
var types = goog.proto2.TextFormatSerializer.Tokenizer_.TokenTypes; |
| 252 |
assertTokens(comment, {type: types.COMMENT, value: comment}); |
| 253 |
} |
| 254 |
|
| 255 |
function assertString(str) { |
| 256 |
var types = goog.proto2.TextFormatSerializer.Tokenizer_.TokenTypes; |
| 257 |
assertTokens(str, {type: types.STRING, value: str}); |
| 258 |
} |
| 259 |
|
| 260 |
function assertNumber(num) { |
| 261 |
num = num.toString(); |
| 262 |
var types = goog.proto2.TextFormatSerializer.Tokenizer_.TokenTypes; |
| 263 |
assertTokens(num, {type: types.NUMBER, value: num}); |
| 264 |
} |
| 265 |
|
| 266 |
function testTokenizerSingleTokens() { |
| 267 |
var types = goog.proto2.TextFormatSerializer.Tokenizer_.TokenTypes; |
| 268 |
assertTokens('{', {type: types.OPEN_BRACE}); |
| 269 |
assertTokens('}', {type: types.CLOSE_BRACE}); |
| 270 |
assertTokens('<', {type: types.OPEN_TAG}); |
| 271 |
assertTokens('>', {type: types.CLOSE_TAG}); |
| 272 |
assertTokens(':', {type: types.COLON}); |
| 273 |
assertTokens(',', {type: types.COMMA}); |
| 274 |
assertTokens(';', {type: types.SEMI}); |
| 275 |
|
| 276 |
assertIdentifier('abcd'); |
| 277 |
assertIdentifier('Abcd'); |
| 278 |
assertIdentifier('ABcd'); |
| 279 |
assertIdentifier('ABcD'); |
| 280 |
assertIdentifier('a123nc'); |
| 281 |
assertIdentifier('a45_bC'); |
| 282 |
assertIdentifier('A45_bC'); |
| 283 |
|
| 284 |
assertIdentifier('inf'); |
| 285 |
assertIdentifier('infinity'); |
| 286 |
assertIdentifier('nan'); |
| 287 |
|
| 288 |
assertNumber(0); |
| 289 |
assertNumber(10); |
| 290 |
assertNumber(123); |
| 291 |
assertNumber(1234); |
| 292 |
assertNumber(123.56); |
| 293 |
assertNumber(-124); |
| 294 |
assertNumber(-1234); |
| 295 |
assertNumber(-123.56); |
| 296 |
assertNumber('123f'); |
| 297 |
assertNumber('123.6f'); |
| 298 |
assertNumber('-123f'); |
| 299 |
assertNumber('-123.8f'); |
| 300 |
assertNumber('0x1234'); |
| 301 |
assertNumber('0x12ac34'); |
| 302 |
assertNumber('0x49e281db686fb'); |
| 303 |
// Floating point numbers might be serialized in exponential |
| 304 |
// notation: |
| 305 |
assertNumber('1.2345e+3'); |
| 306 |
assertNumber('1.2345e3'); |
| 307 |
assertNumber('1.2345e-2'); |
| 308 |
|
| 309 |
assertString('""'); |
| 310 |
assertString('"hello world"'); |
| 311 |
assertString('"hello # world"'); |
| 312 |
assertString('"hello #\\" world"'); |
| 313 |
assertString('"|"'); |
| 314 |
assertString('"\\"\\""'); |
| 315 |
assertString('"\\"foo\\""'); |
| 316 |
assertString('"\\"foo\\" and \\"bar\\""'); |
| 317 |
assertString('"foo \\"and\\" bar"'); |
| 318 |
|
| 319 |
assertComment('# foo bar baz'); |
| 320 |
assertComment('# foo ## bar baz'); |
| 321 |
assertComment('# foo "bar" baz'); |
| 322 |
} |
| 323 |
|
| 324 |
function testSerializationOfStringWithQuotes() { |
| 325 |
var nestedUnknown = new proto2.TestAllTypes(); |
| 326 |
var message = new proto2.TestAllTypes(); |
| 327 |
message.setOptionalString('hello "world"'); |
| 328 |
|
| 329 |
// Serialize. |
| 330 |
var simplified = new goog.proto2.TextFormatSerializer().serialize(message); |
| 331 |
var expected = 'optional_string: "hello \\"world\\""\n'; |
| 332 |
assertEquals(expected, simplified); |
| 333 |
} |
| 334 |
|
| 335 |
function testDeserialization() { |
| 336 |
var message = new proto2.TestAllTypes(); |
| 337 |
var value = 'optional_int32: 101\n' + |
| 338 |
'repeated_int32: 201\n' + |
| 339 |
'repeated_int32: 202\n' + |
| 340 |
'optional_float: 123.4'; |
| 341 |
|
| 342 |
new goog.proto2.TextFormatSerializer().deserializeTo(message, value); |
| 343 |
|
| 344 |
assertEquals(101, message.getOptionalInt32()); |
| 345 |
assertEquals(201, message.getRepeatedInt32(0)); |
| 346 |
assertEquals(202, message.getRepeatedInt32(1)); |
| 347 |
assertEquals(123.4, message.getOptionalFloat()); |
| 348 |
} |
| 349 |
|
| 350 |
function testDeserializationOfList() { |
| 351 |
var message = new proto2.TestAllTypes(); |
| 352 |
var value = 'optional_int32: 101\n' + |
| 353 |
'repeated_int32: [201, 202]\n' + |
| 354 |
'optional_float: 123.4'; |
| 355 |
|
| 356 |
new goog.proto2.TextFormatSerializer().deserializeTo(message, value); |
| 357 |
|
| 358 |
assertEquals(101, message.getOptionalInt32()); |
| 359 |
assertEquals(201, message.getRepeatedInt32(0)); |
| 360 |
assertEquals(123.4, message.getOptionalFloat()); |
| 361 |
} |
| 362 |
|
| 363 |
function testDeserializationOfIntegerAsHexadecimalString() { |
| 364 |
var message = new proto2.TestAllTypes(); |
| 365 |
var value = 'optional_int32: 0x1\n' + |
| 366 |
'optional_sint32: 0xf\n' + |
| 367 |
'optional_uint32: 0xffffffff\n' + |
| 368 |
'repeated_int32: [0x0, 0xff]\n'; |
| 369 |
|
| 370 |
new goog.proto2.TextFormatSerializer().deserializeTo(message, value); |
| 371 |
|
| 372 |
assertEquals(1, message.getOptionalInt32()); |
| 373 |
assertEquals(15, message.getOptionalSint32()); |
| 374 |
assertEquals(4294967295, message.getOptionalUint32()); |
| 375 |
assertEquals(0, message.getRepeatedInt32(0)); |
| 376 |
assertEquals(255, message.getRepeatedInt32(1)); |
| 377 |
} |
| 378 |
|
| 379 |
function testDeserializationOfInt64AsHexadecimalString() { |
| 380 |
var message = new proto2.TestAllTypes(); |
| 381 |
var value = 'optional_int64: 0xf'; |
| 382 |
|
| 383 |
new goog.proto2.TextFormatSerializer().deserializeTo(message, value); |
| 384 |
|
| 385 |
assertEquals('0xf', message.getOptionalInt64()); |
| 386 |
} |
| 387 |
|
| 388 |
function testDeserializationOfZeroFalseAndEmptyString() { |
| 389 |
var message = new proto2.TestAllTypes(); |
| 390 |
var value = 'optional_int32: 0\n' + |
| 391 |
'optional_bool: false\n' + |
| 392 |
'optional_string: ""'; |
| 393 |
|
| 394 |
new goog.proto2.TextFormatSerializer().deserializeTo(message, value); |
| 395 |
|
| 396 |
assertEquals(0, message.getOptionalInt32()); |
| 397 |
assertEquals(false, message.getOptionalBool()); |
| 398 |
assertEquals('', message.getOptionalString()); |
| 399 |
} |
| 400 |
|
| 401 |
function testDeserializationOfConcatenatedString() { |
| 402 |
var message = new proto2.TestAllTypes(); |
| 403 |
var value = 'optional_int32: 123\n' + |
| 404 |
'optional_string:\n' + |
| 405 |
' "FirstLine"\n' + |
| 406 |
' "SecondLine"\n' + |
| 407 |
'optional_float: 456.7'; |
| 408 |
|
| 409 |
new goog.proto2.TextFormatSerializer().deserializeTo(message, value); |
| 410 |
|
| 411 |
assertEquals(123, message.getOptionalInt32()); |
| 412 |
assertEquals('FirstLineSecondLine', message.getOptionalString()); |
| 413 |
assertEquals(456.7, message.getOptionalFloat()); |
| 414 |
} |
| 415 |
|
| 416 |
function testDeserializationSkipComment() { |
| 417 |
var message = new proto2.TestAllTypes(); |
| 418 |
var value = 'optional_int32: 101\n' + |
| 419 |
'repeated_int32: 201\n' + |
| 420 |
'# Some comment.\n' + |
| 421 |
'repeated_int32: 202\n' + |
| 422 |
'optional_float: 123.4'; |
| 423 |
|
| 424 |
var parser = new goog.proto2.TextFormatSerializer.Parser(); |
| 425 |
assertTrue(parser.parse(message, value)); |
| 426 |
|
| 427 |
assertEquals(101, message.getOptionalInt32()); |
| 428 |
assertEquals(201, message.getRepeatedInt32(0)); |
| 429 |
assertEquals(202, message.getRepeatedInt32(1)); |
| 430 |
assertEquals(123.4, message.getOptionalFloat()); |
| 431 |
} |
| 432 |
|
| 433 |
function testDeserializationSkipTrailingComment() { |
| 434 |
var message = new proto2.TestAllTypes(); |
| 435 |
var value = 'optional_int32: 101\n' + |
| 436 |
'repeated_int32: 201\n' + |
| 437 |
'repeated_int32: 202 # Some trailing comment.\n' + |
| 438 |
'optional_float: 123.4'; |
| 439 |
|
| 440 |
var parser = new goog.proto2.TextFormatSerializer.Parser(); |
| 441 |
assertTrue(parser.parse(message, value)); |
| 442 |
|
| 443 |
assertEquals(101, message.getOptionalInt32()); |
| 444 |
assertEquals(201, message.getRepeatedInt32(0)); |
| 445 |
assertEquals(202, message.getRepeatedInt32(1)); |
| 446 |
assertEquals(123.4, message.getOptionalFloat()); |
| 447 |
} |
| 448 |
|
| 449 |
function testDeserializationSkipUnknown() { |
| 450 |
var message = new proto2.TestAllTypes(); |
| 451 |
var value = 'optional_int32: 101\n' + |
| 452 |
'repeated_int32: 201\n' + |
| 453 |
'some_unknown: true\n' + |
| 454 |
'repeated_int32: 202\n' + |
| 455 |
'optional_float: 123.4'; |
| 456 |
|
| 457 |
var parser = new goog.proto2.TextFormatSerializer.Parser(); |
| 458 |
assertTrue(parser.parse(message, value, true)); |
| 459 |
|
| 460 |
assertEquals(101, message.getOptionalInt32()); |
| 461 |
assertEquals(201, message.getRepeatedInt32(0)); |
| 462 |
assertEquals(202, message.getRepeatedInt32(1)); |
| 463 |
assertEquals(123.4, message.getOptionalFloat()); |
| 464 |
} |
| 465 |
|
| 466 |
function testDeserializationSkipUnknownList() { |
| 467 |
var message = new proto2.TestAllTypes(); |
| 468 |
var value = 'optional_int32: 101\n' + |
| 469 |
'repeated_int32: 201\n' + |
| 470 |
'some_unknown: [true, 1, 201, "hello"]\n' + |
| 471 |
'repeated_int32: 202\n' + |
| 472 |
'optional_float: 123.4'; |
| 473 |
|
| 474 |
var parser = new goog.proto2.TextFormatSerializer.Parser(); |
| 475 |
assertTrue(parser.parse(message, value, true)); |
| 476 |
|
| 477 |
assertEquals(101, message.getOptionalInt32()); |
| 478 |
assertEquals(201, message.getRepeatedInt32(0)); |
| 479 |
assertEquals(202, message.getRepeatedInt32(1)); |
| 480 |
assertEquals(123.4, message.getOptionalFloat()); |
| 481 |
} |
| 482 |
|
| 483 |
function testDeserializationSkipUnknownNested() { |
| 484 |
var message = new proto2.TestAllTypes(); |
| 485 |
var value = 'optional_int32: 101\n' + |
| 486 |
'repeated_int32: 201\n' + |
| 487 |
'some_unknown: <\n' + |
| 488 |
' a: 1\n' + |
| 489 |
' b: 2\n' + |
| 490 |
'>\n' + |
| 491 |
'repeated_int32: 202\n' + |
| 492 |
'optional_float: 123.4'; |
| 493 |
|
| 494 |
var parser = new goog.proto2.TextFormatSerializer.Parser(); |
| 495 |
assertTrue(parser.parse(message, value, true)); |
| 496 |
|
| 497 |
assertEquals(101, message.getOptionalInt32()); |
| 498 |
assertEquals(201, message.getRepeatedInt32(0)); |
| 499 |
assertEquals(202, message.getRepeatedInt32(1)); |
| 500 |
assertEquals(123.4, message.getOptionalFloat()); |
| 501 |
} |
| 502 |
|
| 503 |
function testDeserializationSkipUnknownNestedInvalid() { |
| 504 |
var message = new proto2.TestAllTypes(); |
| 505 |
var value = 'optional_int32: 101\n' + |
| 506 |
'repeated_int32: 201\n' + |
| 507 |
'some_unknown: <\n' + |
| 508 |
' a: \n' + // Missing value. |
| 509 |
' b: 2\n' + |
| 510 |
'>\n' + |
| 511 |
'repeated_int32: 202\n' + |
| 512 |
'optional_float: 123.4'; |
| 513 |
|
| 514 |
var parser = new goog.proto2.TextFormatSerializer.Parser(); |
| 515 |
assertFalse(parser.parse(message, value, true)); |
| 516 |
} |
| 517 |
|
| 518 |
function testDeserializationSkipUnknownNestedInvalid2() { |
| 519 |
var message = new proto2.TestAllTypes(); |
| 520 |
var value = 'optional_int32: 101\n' + |
| 521 |
'repeated_int32: 201\n' + |
| 522 |
'some_unknown: <\n' + |
| 523 |
' a: 2\n' + |
| 524 |
' b: 2\n' + |
| 525 |
'}\n' + // Delimiter mismatch |
| 526 |
'repeated_int32: 202\n' + |
| 527 |
'optional_float: 123.4'; |
| 528 |
|
| 529 |
var parser = new goog.proto2.TextFormatSerializer.Parser(); |
| 530 |
assertFalse(parser.parse(message, value, true)); |
| 531 |
} |
| 532 |
|
| 533 |
|
| 534 |
function testDeserializationLegacyFormat() { |
| 535 |
var message = new proto2.TestAllTypes(); |
| 536 |
var value = 'optional_int32: 101,\n' + |
| 537 |
'repeated_int32: 201,\n' + |
| 538 |
'repeated_int32: 202;\n' + |
| 539 |
'optional_float: 123.4'; |
| 540 |
|
| 541 |
new goog.proto2.TextFormatSerializer().deserializeTo(message, value); |
| 542 |
|
| 543 |
assertEquals(101, message.getOptionalInt32()); |
| 544 |
assertEquals(201, message.getRepeatedInt32(0)); |
| 545 |
assertEquals(202, message.getRepeatedInt32(1)); |
| 546 |
assertEquals(123.4, message.getOptionalFloat()); |
| 547 |
} |
| 548 |
|
| 549 |
function testDeserializationVariedNumbers() { |
| 550 |
var message = new proto2.TestAllTypes(); |
| 551 |
var value = |
| 552 |
('repeated_int32: 23\n' + |
| 553 |
'repeated_int32: -3\n' + |
| 554 |
'repeated_int32: 0xdeadbeef\n' + |
| 555 |
'repeated_float: 123.0\n' + |
| 556 |
'repeated_float: -3.27\n' + |
| 557 |
'repeated_float: -35.5f\n'); |
| 558 |
|
| 559 |
new goog.proto2.TextFormatSerializer().deserializeTo(message, value); |
| 560 |
|
| 561 |
assertEquals(23, message.getRepeatedInt32(0)); |
| 562 |
assertEquals(-3, message.getRepeatedInt32(1)); |
| 563 |
assertEquals(3735928559, message.getRepeatedInt32(2)); |
| 564 |
assertEquals(123.0, message.getRepeatedFloat(0)); |
| 565 |
assertEquals(-3.27, message.getRepeatedFloat(1)); |
| 566 |
assertEquals(-35.5, message.getRepeatedFloat(2)); |
| 567 |
} |
| 568 |
|
| 569 |
function testDeserializationScientificNotation() { |
| 570 |
var message = new proto2.TestAllTypes(); |
| 571 |
var value = 'repeated_float: 1.1e5\n' + |
| 572 |
'repeated_float: 1.1e-5\n' + |
| 573 |
'repeated_double: 1.1e5\n' + |
| 574 |
'repeated_double: 1.1e-5\n'; |
| 575 |
new goog.proto2.TextFormatSerializer().deserializeTo(message, value); |
| 576 |
assertEquals(1.1e5, message.getRepeatedFloat(0)); |
| 577 |
assertEquals(1.1e-5, message.getRepeatedFloat(1)); |
| 578 |
assertEquals(1.1e5, message.getRepeatedDouble(0)); |
| 579 |
assertEquals(1.1e-5, message.getRepeatedDouble(1)); |
| 580 |
} |
| 581 |
|
| 582 |
function testParseNumericalConstant() { |
| 583 |
var parseNumericalConstant = |
| 584 |
goog.proto2.TextFormatSerializer.Parser.parseNumericalConstant_; |
| 585 |
|
| 586 |
assertEquals(Infinity, parseNumericalConstant('inf')); |
| 587 |
assertEquals(Infinity, parseNumericalConstant('inff')); |
| 588 |
assertEquals(Infinity, parseNumericalConstant('infinity')); |
| 589 |
assertEquals(Infinity, parseNumericalConstant('infinityf')); |
| 590 |
assertEquals(Infinity, parseNumericalConstant('Infinityf')); |
| 591 |
|
| 592 |
assertEquals(-Infinity, parseNumericalConstant('-inf')); |
| 593 |
assertEquals(-Infinity, parseNumericalConstant('-inff')); |
| 594 |
assertEquals(-Infinity, parseNumericalConstant('-infinity')); |
| 595 |
assertEquals(-Infinity, parseNumericalConstant('-infinityf')); |
| 596 |
assertEquals(-Infinity, parseNumericalConstant('-Infinity')); |
| 597 |
|
| 598 |
assertNull(parseNumericalConstant('-infin')); |
| 599 |
assertNull(parseNumericalConstant('infin')); |
| 600 |
assertNull(parseNumericalConstant('-infinite')); |
| 601 |
|
| 602 |
assertNull(parseNumericalConstant('-infin')); |
| 603 |
assertNull(parseNumericalConstant('infin')); |
| 604 |
assertNull(parseNumericalConstant('-infinite')); |
| 605 |
|
| 606 |
assertTrue(isNaN(parseNumericalConstant('Nan'))); |
| 607 |
assertTrue(isNaN(parseNumericalConstant('NaN'))); |
| 608 |
assertTrue(isNaN(parseNumericalConstant('NAN'))); |
| 609 |
assertTrue(isNaN(parseNumericalConstant('nan'))); |
| 610 |
assertTrue(isNaN(parseNumericalConstant('nanf'))); |
| 611 |
assertTrue(isNaN(parseNumericalConstant('NaNf'))); |
| 612 |
|
| 613 |
assertEquals(Number.POSITIVE_INFINITY, parseNumericalConstant('infinity')); |
| 614 |
assertEquals(Number.NEGATIVE_INFINITY, parseNumericalConstant('-inf')); |
| 615 |
assertEquals(Number.NEGATIVE_INFINITY, parseNumericalConstant('-infinity')); |
| 616 |
|
| 617 |
assertNull(parseNumericalConstant('na')); |
| 618 |
assertNull(parseNumericalConstant('-nan')); |
| 619 |
assertNull(parseNumericalConstant('none')); |
| 620 |
} |
| 621 |
|
| 622 |
function testDeserializationOfNumericalConstants() { |
| 623 |
var message = new proto2.TestAllTypes(); |
| 624 |
var value = |
| 625 |
('repeated_float: inf\n' + |
| 626 |
'repeated_float: -inf\n' + |
| 627 |
'repeated_float: nan\n' + |
| 628 |
'repeated_float: 300.2\n'); |
| 629 |
|
| 630 |
new goog.proto2.TextFormatSerializer().deserializeTo(message, value); |
| 631 |
|
| 632 |
assertEquals(Infinity, message.getRepeatedFloat(0)); |
| 633 |
assertEquals(-Infinity, message.getRepeatedFloat(1)); |
| 634 |
assertTrue(isNaN(message.getRepeatedFloat(2))); |
| 635 |
assertEquals(300.2, message.getRepeatedFloat(3)); |
| 636 |
} |
| 637 |
|
| 638 |
var floatFormatCases = [ |
| 639 |
{given: '1.69e+06', expect: 1.69e+06}, {given: '1.69e6', expect: 1.69e+06}, |
| 640 |
{given: '2.468e-2', expect: 0.02468} |
| 641 |
]; |
| 642 |
|
| 643 |
function testGetNumberFromStringExponentialNotation() { |
| 644 |
for (var i = 0; i < floatFormatCases.length; ++i) { |
| 645 |
var thistest = floatFormatCases[i]; |
| 646 |
var result = goog.proto2.TextFormatSerializer.Parser.getNumberFromString_( |
| 647 |
thistest.given); |
| 648 |
assertEquals(thistest.expect, result); |
| 649 |
} |
| 650 |
} |
| 651 |
|
| 652 |
function testDeserializationExponentialFloat() { |
| 653 |
var parser = new goog.proto2.TextFormatSerializer.Parser(); |
| 654 |
for (var i = 0; i < floatFormatCases.length; ++i) { |
| 655 |
var thistest = floatFormatCases[i]; |
| 656 |
var message = new proto2.TestAllTypes(); |
| 657 |
var value = 'optional_float: ' + thistest.given; |
| 658 |
assertTrue(parser.parse(message, value, true)); |
| 659 |
assertEquals(thistest.expect, message.getOptionalFloat()); |
| 660 |
} |
| 661 |
} |
| 662 |
|
| 663 |
function testGetNumberFromString() { |
| 664 |
var getNumberFromString = |
| 665 |
goog.proto2.TextFormatSerializer.Parser.getNumberFromString_; |
| 666 |
|
| 667 |
assertEquals(3735928559, getNumberFromString('0xdeadbeef')); |
| 668 |
assertEquals(4276215469, getNumberFromString('0xFEE1DEAD')); |
| 669 |
assertEquals(123.1, getNumberFromString('123.1')); |
| 670 |
assertEquals(123.0, getNumberFromString('123.0')); |
| 671 |
assertEquals(-29.3, getNumberFromString('-29.3f')); |
| 672 |
assertEquals(23, getNumberFromString('23')); |
| 673 |
assertEquals(-3, getNumberFromString('-3')); |
| 674 |
assertEquals(-3.27, getNumberFromString('-3.27')); |
| 675 |
|
| 676 |
assertThrows(goog.partial(getNumberFromString, 'cat')); |
| 677 |
assertThrows(goog.partial(getNumberFromString, 'NaN')); |
| 678 |
assertThrows(goog.partial(getNumberFromString, 'inf')); |
| 679 |
} |
| 680 |
|
| 681 |
function testDeserializationError() { |
| 682 |
var message = new proto2.TestAllTypes(); |
| 683 |
var value = 'optional_int33: 101\n'; |
| 684 |
var result = |
| 685 |
new goog.proto2.TextFormatSerializer().deserializeTo(message, value); |
| 686 |
assertEquals(result, 'Unknown field: optional_int33'); |
| 687 |
} |
| 688 |
|
| 689 |
function testNestedDeserialization() { |
| 690 |
var message = new proto2.TestAllTypes(); |
| 691 |
var value = 'optional_int32: 101\n' + |
| 692 |
'optional_nested_message: {\n' + |
| 693 |
' b: 301\n' + |
| 694 |
'}'; |
| 695 |
|
| 696 |
new goog.proto2.TextFormatSerializer().deserializeTo(message, value); |
| 697 |
|
| 698 |
assertEquals(101, message.getOptionalInt32()); |
| 699 |
assertEquals(301, message.getOptionalNestedMessage().getB()); |
| 700 |
} |
| 701 |
|
| 702 |
function testNestedDeserializationLegacyFormat() { |
| 703 |
var message = new proto2.TestAllTypes(); |
| 704 |
var value = 'optional_int32: 101\n' + |
| 705 |
'optional_nested_message: <\n' + |
| 706 |
' b: 301\n' + |
| 707 |
'>'; |
| 708 |
|
| 709 |
new goog.proto2.TextFormatSerializer().deserializeTo(message, value); |
| 710 |
|
| 711 |
assertEquals(101, message.getOptionalInt32()); |
| 712 |
assertEquals(301, message.getOptionalNestedMessage().getB()); |
| 713 |
} |
| 714 |
|
| 715 |
function testBidirectional() { |
| 716 |
var message = new proto2.TestAllTypes(); |
| 717 |
|
| 718 |
// Set the fields. |
| 719 |
// Singular. |
| 720 |
message.setOptionalInt32(101); |
| 721 |
message.setOptionalInt64('102'); |
| 722 |
message.setOptionalUint32(103); |
| 723 |
message.setOptionalUint64('104'); |
| 724 |
message.setOptionalSint32(105); |
| 725 |
message.setOptionalSint64('106'); |
| 726 |
message.setOptionalFixed32(107); |
| 727 |
message.setOptionalFixed64('108'); |
| 728 |
message.setOptionalSfixed32(109); |
| 729 |
message.setOptionalSfixed64('110'); |
| 730 |
message.setOptionalFloat(111.5); |
| 731 |
message.setOptionalDouble(112.5); |
| 732 |
message.setOptionalBool(true); |
| 733 |
message.setOptionalString('test'); |
| 734 |
message.setOptionalBytes('abcd'); |
| 735 |
|
| 736 |
var group = new proto2.TestAllTypes.OptionalGroup(); |
| 737 |
group.setA(111); |
| 738 |
|
| 739 |
message.setOptionalgroup(group); |
| 740 |
|
| 741 |
var nestedMessage = new proto2.TestAllTypes.NestedMessage(); |
| 742 |
nestedMessage.setB(112); |
| 743 |
|
| 744 |
message.setOptionalNestedMessage(nestedMessage); |
| 745 |
|
| 746 |
message.setOptionalNestedEnum(proto2.TestAllTypes.NestedEnum.FOO); |
| 747 |
|
| 748 |
// Repeated. |
| 749 |
message.addRepeatedInt32(201); |
| 750 |
message.addRepeatedInt32(202); |
| 751 |
message.addRepeatedString('hello "world"'); |
| 752 |
|
| 753 |
// Serialize the message to text form. |
| 754 |
var serializer = new goog.proto2.TextFormatSerializer(); |
| 755 |
var textform = serializer.serialize(message); |
| 756 |
|
| 757 |
// Create a copy and deserialize into the copy. |
| 758 |
var copy = new proto2.TestAllTypes(); |
| 759 |
serializer.deserializeTo(copy, textform); |
| 760 |
|
| 761 |
// Assert that the messages are structurally equivalent. |
| 762 |
assertTrue(copy.equals(message)); |
| 763 |
} |
| 764 |
|
| 765 |
function testBidirectional64BitNumber() { |
| 766 |
var message = new proto2.TestAllTypes(); |
| 767 |
message.setOptionalInt64Number(10000000); |
| 768 |
message.setOptionalInt64String('200000000000000000'); |
| 769 |
|
| 770 |
// Serialize the message to text form. |
| 771 |
var serializer = new goog.proto2.TextFormatSerializer(); |
| 772 |
var textform = serializer.serialize(message); |
| 773 |
|
| 774 |
// Create a copy and deserialize into the copy. |
| 775 |
var copy = new proto2.TestAllTypes(); |
| 776 |
serializer.deserializeTo(copy, textform); |
| 777 |
|
| 778 |
// Assert that the messages are structurally equivalent. |
| 779 |
assertTrue(copy.equals(message)); |
| 780 |
} |
| 781 |
|
| 782 |
function testUseEnumValues() { |
| 783 |
var message = new proto2.TestAllTypes(); |
| 784 |
message.setOptionalNestedEnum(proto2.TestAllTypes.NestedEnum.FOO); |
| 785 |
|
| 786 |
var serializer = new goog.proto2.TextFormatSerializer(false, true); |
| 787 |
var textform = serializer.serialize(message); |
| 788 |
|
| 789 |
var expected = 'optional_nested_enum: 0\n'; |
| 790 |
|
| 791 |
assertEquals(expected, textform); |
| 792 |
|
| 793 |
var deserializedMessage = new proto2.TestAllTypes(); |
| 794 |
serializer.deserializeTo(deserializedMessage, textform); |
| 795 |
|
| 796 |
assertEquals( |
| 797 |
proto2.TestAllTypes.NestedEnum.FOO, |
| 798 |
deserializedMessage.getOptionalNestedEnum()); |
| 799 |
} |