maps_test.js 17 KB

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