123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509 |
- // Copyright 2008 The Closure Library Authors. All Rights Reserved.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS-IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- goog.provide('goog.proto2.PbLiteSerializerTest');
- goog.setTestOnly('goog.proto2.PbLiteSerializerTest');
- goog.require('goog.proto2.PbLiteSerializer');
- goog.require('goog.testing.jsunit');
- goog.require('proto2.TestAllTypes');
- function testSerializationAndDeserialization() {
- var message = createPopulatedMessage();
- // Serialize.
- var serializer = new goog.proto2.PbLiteSerializer();
- var pblite = serializer.serialize(message);
- assertTrue(goog.isArray(pblite));
- // Assert that everything serialized properly.
- assertEquals(101, pblite[1]);
- assertEquals('102', pblite[2]);
- assertEquals(103, pblite[3]);
- assertEquals('104', pblite[4]);
- assertEquals(105, pblite[5]);
- assertEquals('106', pblite[6]);
- assertEquals(107, pblite[7]);
- assertEquals('108', pblite[8]);
- assertEquals(109, pblite[9]);
- assertEquals('110', pblite[10]);
- assertEquals(111.5, pblite[11]);
- assertEquals(112.5, pblite[12]);
- assertEquals(1, pblite[13]); // true is serialized as 1
- assertEquals('test', pblite[14]);
- assertEquals('abcd', pblite[15]);
- assertEquals(111, pblite[16][17]);
- assertEquals(112, pblite[18][1]);
- assertTrue(pblite[19] === undefined);
- assertTrue(pblite[20] === undefined);
- assertEquals(proto2.TestAllTypes.NestedEnum.FOO, pblite[21]);
- assertEquals(201, pblite[31][0]);
- assertEquals(202, pblite[31][1]);
- assertEquals('foo', pblite[44][0]);
- assertEquals('bar', pblite[44][1]);
- var serializer = new goog.proto2.PbLiteSerializer();
- // Deserialize.
- var messageCopy =
- serializer.deserialize(proto2.TestAllTypes.getDescriptor(), pblite);
- assertNotEquals(messageCopy, message);
- assertDeserializationMatches(messageCopy);
- }
- function testZeroBasedSerializationAndDeserialization() {
- var message = createPopulatedMessage();
- // Serialize.
- var serializer = new goog.proto2.PbLiteSerializer();
- serializer.setZeroIndexed(true);
- var pblite = serializer.serialize(message);
- assertTrue(goog.isArray(pblite));
- // Assert that everything serialized properly.
- assertEquals(101, pblite[0]);
- assertEquals('102', pblite[1]);
- assertEquals(103, pblite[2]);
- assertEquals('104', pblite[3]);
- assertEquals(105, pblite[4]);
- assertEquals('106', pblite[5]);
- assertEquals(107, pblite[6]);
- assertEquals('108', pblite[7]);
- assertEquals(109, pblite[8]);
- assertEquals('110', pblite[9]);
- assertEquals(111.5, pblite[10]);
- assertEquals(112.5, pblite[11]);
- assertEquals(1, pblite[12]); // true is serialized as 1
- assertEquals('test', pblite[13]);
- assertEquals('abcd', pblite[14]);
- assertEquals(111, pblite[15][16]);
- assertEquals(112, pblite[17][0]);
- assertTrue(pblite[18] === undefined);
- assertTrue(pblite[19] === undefined);
- assertEquals(proto2.TestAllTypes.NestedEnum.FOO, pblite[20]);
- assertEquals(201, pblite[30][0]);
- assertEquals(202, pblite[30][1]);
- assertEquals('foo', pblite[43][0]);
- assertEquals('bar', pblite[43][1]);
- // Deserialize.
- var messageCopy =
- serializer.deserialize(proto2.TestAllTypes.getDescriptor(), pblite);
- assertNotEquals(messageCopy, message);
- assertEquals(message.getOptionalInt32(), messageCopy.getOptionalInt32());
- assertDeserializationMatches(messageCopy);
- }
- function createPopulatedMessage() {
- var message = new proto2.TestAllTypes();
- // Set the fields.
- // Singular.
- message.setOptionalInt32(101);
- message.setOptionalInt64('102');
- message.setOptionalUint32(103);
- message.setOptionalUint64('104');
- message.setOptionalSint32(105);
- message.setOptionalSint64('106');
- message.setOptionalFixed32(107);
- message.setOptionalFixed64('108');
- message.setOptionalSfixed32(109);
- message.setOptionalSfixed64('110');
- message.setOptionalFloat(111.5);
- message.setOptionalDouble(112.5);
- message.setOptionalBool(true);
- message.setOptionalString('test');
- message.setOptionalBytes('abcd');
- var group = new proto2.TestAllTypes.OptionalGroup();
- group.setA(111);
- message.setOptionalgroup(group);
- var nestedMessage = new proto2.TestAllTypes.NestedMessage();
- nestedMessage.setB(112);
- message.setOptionalNestedMessage(nestedMessage);
- message.setOptionalNestedEnum(proto2.TestAllTypes.NestedEnum.FOO);
- // Repeated.
- message.addRepeatedInt32(201);
- message.addRepeatedInt32(202);
- // Skip a few repeated fields so we can test how null array values are
- // handled.
- message.addRepeatedString('foo');
- message.addRepeatedString('bar');
- return message;
- }
- function testDeserializationFromExternalSource() {
- // Test deserialization where the JSON array is initialized from something
- // outside the Closure proto2 library, such as the JsPbLite library, or
- // manually as in this test.
- var pblite = [
- , // 0
- 101, // 1
- '102', // 2
- 103, // 3
- '104', // 4
- 105, // 5
- '106', // 6
- 107, // 7
- '108', // 8
- 109, // 9
- '110', // 10
- 111.5, // 11
- 112.5, // 12
- 1, // 13
- 'test', // 14
- 'abcd', // 15
- [
- , , , , , , , , , , , , , , , , , 111
- ], // 16, note the 17 commas so value is index 17
- , // 17
- [, 112], // 18
- ,
- , // 19-20
- proto2.TestAllTypes.NestedEnum.FOO, // 21
- , , , , , , , , , // 22-30
- [201, 202], // 31
- , , , , , , , , , , , , // 32-43
- ['foo', 'bar'] // 44
- ];
- pblite.length = 50;
- // Deserialize.
- var serializer = new goog.proto2.PbLiteSerializer();
- var messageCopy =
- serializer.deserialize(proto2.TestAllTypes.getDescriptor(), pblite);
- assertDeserializationMatches(messageCopy);
- // http://b/issue?id=2928075
- assertFalse(messageCopy.hasRepeatedInt64());
- assertEquals(0, messageCopy.repeatedInt64Count());
- messageCopy.repeatedInt64Array();
- assertFalse(messageCopy.hasRepeatedInt64());
- assertEquals(0, messageCopy.repeatedInt64Count());
- // Access a nested message to ensure it is deserialized.
- assertNotNull(messageCopy.getOptionalNestedMessage());
- // Verify that the pblite array itself has not been replaced by the
- // deserialization.
- assertEquals('array', goog.typeOf(pblite[16]));
- // Update some fields and verify that the changes work with the lazy
- // deserializer.
- messageCopy.setOptionalBool(true);
- assertTrue(messageCopy.getOptionalBool());
- messageCopy.setOptionalBool(false);
- assertFalse(messageCopy.getOptionalBool());
- messageCopy.setOptionalInt32(1234);
- assertEquals(1234, messageCopy.getOptionalInt32());
- }
- function testModifyLazyDeserializedMessage() {
- var pblite = [
- , // 0
- 101, // 1
- '102', // 2
- 103, // 3
- '104', // 4
- 105, // 5
- '106', // 6
- 107, // 7
- '108', // 8
- 109, // 9
- '110', // 10
- 111.5, // 11
- 112.5, // 12
- 1, // 13
- 'test', // 14
- 'abcd', // 15
- [
- , , , , , , , , , , , , , , , , , 111
- ], // 16, note the 17 commas so value is index 17
- , // 17
- [, 112], // 18
- ,
- , // 19-20
- proto2.TestAllTypes.NestedEnum.FOO, // 21
- , , , , , , , , , // 22-30
- [201, 202], // 31
- , , , , , , , , , , , , // 32-43
- ['foo', 'bar'] // 44
- ];
- pblite.length = 50;
- // Deserialize.
- var serializer = new goog.proto2.PbLiteSerializer();
- var message =
- serializer.deserialize(proto2.TestAllTypes.getDescriptor(), pblite);
- // Conduct some operations, ensuring that they all work as expected, even with
- // the lazily deserialized data.
- assertEquals(101, message.getOptionalInt32());
- message.setOptionalInt32(401);
- assertEquals(401, message.getOptionalInt32());
- assertEquals(2, message.repeatedInt32Count());
- assertEquals(201, message.getRepeatedInt32(0));
- assertEquals(202, message.getRepeatedInt32(1));
- message.clearRepeatedInt32();
- assertEquals(0, message.repeatedInt32Count());
- message.addRepeatedInt32(101);
- assertEquals(1, message.repeatedInt32Count());
- assertEquals(101, message.getRepeatedInt32(0));
- message.setUnknown(12345, 601);
- message.forEachUnknown(function(tag, value) {
- assertEquals(12345, tag);
- assertEquals(601, value);
- });
- // Create a copy of the message.
- var messageCopy = new proto2.TestAllTypes();
- messageCopy.copyFrom(message);
- assertEquals(1, messageCopy.repeatedInt32Count());
- assertEquals(101, messageCopy.getRepeatedInt32(0));
- }
- function testModifyLazyDeserializedMessageByAddingMessage() {
- var pblite = [
- , // 0
- 101, // 1
- '102', // 2
- 103, // 3
- '104', // 4
- 105, // 5
- '106', // 6
- 107, // 7
- '108', // 8
- 109, // 9
- '110', // 10
- 111.5, // 11
- 112.5, // 12
- 1, // 13
- 'test', // 14
- 'abcd', // 15
- [
- , , , , , , , , , , , , , , , , , 111
- ], // 16, note the 17 commas so value is index 17
- , // 17
- [, 112], // 18
- ,
- , // 19-20
- proto2.TestAllTypes.NestedEnum.FOO, // 21
- , , , , , , , , , // 22-30
- [201, 202], // 31
- , , , , , , , , , , , , // 32-43
- ['foo', 'bar'] // 44
- ];
- pblite.length = 50;
- // Deserialize.
- var serializer = new goog.proto2.PbLiteSerializer();
- var message =
- serializer.deserialize(proto2.TestAllTypes.getDescriptor(), pblite);
- // Add a new nested message.
- var nested1 = new proto2.TestAllTypes.NestedMessage();
- nested1.setB(1234);
- var nested2 = new proto2.TestAllTypes.NestedMessage();
- nested2.setB(4567);
- message.addRepeatedNestedMessage(nested1);
- // Check the new nested message.
- assertEquals(1, message.repeatedNestedMessageArray().length);
- assertTrue(message.repeatedNestedMessageArray()[0].equals(nested1));
- // Add another nested message.
- message.addRepeatedNestedMessage(nested2);
- // Check both nested messages.
- assertEquals(2, message.repeatedNestedMessageArray().length);
- assertTrue(message.repeatedNestedMessageArray()[0].equals(nested1));
- assertTrue(message.repeatedNestedMessageArray()[1].equals(nested2));
- }
- function assertDeserializationMatches(messageCopy) {
- assertNotNull(messageCopy);
- assertTrue(messageCopy.hasOptionalInt32());
- assertTrue(messageCopy.hasOptionalInt64());
- assertTrue(messageCopy.hasOptionalUint32());
- assertTrue(messageCopy.hasOptionalUint64());
- assertTrue(messageCopy.hasOptionalSint32());
- assertTrue(messageCopy.hasOptionalSint64());
- assertTrue(messageCopy.hasOptionalFixed32());
- assertTrue(messageCopy.hasOptionalFixed64());
- assertTrue(messageCopy.hasOptionalSfixed32());
- assertTrue(messageCopy.hasOptionalSfixed64());
- assertTrue(messageCopy.hasOptionalFloat());
- assertTrue(messageCopy.hasOptionalDouble());
- assertTrue(messageCopy.hasOptionalBool());
- assertTrue(messageCopy.hasOptionalString());
- assertTrue(messageCopy.hasOptionalBytes());
- assertTrue(messageCopy.hasOptionalgroup());
- assertTrue(messageCopy.hasOptionalNestedMessage());
- assertTrue(messageCopy.hasOptionalNestedEnum());
- assertTrue(messageCopy.hasRepeatedInt32());
- assertFalse(messageCopy.hasRepeatedInt64());
- assertFalse(messageCopy.hasRepeatedUint32());
- assertFalse(messageCopy.hasRepeatedUint64());
- assertFalse(messageCopy.hasRepeatedSint32());
- assertFalse(messageCopy.hasRepeatedSint64());
- assertFalse(messageCopy.hasRepeatedFixed32());
- assertFalse(messageCopy.hasRepeatedFixed64());
- assertFalse(messageCopy.hasRepeatedSfixed32());
- assertFalse(messageCopy.hasRepeatedSfixed64());
- assertFalse(messageCopy.hasRepeatedFloat());
- assertFalse(messageCopy.hasRepeatedDouble());
- assertFalse(messageCopy.hasRepeatedBool());
- assertTrue(messageCopy.hasRepeatedString());
- assertFalse(messageCopy.hasRepeatedBytes());
- assertFalse(messageCopy.hasRepeatedgroup());
- assertFalse(messageCopy.hasRepeatedNestedMessage());
- assertFalse(messageCopy.hasRepeatedNestedEnum());
- assertEquals(1, messageCopy.optionalInt32Count());
- assertEquals(1, messageCopy.optionalInt64Count());
- assertEquals(1, messageCopy.optionalUint32Count());
- assertEquals(1, messageCopy.optionalUint64Count());
- assertEquals(1, messageCopy.optionalSint32Count());
- assertEquals(1, messageCopy.optionalSint64Count());
- assertEquals(1, messageCopy.optionalFixed32Count());
- assertEquals(1, messageCopy.optionalFixed64Count());
- assertEquals(1, messageCopy.optionalSfixed32Count());
- assertEquals(1, messageCopy.optionalSfixed64Count());
- assertEquals(1, messageCopy.optionalFloatCount());
- assertEquals(1, messageCopy.optionalDoubleCount());
- assertEquals(1, messageCopy.optionalBoolCount());
- assertEquals(1, messageCopy.optionalStringCount());
- assertEquals(1, messageCopy.optionalBytesCount());
- assertEquals(1, messageCopy.optionalgroupCount());
- assertEquals(1, messageCopy.optionalNestedMessageCount());
- assertEquals(1, messageCopy.optionalNestedEnumCount());
- assertEquals(2, messageCopy.repeatedInt32Count());
- assertEquals(0, messageCopy.repeatedInt64Count());
- assertEquals(0, messageCopy.repeatedUint32Count());
- assertEquals(0, messageCopy.repeatedUint64Count());
- assertEquals(0, messageCopy.repeatedSint32Count());
- assertEquals(0, messageCopy.repeatedSint64Count());
- assertEquals(0, messageCopy.repeatedFixed32Count());
- assertEquals(0, messageCopy.repeatedFixed64Count());
- assertEquals(0, messageCopy.repeatedSfixed32Count());
- assertEquals(0, messageCopy.repeatedSfixed64Count());
- assertEquals(0, messageCopy.repeatedFloatCount());
- assertEquals(0, messageCopy.repeatedDoubleCount());
- assertEquals(0, messageCopy.repeatedBoolCount());
- assertEquals(2, messageCopy.repeatedStringCount());
- assertEquals(0, messageCopy.repeatedBytesCount());
- assertEquals(0, messageCopy.repeatedgroupCount());
- assertEquals(0, messageCopy.repeatedNestedMessageCount());
- assertEquals(0, messageCopy.repeatedNestedEnumCount());
- assertEquals(101, messageCopy.getOptionalInt32());
- assertEquals('102', messageCopy.getOptionalInt64());
- assertEquals(103, messageCopy.getOptionalUint32());
- assertEquals('104', messageCopy.getOptionalUint64());
- assertEquals(105, messageCopy.getOptionalSint32());
- assertEquals('106', messageCopy.getOptionalSint64());
- assertEquals(107, messageCopy.getOptionalFixed32());
- assertEquals('108', messageCopy.getOptionalFixed64());
- assertEquals(109, messageCopy.getOptionalSfixed32());
- assertEquals('110', messageCopy.getOptionalSfixed64());
- assertEquals(111.5, messageCopy.getOptionalFloat());
- assertEquals(112.5, messageCopy.getOptionalDouble());
- assertEquals(true, messageCopy.getOptionalBool());
- assertEquals('test', messageCopy.getOptionalString());
- assertEquals('abcd', messageCopy.getOptionalBytes());
- assertEquals(111, messageCopy.getOptionalgroup().getA());
- assertEquals(112, messageCopy.getOptionalNestedMessage().getB());
- assertEquals(
- proto2.TestAllTypes.NestedEnum.FOO, messageCopy.getOptionalNestedEnum());
- assertEquals(201, messageCopy.getRepeatedInt32(0));
- assertEquals(202, messageCopy.getRepeatedInt32(1));
- }
- function testMergeFromLazyTarget() {
- var serializer = new goog.proto2.PbLiteSerializer();
- var source = new proto2.TestAllTypes();
- var nested = new proto2.TestAllTypes.NestedMessage();
- nested.setB(66);
- source.setOptionalNestedMessage(nested);
- source.setOptionalInt32(32);
- source.setOptionalString('foo');
- source.setOptionalNestedEnum(proto2.TestAllTypes.NestedEnum.FOO);
- source.addRepeatedInt32(2);
- var target = new proto2.TestAllTypes();
- nested = new proto2.TestAllTypes.NestedMessage();
- nested.setC(77);
- target.setOptionalNestedMessage(nested);
- target.setOptionalInt64('64');
- target.setOptionalString('bar');
- target.setOptionalNestedEnum(proto2.TestAllTypes.NestedEnum.BAR);
- target.addRepeatedInt32(1);
- var pbliteTarget = serializer.serialize(target);
- var lazyTarget =
- serializer.deserialize(proto2.TestAllTypes.getDescriptor(), pbliteTarget);
- var expected = new proto2.TestAllTypes();
- nested = new proto2.TestAllTypes.NestedMessage();
- nested.setB(66);
- nested.setC(77);
- expected.setOptionalNestedMessage(nested);
- expected.setOptionalInt32(32);
- expected.setOptionalInt64('64');
- expected.setOptionalString('foo');
- expected.setOptionalNestedEnum(proto2.TestAllTypes.NestedEnum.FOO);
- expected.addRepeatedInt32(1);
- expected.addRepeatedInt32(2);
- lazyTarget.mergeFrom(source);
- assertTrue(
- 'expected and lazyTarget are equal after mergeFrom',
- lazyTarget.equals(expected));
- }
|