1ffe3c632Sopenharmony_ci// Protocol Buffers - Google's data interchange format 2ffe3c632Sopenharmony_ci// Copyright 2008 Google Inc. All rights reserved. 3ffe3c632Sopenharmony_ci// https://developers.google.com/protocol-buffers/ 4ffe3c632Sopenharmony_ci// 5ffe3c632Sopenharmony_ci// Redistribution and use in source and binary forms, with or without 6ffe3c632Sopenharmony_ci// modification, are permitted provided that the following conditions are 7ffe3c632Sopenharmony_ci// met: 8ffe3c632Sopenharmony_ci// 9ffe3c632Sopenharmony_ci// * Redistributions of source code must retain the above copyright 10ffe3c632Sopenharmony_ci// notice, this list of conditions and the following disclaimer. 11ffe3c632Sopenharmony_ci// * Redistributions in binary form must reproduce the above 12ffe3c632Sopenharmony_ci// copyright notice, this list of conditions and the following disclaimer 13ffe3c632Sopenharmony_ci// in the documentation and/or other materials provided with the 14ffe3c632Sopenharmony_ci// distribution. 15ffe3c632Sopenharmony_ci// * Neither the name of Google Inc. nor the names of its 16ffe3c632Sopenharmony_ci// contributors may be used to endorse or promote products derived from 17ffe3c632Sopenharmony_ci// this software without specific prior written permission. 18ffe3c632Sopenharmony_ci// 19ffe3c632Sopenharmony_ci// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20ffe3c632Sopenharmony_ci// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21ffe3c632Sopenharmony_ci// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22ffe3c632Sopenharmony_ci// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23ffe3c632Sopenharmony_ci// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24ffe3c632Sopenharmony_ci// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25ffe3c632Sopenharmony_ci// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26ffe3c632Sopenharmony_ci// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27ffe3c632Sopenharmony_ci// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28ffe3c632Sopenharmony_ci// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29ffe3c632Sopenharmony_ci// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30ffe3c632Sopenharmony_ci 31ffe3c632Sopenharmony_cigoog.require('goog.testing.asserts'); 32ffe3c632Sopenharmony_cigoog.require('goog.userAgent'); 33ffe3c632Sopenharmony_ci 34ffe3c632Sopenharmony_ci// CommonJS-LoadFromFile: testbinary_pb proto.jspb.test 35ffe3c632Sopenharmony_cigoog.require('proto.jspb.test.MapValueEnum'); 36ffe3c632Sopenharmony_cigoog.require('proto.jspb.test.MapValueMessage'); 37ffe3c632Sopenharmony_cigoog.require('proto.jspb.test.TestMapFields'); 38ffe3c632Sopenharmony_cigoog.require('proto.jspb.test.TestMapFieldsOptionalKeys'); 39ffe3c632Sopenharmony_cigoog.require('proto.jspb.test.TestMapFieldsOptionalValues'); 40ffe3c632Sopenharmony_cigoog.require('proto.jspb.test.MapEntryOptionalKeysStringKey'); 41ffe3c632Sopenharmony_cigoog.require('proto.jspb.test.MapEntryOptionalKeysInt32Key'); 42ffe3c632Sopenharmony_cigoog.require('proto.jspb.test.MapEntryOptionalKeysInt64Key'); 43ffe3c632Sopenharmony_cigoog.require('proto.jspb.test.MapEntryOptionalKeysBoolKey'); 44ffe3c632Sopenharmony_cigoog.require('proto.jspb.test.MapEntryOptionalValuesStringValue'); 45ffe3c632Sopenharmony_cigoog.require('proto.jspb.test.MapEntryOptionalValuesInt32Value'); 46ffe3c632Sopenharmony_cigoog.require('proto.jspb.test.MapEntryOptionalValuesInt64Value'); 47ffe3c632Sopenharmony_cigoog.require('proto.jspb.test.MapEntryOptionalValuesBoolValue'); 48ffe3c632Sopenharmony_cigoog.require('proto.jspb.test.MapEntryOptionalValuesDoubleValue'); 49ffe3c632Sopenharmony_cigoog.require('proto.jspb.test.MapEntryOptionalValuesEnumValue'); 50ffe3c632Sopenharmony_cigoog.require('proto.jspb.test.MapEntryOptionalValuesMessageValue'); 51ffe3c632Sopenharmony_ci 52ffe3c632Sopenharmony_ci// CommonJS-LoadFromFile: test_pb proto.jspb.test 53ffe3c632Sopenharmony_cigoog.require('proto.jspb.test.MapValueMessageNoBinary'); 54ffe3c632Sopenharmony_cigoog.require('proto.jspb.test.TestMapFieldsNoBinary'); 55ffe3c632Sopenharmony_ci 56ffe3c632Sopenharmony_ci/** 57ffe3c632Sopenharmony_ci * Helper: check that the given map has exactly this set of (sorted) entries. 58ffe3c632Sopenharmony_ci * @param {!jspb.Map} map 59ffe3c632Sopenharmony_ci * @param {!Array<!Array<?>>} entries 60ffe3c632Sopenharmony_ci */ 61ffe3c632Sopenharmony_cifunction checkMapEquals(map, entries) { 62ffe3c632Sopenharmony_ci var arr = map.toArray(); 63ffe3c632Sopenharmony_ci assertEquals(arr.length, entries.length); 64ffe3c632Sopenharmony_ci for (var i = 0; i < arr.length; i++) { 65ffe3c632Sopenharmony_ci if (Array.isArray(arr[i])) { 66ffe3c632Sopenharmony_ci assertTrue(Array.isArray(entries[i])); 67ffe3c632Sopenharmony_ci assertArrayEquals(arr[i], entries[i]); 68ffe3c632Sopenharmony_ci } else { 69ffe3c632Sopenharmony_ci assertElementsEquals(arr[i], entries[i]); 70ffe3c632Sopenharmony_ci } 71ffe3c632Sopenharmony_ci } 72ffe3c632Sopenharmony_ci} 73ffe3c632Sopenharmony_ci 74ffe3c632Sopenharmony_ci/** 75ffe3c632Sopenharmony_ci * Converts an ES6 iterator to an array. 76ffe3c632Sopenharmony_ci * @template T 77ffe3c632Sopenharmony_ci * @param {!Iterator<T>} iter an iterator 78ffe3c632Sopenharmony_ci * @return {!Array<T>} 79ffe3c632Sopenharmony_ci */ 80ffe3c632Sopenharmony_cifunction toArray(iter) { 81ffe3c632Sopenharmony_ci var arr = []; 82ffe3c632Sopenharmony_ci while (true) { 83ffe3c632Sopenharmony_ci var val = iter.next(); 84ffe3c632Sopenharmony_ci if (val.done) { 85ffe3c632Sopenharmony_ci break; 86ffe3c632Sopenharmony_ci } 87ffe3c632Sopenharmony_ci arr.push(val.value); 88ffe3c632Sopenharmony_ci } 89ffe3c632Sopenharmony_ci return arr; 90ffe3c632Sopenharmony_ci} 91ffe3c632Sopenharmony_ci 92ffe3c632Sopenharmony_ci 93ffe3c632Sopenharmony_ci/** 94ffe3c632Sopenharmony_ci * Helper: generate test methods for this TestMapFields class. 95ffe3c632Sopenharmony_ci * @param {?} msgInfo 96ffe3c632Sopenharmony_ci * @param {?} submessageCtor 97ffe3c632Sopenharmony_ci * @param {string} suffix 98ffe3c632Sopenharmony_ci */ 99ffe3c632Sopenharmony_cifunction makeTests(msgInfo, submessageCtor, suffix) { 100ffe3c632Sopenharmony_ci /** 101ffe3c632Sopenharmony_ci * Helper: fill all maps on a TestMapFields. 102ffe3c632Sopenharmony_ci * @param {?} msg 103ffe3c632Sopenharmony_ci */ 104ffe3c632Sopenharmony_ci var fillMapFields = function(msg) { 105ffe3c632Sopenharmony_ci msg.getMapStringStringMap().set('asdf', 'jkl;').set('key 2', 'hello world'); 106ffe3c632Sopenharmony_ci msg.getMapStringInt32Map().set('a', 1).set('b', -2); 107ffe3c632Sopenharmony_ci msg.getMapStringInt64Map().set('c', 0x100000000).set('d', 0x200000000); 108ffe3c632Sopenharmony_ci msg.getMapStringBoolMap().set('e', true).set('f', false); 109ffe3c632Sopenharmony_ci msg.getMapStringDoubleMap().set('g', 3.14159).set('h', 2.71828); 110ffe3c632Sopenharmony_ci msg.getMapStringEnumMap() 111ffe3c632Sopenharmony_ci .set('i', proto.jspb.test.MapValueEnum.MAP_VALUE_BAR) 112ffe3c632Sopenharmony_ci .set('j', proto.jspb.test.MapValueEnum.MAP_VALUE_BAZ); 113ffe3c632Sopenharmony_ci msg.getMapStringMsgMap() 114ffe3c632Sopenharmony_ci .set('k', new submessageCtor()) 115ffe3c632Sopenharmony_ci .set('l', new submessageCtor()); 116ffe3c632Sopenharmony_ci msg.getMapStringMsgMap().get('k').setFoo(42); 117ffe3c632Sopenharmony_ci msg.getMapStringMsgMap().get('l').setFoo(84); 118ffe3c632Sopenharmony_ci msg.getMapInt32StringMap().set(-1, 'a').set(42, 'b'); 119ffe3c632Sopenharmony_ci msg.getMapInt64StringMap().set(0x123456789abc, 'c').set(0xcba987654321, 'd'); 120ffe3c632Sopenharmony_ci msg.getMapBoolStringMap().set(false, 'e').set(true, 'f'); 121ffe3c632Sopenharmony_ci }; 122ffe3c632Sopenharmony_ci 123ffe3c632Sopenharmony_ci /** 124ffe3c632Sopenharmony_ci * Helper: check all maps on a TestMapFields. 125ffe3c632Sopenharmony_ci * @param {?} msg 126ffe3c632Sopenharmony_ci */ 127ffe3c632Sopenharmony_ci var checkMapFields = function(msg) { 128ffe3c632Sopenharmony_ci checkMapEquals(msg.getMapStringStringMap(), [ 129ffe3c632Sopenharmony_ci ['asdf', 'jkl;'], 130ffe3c632Sopenharmony_ci ['key 2', 'hello world'] 131ffe3c632Sopenharmony_ci ]); 132ffe3c632Sopenharmony_ci checkMapEquals(msg.getMapStringInt32Map(), [ 133ffe3c632Sopenharmony_ci ['a', 1], 134ffe3c632Sopenharmony_ci ['b', -2] 135ffe3c632Sopenharmony_ci ]); 136ffe3c632Sopenharmony_ci checkMapEquals(msg.getMapStringInt64Map(), [ 137ffe3c632Sopenharmony_ci ['c', 0x100000000], 138ffe3c632Sopenharmony_ci ['d', 0x200000000] 139ffe3c632Sopenharmony_ci ]); 140ffe3c632Sopenharmony_ci checkMapEquals(msg.getMapStringBoolMap(), [ 141ffe3c632Sopenharmony_ci ['e', true], 142ffe3c632Sopenharmony_ci ['f', false] 143ffe3c632Sopenharmony_ci ]); 144ffe3c632Sopenharmony_ci checkMapEquals(msg.getMapStringDoubleMap(), [ 145ffe3c632Sopenharmony_ci ['g', 3.14159], 146ffe3c632Sopenharmony_ci ['h', 2.71828] 147ffe3c632Sopenharmony_ci ]); 148ffe3c632Sopenharmony_ci checkMapEquals(msg.getMapStringEnumMap(), [ 149ffe3c632Sopenharmony_ci ['i', proto.jspb.test.MapValueEnum.MAP_VALUE_BAR], 150ffe3c632Sopenharmony_ci ['j', proto.jspb.test.MapValueEnum.MAP_VALUE_BAZ] 151ffe3c632Sopenharmony_ci ]); 152ffe3c632Sopenharmony_ci checkMapEquals(msg.getMapInt32StringMap(), [ 153ffe3c632Sopenharmony_ci [-1, 'a'], 154ffe3c632Sopenharmony_ci [42, 'b'] 155ffe3c632Sopenharmony_ci ]); 156ffe3c632Sopenharmony_ci checkMapEquals(msg.getMapInt64StringMap(), [ 157ffe3c632Sopenharmony_ci [0x123456789abc, 'c'], 158ffe3c632Sopenharmony_ci [0xcba987654321, 'd'] 159ffe3c632Sopenharmony_ci ]); 160ffe3c632Sopenharmony_ci checkMapEquals(msg.getMapBoolStringMap(), [ 161ffe3c632Sopenharmony_ci [false, 'e'], 162ffe3c632Sopenharmony_ci [true, 'f'] 163ffe3c632Sopenharmony_ci ]); 164ffe3c632Sopenharmony_ci 165ffe3c632Sopenharmony_ci assertEquals(msg.getMapStringMsgMap().getLength(), 2); 166ffe3c632Sopenharmony_ci assertEquals(msg.getMapStringMsgMap().get('k').getFoo(), 42); 167ffe3c632Sopenharmony_ci assertEquals(msg.getMapStringMsgMap().get('l').getFoo(), 84); 168ffe3c632Sopenharmony_ci 169ffe3c632Sopenharmony_ci var entries = toArray(msg.getMapStringMsgMap().entries()); 170ffe3c632Sopenharmony_ci assertEquals(entries.length, 2); 171ffe3c632Sopenharmony_ci entries.forEach(function(entry) { 172ffe3c632Sopenharmony_ci var key = entry[0]; 173ffe3c632Sopenharmony_ci var val = entry[1]; 174ffe3c632Sopenharmony_ci assert(val === msg.getMapStringMsgMap().get(key)); 175ffe3c632Sopenharmony_ci }); 176ffe3c632Sopenharmony_ci 177ffe3c632Sopenharmony_ci msg.getMapStringMsgMap().forEach(function(val, key) { 178ffe3c632Sopenharmony_ci assert(val === msg.getMapStringMsgMap().get(key)); 179ffe3c632Sopenharmony_ci }); 180ffe3c632Sopenharmony_ci }; 181ffe3c632Sopenharmony_ci 182ffe3c632Sopenharmony_ci it('testMapStringStringField' + suffix, function() { 183ffe3c632Sopenharmony_ci var msg = new msgInfo.constructor(); 184ffe3c632Sopenharmony_ci assertEquals(msg.getMapStringStringMap().getLength(), 0); 185ffe3c632Sopenharmony_ci assertEquals(msg.getMapStringInt32Map().getLength(), 0); 186ffe3c632Sopenharmony_ci assertEquals(msg.getMapStringInt64Map().getLength(), 0); 187ffe3c632Sopenharmony_ci assertEquals(msg.getMapStringBoolMap().getLength(), 0); 188ffe3c632Sopenharmony_ci assertEquals(msg.getMapStringDoubleMap().getLength(), 0); 189ffe3c632Sopenharmony_ci assertEquals(msg.getMapStringEnumMap().getLength(), 0); 190ffe3c632Sopenharmony_ci assertEquals(msg.getMapStringMsgMap().getLength(), 0); 191ffe3c632Sopenharmony_ci 192ffe3c632Sopenharmony_ci // Re-create to clear out any internally-cached wrappers, etc. 193ffe3c632Sopenharmony_ci msg = new msgInfo.constructor(); 194ffe3c632Sopenharmony_ci var m = msg.getMapStringStringMap(); 195ffe3c632Sopenharmony_ci assertEquals(m.has('asdf'), false); 196ffe3c632Sopenharmony_ci assertEquals(m.get('asdf'), undefined); 197ffe3c632Sopenharmony_ci m.set('asdf', 'hello world'); 198ffe3c632Sopenharmony_ci assertEquals(m.has('asdf'), true); 199ffe3c632Sopenharmony_ci assertEquals(m.get('asdf'), 'hello world'); 200ffe3c632Sopenharmony_ci m.set('jkl;', 'key 2'); 201ffe3c632Sopenharmony_ci assertEquals(m.has('jkl;'), true); 202ffe3c632Sopenharmony_ci assertEquals(m.get('jkl;'), 'key 2'); 203ffe3c632Sopenharmony_ci assertEquals(m.getLength(), 2); 204ffe3c632Sopenharmony_ci var it = m.entries(); 205ffe3c632Sopenharmony_ci assertElementsEquals(it.next().value, ['asdf', 'hello world']); 206ffe3c632Sopenharmony_ci assertElementsEquals(it.next().value, ['jkl;', 'key 2']); 207ffe3c632Sopenharmony_ci assertEquals(it.next().done, true); 208ffe3c632Sopenharmony_ci checkMapEquals(m, [ 209ffe3c632Sopenharmony_ci ['asdf', 'hello world'], 210ffe3c632Sopenharmony_ci ['jkl;', 'key 2'] 211ffe3c632Sopenharmony_ci ]); 212ffe3c632Sopenharmony_ci m.del('jkl;'); 213ffe3c632Sopenharmony_ci assertEquals(m.has('jkl;'), false); 214ffe3c632Sopenharmony_ci assertEquals(m.get('jkl;'), undefined); 215ffe3c632Sopenharmony_ci assertEquals(m.getLength(), 1); 216ffe3c632Sopenharmony_ci it = m.keys(); 217ffe3c632Sopenharmony_ci assertEquals(it.next().value, 'asdf'); 218ffe3c632Sopenharmony_ci assertEquals(it.next().done, true); 219ffe3c632Sopenharmony_ci it = m.values(); 220ffe3c632Sopenharmony_ci assertEquals(it.next().value, 'hello world'); 221ffe3c632Sopenharmony_ci assertEquals(it.next().done, true); 222ffe3c632Sopenharmony_ci 223ffe3c632Sopenharmony_ci var count = 0; 224ffe3c632Sopenharmony_ci m.forEach(function(value, key, map) { 225ffe3c632Sopenharmony_ci assertEquals(map, m); 226ffe3c632Sopenharmony_ci assertEquals(key, 'asdf'); 227ffe3c632Sopenharmony_ci assertEquals(value, 'hello world'); 228ffe3c632Sopenharmony_ci count++; 229ffe3c632Sopenharmony_ci }); 230ffe3c632Sopenharmony_ci assertEquals(count, 1); 231ffe3c632Sopenharmony_ci 232ffe3c632Sopenharmony_ci m.clear(); 233ffe3c632Sopenharmony_ci assertEquals(m.getLength(), 0); 234ffe3c632Sopenharmony_ci }); 235ffe3c632Sopenharmony_ci 236ffe3c632Sopenharmony_ci 237ffe3c632Sopenharmony_ci /** 238ffe3c632Sopenharmony_ci * Tests operations on maps with all key and value types. 239ffe3c632Sopenharmony_ci */ 240ffe3c632Sopenharmony_ci it('testAllMapTypes' + suffix, function() { 241ffe3c632Sopenharmony_ci var msg = new msgInfo.constructor(); 242ffe3c632Sopenharmony_ci fillMapFields(msg); 243ffe3c632Sopenharmony_ci checkMapFields(msg); 244ffe3c632Sopenharmony_ci }); 245ffe3c632Sopenharmony_ci 246ffe3c632Sopenharmony_ci 247ffe3c632Sopenharmony_ci if (msgInfo.deserializeBinary) { 248ffe3c632Sopenharmony_ci /** 249ffe3c632Sopenharmony_ci * Tests serialization and deserialization in binary format. 250ffe3c632Sopenharmony_ci */ 251ffe3c632Sopenharmony_ci it('testBinaryFormat' + suffix, function() { 252ffe3c632Sopenharmony_ci if (goog.userAgent.IE && !goog.userAgent.isDocumentModeOrHigher(10)) { 253ffe3c632Sopenharmony_ci // IE8/9 currently doesn't support binary format because they lack 254ffe3c632Sopenharmony_ci // TypedArray. 255ffe3c632Sopenharmony_ci return; 256ffe3c632Sopenharmony_ci } 257ffe3c632Sopenharmony_ci 258ffe3c632Sopenharmony_ci // Check that the format is correct. 259ffe3c632Sopenharmony_ci var msg = new msgInfo.constructor(); 260ffe3c632Sopenharmony_ci msg.getMapStringStringMap().set('A', 'a'); 261ffe3c632Sopenharmony_ci var serialized = msg.serializeBinary(); 262ffe3c632Sopenharmony_ci var expectedSerialized = [ 263ffe3c632Sopenharmony_ci 0x0a, 0x6, // field 1 (map_string_string), delimited, length 6 264ffe3c632Sopenharmony_ci 0x0a, 0x1, // field 1 in submessage (key), delimited, length 1 265ffe3c632Sopenharmony_ci 0x41, // ASCII 'A' 266ffe3c632Sopenharmony_ci 0x12, 0x1, // field 2 in submessage (value), delimited, length 1 267ffe3c632Sopenharmony_ci 0x61 // ASCII 'a' 268ffe3c632Sopenharmony_ci ]; 269ffe3c632Sopenharmony_ci assertEquals(serialized.length, expectedSerialized.length); 270ffe3c632Sopenharmony_ci for (var i = 0; i < serialized.length; i++) { 271ffe3c632Sopenharmony_ci assertEquals(serialized[i], expectedSerialized[i]); 272ffe3c632Sopenharmony_ci } 273ffe3c632Sopenharmony_ci 274ffe3c632Sopenharmony_ci // Check that all map fields successfully round-trip. 275ffe3c632Sopenharmony_ci msg = new msgInfo.constructor(); 276ffe3c632Sopenharmony_ci fillMapFields(msg); 277ffe3c632Sopenharmony_ci serialized = msg.serializeBinary(); 278ffe3c632Sopenharmony_ci var decoded = msgInfo.deserializeBinary(serialized); 279ffe3c632Sopenharmony_ci checkMapFields(decoded); 280ffe3c632Sopenharmony_ci }); 281ffe3c632Sopenharmony_ci 282ffe3c632Sopenharmony_ci /** 283ffe3c632Sopenharmony_ci * Tests deserialization of undefined map keys go to default values in 284ffe3c632Sopenharmony_ci * binary format. 285ffe3c632Sopenharmony_ci */ 286ffe3c632Sopenharmony_ci it('testMapDeserializationForUndefinedKeys', function() { 287ffe3c632Sopenharmony_ci var testMessageOptionalKeys = new proto.jspb.test.TestMapFieldsOptionalKeys(); 288ffe3c632Sopenharmony_ci var mapEntryStringKey = new proto.jspb.test.MapEntryOptionalKeysStringKey(); 289ffe3c632Sopenharmony_ci mapEntryStringKey.setValue("a"); 290ffe3c632Sopenharmony_ci testMessageOptionalKeys.setMapStringString(mapEntryStringKey); 291ffe3c632Sopenharmony_ci var mapEntryInt32Key = new proto.jspb.test.MapEntryOptionalKeysInt32Key(); 292ffe3c632Sopenharmony_ci mapEntryInt32Key.setValue("b"); 293ffe3c632Sopenharmony_ci testMessageOptionalKeys.setMapInt32String(mapEntryInt32Key); 294ffe3c632Sopenharmony_ci var mapEntryInt64Key = new proto.jspb.test.MapEntryOptionalKeysInt64Key(); 295ffe3c632Sopenharmony_ci mapEntryInt64Key.setValue("c"); 296ffe3c632Sopenharmony_ci testMessageOptionalKeys.setMapInt64String(mapEntryInt64Key); 297ffe3c632Sopenharmony_ci var mapEntryBoolKey = new proto.jspb.test.MapEntryOptionalKeysBoolKey(); 298ffe3c632Sopenharmony_ci mapEntryBoolKey.setValue("d"); 299ffe3c632Sopenharmony_ci testMessageOptionalKeys.setMapBoolString(mapEntryBoolKey); 300ffe3c632Sopenharmony_ci var deserializedMessage = msgInfo.deserializeBinary( 301ffe3c632Sopenharmony_ci testMessageOptionalKeys.serializeBinary() 302ffe3c632Sopenharmony_ci ); 303ffe3c632Sopenharmony_ci checkMapEquals(deserializedMessage.getMapStringStringMap(), [ 304ffe3c632Sopenharmony_ci ['', 'a'] 305ffe3c632Sopenharmony_ci ]); 306ffe3c632Sopenharmony_ci checkMapEquals(deserializedMessage.getMapInt32StringMap(), [ 307ffe3c632Sopenharmony_ci [0, 'b'] 308ffe3c632Sopenharmony_ci ]); 309ffe3c632Sopenharmony_ci checkMapEquals(deserializedMessage.getMapInt64StringMap(), [ 310ffe3c632Sopenharmony_ci [0, 'c'] 311ffe3c632Sopenharmony_ci ]); 312ffe3c632Sopenharmony_ci checkMapEquals(deserializedMessage.getMapBoolStringMap(), [ 313ffe3c632Sopenharmony_ci [false, 'd'] 314ffe3c632Sopenharmony_ci ]); 315ffe3c632Sopenharmony_ci }); 316ffe3c632Sopenharmony_ci 317ffe3c632Sopenharmony_ci /** 318ffe3c632Sopenharmony_ci * Tests deserialization of undefined map values go to default values in 319ffe3c632Sopenharmony_ci * binary format. 320ffe3c632Sopenharmony_ci */ 321ffe3c632Sopenharmony_ci it('testMapDeserializationForUndefinedValues', function() { 322ffe3c632Sopenharmony_ci var testMessageOptionalValues = 323ffe3c632Sopenharmony_ci new proto.jspb.test.TestMapFieldsOptionalValues(); 324ffe3c632Sopenharmony_ci var mapEntryStringValue = 325ffe3c632Sopenharmony_ci new proto.jspb.test.MapEntryOptionalValuesStringValue(); 326ffe3c632Sopenharmony_ci mapEntryStringValue.setKey("a"); 327ffe3c632Sopenharmony_ci testMessageOptionalValues.setMapStringString(mapEntryStringValue); 328ffe3c632Sopenharmony_ci var mapEntryInt32Value = 329ffe3c632Sopenharmony_ci new proto.jspb.test.MapEntryOptionalValuesInt32Value(); 330ffe3c632Sopenharmony_ci mapEntryInt32Value.setKey("b"); 331ffe3c632Sopenharmony_ci testMessageOptionalValues.setMapStringInt32(mapEntryInt32Value); 332ffe3c632Sopenharmony_ci var mapEntryInt64Value = 333ffe3c632Sopenharmony_ci new proto.jspb.test.MapEntryOptionalValuesInt64Value(); 334ffe3c632Sopenharmony_ci mapEntryInt64Value.setKey("c"); 335ffe3c632Sopenharmony_ci testMessageOptionalValues.setMapStringInt64(mapEntryInt64Value); 336ffe3c632Sopenharmony_ci var mapEntryBoolValue = 337ffe3c632Sopenharmony_ci new proto.jspb.test.MapEntryOptionalValuesBoolValue(); 338ffe3c632Sopenharmony_ci mapEntryBoolValue.setKey("d"); 339ffe3c632Sopenharmony_ci testMessageOptionalValues.setMapStringBool(mapEntryBoolValue); 340ffe3c632Sopenharmony_ci var mapEntryDoubleValue = 341ffe3c632Sopenharmony_ci new proto.jspb.test.MapEntryOptionalValuesDoubleValue(); 342ffe3c632Sopenharmony_ci mapEntryDoubleValue.setKey("e"); 343ffe3c632Sopenharmony_ci testMessageOptionalValues.setMapStringDouble(mapEntryDoubleValue); 344ffe3c632Sopenharmony_ci var mapEntryEnumValue = 345ffe3c632Sopenharmony_ci new proto.jspb.test.MapEntryOptionalValuesEnumValue(); 346ffe3c632Sopenharmony_ci mapEntryEnumValue.setKey("f"); 347ffe3c632Sopenharmony_ci testMessageOptionalValues.setMapStringEnum(mapEntryEnumValue); 348ffe3c632Sopenharmony_ci var mapEntryMessageValue = 349ffe3c632Sopenharmony_ci new proto.jspb.test.MapEntryOptionalValuesMessageValue(); 350ffe3c632Sopenharmony_ci mapEntryMessageValue.setKey("g"); 351ffe3c632Sopenharmony_ci testMessageOptionalValues.setMapStringMsg(mapEntryMessageValue); 352ffe3c632Sopenharmony_ci var deserializedMessage = msgInfo.deserializeBinary( 353ffe3c632Sopenharmony_ci testMessageOptionalValues.serializeBinary() 354ffe3c632Sopenharmony_ci ); 355ffe3c632Sopenharmony_ci checkMapEquals(deserializedMessage.getMapStringStringMap(), [ 356ffe3c632Sopenharmony_ci ['a', ''] 357ffe3c632Sopenharmony_ci ]); 358ffe3c632Sopenharmony_ci checkMapEquals(deserializedMessage.getMapStringInt32Map(), [ 359ffe3c632Sopenharmony_ci ['b', 0] 360ffe3c632Sopenharmony_ci ]); 361ffe3c632Sopenharmony_ci checkMapEquals(deserializedMessage.getMapStringInt64Map(), [ 362ffe3c632Sopenharmony_ci ['c', 0] 363ffe3c632Sopenharmony_ci ]); 364ffe3c632Sopenharmony_ci checkMapEquals(deserializedMessage.getMapStringBoolMap(), [ 365ffe3c632Sopenharmony_ci ['d', false] 366ffe3c632Sopenharmony_ci ]); 367ffe3c632Sopenharmony_ci checkMapEquals(deserializedMessage.getMapStringDoubleMap(), [ 368ffe3c632Sopenharmony_ci ['e', 0.0] 369ffe3c632Sopenharmony_ci ]); 370ffe3c632Sopenharmony_ci checkMapEquals(deserializedMessage.getMapStringEnumMap(), [ 371ffe3c632Sopenharmony_ci ['f', 0] 372ffe3c632Sopenharmony_ci ]); 373ffe3c632Sopenharmony_ci checkMapEquals(deserializedMessage.getMapStringMsgMap(), [ 374ffe3c632Sopenharmony_ci ['g', []] 375ffe3c632Sopenharmony_ci ]); 376ffe3c632Sopenharmony_ci }); 377ffe3c632Sopenharmony_ci } 378ffe3c632Sopenharmony_ci 379ffe3c632Sopenharmony_ci 380ffe3c632Sopenharmony_ci /** 381ffe3c632Sopenharmony_ci * Exercises the lazy map<->underlying array sync. 382ffe3c632Sopenharmony_ci */ 383ffe3c632Sopenharmony_ci it('testLazyMapSync' + suffix, function() { 384ffe3c632Sopenharmony_ci // Start with a JSPB array containing a few map entries. 385ffe3c632Sopenharmony_ci var entries = [ 386ffe3c632Sopenharmony_ci ['a', 'entry 1'], 387ffe3c632Sopenharmony_ci ['c', 'entry 2'], 388ffe3c632Sopenharmony_ci ['b', 'entry 3'] 389ffe3c632Sopenharmony_ci ]; 390ffe3c632Sopenharmony_ci var msg = new msgInfo.constructor([entries]); 391ffe3c632Sopenharmony_ci assertEquals(entries.length, 3); 392ffe3c632Sopenharmony_ci assertEquals(entries[0][0], 'a'); 393ffe3c632Sopenharmony_ci assertEquals(entries[1][0], 'c'); 394ffe3c632Sopenharmony_ci assertEquals(entries[2][0], 'b'); 395ffe3c632Sopenharmony_ci msg.getMapStringStringMap().del('a'); 396ffe3c632Sopenharmony_ci assertEquals(entries.length, 3); // not yet sync'd 397ffe3c632Sopenharmony_ci msg.toArray(); // force a sync 398ffe3c632Sopenharmony_ci assertEquals(entries.length, 2); 399ffe3c632Sopenharmony_ci assertEquals(entries[0][0], 'b'); // now in sorted order 400ffe3c632Sopenharmony_ci assertEquals(entries[1][0], 'c'); 401ffe3c632Sopenharmony_ci 402ffe3c632Sopenharmony_ci var a = msg.toArray(); 403ffe3c632Sopenharmony_ci assertEquals(a[0], entries); // retains original reference 404ffe3c632Sopenharmony_ci }); 405ffe3c632Sopenharmony_ci 406ffe3c632Sopenharmony_ci /** 407ffe3c632Sopenharmony_ci * Returns IteratorIterables for entries(), keys() and values(). 408ffe3c632Sopenharmony_ci */ 409ffe3c632Sopenharmony_ci it('testIteratorIterables' + suffix, function() { 410ffe3c632Sopenharmony_ci var msg = new msgInfo.constructor(); 411ffe3c632Sopenharmony_ci var m = msg.getMapStringStringMap(); 412ffe3c632Sopenharmony_ci m.set('key1', 'value1'); 413ffe3c632Sopenharmony_ci m.set('key2', 'value2'); 414ffe3c632Sopenharmony_ci var entryIterator = m.entries(); 415ffe3c632Sopenharmony_ci assertElementsEquals(entryIterator.next().value, ['key1', 'value1']); 416ffe3c632Sopenharmony_ci assertElementsEquals(entryIterator.next().value, ['key2', 'value2']); 417ffe3c632Sopenharmony_ci assertEquals(entryIterator.next().done, true); 418ffe3c632Sopenharmony_ci 419ffe3c632Sopenharmony_ci try { 420ffe3c632Sopenharmony_ci var entryIterable = m.entries()[Symbol.iterator](); 421ffe3c632Sopenharmony_ci assertElementsEquals(entryIterable.next().value, ['key1', 'value1']); 422ffe3c632Sopenharmony_ci assertElementsEquals(entryIterable.next().value, ['key2', 'value2']); 423ffe3c632Sopenharmony_ci assertEquals(entryIterable.next().done, true); 424ffe3c632Sopenharmony_ci } catch (err) { 425ffe3c632Sopenharmony_ci // jspb.Map.ArrayIteratorIterable_.prototype[Symbol.iterator] may be 426ffe3c632Sopenharmony_ci // undefined in some environment. 427ffe3c632Sopenharmony_ci if (err.name != 'TypeError' && err.name != 'ReferenceError') { 428ffe3c632Sopenharmony_ci throw err; 429ffe3c632Sopenharmony_ci } 430ffe3c632Sopenharmony_ci } 431ffe3c632Sopenharmony_ci 432ffe3c632Sopenharmony_ci var keyIterator = m.keys(); 433ffe3c632Sopenharmony_ci assertEquals(keyIterator.next().value, 'key1'); 434ffe3c632Sopenharmony_ci assertEquals(keyIterator.next().value, 'key2'); 435ffe3c632Sopenharmony_ci assertEquals(keyIterator.next().done, true); 436ffe3c632Sopenharmony_ci 437ffe3c632Sopenharmony_ci try { 438ffe3c632Sopenharmony_ci var keyIterable = m.keys()[Symbol.iterator](); 439ffe3c632Sopenharmony_ci assertEquals(keyIterable.next().value, 'key1'); 440ffe3c632Sopenharmony_ci assertEquals(keyIterable.next().value, 'key2'); 441ffe3c632Sopenharmony_ci assertEquals(keyIterable.next().done, true); 442ffe3c632Sopenharmony_ci } catch (err) { 443ffe3c632Sopenharmony_ci // jspb.Map.ArrayIteratorIterable_.prototype[Symbol.iterator] may be 444ffe3c632Sopenharmony_ci // undefined in some environment. 445ffe3c632Sopenharmony_ci if (err.name != 'TypeError' && err.name != 'ReferenceError') { 446ffe3c632Sopenharmony_ci throw err; 447ffe3c632Sopenharmony_ci } 448ffe3c632Sopenharmony_ci } 449ffe3c632Sopenharmony_ci var valueIterator = m.values(); 450ffe3c632Sopenharmony_ci assertEquals(valueIterator.next().value, 'value1'); 451ffe3c632Sopenharmony_ci assertEquals(valueIterator.next().value, 'value2'); 452ffe3c632Sopenharmony_ci assertEquals(valueIterator.next().done, true); 453ffe3c632Sopenharmony_ci 454ffe3c632Sopenharmony_ci try { 455ffe3c632Sopenharmony_ci var valueIterable = m.values()[Symbol.iterator](); 456ffe3c632Sopenharmony_ci assertEquals(valueIterable.next().value, 'value1'); 457ffe3c632Sopenharmony_ci assertEquals(valueIterable.next().value, 'value2'); 458ffe3c632Sopenharmony_ci assertEquals(valueIterable.next().done, true); 459ffe3c632Sopenharmony_ci } catch (err) { 460ffe3c632Sopenharmony_ci // jspb.Map.ArrayIteratorIterable_.prototype[Symbol.iterator] may be 461ffe3c632Sopenharmony_ci // undefined in some environment. 462ffe3c632Sopenharmony_ci if (err.name != 'TypeError' && err.name != 'ReferenceError') { 463ffe3c632Sopenharmony_ci throw err; 464ffe3c632Sopenharmony_ci } 465ffe3c632Sopenharmony_ci } 466ffe3c632Sopenharmony_ci }); 467ffe3c632Sopenharmony_ci} 468ffe3c632Sopenharmony_ci 469ffe3c632Sopenharmony_cidescribe('mapsTest', function() { 470ffe3c632Sopenharmony_ci makeTests( 471ffe3c632Sopenharmony_ci { 472ffe3c632Sopenharmony_ci constructor: proto.jspb.test.TestMapFields, 473ffe3c632Sopenharmony_ci deserializeBinary: proto.jspb.test.TestMapFields.deserializeBinary 474ffe3c632Sopenharmony_ci }, 475ffe3c632Sopenharmony_ci proto.jspb.test.MapValueMessage, '_Binary'); 476ffe3c632Sopenharmony_ci makeTests( 477ffe3c632Sopenharmony_ci { 478ffe3c632Sopenharmony_ci constructor: proto.jspb.test.TestMapFieldsNoBinary, 479ffe3c632Sopenharmony_ci deserializeBinary: null 480ffe3c632Sopenharmony_ci }, 481ffe3c632Sopenharmony_ci proto.jspb.test.MapValueMessageNoBinary, '_NoBinary'); 482ffe3c632Sopenharmony_ci}); 483