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