blob: 7d61dacbb35d659d29e38b58dbfe0d5cb80792fa [file] [log] [blame]
Jakob Buchgraber8cc62f72018-07-09 16:45:04 +02001// 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
31// Test suite is written using Jasmine -- see http://jasmine.github.io/
32
33goog.setTestOnly();
34
35goog.require('goog.json');
36goog.require('goog.string');
37goog.require('goog.testing.asserts');
38goog.require('goog.userAgent');
39
40// CommonJS-LoadFromFile: google-protobuf jspb
41goog.require('jspb.Message');
42
43// CommonJS-LoadFromFile: test8_pb proto.jspb.exttest.nested
44goog.require('proto.jspb.exttest.nested.TestNestedExtensionsMessage');
45goog.require('proto.jspb.exttest.nested.TestOuterMessage');
46
47// CommonJS-LoadFromFile: test5_pb proto.jspb.exttest.beta
48goog.require('proto.jspb.exttest.beta.floatingStrField');
49
50// CommonJS-LoadFromFile: test3_pb proto.jspb.exttest
51goog.require('proto.jspb.exttest.floatingMsgField');
52
53// CommonJS-LoadFromFile: test4_pb proto.jspb.exttest
54goog.require('proto.jspb.exttest.floatingMsgFieldTwo');
55
56// CommonJS-LoadFromFile: test_pb proto.jspb.test
57goog.require('proto.jspb.test.CloneExtension');
58goog.require('proto.jspb.test.Complex');
59goog.require('proto.jspb.test.DefaultValues');
60goog.require('proto.jspb.test.Empty');
61goog.require('proto.jspb.test.EnumContainer');
62goog.require('proto.jspb.test.floatingMsgField');
63goog.require('proto.jspb.test.FloatingPointFields');
64goog.require('proto.jspb.test.floatingStrField');
65goog.require('proto.jspb.test.HasExtensions');
66goog.require('proto.jspb.test.IndirectExtension');
67goog.require('proto.jspb.test.IsExtension');
68goog.require('proto.jspb.test.OptionalFields');
69goog.require('proto.jspb.test.OuterEnum');
70goog.require('proto.jspb.test.OuterMessage.Complex');
71goog.require('proto.jspb.test.Simple1');
72goog.require('proto.jspb.test.Simple2');
73goog.require('proto.jspb.test.SpecialCases');
74goog.require('proto.jspb.test.TestClone');
75goog.require('proto.jspb.test.TestEndsWithBytes');
76goog.require('proto.jspb.test.TestGroup');
77goog.require('proto.jspb.test.TestGroup1');
78goog.require('proto.jspb.test.TestMessageWithOneof');
79goog.require('proto.jspb.test.TestReservedNames');
80goog.require('proto.jspb.test.TestReservedNamesExtension');
81
82// CommonJS-LoadFromFile: test2_pb proto.jspb.test
83goog.require('proto.jspb.test.ExtensionMessage');
84goog.require('proto.jspb.test.TestExtensionsMessage');
85
86
87describe('Message test suite', function() {
88 it('testEmptyProto', function() {
89 var empty1 = new proto.jspb.test.Empty([]);
90 var empty2 = new proto.jspb.test.Empty([]);
91 assertObjectEquals({}, empty1.toObject());
92 assertObjectEquals('Message should not be corrupted:', empty2, empty1);
93 });
94
95 it('testTopLevelEnum', function() {
96 var response = new proto.jspb.test.EnumContainer([]);
97 response.setOuterEnum(proto.jspb.test.OuterEnum.FOO);
98 assertEquals(proto.jspb.test.OuterEnum.FOO, response.getOuterEnum());
99 });
100
101 it('testByteStrings', function() {
102 var data = new proto.jspb.test.DefaultValues([]);
103 data.setBytesField('some_bytes');
104 assertEquals('some_bytes', data.getBytesField());
105 });
106
107 it('testComplexConversion', function() {
108 var data1 = ['a',,, [, 11], [[, 22], [, 33]],, ['s1', 's2'],, 1];
109 var data2 = ['a',,, [, 11], [[, 22], [, 33]],, ['s1', 's2'],, 1];
110 var foo = new proto.jspb.test.Complex(data1);
111 var bar = new proto.jspb.test.Complex(data2);
112 var result = foo.toObject();
113 assertObjectEquals({
114 aString: 'a',
115 anOutOfOrderBool: 1,
116 aNestedMessage: {
117 anInt: 11
118 },
119 aRepeatedMessageList: [{anInt: 22}, {anInt: 33}],
120 aRepeatedStringList: ['s1', 's2']
121 }, result);
122
123 // Now test with the jspb instances included.
124 result = foo.toObject(true /* opt_includeInstance */);
125 assertObjectEquals({
126 aString: 'a',
127 anOutOfOrderBool: 1,
128 aNestedMessage: {
129 anInt: 11,
130 $jspbMessageInstance: foo.getANestedMessage()
131 },
132 aRepeatedMessageList: [
133 {anInt: 22, $jspbMessageInstance: foo.getARepeatedMessageList()[0]},
134 {anInt: 33, $jspbMessageInstance: foo.getARepeatedMessageList()[1]}
135 ],
136 aRepeatedStringList: ['s1', 's2'],
137 $jspbMessageInstance: foo
138 }, result);
139
140 });
141
142 it('testMissingFields', function() {
143 var foo = new proto.jspb.test.Complex([
144 undefined, undefined, undefined, [],
145 undefined, undefined, undefined, undefined]);
146 var bar = new proto.jspb.test.Complex([
147 undefined, undefined, undefined, [],
148 undefined, undefined, undefined, undefined]);
149 var result = foo.toObject();
150 assertObjectEquals({
151 aString: undefined,
152 anOutOfOrderBool: undefined,
153 aNestedMessage: {
154 anInt: undefined
155 },
156 // Note: JsPb converts undefined repeated fields to empty arrays.
157 aRepeatedMessageList: [],
158 aRepeatedStringList: []
159 }, result);
160
161 });
162
163 it('testNestedComplexMessage', function() {
164 // Instantiate the message and set a unique field, just to ensure that we
165 // are not getting jspb.test.Complex instead.
166 var msg = new proto.jspb.test.OuterMessage.Complex();
167 msg.setInnerComplexField(5);
168 });
169
170 it('testSpecialCases', function() {
171 // Note: Some property names are reserved in JavaScript.
172 // These names are converted to the Js property named pb_<reserved_name>.
173 var special =
174 new proto.jspb.test.SpecialCases(['normal', 'default', 'function',
175 'var']);
176 var result = special.toObject();
177 assertObjectEquals({
178 normal: 'normal',
179 pb_default: 'default',
180 pb_function: 'function',
181 pb_var: 'var'
182 }, result);
183 });
184
185 it('testDefaultValues', function() {
186 var defaultString = "default<>\'\"abc";
187 var response = new proto.jspb.test.DefaultValues();
188
189 // Test toObject
190 var expectedObject = {
191 stringField: defaultString,
192 boolField: true,
193 intField: 11,
194 enumField: 13,
195 emptyField: '',
196 bytesField: 'bW9v'
197 };
198 assertObjectEquals(expectedObject, response.toObject());
199
200
201 // Test getters
202 response = new proto.jspb.test.DefaultValues();
203 assertEquals(defaultString, response.getStringField());
204 assertEquals(true, response.getBoolField());
205 assertEquals(11, response.getIntField());
206 assertEquals(13, response.getEnumField());
207 assertEquals('', response.getEmptyField());
208 assertEquals('bW9v', response.getBytesField());
209
210 function makeDefault(values) {
211 return new proto.jspb.test.DefaultValues(values);
212 }
213
214 // Test with undefined values,
215 // Use push to workaround IE treating undefined array elements as holes.
216 response = makeDefault([undefined, undefined, undefined, undefined]);
217 assertEquals(defaultString, response.getStringField());
218 assertEquals(true, response.getBoolField());
219 assertEquals(11, response.getIntField());
220 assertEquals(13, response.getEnumField());
221 assertFalse(response.hasStringField());
222 assertFalse(response.hasBoolField());
223 assertFalse(response.hasIntField());
224 assertFalse(response.hasEnumField());
225
226 // Test with null values, as would be returned by a JSON serializer.
227 response = makeDefault([null, null, null, null]);
228 assertEquals(defaultString, response.getStringField());
229 assertEquals(true, response.getBoolField());
230 assertEquals(11, response.getIntField());
231 assertEquals(13, response.getEnumField());
232 assertFalse(response.hasStringField());
233 assertFalse(response.hasBoolField());
234 assertFalse(response.hasIntField());
235 assertFalse(response.hasEnumField());
236
237 // Test with false-like values.
238 response = makeDefault(['', false, 0, 0]);
239 assertEquals('', response.getStringField());
240 assertEquals(false, response.getBoolField());
241 assertEquals(true, response.getIntField() == 0);
242 assertEquals(true, response.getEnumField() == 0);
243 assertTrue(response.hasStringField());
244 assertTrue(response.hasBoolField());
245 assertTrue(response.hasIntField());
246 assertTrue(response.hasEnumField());
247
248 // Test that clearing the values reverts them to the default state.
249 response = makeDefault(['blah', false, 111, 77]);
250 response.clearStringField(); response.clearBoolField();
251 response.clearIntField(); response.clearEnumField();
252 assertEquals(defaultString, response.getStringField());
253 assertEquals(true, response.getBoolField());
254 assertEquals(11, response.getIntField());
255 assertEquals(13, response.getEnumField());
256 assertFalse(response.hasStringField());
257 assertFalse(response.hasBoolField());
258 assertFalse(response.hasIntField());
259 assertFalse(response.hasEnumField());
260
261 // Test that setFoo(null) clears the values.
262 response = makeDefault(['blah', false, 111, 77]);
263 response.setStringField(null); response.setBoolField(null);
264 response.setIntField(undefined); response.setEnumField(undefined);
265 assertEquals(defaultString, response.getStringField());
266 assertEquals(true, response.getBoolField());
267 assertEquals(11, response.getIntField());
268 assertEquals(13, response.getEnumField());
269 assertFalse(response.hasStringField());
270 assertFalse(response.hasBoolField());
271 assertFalse(response.hasIntField());
272 assertFalse(response.hasEnumField());
273 });
274
275 it('testClearFields', function() {
276 var data = ['str', true, [11], [[22], [33]], ['s1', 's2']];
277 var foo = new proto.jspb.test.OptionalFields(data);
278 foo.clearAString();
279 foo.clearABool();
280 foo.clearANestedMessage();
281 foo.clearARepeatedMessageList();
282 foo.clearARepeatedStringList();
283 assertEquals('', foo.getAString());
284 assertEquals(false, foo.getABool());
285 assertUndefined(foo.getANestedMessage());
286 assertFalse(foo.hasAString());
287 assertFalse(foo.hasABool());
288 assertObjectEquals([], foo.getARepeatedMessageList());
289 assertObjectEquals([], foo.getARepeatedStringList());
290 // NOTE: We want the missing fields in 'expected' to be undefined,
291 // but we actually get a sparse array instead. We could use something
292 // like [1,undefined,2] to avoid this, except that this is still
293 // sparse on IE. No comment...
294 var expected = [,,, [], []];
295 expected[0] = expected[1] = expected[2] = undefined;
296 assertObjectEquals(expected, foo.toArray());
297 });
298
299 it('testDifferenceRawObject', /** @suppress {visibility} */ function() {
300 var p1 = new proto.jspb.test.HasExtensions(['hi', 'diff', {}]);
301 var p2 = new proto.jspb.test.HasExtensions(['hi', 'what',
302 {1000: 'unique'}]);
303 var diff = /** @type {proto.jspb.test.HasExtensions} */
304 (jspb.Message.difference(p1, p2));
305 assertEquals('', diff.getStr1());
306 assertEquals('what', diff.getStr2());
307 assertEquals('', diff.getStr3());
308 assertEquals('unique', diff.extensionObject_[1000]);
309 });
310
311 it('testEqualsSimple', function() {
312 var s1 = new proto.jspb.test.Simple1(['hi']);
313 assertTrue(jspb.Message.equals(s1, new proto.jspb.test.Simple1(['hi'])));
314 assertFalse(jspb.Message.equals(s1, new proto.jspb.test.Simple1(['bye'])));
315 var s1b = new proto.jspb.test.Simple1(['hi', ['hello']]);
316 assertTrue(jspb.Message.equals(s1b,
317 new proto.jspb.test.Simple1(['hi', ['hello']])));
318 assertTrue(jspb.Message.equals(s1b,
319 new proto.jspb.test.Simple1(['hi', ['hello', undefined,
320 undefined, undefined]])));
321 assertFalse(jspb.Message.equals(s1b,
322 new proto.jspb.test.Simple1(['no', ['hello']])));
323 // Test with messages of different types
324 var s2 = new proto.jspb.test.Simple2(['hi']);
325 assertFalse(jspb.Message.equals(s1, s2));
326 });
327
328 it('testEquals_softComparison', function() {
329 var s1 = new proto.jspb.test.Simple1(['hi', [], null]);
330 assertTrue(jspb.Message.equals(s1,
331 new proto.jspb.test.Simple1(['hi', []])));
332
333 var s1b = new proto.jspb.test.Simple1(['hi', [], true]);
334 assertTrue(jspb.Message.equals(s1b,
335 new proto.jspb.test.Simple1(['hi', [], 1])));
336 });
337
338 it('testEqualsComplex', function() {
339 var data1 = ['a',,, [, 11], [[, 22], [, 33]],, ['s1', 's2'],, 1];
340 var data2 = ['a',,, [, 11], [[, 22], [, 34]],, ['s1', 's2'],, 1];
341 var data3 = ['a',,, [, 11], [[, 22]],, ['s1', 's2'],, 1];
342 var data4 = ['hi'];
343 var c1a = new proto.jspb.test.Complex(data1);
344 var c1b = new proto.jspb.test.Complex(data1);
345 var c2 = new proto.jspb.test.Complex(data2);
346 var c3 = new proto.jspb.test.Complex(data3);
347 var s1 = new proto.jspb.test.Simple1(data4);
348
349 assertTrue(jspb.Message.equals(c1a, c1b));
350 assertFalse(jspb.Message.equals(c1a, c2));
351 assertFalse(jspb.Message.equals(c2, c3));
352 assertFalse(jspb.Message.equals(c1a, s1));
353 });
354
355 it('testEqualsExtensionsConstructed', function() {
356 assertTrue(jspb.Message.equals(
357 new proto.jspb.test.HasExtensions([]),
358 new proto.jspb.test.HasExtensions([{}])
359 ));
360 assertTrue(jspb.Message.equals(
361 new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'a'}]}]),
362 new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'a'}]}])
363 ));
364 assertFalse(jspb.Message.equals(
365 new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'a'}]}]),
366 new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'b'}]}])
367 ));
368 assertTrue(jspb.Message.equals(
369 new proto.jspb.test.HasExtensions([{100: [{200: 'a'}]}]),
370 new proto.jspb.test.HasExtensions([{100: [{200: 'a'}]}])
371 ));
372 assertTrue(jspb.Message.equals(
373 new proto.jspb.test.HasExtensions([{100: [{200: 'a'}]}]),
374 new proto.jspb.test.HasExtensions([,,, {100: [{200: 'a'}]}])
375 ));
376 assertTrue(jspb.Message.equals(
377 new proto.jspb.test.HasExtensions([,,, {100: [{200: 'a'}]}]),
378 new proto.jspb.test.HasExtensions([{100: [{200: 'a'}]}])
379 ));
380 assertTrue(jspb.Message.equals(
381 new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'a'}]}]),
382 new proto.jspb.test.HasExtensions(['hi',,, {100: [{200: 'a'}]}])
383 ));
384 assertTrue(jspb.Message.equals(
385 new proto.jspb.test.HasExtensions(['hi',,, {100: [{200: 'a'}]}]),
386 new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'a'}]}])
387 ));
388 });
389
390 it('testEqualsExtensionsUnconstructed', function() {
391 assertTrue(jspb.Message.compareFields([], [{}]));
392 assertTrue(jspb.Message.compareFields([,,, {}], []));
393 assertTrue(jspb.Message.compareFields([,,, {}], [,, {}]));
394 assertTrue(jspb.Message.compareFields(
395 ['hi', {100: [{200: 'a'}]}], ['hi', {100: [{200: 'a'}]}]));
396 assertFalse(jspb.Message.compareFields(
397 ['hi', {100: [{200: 'a'}]}], ['hi', {100: [{200: 'b'}]}]));
398 assertTrue(jspb.Message.compareFields(
399 [{100: [{200: 'a'}]}], [{100: [{200: 'a'}]}]));
400 assertTrue(jspb.Message.compareFields(
401 [{100: [{200: 'a'}]}], [,,, {100: [{200: 'a'}]}]));
402 assertTrue(jspb.Message.compareFields(
403 [,,, {100: [{200: 'a'}]}], [{100: [{200: 'a'}]}]));
404 assertTrue(jspb.Message.compareFields(
405 ['hi', {100: [{200: 'a'}]}], ['hi',,, {100: [{200: 'a'}]}]));
406 assertTrue(jspb.Message.compareFields(
407 ['hi',,, {100: [{200: 'a'}]}], ['hi', {100: [{200: 'a'}]}]));
408 });
409
410 it('testToMap', function() {
411 var p1 = new proto.jspb.test.Simple1(['k', ['v']]);
412 var p2 = new proto.jspb.test.Simple1(['k1', ['v1', 'v2']]);
413 var soymap = jspb.Message.toMap([p1, p2],
414 proto.jspb.test.Simple1.prototype.getAString,
415 proto.jspb.test.Simple1.prototype.toObject);
416 assertEquals('k', soymap['k'].aString);
417 assertArrayEquals(['v'], soymap['k'].aRepeatedStringList);
418 var protomap = jspb.Message.toMap([p1, p2],
419 proto.jspb.test.Simple1.prototype.getAString);
420 assertEquals('k', protomap['k'].getAString());
421 assertArrayEquals(['v'], protomap['k'].getARepeatedStringList());
422 });
423
424 it('testClone', function() {
425 var supportsUint8Array =
426 !goog.userAgent.IE || goog.userAgent.isVersionOrHigher('10');
427 var original = new proto.jspb.test.TestClone();
428 original.setStr('v1');
429 var simple1 = new proto.jspb.test.Simple1(['x1', ['y1', 'z1']]);
430 var simple2 = new proto.jspb.test.Simple1(['x2', ['y2', 'z2']]);
431 var simple3 = new proto.jspb.test.Simple1(['x3', ['y3', 'z3']]);
432 original.setSimple1(simple1);
433 original.setSimple2List([simple2, simple3]);
434 var bytes1 = supportsUint8Array ? new Uint8Array([1, 2, 3]) : '123';
435 original.setBytesField(bytes1);
436 var extension = new proto.jspb.test.CloneExtension();
437 extension.setExt('e1');
438 original.setExtension(proto.jspb.test.IsExtension.extField, extension);
439 var clone = original.clone();
440 assertArrayEquals(['v1',, ['x1', ['y1', 'z1']],,
441 [['x2', ['y2', 'z2']], ['x3', ['y3', 'z3']]], bytes1,, { 100: [, 'e1'] }],
442 clone.toArray());
443 clone.setStr('v2');
444 var simple4 = new proto.jspb.test.Simple1(['a1', ['b1', 'c1']]);
445 var simple5 = new proto.jspb.test.Simple1(['a2', ['b2', 'c2']]);
446 var simple6 = new proto.jspb.test.Simple1(['a3', ['b3', 'c3']]);
447 clone.setSimple1(simple4);
448 clone.setSimple2List([simple5, simple6]);
449 if (supportsUint8Array) {
450 clone.getBytesField()[0] = 4;
451 assertObjectEquals(bytes1, original.getBytesField());
452 }
453 var bytes2 = supportsUint8Array ? new Uint8Array([4, 5, 6]) : '456';
454 clone.setBytesField(bytes2);
455 var newExtension = new proto.jspb.test.CloneExtension();
456 newExtension.setExt('e2');
457 clone.setExtension(proto.jspb.test.CloneExtension.extField, newExtension);
458 assertArrayEquals(['v2',, ['a1', ['b1', 'c1']],,
459 [['a2', ['b2', 'c2']], ['a3', ['b3', 'c3']]], bytes2,, { 100: [, 'e2'] }],
460 clone.toArray());
461 assertArrayEquals(['v1',, ['x1', ['y1', 'z1']],,
462 [['x2', ['y2', 'z2']], ['x3', ['y3', 'z3']]], bytes1,, { 100: [, 'e1'] }],
463 original.toArray());
464 });
465
466 it('testCopyInto', function() {
467 var supportsUint8Array =
468 !goog.userAgent.IE || goog.userAgent.isVersionOrHigher('10');
469 var original = new proto.jspb.test.TestClone();
470 original.setStr('v1');
471 var dest = new proto.jspb.test.TestClone();
472 dest.setStr('override');
473 var simple1 = new proto.jspb.test.Simple1(['x1', ['y1', 'z1']]);
474 var simple2 = new proto.jspb.test.Simple1(['x2', ['y2', 'z2']]);
475 var simple3 = new proto.jspb.test.Simple1(['x3', ['y3', 'z3']]);
476 var destSimple1 = new proto.jspb.test.Simple1(['ox1', ['oy1', 'oz1']]);
477 var destSimple2 = new proto.jspb.test.Simple1(['ox2', ['oy2', 'oz2']]);
478 var destSimple3 = new proto.jspb.test.Simple1(['ox3', ['oy3', 'oz3']]);
479 original.setSimple1(simple1);
480 original.setSimple2List([simple2, simple3]);
481 dest.setSimple1(destSimple1);
482 dest.setSimple2List([destSimple2, destSimple3]);
483 var bytes1 = supportsUint8Array ? new Uint8Array([1, 2, 3]) : '123';
484 var bytes2 = supportsUint8Array ? new Uint8Array([4, 5, 6]) : '456';
485 original.setBytesField(bytes1);
486 dest.setBytesField(bytes2);
487 var extension = new proto.jspb.test.CloneExtension();
488 extension.setExt('e1');
489 original.setExtension(proto.jspb.test.CloneExtension.extField, extension);
490
491 jspb.Message.copyInto(original, dest);
492 assertArrayEquals(original.toArray(), dest.toArray());
493 assertEquals('x1', dest.getSimple1().getAString());
494 assertEquals('e1',
495 dest.getExtension(proto.jspb.test.CloneExtension.extField).getExt());
496 dest.getSimple1().setAString('new value');
497 assertNotEquals(dest.getSimple1().getAString(),
498 original.getSimple1().getAString());
499 if (supportsUint8Array) {
500 dest.getBytesField()[0] = 7;
501 assertObjectEquals(bytes1, original.getBytesField());
502 assertObjectEquals(new Uint8Array([7, 2, 3]), dest.getBytesField());
503 } else {
504 dest.setBytesField('789');
505 assertObjectEquals(bytes1, original.getBytesField());
506 assertObjectEquals('789', dest.getBytesField());
507 }
508 dest.getExtension(proto.jspb.test.CloneExtension.extField).
509 setExt('new value');
510 assertNotEquals(
511 dest.getExtension(proto.jspb.test.CloneExtension.extField).getExt(),
512 original.getExtension(
513 proto.jspb.test.CloneExtension.extField).getExt());
514 });
515
516 it('testCopyInto_notSameType', function() {
517 var a = new proto.jspb.test.TestClone();
518 var b = new proto.jspb.test.Simple1(['str', ['s1', 's2']]);
519
520 var e = assertThrows(function() {
521 jspb.Message.copyInto(a, b);
522 });
523 assertContains('should have the same type', e.message);
524 });
525
526 it('testExtensions', function() {
527 var extension1 = new proto.jspb.test.IsExtension(['ext1field']);
528 var extension2 = new proto.jspb.test.Simple1(['str', ['s1', 's2']]);
529 var extendable = new proto.jspb.test.HasExtensions(['v1', 'v2', 'v3']);
530 extendable.setExtension(proto.jspb.test.IsExtension.extField, extension1);
531 extendable.setExtension(proto.jspb.test.IndirectExtension.simple,
532 extension2);
533 extendable.setExtension(proto.jspb.test.IndirectExtension.str, 'xyzzy');
534 extendable.setExtension(proto.jspb.test.IndirectExtension.repeatedStrList,
535 ['a', 'b']);
536 var s1 = new proto.jspb.test.Simple1(['foo', ['s1', 's2']]);
537 var s2 = new proto.jspb.test.Simple1(['bar', ['t1', 't2']]);
538 extendable.setExtension(
539 proto.jspb.test.IndirectExtension.repeatedSimpleList,
540 [s1, s2]);
541 assertObjectEquals(extension1,
542 extendable.getExtension(proto.jspb.test.IsExtension.extField));
543 assertObjectEquals(extension2,
544 extendable.getExtension(proto.jspb.test.IndirectExtension.simple));
545 assertObjectEquals('xyzzy',
546 extendable.getExtension(proto.jspb.test.IndirectExtension.str));
547 assertObjectEquals(['a', 'b'], extendable.getExtension(
548 proto.jspb.test.IndirectExtension.repeatedStrList));
549 assertObjectEquals([s1, s2], extendable.getExtension(
550 proto.jspb.test.IndirectExtension.repeatedSimpleList));
551 // Not supported yet, but it should work...
552 extendable.setExtension(proto.jspb.test.IndirectExtension.simple, null);
553 assertNull(
554 extendable.getExtension(proto.jspb.test.IndirectExtension.simple));
555 extendable.setExtension(proto.jspb.test.IndirectExtension.str, null);
556 assertNull(extendable.getExtension(proto.jspb.test.IndirectExtension.str));
557
558
559 // Extension fields with jspb.ignore = true are ignored.
560 assertUndefined(proto.jspb.test.IndirectExtension['ignored']);
561 assertUndefined(proto.jspb.test.HasExtensions['ignoredFloating']);
562 });
563
564 it('testFloatingExtensions', function() {
565 // From an autogenerated container.
566 var extendable = new proto.jspb.test.HasExtensions(['v1', 'v2', 'v3']);
567 var extension = new proto.jspb.test.Simple1(['foo', ['s1', 's2']]);
568 extendable.setExtension(proto.jspb.test.simple1, extension);
569 assertObjectEquals(extension,
570 extendable.getExtension(proto.jspb.test.simple1));
571
572 // From _lib mode.
573 extension = new proto.jspb.test.ExtensionMessage(['s1']);
574 extendable = new proto.jspb.test.TestExtensionsMessage([16]);
575 extendable.setExtension(proto.jspb.test.floatingMsgField, extension);
576 extendable.setExtension(proto.jspb.test.floatingStrField, 's2');
577 assertObjectEquals(extension,
578 extendable.getExtension(proto.jspb.test.floatingMsgField));
579 assertObjectEquals('s2',
580 extendable.getExtension(proto.jspb.test.floatingStrField));
581 assertNotUndefined(proto.jspb.exttest.floatingMsgField);
582 assertNotUndefined(proto.jspb.exttest.floatingMsgFieldTwo);
583 assertNotUndefined(proto.jspb.exttest.beta.floatingStrField);
584 });
585
586 it('testNestedExtensions', function() {
587 var extendable = new proto.jspb.exttest.nested.TestNestedExtensionsMessage();
588 var extension = new proto.jspb.exttest.nested.TestOuterMessage.NestedExtensionMessage(['s1']);
589 extendable.setExtension(proto.jspb.exttest.nested.TestOuterMessage.innerExtension, extension);
590 assertObjectEquals(extension,
591 extendable.getExtension(proto.jspb.exttest.nested.TestOuterMessage.innerExtension));
592 });
593
594 it('testToObject_extendedObject', function() {
595 var extension1 = new proto.jspb.test.IsExtension(['ext1field']);
596 var extension2 = new proto.jspb.test.Simple1(['str', ['s1', 's2'], true]);
597 var extendable = new proto.jspb.test.HasExtensions(['v1', 'v2', 'v3']);
598 extendable.setExtension(proto.jspb.test.IsExtension.extField, extension1);
599 extendable.setExtension(proto.jspb.test.IndirectExtension.simple,
600 extension2);
601 extendable.setExtension(proto.jspb.test.IndirectExtension.str, 'xyzzy');
602 extendable.setExtension(proto.jspb.test.IndirectExtension.repeatedStrList,
603 ['a', 'b']);
604 var s1 = new proto.jspb.test.Simple1(['foo', ['s1', 's2'], true]);
605 var s2 = new proto.jspb.test.Simple1(['bar', ['t1', 't2'], false]);
606 extendable.setExtension(
607 proto.jspb.test.IndirectExtension.repeatedSimpleList,
608 [s1, s2]);
609 assertObjectEquals({
610 str1: 'v1', str2: 'v2', str3: 'v3',
611 extField: { ext1: 'ext1field' },
612 simple: {
613 aString: 'str', aRepeatedStringList: ['s1', 's2'], aBoolean: true
614 },
615 str: 'xyzzy',
616 repeatedStrList: ['a', 'b'],
617 repeatedSimpleList: [
618 { aString: 'foo', aRepeatedStringList: ['s1', 's2'], aBoolean: true},
619 { aString: 'bar', aRepeatedStringList: ['t1', 't2'], aBoolean: false}
620 ]
621 }, extendable.toObject());
622
623 // Now, with instances included.
624 assertObjectEquals({
625 str1: 'v1', str2: 'v2', str3: 'v3',
626 extField: {
627 ext1: 'ext1field',
628 $jspbMessageInstance:
629 extendable.getExtension(proto.jspb.test.IsExtension.extField)
630 },
631 simple: {
632 aString: 'str',
633 aRepeatedStringList: ['s1', 's2'],
634 aBoolean: true,
635 $jspbMessageInstance:
636 extendable.getExtension(proto.jspb.test.IndirectExtension.simple)
637 },
638 str: 'xyzzy',
639 repeatedStrList: ['a', 'b'],
640 repeatedSimpleList: [{
641 aString: 'foo',
642 aRepeatedStringList: ['s1', 's2'],
643 aBoolean: true,
644 $jspbMessageInstance: s1
645 }, {
646 aString: 'bar',
647 aRepeatedStringList: ['t1', 't2'],
648 aBoolean: false,
649 $jspbMessageInstance: s2
650 }],
651 $jspbMessageInstance: extendable
652 }, extendable.toObject(true /* opt_includeInstance */));
653 });
654
655 it('testInitialization_emptyArray', function() {
656 var msg = new proto.jspb.test.HasExtensions([]);
657 assertArrayEquals([], msg.toArray());
658 });
659
660 it('testInitialization_justExtensionObject', function() {
661 var msg = new proto.jspb.test.Empty([{1: 'hi'}]);
662 // The extensionObject is not moved from its original location.
663 assertArrayEquals([{1: 'hi'}], msg.toArray());
664 });
665
666 it('testInitialization_incompleteList', function() {
667 var msg = new proto.jspb.test.Empty([1, {4: 'hi'}]);
668 // The extensionObject is not moved from its original location.
669 assertArrayEquals([1, {4: 'hi'}], msg.toArray());
670 });
671
672 it('testInitialization_forwardCompatible', function() {
673 var msg = new proto.jspb.test.Empty([1, 2, 3, {1: 'hi'}]);
674 assertArrayEquals([1, 2, 3, {1: 'hi'}], msg.toArray());
675 });
676
677 it('testExtendedMessageEnsureObject',
678 /** @suppress {visibility} */ function() {
679 var data =
680 new proto.jspb.test.HasExtensions(['str1', {'a_key': 'an_object'}]);
681 assertEquals('an_object', data.extensionObject_['a_key']);
682 });
683
684 it('testToObject_hasExtensionField', function() {
685 var data = new proto.jspb.test.HasExtensions(['str1', {100: ['ext1'], 102: ''}]);
686 var obj = data.toObject();
687 assertEquals('str1', obj.str1);
688 assertEquals('ext1', obj.extField.ext1);
689 assertEquals('', obj.str);
690 });
691
692 it('testGetExtension', function() {
693 var data = new proto.jspb.test.HasExtensions(['str1', {100: ['ext1']}]);
694 assertEquals('str1', data.getStr1());
695 var extension = data.getExtension(proto.jspb.test.IsExtension.extField);
696 assertNotNull(extension);
697 assertEquals('ext1', extension.getExt1());
698 });
699
700 it('testSetExtension', function() {
701 var data = new proto.jspb.test.HasExtensions();
702 var extensionMessage = new proto.jspb.test.IsExtension(['is_extension']);
703 data.setExtension(proto.jspb.test.IsExtension.extField, extensionMessage);
704 var obj = data.toObject();
705 assertNotNull(
706 data.getExtension(proto.jspb.test.IsExtension.extField));
707 assertEquals('is_extension', obj.extField.ext1);
708 });
709
710 /**
711 * Note that group is long deprecated, we only support it because JsPb has
712 * a goal of being able to generate JS classes for all proto descriptors.
713 */
714 it('testGroups', function() {
715 var group = new proto.jspb.test.TestGroup();
716 var someGroup = new proto.jspb.test.TestGroup.RepeatedGroup();
717 someGroup.setId('g1');
718 someGroup.setSomeBoolList([true, false]);
719 group.setRepeatedGroupList([someGroup]);
720 var groups = group.getRepeatedGroupList();
721 assertEquals('g1', groups[0].getId());
722 assertObjectEquals([true, false], groups[0].getSomeBoolList());
723 assertObjectEquals({id: 'g1', someBoolList: [true, false]},
724 groups[0].toObject());
725 assertObjectEquals({
726 repeatedGroupList: [{id: 'g1', someBoolList: [true, false]}],
727 requiredGroup: {id: undefined},
728 optionalGroup: undefined,
729 requiredSimple: {aRepeatedStringList: [], aString: undefined},
730 optionalSimple: undefined,
731 id: undefined
732 }, group.toObject());
733 var group1 = new proto.jspb.test.TestGroup1();
734 group1.setGroup(someGroup);
735 assertEquals(someGroup, group1.getGroup());
736 });
737
738 it('testNonExtensionFieldsAfterExtensionRange', function() {
739 var data = [{'1': 'a_string'}];
740 var message = new proto.jspb.test.Complex(data);
741 assertArrayEquals([], message.getARepeatedStringList());
742 });
743
744 it('testReservedGetterNames', function() {
745 var message = new proto.jspb.test.TestReservedNames();
746 message.setExtension$(11);
747 message.setExtension(proto.jspb.test.TestReservedNamesExtension.foo, 12);
748 assertEquals(11, message.getExtension$());
749 assertEquals(12, message.getExtension(
750 proto.jspb.test.TestReservedNamesExtension.foo));
751 assertObjectEquals({extension: 11, foo: 12}, message.toObject());
752 });
753
754 it('testInitializeMessageWithUnsetOneof', function() {
755 var message = new proto.jspb.test.TestMessageWithOneof([]);
756 assertEquals(
757 proto.jspb.test.TestMessageWithOneof.PartialOneofCase.
758 PARTIAL_ONEOF_NOT_SET,
759 message.getPartialOneofCase());
760 assertEquals(
761 proto.jspb.test.TestMessageWithOneof.RecursiveOneofCase.
762 RECURSIVE_ONEOF_NOT_SET,
763 message.getRecursiveOneofCase());
764 });
765
766 it('testInitializeMessageWithSingleValueSetInOneof', function() {
767 var message = new proto.jspb.test.TestMessageWithOneof([,, 'x']);
768
769 assertEquals('x', message.getPone());
770 assertEquals('', message.getPthree());
771 assertEquals(
772 proto.jspb.test.TestMessageWithOneof.PartialOneofCase.PONE,
773 message.getPartialOneofCase());
774 });
775
776 it('testKeepsLastWireValueSetInUnion_multipleValues', function() {
777 var message = new proto.jspb.test.TestMessageWithOneof([,, 'x',, 'y']);
778
779 assertEquals('', message.getPone());
780 assertEquals('y', message.getPthree());
781 assertEquals(
782 proto.jspb.test.TestMessageWithOneof.PartialOneofCase.PTHREE,
783 message.getPartialOneofCase());
784 });
785
786 it('testSettingOneofFieldClearsOthers', function() {
787 var message = new proto.jspb.test.TestMessageWithOneof;
788 assertEquals('', message.getPone());
789 assertEquals('', message.getPthree());
790 assertFalse(message.hasPone());
791 assertFalse(message.hasPthree());
792
793 message.setPone('hi');
794 assertEquals('hi', message.getPone());
795 assertEquals('', message.getPthree());
796 assertTrue(message.hasPone());
797 assertFalse(message.hasPthree());
798
799 message.setPthree('bye');
800 assertEquals('', message.getPone());
801 assertEquals('bye', message.getPthree());
802 assertFalse(message.hasPone());
803 assertTrue(message.hasPthree());
804 });
805
806 it('testSettingOneofFieldDoesNotClearFieldsFromOtherUnions', function() {
807 var other = new proto.jspb.test.TestMessageWithOneof;
808 var message = new proto.jspb.test.TestMessageWithOneof;
809 assertEquals('', message.getPone());
810 assertEquals('', message.getPthree());
811 assertUndefined(message.getRone());
812 assertFalse(message.hasPone());
813 assertFalse(message.hasPthree());
814
815 message.setPone('hi');
816 message.setRone(other);
817 assertEquals('hi', message.getPone());
818 assertEquals('', message.getPthree());
819 assertEquals(other, message.getRone());
820 assertTrue(message.hasPone());
821 assertFalse(message.hasPthree());
822
823 message.setPthree('bye');
824 assertEquals('', message.getPone());
825 assertEquals('bye', message.getPthree());
826 assertEquals(other, message.getRone());
827 assertFalse(message.hasPone());
828 assertTrue(message.hasPthree());
829 });
830
831 it('testUnsetsOneofCaseWhenFieldIsCleared', function() {
832 var message = new proto.jspb.test.TestMessageWithOneof;
833 assertEquals(
834 proto.jspb.test.TestMessageWithOneof.PartialOneofCase.
835 PARTIAL_ONEOF_NOT_SET,
836 message.getPartialOneofCase());
837
838 message.setPone('hi');
839 assertEquals(
840 proto.jspb.test.TestMessageWithOneof.PartialOneofCase.PONE,
841 message.getPartialOneofCase());
842
843 message.clearPone();
844 assertEquals(
845 proto.jspb.test.TestMessageWithOneof.PartialOneofCase.
846 PARTIAL_ONEOF_NOT_SET,
847 message.getPartialOneofCase());
848 });
849
850 it('testMessageWithDefaultOneofValues', function() {
851 var message = new proto.jspb.test.TestMessageWithOneof;
852 assertEquals(1234, message.getAone());
853 assertEquals(0, message.getAtwo());
854 assertEquals(
855 proto.jspb.test.TestMessageWithOneof.DefaultOneofACase
856 .DEFAULT_ONEOF_A_NOT_SET,
857 message.getDefaultOneofACase());
858
859 message.setAone(567);
860 assertEquals(567, message.getAone());
861 assertEquals(0, message.getAtwo());
862 assertEquals(
863 proto.jspb.test.TestMessageWithOneof.DefaultOneofACase.AONE,
864 message.getDefaultOneofACase());
865
866 message.setAtwo(890);
867 assertEquals(1234, message.getAone());
868 assertEquals(890, message.getAtwo());
869 assertEquals(
870 proto.jspb.test.TestMessageWithOneof.DefaultOneofACase.ATWO,
871 message.getDefaultOneofACase());
872
873 message.clearAtwo();
874 assertEquals(1234, message.getAone());
875 assertEquals(0, message.getAtwo());
876 assertEquals(
877 proto.jspb.test.TestMessageWithOneof.DefaultOneofACase
878 .DEFAULT_ONEOF_A_NOT_SET,
879 message.getDefaultOneofACase());
880 });
881
882 it('testMessageWithDefaultOneofValues_defaultNotOnFirstField', function() {
883 var message = new proto.jspb.test.TestMessageWithOneof;
884 assertEquals(0, message.getBone());
885 assertEquals(1234, message.getBtwo());
886 assertFalse(message.hasBone());
887 assertFalse(message.hasBtwo());
888 assertEquals(
889 proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase
890 .DEFAULT_ONEOF_B_NOT_SET,
891 message.getDefaultOneofBCase());
892
893 message.setBone(2);
894 assertEquals(2, message.getBone());
895 assertEquals(1234, message.getBtwo());
896 assertTrue(message.hasBone());
897 assertFalse(message.hasBtwo());
898 assertEquals(
899 proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase.BONE,
900 message.getDefaultOneofBCase());
901
902 message.setBtwo(3);
903 assertEquals(0, message.getBone());
904 assertFalse(message.hasBone());
905 assertTrue(message.hasBtwo());
906 assertEquals(3, message.getBtwo());
907 assertEquals(
908 proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase.BTWO,
909 message.getDefaultOneofBCase());
910
911 message.clearBtwo();
912 assertEquals(0, message.getBone());
913 assertFalse(message.hasBone());
914 assertFalse(message.hasBtwo());
915 assertEquals(1234, message.getBtwo());
916 assertEquals(
917 proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase
918 .DEFAULT_ONEOF_B_NOT_SET,
919 message.getDefaultOneofBCase());
920 });
921
922 it('testInitializeMessageWithOneofDefaults', function() {
923 var message =
924 new proto.jspb.test.TestMessageWithOneof(new Array(9).concat(567));
925 assertEquals(567, message.getAone());
926 assertEquals(0, message.getAtwo());
927 assertEquals(
928 proto.jspb.test.TestMessageWithOneof.DefaultOneofACase.AONE,
929 message.getDefaultOneofACase());
930
931 message =
932 new proto.jspb.test.TestMessageWithOneof(new Array(10).concat(890));
933 assertEquals(1234, message.getAone());
934 assertEquals(890, message.getAtwo());
935 assertEquals(
936 proto.jspb.test.TestMessageWithOneof.DefaultOneofACase.ATWO,
937 message.getDefaultOneofACase());
938
939 message =
940 new proto.jspb.test.TestMessageWithOneof(new Array(9).concat(567, 890));
941 assertEquals(1234, message.getAone());
942 assertEquals(890, message.getAtwo());
943 assertEquals(
944 proto.jspb.test.TestMessageWithOneof.DefaultOneofACase.ATWO,
945 message.getDefaultOneofACase());
946 });
947
948 it('testInitializeMessageWithOneofDefaults_defaultNotSetOnFirstField',
949 function() {
950 var message;
951
952 message =
953 new proto.jspb.test.TestMessageWithOneof(new Array(11).concat(567));
954 assertEquals(567, message.getBone());
955 assertEquals(1234, message.getBtwo());
956 assertEquals(
957 proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase.BONE,
958 message.getDefaultOneofBCase());
959
960 message =
961 new proto.jspb.test.TestMessageWithOneof(new Array(12).concat(890));
962 assertEquals(0, message.getBone());
963 assertEquals(890, message.getBtwo());
964 assertEquals(
965 proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase.BTWO,
966 message.getDefaultOneofBCase());
967
968 message = new proto.jspb.test.TestMessageWithOneof(
969 new Array(11).concat(567, 890));
970 assertEquals(0, message.getBone());
971 assertEquals(890, message.getBtwo());
972 assertEquals(
973 proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase.BTWO,
974 message.getDefaultOneofBCase());
975 });
976
977 it('testOneofContainingAnotherMessage', function() {
978 var message = new proto.jspb.test.TestMessageWithOneof;
979 assertEquals(
980 proto.jspb.test.TestMessageWithOneof.RecursiveOneofCase.
981 RECURSIVE_ONEOF_NOT_SET,
982 message.getRecursiveOneofCase());
983
984 var other = new proto.jspb.test.TestMessageWithOneof;
985 message.setRone(other);
986 assertEquals(other, message.getRone());
987 assertEquals('', message.getRtwo());
988 assertEquals(
989 proto.jspb.test.TestMessageWithOneof.RecursiveOneofCase.RONE,
990 message.getRecursiveOneofCase());
991
992 message.setRtwo('hi');
993 assertUndefined(message.getRone());
994 assertEquals('hi', message.getRtwo());
995 assertEquals(
996 proto.jspb.test.TestMessageWithOneof.RecursiveOneofCase.RTWO,
997 message.getRecursiveOneofCase());
998 });
999
1000 it('testQueryingOneofCaseEnsuresOnlyOneFieldIsSetInUnderlyingArray',
1001 function() {
1002 var message = new proto.jspb.test.TestMessageWithOneof;
1003 message.setPone('x');
1004 assertEquals('x', message.getPone());
1005 assertEquals('', message.getPthree());
1006 assertEquals(
1007 proto.jspb.test.TestMessageWithOneof.PartialOneofCase.PONE,
1008 message.getPartialOneofCase());
1009
1010 var array = message.toArray();
1011 assertEquals('x', array[2]);
1012 assertUndefined(array[4]);
1013 array[4] = 'y';
1014
1015 assertEquals(
1016 proto.jspb.test.TestMessageWithOneof.PartialOneofCase.PTHREE,
1017 message.getPartialOneofCase());
1018 assertUndefined(array[2]);
1019 assertEquals('y', array[4]);
1020 });
1021
1022 it('testFloatingPointFieldsSupportNan', function() {
1023 var assertNan = function(x) {
1024 assertTrue('Expected ' + x + ' (' + goog.typeOf(x) + ') to be NaN.',
1025 goog.isNumber(x) && isNaN(x));
1026 };
1027
1028 var message = new proto.jspb.test.FloatingPointFields([
1029 'NaN', 'NaN', ['NaN', 'NaN'], 'NaN',
1030 'NaN', 'NaN', ['NaN', 'NaN'], 'NaN'
1031 ]);
1032 assertNan(message.getOptionalFloatField());
1033 assertNan(message.getRequiredFloatField());
1034 assertNan(message.getRepeatedFloatFieldList()[0]);
1035 assertNan(message.getRepeatedFloatFieldList()[1]);
1036 assertNan(message.getDefaultFloatField());
1037 assertNan(message.getOptionalDoubleField());
1038 assertNan(message.getRequiredDoubleField());
1039 assertNan(message.getRepeatedDoubleFieldList()[0]);
1040 assertNan(message.getRepeatedDoubleFieldList()[1]);
1041 assertNan(message.getDefaultDoubleField());
1042 });
1043
1044});