blob: 082da944b3b1ff4c61932dbd91fa11dbb308e14e [file] [log] [blame]
Feng Xiaoe841bac2015-12-11 17:09:20 -08001// 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.testing.asserts');
Adam Cozzetted64a2d92016-06-29 15:23:27 -070037goog.require('goog.userAgent');
Josh Habermane9f31ee2016-02-04 10:29:27 -080038
Josh Haberman77af5d02016-02-04 16:11:07 -080039// CommonJS-LoadFromFile: google-protobuf jspb
Feng Xiaoe841bac2015-12-11 17:09:20 -080040goog.require('jspb.Message');
Josh Habermane9f31ee2016-02-04 10:29:27 -080041
Josh Haberman77af5d02016-02-04 16:11:07 -080042// CommonJS-LoadFromFile: test5_pb proto.jspb.exttest.beta
Feng Xiaoe841bac2015-12-11 17:09:20 -080043goog.require('proto.jspb.exttest.beta.floatingStrField');
Josh Habermane9f31ee2016-02-04 10:29:27 -080044
Josh Haberman77af5d02016-02-04 16:11:07 -080045// CommonJS-LoadFromFile: test3_pb proto.jspb.exttest
Feng Xiaoe841bac2015-12-11 17:09:20 -080046goog.require('proto.jspb.exttest.floatingMsgField');
Josh Habermane9f31ee2016-02-04 10:29:27 -080047
Josh Haberman77af5d02016-02-04 16:11:07 -080048// CommonJS-LoadFromFile: test4_pb proto.jspb.exttest
Feng Xiaoe841bac2015-12-11 17:09:20 -080049goog.require('proto.jspb.exttest.floatingMsgFieldTwo');
Josh Habermane9f31ee2016-02-04 10:29:27 -080050
Josh Haberman77af5d02016-02-04 16:11:07 -080051// CommonJS-LoadFromFile: test_pb proto.jspb.test
Feng Xiaoe841bac2015-12-11 17:09:20 -080052goog.require('proto.jspb.test.CloneExtension');
53goog.require('proto.jspb.test.Complex');
54goog.require('proto.jspb.test.DefaultValues');
55goog.require('proto.jspb.test.Empty');
56goog.require('proto.jspb.test.EnumContainer');
Jisi Liu3b3c8ab2016-03-30 11:39:59 -070057goog.require('proto.jspb.test.floatingMsgField');
58goog.require('proto.jspb.test.FloatingPointFields');
Feng Xiaoe841bac2015-12-11 17:09:20 -080059goog.require('proto.jspb.test.floatingStrField');
60goog.require('proto.jspb.test.HasExtensions');
61goog.require('proto.jspb.test.IndirectExtension');
62goog.require('proto.jspb.test.IsExtension');
63goog.require('proto.jspb.test.OptionalFields');
64goog.require('proto.jspb.test.OuterEnum');
Josh Haberman77af5d02016-02-04 16:11:07 -080065goog.require('proto.jspb.test.OuterMessage.Complex');
Feng Xiaoe841bac2015-12-11 17:09:20 -080066goog.require('proto.jspb.test.Simple1');
67goog.require('proto.jspb.test.Simple2');
68goog.require('proto.jspb.test.SpecialCases');
69goog.require('proto.jspb.test.TestClone');
Adam Cozzetted64a2d92016-06-29 15:23:27 -070070goog.require('proto.jspb.test.TestEndsWithBytes');
Feng Xiaoe841bac2015-12-11 17:09:20 -080071goog.require('proto.jspb.test.TestGroup');
72goog.require('proto.jspb.test.TestGroup1');
73goog.require('proto.jspb.test.TestMessageWithOneof');
74goog.require('proto.jspb.test.TestReservedNames');
75goog.require('proto.jspb.test.TestReservedNamesExtension');
Adam Cozzette5a76e632016-11-17 16:48:38 -080076goog.require('proto.jspb.test.Deeply.Nested.Message');
Feng Xiaoe841bac2015-12-11 17:09:20 -080077
Josh Haberman77af5d02016-02-04 16:11:07 -080078// CommonJS-LoadFromFile: test2_pb proto.jspb.test
Josh Habermane9f31ee2016-02-04 10:29:27 -080079goog.require('proto.jspb.test.ExtensionMessage');
80goog.require('proto.jspb.test.TestExtensionsMessage');
Adam Cozzette5a76e632016-11-17 16:48:38 -080081goog.require('proto.jspb.test.ForeignNestedFieldMessage');
Jisi Liu3b3c8ab2016-03-30 11:39:59 -070082
Feng Xiaoe841bac2015-12-11 17:09:20 -080083
84
85
86describe('Message test suite', function() {
87 it('testEmptyProto', function() {
88 var empty1 = new proto.jspb.test.Empty([]);
89 var empty2 = new proto.jspb.test.Empty([]);
90 assertObjectEquals({}, empty1.toObject());
91 assertObjectEquals('Message should not be corrupted:', empty2, empty1);
92 });
93
94 it('testTopLevelEnum', function() {
95 var response = new proto.jspb.test.EnumContainer([]);
96 response.setOuterEnum(proto.jspb.test.OuterEnum.FOO);
97 assertEquals(proto.jspb.test.OuterEnum.FOO, response.getOuterEnum());
98 });
99
100 it('testByteStrings', function() {
101 var data = new proto.jspb.test.DefaultValues([]);
102 data.setBytesField('some_bytes');
103 assertEquals('some_bytes', data.getBytesField());
104 });
105
106 it('testComplexConversion', function() {
107 var data1 = ['a',,, [, 11], [[, 22], [, 33]],, ['s1', 's2'],, 1];
108 var data2 = ['a',,, [, 11], [[, 22], [, 33]],, ['s1', 's2'],, 1];
109 var foo = new proto.jspb.test.Complex(data1);
110 var bar = new proto.jspb.test.Complex(data2);
111 var result = foo.toObject();
112 assertObjectEquals({
113 aString: 'a',
114 anOutOfOrderBool: 1,
115 aNestedMessage: {
116 anInt: 11
117 },
118 aRepeatedMessageList: [{anInt: 22}, {anInt: 33}],
119 aRepeatedStringList: ['s1', 's2']
120 }, result);
121
122 // Now test with the jspb instances included.
123 result = foo.toObject(true /* opt_includeInstance */);
124 assertObjectEquals({
125 aString: 'a',
126 anOutOfOrderBool: 1,
127 aNestedMessage: {
128 anInt: 11,
129 $jspbMessageInstance: foo.getANestedMessage()
130 },
131 aRepeatedMessageList: [
132 {anInt: 22, $jspbMessageInstance: foo.getARepeatedMessageList()[0]},
133 {anInt: 33, $jspbMessageInstance: foo.getARepeatedMessageList()[1]}
134 ],
135 aRepeatedStringList: ['s1', 's2'],
136 $jspbMessageInstance: foo
137 }, result);
138
139 });
140
141 it('testMissingFields', function() {
142 var foo = new proto.jspb.test.Complex([
143 undefined, undefined, undefined, [],
144 undefined, undefined, undefined, undefined]);
145 var bar = new proto.jspb.test.Complex([
146 undefined, undefined, undefined, [],
147 undefined, undefined, undefined, undefined]);
148 var result = foo.toObject();
149 assertObjectEquals({
Nikolai Vavilove4b129f2016-07-19 10:34:58 +0300150 aString: undefined,
151 anOutOfOrderBool: undefined,
152 aNestedMessage: {
153 anInt: undefined
154 },
Feng Xiaoe841bac2015-12-11 17:09:20 -0800155 // Note: JsPb converts undefined repeated fields to empty arrays.
156 aRepeatedMessageList: [],
157 aRepeatedStringList: []
158 }, result);
159
160 });
161
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700162 it('testNestedComplexMessage', function() {
163 // Instantiate the message and set a unique field, just to ensure that we
164 // are not getting jspb.test.Complex instead.
165 var msg = new proto.jspb.test.OuterMessage.Complex();
166 msg.setInnerComplexField(5);
167 });
168
Feng Xiaoe841bac2015-12-11 17:09:20 -0800169 it('testSpecialCases', function() {
170 // Note: Some property names are reserved in JavaScript.
171 // These names are converted to the Js property named pb_<reserved_name>.
172 var special =
173 new proto.jspb.test.SpecialCases(['normal', 'default', 'function',
174 'var']);
175 var result = special.toObject();
176 assertObjectEquals({
177 normal: 'normal',
178 pb_default: 'default',
179 pb_function: 'function',
180 pb_var: 'var'
181 }, result);
182 });
183
184 it('testDefaultValues', function() {
185 var defaultString = "default<>\'\"abc";
186 var response = new proto.jspb.test.DefaultValues();
187
188 // Test toObject
Nikolai Vavilove4b129f2016-07-19 10:34:58 +0300189 var expectedObject = {
190 stringField: defaultString,
191 boolField: true,
192 intField: 11,
193 enumField: 13,
194 emptyField: '',
195 bytesField: 'bW9v'
196 };
Feng Xiaoe841bac2015-12-11 17:09:20 -0800197 assertObjectEquals(expectedObject, response.toObject());
198
199
200 // Test getters
201 response = new proto.jspb.test.DefaultValues();
202 assertEquals(defaultString, response.getStringField());
203 assertEquals(true, response.getBoolField());
204 assertEquals(11, response.getIntField());
205 assertEquals(13, response.getEnumField());
206 assertEquals('', response.getEmptyField());
207 assertEquals('bW9v', response.getBytesField());
208
209 function makeDefault(values) {
210 return new proto.jspb.test.DefaultValues(values);
211 }
212
213 // Test with undefined values,
214 // Use push to workaround IE treating undefined array elements as holes.
215 response = makeDefault([undefined, undefined, undefined, undefined]);
216 assertEquals(defaultString, response.getStringField());
217 assertEquals(true, response.getBoolField());
218 assertEquals(11, response.getIntField());
219 assertEquals(13, response.getEnumField());
Feng Xiao9086d962016-07-13 13:47:51 -0700220 assertFalse(response.hasStringField());
221 assertFalse(response.hasBoolField());
222 assertFalse(response.hasIntField());
223 assertFalse(response.hasEnumField());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800224
225 // Test with null values, as would be returned by a JSON serializer.
226 response = makeDefault([null, null, null, null]);
227 assertEquals(defaultString, response.getStringField());
228 assertEquals(true, response.getBoolField());
229 assertEquals(11, response.getIntField());
230 assertEquals(13, response.getEnumField());
Feng Xiao9086d962016-07-13 13:47:51 -0700231 assertFalse(response.hasStringField());
232 assertFalse(response.hasBoolField());
233 assertFalse(response.hasIntField());
234 assertFalse(response.hasEnumField());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800235
236 // Test with false-like values.
237 response = makeDefault(['', false, 0, 0]);
238 assertEquals('', response.getStringField());
239 assertEquals(false, response.getBoolField());
240 assertEquals(true, response.getIntField() == 0);
241 assertEquals(true, response.getEnumField() == 0);
Feng Xiao9086d962016-07-13 13:47:51 -0700242 assertTrue(response.hasStringField());
243 assertTrue(response.hasBoolField());
244 assertTrue(response.hasIntField());
245 assertTrue(response.hasEnumField());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800246
247 // Test that clearing the values reverts them to the default state.
248 response = makeDefault(['blah', false, 111, 77]);
249 response.clearStringField(); response.clearBoolField();
250 response.clearIntField(); response.clearEnumField();
251 assertEquals(defaultString, response.getStringField());
252 assertEquals(true, response.getBoolField());
253 assertEquals(11, response.getIntField());
254 assertEquals(13, response.getEnumField());
Feng Xiao9086d962016-07-13 13:47:51 -0700255 assertFalse(response.hasStringField());
256 assertFalse(response.hasBoolField());
257 assertFalse(response.hasIntField());
258 assertFalse(response.hasEnumField());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800259
260 // Test that setFoo(null) clears the values.
261 response = makeDefault(['blah', false, 111, 77]);
262 response.setStringField(null); response.setBoolField(null);
263 response.setIntField(undefined); response.setEnumField(undefined);
264 assertEquals(defaultString, response.getStringField());
265 assertEquals(true, response.getBoolField());
266 assertEquals(11, response.getIntField());
267 assertEquals(13, response.getEnumField());
Feng Xiao9086d962016-07-13 13:47:51 -0700268 assertFalse(response.hasStringField());
269 assertFalse(response.hasBoolField());
270 assertFalse(response.hasIntField());
271 assertFalse(response.hasEnumField());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800272 });
273
Bo Yangcc8ca5b2016-09-19 13:45:07 -0700274 it('testMessageRegistration', /** @suppress {visibility} */ function() {
Feng Xiaoe841bac2015-12-11 17:09:20 -0800275 // goog.require(SomeResponse) will include its library, which will in
276 // turn add SomeResponse to the message registry.
277 assertEquals(jspb.Message.registry_['res'], proto.jspb.test.SomeResponse);
278 });
279
280 it('testClearFields', function() {
Feng Xiaoe841bac2015-12-11 17:09:20 -0800281 var data = ['str', true, [11], [[22], [33]], ['s1', 's2']];
282 var foo = new proto.jspb.test.OptionalFields(data);
283 foo.clearAString();
284 foo.clearABool();
285 foo.clearANestedMessage();
286 foo.clearARepeatedMessageList();
287 foo.clearARepeatedStringList();
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300288 assertEquals('', foo.getAString());
289 assertEquals(false, foo.getABool());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800290 assertUndefined(foo.getANestedMessage());
Feng Xiao9086d962016-07-13 13:47:51 -0700291 assertFalse(foo.hasAString());
292 assertFalse(foo.hasABool());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800293 assertObjectEquals([], foo.getARepeatedMessageList());
294 assertObjectEquals([], foo.getARepeatedStringList());
295 // NOTE: We want the missing fields in 'expected' to be undefined,
296 // but we actually get a sparse array instead. We could use something
297 // like [1,undefined,2] to avoid this, except that this is still
298 // sparse on IE. No comment...
299 var expected = [,,, [], []];
300 expected[0] = expected[1] = expected[2] = undefined;
301 assertObjectEquals(expected, foo.toArray());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800302 });
303
Bo Yangcc8ca5b2016-09-19 13:45:07 -0700304 it('testDifferenceRawObject', /** @suppress {visibility} */ function() {
Feng Xiaoe841bac2015-12-11 17:09:20 -0800305 var p1 = new proto.jspb.test.HasExtensions(['hi', 'diff', {}]);
306 var p2 = new proto.jspb.test.HasExtensions(['hi', 'what',
307 {1000: 'unique'}]);
308 var diff = /** @type {proto.jspb.test.HasExtensions} */
309 (jspb.Message.difference(p1, p2));
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300310 assertEquals('', diff.getStr1());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800311 assertEquals('what', diff.getStr2());
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300312 assertEquals('', diff.getStr3());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800313 assertEquals('unique', diff.extensionObject_[1000]);
314 });
315
316 it('testEqualsSimple', function() {
317 var s1 = new proto.jspb.test.Simple1(['hi']);
318 assertTrue(jspb.Message.equals(s1, new proto.jspb.test.Simple1(['hi'])));
319 assertFalse(jspb.Message.equals(s1, new proto.jspb.test.Simple1(['bye'])));
320 var s1b = new proto.jspb.test.Simple1(['hi', ['hello']]);
321 assertTrue(jspb.Message.equals(s1b,
322 new proto.jspb.test.Simple1(['hi', ['hello']])));
323 assertTrue(jspb.Message.equals(s1b,
324 new proto.jspb.test.Simple1(['hi', ['hello', undefined,
325 undefined, undefined]])));
326 assertFalse(jspb.Message.equals(s1b,
327 new proto.jspb.test.Simple1(['no', ['hello']])));
328 // Test with messages of different types
329 var s2 = new proto.jspb.test.Simple2(['hi']);
330 assertFalse(jspb.Message.equals(s1, s2));
331 });
332
333 it('testEquals_softComparison', function() {
334 var s1 = new proto.jspb.test.Simple1(['hi', [], null]);
335 assertTrue(jspb.Message.equals(s1,
336 new proto.jspb.test.Simple1(['hi', []])));
337
338 var s1b = new proto.jspb.test.Simple1(['hi', [], true]);
339 assertTrue(jspb.Message.equals(s1b,
340 new proto.jspb.test.Simple1(['hi', [], 1])));
341 });
342
343 it('testEqualsComplex', function() {
344 var data1 = ['a',,, [, 11], [[, 22], [, 33]],, ['s1', 's2'],, 1];
345 var data2 = ['a',,, [, 11], [[, 22], [, 34]],, ['s1', 's2'],, 1];
346 var data3 = ['a',,, [, 11], [[, 22]],, ['s1', 's2'],, 1];
347 var data4 = ['hi'];
348 var c1a = new proto.jspb.test.Complex(data1);
349 var c1b = new proto.jspb.test.Complex(data1);
350 var c2 = new proto.jspb.test.Complex(data2);
351 var c3 = new proto.jspb.test.Complex(data3);
352 var s1 = new proto.jspb.test.Simple1(data4);
353
354 assertTrue(jspb.Message.equals(c1a, c1b));
355 assertFalse(jspb.Message.equals(c1a, c2));
356 assertFalse(jspb.Message.equals(c2, c3));
357 assertFalse(jspb.Message.equals(c1a, s1));
358 });
359
360 it('testEqualsExtensionsConstructed', function() {
361 assertTrue(jspb.Message.equals(
362 new proto.jspb.test.HasExtensions([]),
363 new proto.jspb.test.HasExtensions([{}])
364 ));
365 assertTrue(jspb.Message.equals(
366 new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'a'}]}]),
367 new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'a'}]}])
368 ));
369 assertFalse(jspb.Message.equals(
370 new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'a'}]}]),
371 new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'b'}]}])
372 ));
373 assertTrue(jspb.Message.equals(
374 new proto.jspb.test.HasExtensions([{100: [{200: 'a'}]}]),
375 new proto.jspb.test.HasExtensions([{100: [{200: 'a'}]}])
376 ));
377 assertTrue(jspb.Message.equals(
378 new proto.jspb.test.HasExtensions([{100: [{200: 'a'}]}]),
379 new proto.jspb.test.HasExtensions([,,, {100: [{200: 'a'}]}])
380 ));
381 assertTrue(jspb.Message.equals(
382 new proto.jspb.test.HasExtensions([,,, {100: [{200: 'a'}]}]),
383 new proto.jspb.test.HasExtensions([{100: [{200: 'a'}]}])
384 ));
385 assertTrue(jspb.Message.equals(
386 new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'a'}]}]),
387 new proto.jspb.test.HasExtensions(['hi',,, {100: [{200: 'a'}]}])
388 ));
389 assertTrue(jspb.Message.equals(
390 new proto.jspb.test.HasExtensions(['hi',,, {100: [{200: 'a'}]}]),
391 new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'a'}]}])
392 ));
393 });
394
395 it('testEqualsExtensionsUnconstructed', function() {
396 assertTrue(jspb.Message.compareFields([], [{}]));
397 assertTrue(jspb.Message.compareFields([,,, {}], []));
398 assertTrue(jspb.Message.compareFields([,,, {}], [,, {}]));
399 assertTrue(jspb.Message.compareFields(
400 ['hi', {100: [{200: 'a'}]}], ['hi', {100: [{200: 'a'}]}]));
401 assertFalse(jspb.Message.compareFields(
402 ['hi', {100: [{200: 'a'}]}], ['hi', {100: [{200: 'b'}]}]));
403 assertTrue(jspb.Message.compareFields(
404 [{100: [{200: 'a'}]}], [{100: [{200: 'a'}]}]));
405 assertTrue(jspb.Message.compareFields(
406 [{100: [{200: 'a'}]}], [,,, {100: [{200: 'a'}]}]));
407 assertTrue(jspb.Message.compareFields(
408 [,,, {100: [{200: 'a'}]}], [{100: [{200: 'a'}]}]));
409 assertTrue(jspb.Message.compareFields(
410 ['hi', {100: [{200: 'a'}]}], ['hi',,, {100: [{200: 'a'}]}]));
411 assertTrue(jspb.Message.compareFields(
412 ['hi',,, {100: [{200: 'a'}]}], ['hi', {100: [{200: 'a'}]}]));
413 });
414
415 it('testToMap', function() {
416 var p1 = new proto.jspb.test.Simple1(['k', ['v']]);
417 var p2 = new proto.jspb.test.Simple1(['k1', ['v1', 'v2']]);
418 var soymap = jspb.Message.toMap([p1, p2],
419 proto.jspb.test.Simple1.prototype.getAString,
420 proto.jspb.test.Simple1.prototype.toObject);
421 assertEquals('k', soymap['k'].aString);
422 assertArrayEquals(['v'], soymap['k'].aRepeatedStringList);
423 var protomap = jspb.Message.toMap([p1, p2],
424 proto.jspb.test.Simple1.prototype.getAString);
425 assertEquals('k', protomap['k'].getAString());
426 assertArrayEquals(['v'], protomap['k'].getARepeatedStringList());
427 });
428
429 it('testClone', function() {
Adam Cozzetted64a2d92016-06-29 15:23:27 -0700430 var supportsUint8Array =
431 !goog.userAgent.IE || goog.userAgent.isVersionOrHigher('10');
Feng Xiaoe841bac2015-12-11 17:09:20 -0800432 var original = new proto.jspb.test.TestClone();
433 original.setStr('v1');
434 var simple1 = new proto.jspb.test.Simple1(['x1', ['y1', 'z1']]);
435 var simple2 = new proto.jspb.test.Simple1(['x2', ['y2', 'z2']]);
436 var simple3 = new proto.jspb.test.Simple1(['x3', ['y3', 'z3']]);
437 original.setSimple1(simple1);
438 original.setSimple2List([simple2, simple3]);
Adam Cozzetted64a2d92016-06-29 15:23:27 -0700439 var bytes1 = supportsUint8Array ? new Uint8Array([1, 2, 3]) : '123';
440 original.setBytesField(bytes1);
Feng Xiaoe841bac2015-12-11 17:09:20 -0800441 var extension = new proto.jspb.test.CloneExtension();
442 extension.setExt('e1');
443 original.setExtension(proto.jspb.test.IsExtension.extField, extension);
Bo Yangcc8ca5b2016-09-19 13:45:07 -0700444 var clone = original.clone();
Feng Xiaoe841bac2015-12-11 17:09:20 -0800445 assertArrayEquals(['v1',, ['x1', ['y1', 'z1']],,
Adam Cozzetted64a2d92016-06-29 15:23:27 -0700446 [['x2', ['y2', 'z2']], ['x3', ['y3', 'z3']]], bytes1,, { 100: [, 'e1'] }],
Feng Xiaoe841bac2015-12-11 17:09:20 -0800447 clone.toArray());
448 clone.setStr('v2');
449 var simple4 = new proto.jspb.test.Simple1(['a1', ['b1', 'c1']]);
450 var simple5 = new proto.jspb.test.Simple1(['a2', ['b2', 'c2']]);
451 var simple6 = new proto.jspb.test.Simple1(['a3', ['b3', 'c3']]);
452 clone.setSimple1(simple4);
453 clone.setSimple2List([simple5, simple6]);
Adam Cozzetted64a2d92016-06-29 15:23:27 -0700454 if (supportsUint8Array) {
455 clone.getBytesField()[0] = 4;
456 assertObjectEquals(bytes1, original.getBytesField());
457 }
458 var bytes2 = supportsUint8Array ? new Uint8Array([4, 5, 6]) : '456';
459 clone.setBytesField(bytes2);
Feng Xiaoe841bac2015-12-11 17:09:20 -0800460 var newExtension = new proto.jspb.test.CloneExtension();
461 newExtension.setExt('e2');
462 clone.setExtension(proto.jspb.test.CloneExtension.extField, newExtension);
463 assertArrayEquals(['v2',, ['a1', ['b1', 'c1']],,
Adam Cozzetted64a2d92016-06-29 15:23:27 -0700464 [['a2', ['b2', 'c2']], ['a3', ['b3', 'c3']]], bytes2,, { 100: [, 'e2'] }],
Feng Xiaoe841bac2015-12-11 17:09:20 -0800465 clone.toArray());
466 assertArrayEquals(['v1',, ['x1', ['y1', 'z1']],,
Adam Cozzetted64a2d92016-06-29 15:23:27 -0700467 [['x2', ['y2', 'z2']], ['x3', ['y3', 'z3']]], bytes1,, { 100: [, 'e1'] }],
Feng Xiaoe841bac2015-12-11 17:09:20 -0800468 original.toArray());
469 });
470
471 it('testCopyInto', function() {
Adam Cozzetted64a2d92016-06-29 15:23:27 -0700472 var supportsUint8Array =
473 !goog.userAgent.IE || goog.userAgent.isVersionOrHigher('10');
Feng Xiaoe841bac2015-12-11 17:09:20 -0800474 var original = new proto.jspb.test.TestClone();
475 original.setStr('v1');
476 var dest = new proto.jspb.test.TestClone();
477 dest.setStr('override');
478 var simple1 = new proto.jspb.test.Simple1(['x1', ['y1', 'z1']]);
479 var simple2 = new proto.jspb.test.Simple1(['x2', ['y2', 'z2']]);
480 var simple3 = new proto.jspb.test.Simple1(['x3', ['y3', 'z3']]);
481 var destSimple1 = new proto.jspb.test.Simple1(['ox1', ['oy1', 'oz1']]);
482 var destSimple2 = new proto.jspb.test.Simple1(['ox2', ['oy2', 'oz2']]);
483 var destSimple3 = new proto.jspb.test.Simple1(['ox3', ['oy3', 'oz3']]);
484 original.setSimple1(simple1);
485 original.setSimple2List([simple2, simple3]);
486 dest.setSimple1(destSimple1);
487 dest.setSimple2List([destSimple2, destSimple3]);
Adam Cozzetted64a2d92016-06-29 15:23:27 -0700488 var bytes1 = supportsUint8Array ? new Uint8Array([1, 2, 3]) : '123';
489 var bytes2 = supportsUint8Array ? new Uint8Array([4, 5, 6]) : '456';
490 original.setBytesField(bytes1);
491 dest.setBytesField(bytes2);
Feng Xiaoe841bac2015-12-11 17:09:20 -0800492 var extension = new proto.jspb.test.CloneExtension();
493 extension.setExt('e1');
494 original.setExtension(proto.jspb.test.CloneExtension.extField, extension);
495
496 jspb.Message.copyInto(original, dest);
497 assertArrayEquals(original.toArray(), dest.toArray());
498 assertEquals('x1', dest.getSimple1().getAString());
499 assertEquals('e1',
500 dest.getExtension(proto.jspb.test.CloneExtension.extField).getExt());
501 dest.getSimple1().setAString('new value');
502 assertNotEquals(dest.getSimple1().getAString(),
503 original.getSimple1().getAString());
Adam Cozzetted64a2d92016-06-29 15:23:27 -0700504 if (supportsUint8Array) {
505 dest.getBytesField()[0] = 7;
506 assertObjectEquals(bytes1, original.getBytesField());
507 assertObjectEquals(new Uint8Array([7, 2, 3]), dest.getBytesField());
508 } else {
509 dest.setBytesField('789');
510 assertObjectEquals(bytes1, original.getBytesField());
511 assertObjectEquals('789', dest.getBytesField());
512 }
Feng Xiaoe841bac2015-12-11 17:09:20 -0800513 dest.getExtension(proto.jspb.test.CloneExtension.extField).
514 setExt('new value');
515 assertNotEquals(
516 dest.getExtension(proto.jspb.test.CloneExtension.extField).getExt(),
517 original.getExtension(
518 proto.jspb.test.CloneExtension.extField).getExt());
519 });
520
521 it('testCopyInto_notSameType', function() {
522 var a = new proto.jspb.test.TestClone();
523 var b = new proto.jspb.test.Simple1(['str', ['s1', 's2']]);
524
525 var e = assertThrows(function() {
526 jspb.Message.copyInto(a, b);
527 });
528 assertContains('should have the same type', e.message);
529 });
530
531 it('testExtensions', function() {
532 var extension1 = new proto.jspb.test.IsExtension(['ext1field']);
533 var extension2 = new proto.jspb.test.Simple1(['str', ['s1', 's2']]);
534 var extendable = new proto.jspb.test.HasExtensions(['v1', 'v2', 'v3']);
535 extendable.setExtension(proto.jspb.test.IsExtension.extField, extension1);
536 extendable.setExtension(proto.jspb.test.IndirectExtension.simple,
537 extension2);
538 extendable.setExtension(proto.jspb.test.IndirectExtension.str, 'xyzzy');
539 extendable.setExtension(proto.jspb.test.IndirectExtension.repeatedStrList,
540 ['a', 'b']);
541 var s1 = new proto.jspb.test.Simple1(['foo', ['s1', 's2']]);
542 var s2 = new proto.jspb.test.Simple1(['bar', ['t1', 't2']]);
543 extendable.setExtension(
544 proto.jspb.test.IndirectExtension.repeatedSimpleList,
545 [s1, s2]);
546 assertObjectEquals(extension1,
547 extendable.getExtension(proto.jspb.test.IsExtension.extField));
548 assertObjectEquals(extension2,
549 extendable.getExtension(proto.jspb.test.IndirectExtension.simple));
550 assertObjectEquals('xyzzy',
551 extendable.getExtension(proto.jspb.test.IndirectExtension.str));
552 assertObjectEquals(['a', 'b'], extendable.getExtension(
553 proto.jspb.test.IndirectExtension.repeatedStrList));
554 assertObjectEquals([s1, s2], extendable.getExtension(
555 proto.jspb.test.IndirectExtension.repeatedSimpleList));
556 // Not supported yet, but it should work...
557 extendable.setExtension(proto.jspb.test.IndirectExtension.simple, null);
558 assertNull(
559 extendable.getExtension(proto.jspb.test.IndirectExtension.simple));
560 extendable.setExtension(proto.jspb.test.IndirectExtension.str, null);
561 assertNull(extendable.getExtension(proto.jspb.test.IndirectExtension.str));
562
Josh Habermane9cf31e2015-12-21 15:18:17 -0800563
Feng Xiaoe841bac2015-12-11 17:09:20 -0800564 // Extension fields with jspb.ignore = true are ignored.
565 assertUndefined(proto.jspb.test.IndirectExtension['ignored']);
566 assertUndefined(proto.jspb.test.HasExtensions['ignoredFloating']);
567 });
568
569 it('testFloatingExtensions', function() {
570 // From an autogenerated container.
571 var extendable = new proto.jspb.test.HasExtensions(['v1', 'v2', 'v3']);
572 var extension = new proto.jspb.test.Simple1(['foo', ['s1', 's2']]);
573 extendable.setExtension(proto.jspb.test.simple1, extension);
574 assertObjectEquals(extension,
575 extendable.getExtension(proto.jspb.test.simple1));
576
577 // From _lib mode.
578 extension = new proto.jspb.test.ExtensionMessage(['s1']);
579 extendable = new proto.jspb.test.TestExtensionsMessage([16]);
580 extendable.setExtension(proto.jspb.test.floatingMsgField, extension);
581 extendable.setExtension(proto.jspb.test.floatingStrField, 's2');
582 assertObjectEquals(extension,
583 extendable.getExtension(proto.jspb.test.floatingMsgField));
584 assertObjectEquals('s2',
585 extendable.getExtension(proto.jspb.test.floatingStrField));
586 assertNotUndefined(proto.jspb.exttest.floatingMsgField);
587 assertNotUndefined(proto.jspb.exttest.floatingMsgFieldTwo);
588 assertNotUndefined(proto.jspb.exttest.beta.floatingStrField);
589 });
590
591 it('testToObject_extendedObject', function() {
592 var extension1 = new proto.jspb.test.IsExtension(['ext1field']);
593 var extension2 = new proto.jspb.test.Simple1(['str', ['s1', 's2'], true]);
594 var extendable = new proto.jspb.test.HasExtensions(['v1', 'v2', 'v3']);
595 extendable.setExtension(proto.jspb.test.IsExtension.extField, extension1);
596 extendable.setExtension(proto.jspb.test.IndirectExtension.simple,
597 extension2);
598 extendable.setExtension(proto.jspb.test.IndirectExtension.str, 'xyzzy');
599 extendable.setExtension(proto.jspb.test.IndirectExtension.repeatedStrList,
600 ['a', 'b']);
601 var s1 = new proto.jspb.test.Simple1(['foo', ['s1', 's2'], true]);
602 var s2 = new proto.jspb.test.Simple1(['bar', ['t1', 't2'], false]);
603 extendable.setExtension(
604 proto.jspb.test.IndirectExtension.repeatedSimpleList,
605 [s1, s2]);
606 assertObjectEquals({
607 str1: 'v1', str2: 'v2', str3: 'v3',
608 extField: { ext1: 'ext1field' },
609 simple: {
610 aString: 'str', aRepeatedStringList: ['s1', 's2'], aBoolean: true
611 },
612 str: 'xyzzy',
613 repeatedStrList: ['a', 'b'],
614 repeatedSimpleList: [
615 { aString: 'foo', aRepeatedStringList: ['s1', 's2'], aBoolean: true},
616 { aString: 'bar', aRepeatedStringList: ['t1', 't2'], aBoolean: false}
617 ]
618 }, extendable.toObject());
619
620 // Now, with instances included.
621 assertObjectEquals({
622 str1: 'v1', str2: 'v2', str3: 'v3',
623 extField: {
624 ext1: 'ext1field',
625 $jspbMessageInstance:
626 extendable.getExtension(proto.jspb.test.IsExtension.extField)
627 },
628 simple: {
629 aString: 'str',
630 aRepeatedStringList: ['s1', 's2'],
631 aBoolean: true,
632 $jspbMessageInstance:
633 extendable.getExtension(proto.jspb.test.IndirectExtension.simple)
634 },
635 str: 'xyzzy',
636 repeatedStrList: ['a', 'b'],
637 repeatedSimpleList: [{
638 aString: 'foo',
639 aRepeatedStringList: ['s1', 's2'],
640 aBoolean: true,
641 $jspbMessageInstance: s1
642 }, {
643 aString: 'bar',
644 aRepeatedStringList: ['t1', 't2'],
645 aBoolean: false,
646 $jspbMessageInstance: s2
647 }],
648 $jspbMessageInstance: extendable
649 }, extendable.toObject(true /* opt_includeInstance */));
650 });
651
652 it('testInitialization_emptyArray', function() {
653 var msg = new proto.jspb.test.HasExtensions([]);
654 if (jspb.Message.MINIMIZE_MEMORY_ALLOCATIONS) {
655 assertArrayEquals([], msg.toArray());
656 } else {
657 // Extension object is created past all regular fields.
658 assertArrayEquals([,,, {}], msg.toArray());
659 }
660 });
661
662 it('testInitialization_justExtensionObject', function() {
663 var msg = new proto.jspb.test.Empty([{1: 'hi'}]);
664 // The extensionObject is not moved from its original location.
665 assertArrayEquals([{1: 'hi'}], msg.toArray());
666 });
667
668 it('testInitialization_incompleteList', function() {
669 var msg = new proto.jspb.test.Empty([1, {4: 'hi'}]);
670 // The extensionObject is not moved from its original location.
671 assertArrayEquals([1, {4: 'hi'}], msg.toArray());
672 });
673
674 it('testInitialization_forwardCompatible', function() {
675 var msg = new proto.jspb.test.Empty([1, 2, 3, {1: 'hi'}]);
676 assertArrayEquals([1, 2, 3, {1: 'hi'}], msg.toArray());
677 });
678
Bo Yangcc8ca5b2016-09-19 13:45:07 -0700679 it('testExtendedMessageEnsureObject',
680 /** @suppress {visibility} */ function() {
681 var data =
682 new proto.jspb.test.HasExtensions(['str1', {'a_key': 'an_object'}]);
683 assertEquals('an_object', data.extensionObject_['a_key']);
684 });
Feng Xiaoe841bac2015-12-11 17:09:20 -0800685
686 it('testToObject_hasExtensionField', function() {
687 var data = new proto.jspb.test.HasExtensions(['str1', {100: ['ext1']}]);
688 var obj = data.toObject();
689 assertEquals('str1', obj.str1);
690 assertEquals('ext1', obj.extField.ext1);
691 });
692
693 it('testGetExtension', function() {
694 var data = new proto.jspb.test.HasExtensions(['str1', {100: ['ext1']}]);
695 assertEquals('str1', data.getStr1());
696 var extension = data.getExtension(proto.jspb.test.IsExtension.extField);
697 assertNotNull(extension);
698 assertEquals('ext1', extension.getExt1());
699 });
700
701 it('testSetExtension', function() {
702 var data = new proto.jspb.test.HasExtensions();
703 var extensionMessage = new proto.jspb.test.IsExtension(['is_extension']);
704 data.setExtension(proto.jspb.test.IsExtension.extField, extensionMessage);
705 var obj = data.toObject();
706 assertNotNull(
707 data.getExtension(proto.jspb.test.IsExtension.extField));
708 assertEquals('is_extension', obj.extField.ext1);
709 });
710
711 /**
712 * Note that group is long deprecated, we only support it because JsPb has
713 * a goal of being able to generate JS classes for all proto descriptors.
714 */
715 it('testGroups', function() {
716 var group = new proto.jspb.test.TestGroup();
717 var someGroup = new proto.jspb.test.TestGroup.RepeatedGroup();
718 someGroup.setId('g1');
719 someGroup.setSomeBoolList([true, false]);
720 group.setRepeatedGroupList([someGroup]);
721 var groups = group.getRepeatedGroupList();
722 assertEquals('g1', groups[0].getId());
723 assertObjectEquals([true, false], groups[0].getSomeBoolList());
724 assertObjectEquals({id: 'g1', someBoolList: [true, false]},
725 groups[0].toObject());
726 assertObjectEquals({
Nikolai Vavilove4b129f2016-07-19 10:34:58 +0300727 repeatedGroupList: [{id: 'g1', someBoolList: [true, false]}],
728 requiredGroup: {id: undefined},
729 optionalGroup: undefined,
730 requiredSimple: {aRepeatedStringList: [], aString: undefined},
731 optionalSimple: undefined,
732 id: undefined
Feng Xiaoe841bac2015-12-11 17:09:20 -0800733 }, group.toObject());
734 var group1 = new proto.jspb.test.TestGroup1();
735 group1.setGroup(someGroup);
736 assertEquals(someGroup, group1.getGroup());
737 });
738
739 it('testNonExtensionFieldsAfterExtensionRange', function() {
740 var data = [{'1': 'a_string'}];
741 var message = new proto.jspb.test.Complex(data);
742 assertArrayEquals([], message.getARepeatedStringList());
743 });
744
745 it('testReservedGetterNames', function() {
746 var message = new proto.jspb.test.TestReservedNames();
747 message.setExtension$(11);
748 message.setExtension(proto.jspb.test.TestReservedNamesExtension.foo, 12);
749 assertEquals(11, message.getExtension$());
750 assertEquals(12, message.getExtension(
751 proto.jspb.test.TestReservedNamesExtension.foo));
752 assertObjectEquals({extension: 11, foo: 12}, message.toObject());
753 });
754
755 it('testInitializeMessageWithUnsetOneof', function() {
756 var message = new proto.jspb.test.TestMessageWithOneof([]);
757 assertEquals(
758 proto.jspb.test.TestMessageWithOneof.PartialOneofCase.
759 PARTIAL_ONEOF_NOT_SET,
760 message.getPartialOneofCase());
761 assertEquals(
762 proto.jspb.test.TestMessageWithOneof.RecursiveOneofCase.
763 RECURSIVE_ONEOF_NOT_SET,
764 message.getRecursiveOneofCase());
765 });
766
767 it('testInitializeMessageWithSingleValueSetInOneof', function() {
768 var message = new proto.jspb.test.TestMessageWithOneof([,, 'x']);
769
770 assertEquals('x', message.getPone());
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300771 assertEquals('', message.getPthree());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800772 assertEquals(
773 proto.jspb.test.TestMessageWithOneof.PartialOneofCase.PONE,
774 message.getPartialOneofCase());
775 });
776
777 it('testKeepsLastWireValueSetInUnion_multipleValues', function() {
778 var message = new proto.jspb.test.TestMessageWithOneof([,, 'x',, 'y']);
779
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300780 assertEquals('', message.getPone());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800781 assertEquals('y', message.getPthree());
782 assertEquals(
783 proto.jspb.test.TestMessageWithOneof.PartialOneofCase.PTHREE,
784 message.getPartialOneofCase());
785 });
786
787 it('testSettingOneofFieldClearsOthers', function() {
788 var message = new proto.jspb.test.TestMessageWithOneof;
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300789 assertEquals('', message.getPone());
790 assertEquals('', message.getPthree());
Feng Xiao9086d962016-07-13 13:47:51 -0700791 assertFalse(message.hasPone());
792 assertFalse(message.hasPthree());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800793
794 message.setPone('hi');
795 assertEquals('hi', message.getPone());
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300796 assertEquals('', message.getPthree());
Feng Xiao9086d962016-07-13 13:47:51 -0700797 assertTrue(message.hasPone());
798 assertFalse(message.hasPthree());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800799
800 message.setPthree('bye');
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300801 assertEquals('', message.getPone());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800802 assertEquals('bye', message.getPthree());
Feng Xiao9086d962016-07-13 13:47:51 -0700803 assertFalse(message.hasPone());
804 assertTrue(message.hasPthree());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800805 });
806
807 it('testSettingOneofFieldDoesNotClearFieldsFromOtherUnions', function() {
808 var other = new proto.jspb.test.TestMessageWithOneof;
809 var message = new proto.jspb.test.TestMessageWithOneof;
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300810 assertEquals('', message.getPone());
811 assertEquals('', message.getPthree());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800812 assertUndefined(message.getRone());
Feng Xiao9086d962016-07-13 13:47:51 -0700813 assertFalse(message.hasPone());
814 assertFalse(message.hasPthree());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800815
816 message.setPone('hi');
817 message.setRone(other);
818 assertEquals('hi', message.getPone());
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300819 assertEquals('', message.getPthree());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800820 assertEquals(other, message.getRone());
Feng Xiao9086d962016-07-13 13:47:51 -0700821 assertTrue(message.hasPone());
822 assertFalse(message.hasPthree());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800823
824 message.setPthree('bye');
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300825 assertEquals('', message.getPone());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800826 assertEquals('bye', message.getPthree());
827 assertEquals(other, message.getRone());
Feng Xiao9086d962016-07-13 13:47:51 -0700828 assertFalse(message.hasPone());
829 assertTrue(message.hasPthree());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800830 });
831
832 it('testUnsetsOneofCaseWhenFieldIsCleared', function() {
833 var message = new proto.jspb.test.TestMessageWithOneof;
834 assertEquals(
835 proto.jspb.test.TestMessageWithOneof.PartialOneofCase.
836 PARTIAL_ONEOF_NOT_SET,
837 message.getPartialOneofCase());
838
839 message.setPone('hi');
840 assertEquals(
841 proto.jspb.test.TestMessageWithOneof.PartialOneofCase.PONE,
842 message.getPartialOneofCase());
843
844 message.clearPone();
845 assertEquals(
846 proto.jspb.test.TestMessageWithOneof.PartialOneofCase.
847 PARTIAL_ONEOF_NOT_SET,
848 message.getPartialOneofCase());
849 });
850
851 it('testMessageWithDefaultOneofValues', function() {
852 var message = new proto.jspb.test.TestMessageWithOneof;
853 assertEquals(1234, message.getAone());
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300854 assertEquals(0, message.getAtwo());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800855 assertEquals(
856 proto.jspb.test.TestMessageWithOneof.DefaultOneofACase
857 .DEFAULT_ONEOF_A_NOT_SET,
858 message.getDefaultOneofACase());
859
860 message.setAone(567);
861 assertEquals(567, message.getAone());
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300862 assertEquals(0, message.getAtwo());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800863 assertEquals(
864 proto.jspb.test.TestMessageWithOneof.DefaultOneofACase.AONE,
865 message.getDefaultOneofACase());
866
867 message.setAtwo(890);
868 assertEquals(1234, message.getAone());
869 assertEquals(890, message.getAtwo());
870 assertEquals(
871 proto.jspb.test.TestMessageWithOneof.DefaultOneofACase.ATWO,
872 message.getDefaultOneofACase());
873
874 message.clearAtwo();
875 assertEquals(1234, message.getAone());
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300876 assertEquals(0, message.getAtwo());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800877 assertEquals(
878 proto.jspb.test.TestMessageWithOneof.DefaultOneofACase
879 .DEFAULT_ONEOF_A_NOT_SET,
880 message.getDefaultOneofACase());
881 });
882
883 it('testMessageWithDefaultOneofValues_defaultNotOnFirstField', function() {
884 var message = new proto.jspb.test.TestMessageWithOneof;
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300885 assertEquals(0, message.getBone());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800886 assertEquals(1234, message.getBtwo());
Feng Xiao9086d962016-07-13 13:47:51 -0700887 assertFalse(message.hasBone());
888 assertFalse(message.hasBtwo());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800889 assertEquals(
890 proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase
891 .DEFAULT_ONEOF_B_NOT_SET,
892 message.getDefaultOneofBCase());
893
894 message.setBone(2);
895 assertEquals(2, message.getBone());
896 assertEquals(1234, message.getBtwo());
Feng Xiao9086d962016-07-13 13:47:51 -0700897 assertTrue(message.hasBone());
898 assertFalse(message.hasBtwo());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800899 assertEquals(
900 proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase.BONE,
901 message.getDefaultOneofBCase());
902
903 message.setBtwo(3);
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300904 assertEquals(0, message.getBone());
Feng Xiao9086d962016-07-13 13:47:51 -0700905 assertFalse(message.hasBone());
906 assertTrue(message.hasBtwo());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800907 assertEquals(3, message.getBtwo());
908 assertEquals(
909 proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase.BTWO,
910 message.getDefaultOneofBCase());
911
912 message.clearBtwo();
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300913 assertEquals(0, message.getBone());
Feng Xiao9086d962016-07-13 13:47:51 -0700914 assertFalse(message.hasBone());
915 assertFalse(message.hasBtwo());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800916 assertEquals(1234, message.getBtwo());
917 assertEquals(
918 proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase
919 .DEFAULT_ONEOF_B_NOT_SET,
920 message.getDefaultOneofBCase());
921 });
922
923 it('testInitializeMessageWithOneofDefaults', function() {
924 var message =
925 new proto.jspb.test.TestMessageWithOneof(new Array(9).concat(567));
926 assertEquals(567, message.getAone());
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300927 assertEquals(0, message.getAtwo());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800928 assertEquals(
929 proto.jspb.test.TestMessageWithOneof.DefaultOneofACase.AONE,
930 message.getDefaultOneofACase());
931
932 message =
933 new proto.jspb.test.TestMessageWithOneof(new Array(10).concat(890));
934 assertEquals(1234, message.getAone());
935 assertEquals(890, message.getAtwo());
936 assertEquals(
937 proto.jspb.test.TestMessageWithOneof.DefaultOneofACase.ATWO,
938 message.getDefaultOneofACase());
939
940 message =
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700941 new proto.jspb.test.TestMessageWithOneof(new Array(9).concat(567, 890));
Feng Xiaoe841bac2015-12-11 17:09:20 -0800942 assertEquals(1234, message.getAone());
943 assertEquals(890, message.getAtwo());
944 assertEquals(
945 proto.jspb.test.TestMessageWithOneof.DefaultOneofACase.ATWO,
946 message.getDefaultOneofACase());
947 });
948
949 it('testInitializeMessageWithOneofDefaults_defaultNotSetOnFirstField',
950 function() {
951 var message;
952
953 message =
954 new proto.jspb.test.TestMessageWithOneof(new Array(11).concat(567));
955 assertEquals(567, message.getBone());
956 assertEquals(1234, message.getBtwo());
957 assertEquals(
958 proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase.BONE,
959 message.getDefaultOneofBCase());
960
961 message =
962 new proto.jspb.test.TestMessageWithOneof(new Array(12).concat(890));
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300963 assertEquals(0, message.getBone());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800964 assertEquals(890, message.getBtwo());
965 assertEquals(
966 proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase.BTWO,
967 message.getDefaultOneofBCase());
968
969 message = new proto.jspb.test.TestMessageWithOneof(
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700970 new Array(11).concat(567, 890));
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300971 assertEquals(0, message.getBone());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800972 assertEquals(890, message.getBtwo());
973 assertEquals(
974 proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase.BTWO,
975 message.getDefaultOneofBCase());
976 });
977
978 it('testOneofContainingAnotherMessage', function() {
979 var message = new proto.jspb.test.TestMessageWithOneof;
980 assertEquals(
981 proto.jspb.test.TestMessageWithOneof.RecursiveOneofCase.
982 RECURSIVE_ONEOF_NOT_SET,
983 message.getRecursiveOneofCase());
984
985 var other = new proto.jspb.test.TestMessageWithOneof;
986 message.setRone(other);
987 assertEquals(other, message.getRone());
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300988 assertEquals('', message.getRtwo());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800989 assertEquals(
990 proto.jspb.test.TestMessageWithOneof.RecursiveOneofCase.RONE,
991 message.getRecursiveOneofCase());
992
993 message.setRtwo('hi');
994 assertUndefined(message.getRone());
995 assertEquals('hi', message.getRtwo());
996 assertEquals(
997 proto.jspb.test.TestMessageWithOneof.RecursiveOneofCase.RTWO,
998 message.getRecursiveOneofCase());
999 });
1000
1001 it('testQueryingOneofCaseEnsuresOnlyOneFieldIsSetInUnderlyingArray',
1002 function() {
1003 var message = new proto.jspb.test.TestMessageWithOneof;
1004 message.setPone('x');
1005 assertEquals('x', message.getPone());
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +03001006 assertEquals('', message.getPthree());
Feng Xiaoe841bac2015-12-11 17:09:20 -08001007 assertEquals(
1008 proto.jspb.test.TestMessageWithOneof.PartialOneofCase.PONE,
1009 message.getPartialOneofCase());
1010
1011 var array = message.toArray();
1012 assertEquals('x', array[2]);
1013 assertUndefined(array[4]);
1014 array[4] = 'y';
1015
1016 assertEquals(
1017 proto.jspb.test.TestMessageWithOneof.PartialOneofCase.PTHREE,
1018 message.getPartialOneofCase());
1019 assertUndefined(array[2]);
1020 assertEquals('y', array[4]);
1021 });
1022
Jisi Liu3b3c8ab2016-03-30 11:39:59 -07001023 it('testFloatingPointFieldsSupportNan', function() {
1024 var assertNan = function(x) {
1025 assertTrue('Expected ' + x + ' (' + goog.typeOf(x) + ') to be NaN.',
1026 goog.isNumber(x) && isNaN(x));
1027 };
1028
1029 var message = new proto.jspb.test.FloatingPointFields([
1030 'NaN', 'NaN', ['NaN', 'NaN'], 'NaN',
1031 'NaN', 'NaN', ['NaN', 'NaN'], 'NaN'
1032 ]);
1033 assertNan(message.getOptionalFloatField());
1034 assertNan(message.getRequiredFloatField());
1035 assertNan(message.getRepeatedFloatFieldList()[0]);
1036 assertNan(message.getRepeatedFloatFieldList()[1]);
1037 assertNan(message.getDefaultFloatField());
1038 assertNan(message.getOptionalDoubleField());
1039 assertNan(message.getRequiredDoubleField());
1040 assertNan(message.getRepeatedDoubleFieldList()[0]);
1041 assertNan(message.getRepeatedDoubleFieldList()[1]);
1042 assertNan(message.getDefaultDoubleField());
1043 });
1044
Adam Cozzette4a4a1622016-09-27 15:36:41 -07001045 // Verify that we can successfully use a field referring to a nested message
1046 // from a different .proto file.
1047 it('testForeignNestedMessage', function() {
1048 var msg = new proto.jspb.test.ForeignNestedFieldMessage();
1049 var nested = new proto.jspb.test.Deeply.Nested.Message();
1050 nested.setCount(5);
1051 msg.setDeeplyNestedMessage(nested);
Adam Cozzette315350b2016-11-18 11:19:30 -08001052 assertEquals(5, msg.getDeeplyNestedMessage().getCount());
Adam Cozzettefda876a2016-11-21 12:14:42 -08001053
1054 // After a serialization-deserialization round trip we should get back the
1055 // same data we started with.
1056 var serialized = msg.serializeBinary();
1057 var deserialized =
1058 proto.jspb.test.ForeignNestedFieldMessage.deserializeBinary(serialized);
1059 assertEquals(5, deserialized.getDeeplyNestedMessage().getCount());
Adam Cozzette4a4a1622016-09-27 15:36:41 -07001060 });
Adam Cozzette5a76e632016-11-17 16:48:38 -08001061
Feng Xiaoe841bac2015-12-11 17:09:20 -08001062});