maps_test.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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.MapEntryOptionalKeysStringKey');
  38. goog.require('proto.jspb.test.MapEntryOptionalKeysInt32Key');
  39. goog.require('proto.jspb.test.MapEntryOptionalKeysInt64Key');
  40. goog.require('proto.jspb.test.MapEntryOptionalKeysBoolKey');
  41. // CommonJS-LoadFromFile: test_pb proto.jspb.test
  42. goog.require('proto.jspb.test.MapValueMessageNoBinary');
  43. goog.require('proto.jspb.test.TestMapFieldsNoBinary');
  44. /**
  45. * Helper: check that the given map has exactly this set of (sorted) entries.
  46. * @param {!jspb.Map} map
  47. * @param {!Array<!Array<?>>} entries
  48. */
  49. function checkMapEquals(map, entries) {
  50. var arr = map.toArray();
  51. assertEquals(arr.length, entries.length);
  52. for (var i = 0; i < arr.length; i++) {
  53. assertElementsEquals(arr[i], entries[i]);
  54. }
  55. }
  56. /**
  57. * Converts an ES6 iterator to an array.
  58. * @template T
  59. * @param {!Iterator<T>} iter an iterator
  60. * @return {!Array<T>}
  61. */
  62. function toArray(iter) {
  63. var arr = [];
  64. while (true) {
  65. var val = iter.next();
  66. if (val.done) {
  67. break;
  68. }
  69. arr.push(val.value);
  70. }
  71. return arr;
  72. }
  73. /**
  74. * Helper: generate test methods for this TestMapFields class.
  75. * @param {?} msgInfo
  76. * @param {?} submessageCtor
  77. * @param {string} suffix
  78. */
  79. function makeTests(msgInfo, submessageCtor, suffix) {
  80. /**
  81. * Helper: fill all maps on a TestMapFields.
  82. * @param {?} msg
  83. */
  84. var fillMapFields = function(msg) {
  85. msg.getMapStringStringMap().set('asdf', 'jkl;').set('key 2', 'hello world');
  86. msg.getMapStringInt32Map().set('a', 1).set('b', -2);
  87. msg.getMapStringInt64Map().set('c', 0x100000000).set('d', 0x200000000);
  88. msg.getMapStringBoolMap().set('e', true).set('f', false);
  89. msg.getMapStringDoubleMap().set('g', 3.14159).set('h', 2.71828);
  90. msg.getMapStringEnumMap()
  91. .set('i', proto.jspb.test.MapValueEnum.MAP_VALUE_BAR)
  92. .set('j', proto.jspb.test.MapValueEnum.MAP_VALUE_BAZ);
  93. msg.getMapStringMsgMap()
  94. .set('k', new submessageCtor())
  95. .set('l', new submessageCtor());
  96. msg.getMapStringMsgMap().get('k').setFoo(42);
  97. msg.getMapStringMsgMap().get('l').setFoo(84);
  98. msg.getMapInt32StringMap().set(-1, 'a').set(42, 'b');
  99. msg.getMapInt64StringMap().set(0x123456789abc, 'c').set(0xcba987654321, 'd');
  100. msg.getMapBoolStringMap().set(false, 'e').set(true, 'f');
  101. };
  102. /**
  103. * Helper: check all maps on a TestMapFields.
  104. * @param {?} msg
  105. */
  106. var checkMapFields = function(msg) {
  107. checkMapEquals(msg.getMapStringStringMap(), [
  108. ['asdf', 'jkl;'],
  109. ['key 2', 'hello world']
  110. ]);
  111. checkMapEquals(msg.getMapStringInt32Map(), [
  112. ['a', 1],
  113. ['b', -2]
  114. ]);
  115. checkMapEquals(msg.getMapStringInt64Map(), [
  116. ['c', 0x100000000],
  117. ['d', 0x200000000]
  118. ]);
  119. checkMapEquals(msg.getMapStringBoolMap(), [
  120. ['e', true],
  121. ['f', false]
  122. ]);
  123. checkMapEquals(msg.getMapStringDoubleMap(), [
  124. ['g', 3.14159],
  125. ['h', 2.71828]
  126. ]);
  127. checkMapEquals(msg.getMapStringEnumMap(), [
  128. ['i', proto.jspb.test.MapValueEnum.MAP_VALUE_BAR],
  129. ['j', proto.jspb.test.MapValueEnum.MAP_VALUE_BAZ]
  130. ]);
  131. checkMapEquals(msg.getMapInt32StringMap(), [
  132. [-1, 'a'],
  133. [42, 'b']
  134. ]);
  135. checkMapEquals(msg.getMapInt64StringMap(), [
  136. [0x123456789abc, 'c'],
  137. [0xcba987654321, 'd']
  138. ]);
  139. checkMapEquals(msg.getMapBoolStringMap(), [
  140. [false, 'e'],
  141. [true, 'f']
  142. ]);
  143. assertEquals(msg.getMapStringMsgMap().getLength(), 2);
  144. assertEquals(msg.getMapStringMsgMap().get('k').getFoo(), 42);
  145. assertEquals(msg.getMapStringMsgMap().get('l').getFoo(), 84);
  146. var entries = toArray(msg.getMapStringMsgMap().entries());
  147. assertEquals(entries.length, 2);
  148. entries.forEach(function(entry) {
  149. var key = entry[0];
  150. var val = entry[1];
  151. assert(val === msg.getMapStringMsgMap().get(key));
  152. });
  153. msg.getMapStringMsgMap().forEach(function(val, key) {
  154. assert(val === msg.getMapStringMsgMap().get(key));
  155. });
  156. };
  157. it('testMapStringStringField' + suffix, function() {
  158. var msg = new msgInfo.constructor();
  159. assertEquals(msg.getMapStringStringMap().getLength(), 0);
  160. assertEquals(msg.getMapStringInt32Map().getLength(), 0);
  161. assertEquals(msg.getMapStringInt64Map().getLength(), 0);
  162. assertEquals(msg.getMapStringBoolMap().getLength(), 0);
  163. assertEquals(msg.getMapStringDoubleMap().getLength(), 0);
  164. assertEquals(msg.getMapStringEnumMap().getLength(), 0);
  165. assertEquals(msg.getMapStringMsgMap().getLength(), 0);
  166. // Re-create to clear out any internally-cached wrappers, etc.
  167. msg = new msgInfo.constructor();
  168. var m = msg.getMapStringStringMap();
  169. assertEquals(m.has('asdf'), false);
  170. assertEquals(m.get('asdf'), undefined);
  171. m.set('asdf', 'hello world');
  172. assertEquals(m.has('asdf'), true);
  173. assertEquals(m.get('asdf'), 'hello world');
  174. m.set('jkl;', 'key 2');
  175. assertEquals(m.has('jkl;'), true);
  176. assertEquals(m.get('jkl;'), 'key 2');
  177. assertEquals(m.getLength(), 2);
  178. var it = m.entries();
  179. assertElementsEquals(it.next().value, ['asdf', 'hello world']);
  180. assertElementsEquals(it.next().value, ['jkl;', 'key 2']);
  181. assertEquals(it.next().done, true);
  182. checkMapEquals(m, [
  183. ['asdf', 'hello world'],
  184. ['jkl;', 'key 2']
  185. ]);
  186. m.del('jkl;');
  187. assertEquals(m.has('jkl;'), false);
  188. assertEquals(m.get('jkl;'), undefined);
  189. assertEquals(m.getLength(), 1);
  190. it = m.keys();
  191. assertEquals(it.next().value, 'asdf');
  192. assertEquals(it.next().done, true);
  193. it = m.values();
  194. assertEquals(it.next().value, 'hello world');
  195. assertEquals(it.next().done, true);
  196. var count = 0;
  197. m.forEach(function(value, key, map) {
  198. assertEquals(map, m);
  199. assertEquals(key, 'asdf');
  200. assertEquals(value, 'hello world');
  201. count++;
  202. });
  203. assertEquals(count, 1);
  204. m.clear();
  205. assertEquals(m.getLength(), 0);
  206. });
  207. /**
  208. * Tests operations on maps with all key and value types.
  209. */
  210. it('testAllMapTypes' + suffix, function() {
  211. var msg = new msgInfo.constructor();
  212. fillMapFields(msg);
  213. checkMapFields(msg);
  214. });
  215. if (msgInfo.deserializeBinary) {
  216. /**
  217. * Tests serialization and deserialization in binary format.
  218. */
  219. it('testBinaryFormat' + suffix, function() {
  220. if (goog.userAgent.IE && !goog.userAgent.isDocumentModeOrHigher(10)) {
  221. // IE8/9 currently doesn't support binary format because they lack
  222. // TypedArray.
  223. return;
  224. }
  225. // Check that the format is correct.
  226. var msg = new msgInfo.constructor();
  227. msg.getMapStringStringMap().set('A', 'a');
  228. var serialized = msg.serializeBinary();
  229. var expectedSerialized = [
  230. 0x0a, 0x6, // field 1 (map_string_string), delimited, length 6
  231. 0x0a, 0x1, // field 1 in submessage (key), delimited, length 1
  232. 0x41, // ASCII 'A'
  233. 0x12, 0x1, // field 2 in submessage (value), delimited, length 1
  234. 0x61 // ASCII 'a'
  235. ];
  236. assertEquals(serialized.length, expectedSerialized.length);
  237. for (var i = 0; i < serialized.length; i++) {
  238. assertEquals(serialized[i], expectedSerialized[i]);
  239. }
  240. // Check that all map fields successfully round-trip.
  241. msg = new msgInfo.constructor();
  242. fillMapFields(msg);
  243. serialized = msg.serializeBinary();
  244. var decoded = msgInfo.deserializeBinary(serialized);
  245. checkMapFields(decoded);
  246. });
  247. /**
  248. * Tests deserialization of undefined map keys go to default values in binary format.
  249. */
  250. it('testMapDeserializationForUndefinedKeys', function() {
  251. var testMessageOptionalKeys = new proto.jspb.test.TestMapFieldsOptionalKeys();
  252. var mapEntryStringKey = new proto.jspb.test.MapEntryOptionalKeysStringKey();
  253. mapEntryStringKey.setValue("a");
  254. testMessageOptionalKeys.setMapStringString(mapEntryStringKey);
  255. var mapEntryInt32Key = new proto.jspb.test.MapEntryOptionalKeysInt32Key();
  256. mapEntryInt32Key.setValue("b");
  257. testMessageOptionalKeys.setMapInt32String(mapEntryInt32Key);
  258. var mapEntryInt64Key = new proto.jspb.test.MapEntryOptionalKeysInt64Key();
  259. mapEntryInt64Key.setValue("c");
  260. testMessageOptionalKeys.setMapInt64String(mapEntryInt64Key);
  261. var mapEntryBoolKey = new proto.jspb.test.MapEntryOptionalKeysBoolKey();
  262. mapEntryBoolKey.setValue("d");
  263. testMessageOptionalKeys.setMapBoolString(mapEntryBoolKey);
  264. var deserializedMessage = msgInfo.deserializeBinary(
  265. testMessageOptionalKeys.serializeBinary()
  266. );
  267. checkMapEquals(deserializedMessage.getMapStringStringMap(), [
  268. ['', 'a']
  269. ]);
  270. checkMapEquals(deserializedMessage.getMapInt32StringMap(), [
  271. [0, 'b']
  272. ]);
  273. checkMapEquals(deserializedMessage.getMapInt64StringMap(), [
  274. [0, 'c']
  275. ]);
  276. checkMapEquals(deserializedMessage.getMapBoolStringMap(), [
  277. [false, 'd']
  278. ]);
  279. });
  280. }
  281. /**
  282. * Exercises the lazy map<->underlying array sync.
  283. */
  284. it('testLazyMapSync' + suffix, function() {
  285. // Start with a JSPB array containing a few map entries.
  286. var entries = [
  287. ['a', 'entry 1'],
  288. ['c', 'entry 2'],
  289. ['b', 'entry 3']
  290. ];
  291. var msg = new msgInfo.constructor([entries]);
  292. assertEquals(entries.length, 3);
  293. assertEquals(entries[0][0], 'a');
  294. assertEquals(entries[1][0], 'c');
  295. assertEquals(entries[2][0], 'b');
  296. msg.getMapStringStringMap().del('a');
  297. assertEquals(entries.length, 3); // not yet sync'd
  298. msg.toArray(); // force a sync
  299. assertEquals(entries.length, 2);
  300. assertEquals(entries[0][0], 'b'); // now in sorted order
  301. assertEquals(entries[1][0], 'c');
  302. var a = msg.toArray();
  303. assertEquals(a[0], entries); // retains original reference
  304. });
  305. /**
  306. * Returns IteratorIterables for entries(), keys() and values().
  307. */
  308. it('testIteratorIterables' + suffix, function() {
  309. var msg = new msgInfo.constructor();
  310. var m = msg.getMapStringStringMap();
  311. m.set('key1', 'value1');
  312. m.set('key2', 'value2');
  313. var entryIterator = m.entries();
  314. assertElementsEquals(entryIterator.next().value, ['key1', 'value1']);
  315. assertElementsEquals(entryIterator.next().value, ['key2', 'value2']);
  316. assertEquals(entryIterator.next().done, true);
  317. if (typeof(Symbol) != 'undefined') {
  318. var entryIterable = m.entries()[Symbol.iterator]();
  319. assertElementsEquals(entryIterable.next().value, ['key1', 'value1']);
  320. assertElementsEquals(entryIterable.next().value, ['key2', 'value2']);
  321. assertEquals(entryIterable.next().done, true);
  322. }
  323. var keyIterator = m.keys();
  324. assertEquals(keyIterator.next().value, 'key1');
  325. assertEquals(keyIterator.next().value, 'key2');
  326. assertEquals(keyIterator.next().done, true);
  327. if (typeof(Symbol) != 'undefined') {
  328. var keyIterable = m.keys()[Symbol.iterator]();
  329. assertEquals(keyIterable.next().value, 'key1');
  330. assertEquals(keyIterable.next().value, 'key2');
  331. assertEquals(keyIterable.next().done, true);
  332. }
  333. var valueIterator = m.values();
  334. assertEquals(valueIterator.next().value, 'value1');
  335. assertEquals(valueIterator.next().value, 'value2');
  336. assertEquals(valueIterator.next().done, true);
  337. if (typeof(Symbol) != 'undefined') {
  338. var valueIterable = m.values()[Symbol.iterator]();
  339. assertEquals(valueIterable.next().value, 'value1');
  340. assertEquals(valueIterable.next().value, 'value2');
  341. assertEquals(valueIterable.next().done, true);
  342. }
  343. });
  344. }
  345. describe('mapsTest', function() {
  346. makeTests(
  347. {
  348. constructor: proto.jspb.test.TestMapFields,
  349. deserializeBinary: proto.jspb.test.TestMapFields.deserializeBinary
  350. },
  351. proto.jspb.test.MapValueMessage, '_Binary');
  352. makeTests(
  353. {
  354. constructor: proto.jspb.test.TestMapFieldsNoBinary,
  355. deserializeBinary: null
  356. },
  357. proto.jspb.test.MapValueMessageNoBinary, '_NoBinary');
  358. });