maps_test.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. // CommonJS-LoadFromFile: test_pb proto.jspb.test
  37. goog.require('proto.jspb.test.MapValueMessageNoBinary');
  38. goog.require('proto.jspb.test.TestMapFieldsNoBinary');
  39. goog.requireType('jspb.Map');
  40. /**
  41. * Helper: check that the given map has exactly this set of (sorted) entries.
  42. * @param {!jspb.Map} map
  43. * @param {!Array<!Array<?>>} entries
  44. */
  45. function checkMapEquals(map, entries) {
  46. var arr = map.toArray();
  47. assertEquals(arr.length, entries.length);
  48. for (var i = 0; i < arr.length; i++) {
  49. assertElementsEquals(arr[i], entries[i]);
  50. }
  51. }
  52. /**
  53. * Converts an ES6 iterator to an array.
  54. * @template T
  55. * @param {!Iterator<T>} iter an iterator
  56. * @return {!Array<T>}
  57. */
  58. function toArray(iter) {
  59. var arr = [];
  60. while (true) {
  61. var val = iter.next();
  62. if (val.done) {
  63. break;
  64. }
  65. arr.push(val.value);
  66. }
  67. return arr;
  68. }
  69. /**
  70. * Helper: generate test methods for this TestMapFields class.
  71. * @param {?} msgInfo
  72. * @param {?} submessageCtor
  73. * @param {!string} suffix
  74. */
  75. function makeTests(msgInfo, submessageCtor, suffix) {
  76. /**
  77. * Helper: fill all maps on a TestMapFields.
  78. * @param {?} msg
  79. */
  80. var fillMapFields = function(msg) {
  81. msg.getMapStringStringMap().set('asdf', 'jkl;').set('key 2', 'hello world');
  82. msg.getMapStringInt32Map().set('a', 1).set('b', -2);
  83. msg.getMapStringInt64Map().set('c', 0x100000000).set('d', 0x200000000);
  84. msg.getMapStringBoolMap().set('e', true).set('f', false);
  85. msg.getMapStringDoubleMap().set('g', 3.14159).set('h', 2.71828);
  86. msg.getMapStringEnumMap()
  87. .set('i', proto.jspb.test.MapValueEnum.MAP_VALUE_BAR)
  88. .set('j', proto.jspb.test.MapValueEnum.MAP_VALUE_BAZ);
  89. msg.getMapStringMsgMap()
  90. .set('k', new submessageCtor())
  91. .set('l', new submessageCtor());
  92. msg.getMapStringMsgMap().get('k').setFoo(42);
  93. msg.getMapStringMsgMap().get('l').setFoo(84);
  94. msg.getMapInt32StringMap().set(-1, 'a').set(42, 'b');
  95. msg.getMapInt64StringMap()
  96. .set(0x123456789abc, 'c')
  97. .set(0xcba987654321, 'd');
  98. msg.getMapBoolStringMap().set(false, 'e').set(true, 'f');
  99. };
  100. /**
  101. * Helper: check all maps on a TestMapFields.
  102. * @param {?} msg
  103. */
  104. var checkMapFields = function(msg) {
  105. checkMapEquals(
  106. msg.getMapStringStringMap(),
  107. [['asdf', 'jkl;'], ['key 2', 'hello world']]);
  108. checkMapEquals(msg.getMapStringInt32Map(), [['a', 1], ['b', -2]]);
  109. checkMapEquals(
  110. msg.getMapStringInt64Map(), [['c', 0x100000000], ['d', 0x200000000]]);
  111. checkMapEquals(msg.getMapStringBoolMap(), [['e', true], ['f', false]]);
  112. checkMapEquals(
  113. msg.getMapStringDoubleMap(), [['g', 3.14159], ['h', 2.71828]]);
  114. checkMapEquals(msg.getMapStringEnumMap(), [
  115. ['i', proto.jspb.test.MapValueEnum.MAP_VALUE_BAR],
  116. ['j', proto.jspb.test.MapValueEnum.MAP_VALUE_BAZ]
  117. ]);
  118. checkMapEquals(msg.getMapInt32StringMap(), [[-1, 'a'], [42, 'b']]);
  119. checkMapEquals(
  120. msg.getMapInt64StringMap(),
  121. [[0x123456789abc, 'c'], [0xcba987654321, 'd']]);
  122. checkMapEquals(msg.getMapBoolStringMap(), [[false, 'e'], [true, 'f']]);
  123. assertEquals(msg.getMapStringMsgMap().getLength(), 2);
  124. assertEquals(msg.getMapStringMsgMap().get('k').getFoo(), 42);
  125. assertEquals(msg.getMapStringMsgMap().get('l').getFoo(), 84);
  126. var entries = toArray(msg.getMapStringMsgMap().entries());
  127. assertEquals(entries.length, 2);
  128. entries.forEach(function(entry) {
  129. var key = entry[0];
  130. var val = entry[1];
  131. assert(val === msg.getMapStringMsgMap().get(key));
  132. });
  133. msg.getMapStringMsgMap().forEach(function(val, key) {
  134. assert(val === msg.getMapStringMsgMap().get(key));
  135. });
  136. };
  137. it('testMapStringStringField' + suffix, function() {
  138. var msg = new msgInfo.constructor();
  139. assertEquals(msg.getMapStringStringMap().getLength(), 0);
  140. assertEquals(msg.getMapStringInt32Map().getLength(), 0);
  141. assertEquals(msg.getMapStringInt64Map().getLength(), 0);
  142. assertEquals(msg.getMapStringBoolMap().getLength(), 0);
  143. assertEquals(msg.getMapStringDoubleMap().getLength(), 0);
  144. assertEquals(msg.getMapStringEnumMap().getLength(), 0);
  145. assertEquals(msg.getMapStringMsgMap().getLength(), 0);
  146. // Re-create to clear out any internally-cached wrappers, etc.
  147. msg = new msgInfo.constructor();
  148. var m = msg.getMapStringStringMap();
  149. assertEquals(m.has('asdf'), false);
  150. assertEquals(m.get('asdf'), undefined);
  151. m.set('asdf', 'hello world');
  152. assertEquals(m.has('asdf'), true);
  153. assertEquals(m.get('asdf'), 'hello world');
  154. m.set('jkl;', 'key 2');
  155. assertEquals(m.has('jkl;'), true);
  156. assertEquals(m.get('jkl;'), 'key 2');
  157. assertEquals(m.getLength(), 2);
  158. var it = m.entries();
  159. assertElementsEquals(it.next().value, ['asdf', 'hello world']);
  160. assertElementsEquals(it.next().value, ['jkl;', 'key 2']);
  161. assertEquals(it.next().done, true);
  162. checkMapEquals(m, [['asdf', 'hello world'], ['jkl;', 'key 2']]);
  163. m.del('jkl;');
  164. assertEquals(m.has('jkl;'), false);
  165. assertEquals(m.get('jkl;'), undefined);
  166. assertEquals(m.getLength(), 1);
  167. it = m.keys();
  168. assertEquals(it.next().value, 'asdf');
  169. assertEquals(it.next().done, true);
  170. it = m.values();
  171. assertEquals(it.next().value, 'hello world');
  172. assertEquals(it.next().done, true);
  173. var count = 0;
  174. m.forEach(function(value, key, map) {
  175. assertEquals(map, m);
  176. assertEquals(key, 'asdf');
  177. assertEquals(value, 'hello world');
  178. count++;
  179. });
  180. assertEquals(count, 1);
  181. m.clear();
  182. assertEquals(m.getLength(), 0);
  183. });
  184. /**
  185. * Tests operations on maps with all key and value types.
  186. */
  187. it('testAllMapTypes' + suffix, function() {
  188. var msg = new msgInfo.constructor();
  189. fillMapFields(msg);
  190. checkMapFields(msg);
  191. });
  192. if (msgInfo.deserializeBinary) {
  193. /**
  194. * Tests serialization and deserialization in binary format.
  195. */
  196. it('testBinaryFormat' + suffix, function() {
  197. if (goog.userAgent.IE && !goog.userAgent.isDocumentModeOrHigher(10)) {
  198. // IE8/9 currently doesn't support binary format because they lack
  199. // TypedArray.
  200. return;
  201. }
  202. // Check that the format is correct.
  203. var msg = new msgInfo.constructor();
  204. msg.getMapStringStringMap().set('A', 'a');
  205. var serialized = msg.serializeBinary();
  206. var expectedSerialized = [
  207. 0x0a, 0x6, // field 1 (map_string_string), delimited, length 6
  208. 0x0a, 0x1, // field 1 in submessage (key), delimited, length 1
  209. 0x41, // ASCII 'A'
  210. 0x12, 0x1, // field 2 in submessage (value), delimited, length 1
  211. 0x61 // ASCII 'a'
  212. ];
  213. assertEquals(serialized.length, expectedSerialized.length);
  214. for (var i = 0; i < serialized.length; i++) {
  215. assertEquals(serialized[i], expectedSerialized[i]);
  216. }
  217. // Check that all map fields successfully round-trip.
  218. msg = new msgInfo.constructor();
  219. fillMapFields(msg);
  220. serialized = msg.serializeBinary();
  221. var decoded = msgInfo.deserializeBinary(serialized);
  222. checkMapFields(decoded);
  223. });
  224. }
  225. /**
  226. * Exercises the lazy map<->underlying array sync.
  227. */
  228. it('testLazyMapSync' + suffix, function() {
  229. // Start with a JSPB array containing a few map entries.
  230. var entries = [['a', 'entry 1'], ['c', 'entry 2'], ['b', 'entry 3']];
  231. var msg = new msgInfo.constructor([entries]);
  232. assertEquals(entries.length, 3);
  233. assertEquals(entries[0][0], 'a');
  234. assertEquals(entries[1][0], 'c');
  235. assertEquals(entries[2][0], 'b');
  236. msg.getMapStringStringMap().del('a');
  237. assertEquals(entries.length, 3); // not yet sync'd
  238. msg.toArray(); // force a sync
  239. assertEquals(entries.length, 2);
  240. assertEquals(entries[0][0], 'b'); // now in sorted order
  241. assertEquals(entries[1][0], 'c');
  242. var a = msg.toArray();
  243. assertEquals(a[0], entries); // retains original reference
  244. });
  245. }
  246. describe('mapsTest', function() {
  247. makeTests(
  248. {
  249. constructor: proto.jspb.test.TestMapFields,
  250. deserializeBinary: proto.jspb.test.TestMapFields.deserializeBinary
  251. },
  252. proto.jspb.test.MapValueMessage, '_Binary');
  253. makeTests(
  254. {
  255. constructor: proto.jspb.test.TestMapFieldsNoBinary,
  256. deserializeBinary: null
  257. },
  258. proto.jspb.test.MapValueMessageNoBinary, '_NoBinary');
  259. });