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