blob: 6d7cdd21487cc6dde94db500b226832ae8cf4814 [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
Marcus Longmuir2c16f692017-01-07 00:59:47 +000042// CommonJS-LoadFromFile: test8_pb proto.jspb.exttest.nested
43goog.require('proto.jspb.exttest.nested.TestOuterMessage');
44
Josh Haberman77af5d02016-02-04 16:11:07 -080045// CommonJS-LoadFromFile: test5_pb proto.jspb.exttest.beta
Feng Xiaoe841bac2015-12-11 17:09:20 -080046goog.require('proto.jspb.exttest.beta.floatingStrField');
Josh Habermane9f31ee2016-02-04 10:29:27 -080047
Josh Haberman77af5d02016-02-04 16:11:07 -080048// CommonJS-LoadFromFile: test3_pb proto.jspb.exttest
Feng Xiaoe841bac2015-12-11 17:09:20 -080049goog.require('proto.jspb.exttest.floatingMsgField');
Josh Habermane9f31ee2016-02-04 10:29:27 -080050
Josh Haberman77af5d02016-02-04 16:11:07 -080051// CommonJS-LoadFromFile: test4_pb proto.jspb.exttest
Feng Xiaoe841bac2015-12-11 17:09:20 -080052goog.require('proto.jspb.exttest.floatingMsgFieldTwo');
Josh Habermane9f31ee2016-02-04 10:29:27 -080053
Josh Haberman77af5d02016-02-04 16:11:07 -080054// CommonJS-LoadFromFile: test_pb proto.jspb.test
Feng Xiaoe841bac2015-12-11 17:09:20 -080055goog.require('proto.jspb.test.CloneExtension');
56goog.require('proto.jspb.test.Complex');
57goog.require('proto.jspb.test.DefaultValues');
58goog.require('proto.jspb.test.Empty');
59goog.require('proto.jspb.test.EnumContainer');
Jisi Liu3b3c8ab2016-03-30 11:39:59 -070060goog.require('proto.jspb.test.floatingMsgField');
61goog.require('proto.jspb.test.FloatingPointFields');
Feng Xiaoe841bac2015-12-11 17:09:20 -080062goog.require('proto.jspb.test.floatingStrField');
63goog.require('proto.jspb.test.HasExtensions');
64goog.require('proto.jspb.test.IndirectExtension');
65goog.require('proto.jspb.test.IsExtension');
66goog.require('proto.jspb.test.OptionalFields');
67goog.require('proto.jspb.test.OuterEnum');
Josh Haberman77af5d02016-02-04 16:11:07 -080068goog.require('proto.jspb.test.OuterMessage.Complex');
Feng Xiaoe841bac2015-12-11 17:09:20 -080069goog.require('proto.jspb.test.Simple1');
70goog.require('proto.jspb.test.Simple2');
71goog.require('proto.jspb.test.SpecialCases');
72goog.require('proto.jspb.test.TestClone');
Adam Cozzetted64a2d92016-06-29 15:23:27 -070073goog.require('proto.jspb.test.TestEndsWithBytes');
Feng Xiaoe841bac2015-12-11 17:09:20 -080074goog.require('proto.jspb.test.TestGroup');
75goog.require('proto.jspb.test.TestGroup1');
76goog.require('proto.jspb.test.TestMessageWithOneof');
77goog.require('proto.jspb.test.TestReservedNames');
78goog.require('proto.jspb.test.TestReservedNamesExtension');
Adam Cozzette5a76e632016-11-17 16:48:38 -080079goog.require('proto.jspb.test.Deeply.Nested.Message');
Feng Xiaoe841bac2015-12-11 17:09:20 -080080
Josh Haberman77af5d02016-02-04 16:11:07 -080081// CommonJS-LoadFromFile: test2_pb proto.jspb.test
Josh Habermane9f31ee2016-02-04 10:29:27 -080082goog.require('proto.jspb.test.ExtensionMessage');
83goog.require('proto.jspb.test.TestExtensionsMessage');
Adam Cozzette5a76e632016-11-17 16:48:38 -080084goog.require('proto.jspb.test.ForeignNestedFieldMessage');
Jisi Liu3b3c8ab2016-03-30 11:39:59 -070085
Feng Xiaoe841bac2015-12-11 17:09:20 -080086
87
88
89describe('Message test suite', function() {
90 it('testEmptyProto', function() {
91 var empty1 = new proto.jspb.test.Empty([]);
92 var empty2 = new proto.jspb.test.Empty([]);
93 assertObjectEquals({}, empty1.toObject());
94 assertObjectEquals('Message should not be corrupted:', empty2, empty1);
95 });
96
97 it('testTopLevelEnum', function() {
98 var response = new proto.jspb.test.EnumContainer([]);
99 response.setOuterEnum(proto.jspb.test.OuterEnum.FOO);
100 assertEquals(proto.jspb.test.OuterEnum.FOO, response.getOuterEnum());
101 });
102
103 it('testByteStrings', function() {
104 var data = new proto.jspb.test.DefaultValues([]);
105 data.setBytesField('some_bytes');
106 assertEquals('some_bytes', data.getBytesField());
107 });
108
109 it('testComplexConversion', function() {
110 var data1 = ['a',,, [, 11], [[, 22], [, 33]],, ['s1', 's2'],, 1];
111 var data2 = ['a',,, [, 11], [[, 22], [, 33]],, ['s1', 's2'],, 1];
112 var foo = new proto.jspb.test.Complex(data1);
113 var bar = new proto.jspb.test.Complex(data2);
114 var result = foo.toObject();
115 assertObjectEquals({
116 aString: 'a',
117 anOutOfOrderBool: 1,
118 aNestedMessage: {
119 anInt: 11
120 },
121 aRepeatedMessageList: [{anInt: 22}, {anInt: 33}],
122 aRepeatedStringList: ['s1', 's2']
123 }, result);
124
125 // Now test with the jspb instances included.
126 result = foo.toObject(true /* opt_includeInstance */);
127 assertObjectEquals({
128 aString: 'a',
129 anOutOfOrderBool: 1,
130 aNestedMessage: {
131 anInt: 11,
132 $jspbMessageInstance: foo.getANestedMessage()
133 },
134 aRepeatedMessageList: [
135 {anInt: 22, $jspbMessageInstance: foo.getARepeatedMessageList()[0]},
136 {anInt: 33, $jspbMessageInstance: foo.getARepeatedMessageList()[1]}
137 ],
138 aRepeatedStringList: ['s1', 's2'],
139 $jspbMessageInstance: foo
140 }, result);
141
142 });
143
144 it('testMissingFields', function() {
145 var foo = new proto.jspb.test.Complex([
146 undefined, undefined, undefined, [],
147 undefined, undefined, undefined, undefined]);
148 var bar = new proto.jspb.test.Complex([
149 undefined, undefined, undefined, [],
150 undefined, undefined, undefined, undefined]);
151 var result = foo.toObject();
152 assertObjectEquals({
Nikolai Vavilove4b129f2016-07-19 10:34:58 +0300153 aString: undefined,
154 anOutOfOrderBool: undefined,
155 aNestedMessage: {
156 anInt: undefined
157 },
Feng Xiaoe841bac2015-12-11 17:09:20 -0800158 // Note: JsPb converts undefined repeated fields to empty arrays.
159 aRepeatedMessageList: [],
160 aRepeatedStringList: []
161 }, result);
162
163 });
164
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700165 it('testNestedComplexMessage', function() {
166 // Instantiate the message and set a unique field, just to ensure that we
167 // are not getting jspb.test.Complex instead.
168 var msg = new proto.jspb.test.OuterMessage.Complex();
169 msg.setInnerComplexField(5);
170 });
171
Feng Xiaoe841bac2015-12-11 17:09:20 -0800172 it('testSpecialCases', function() {
173 // Note: Some property names are reserved in JavaScript.
174 // These names are converted to the Js property named pb_<reserved_name>.
175 var special =
176 new proto.jspb.test.SpecialCases(['normal', 'default', 'function',
177 'var']);
178 var result = special.toObject();
179 assertObjectEquals({
180 normal: 'normal',
181 pb_default: 'default',
182 pb_function: 'function',
183 pb_var: 'var'
184 }, result);
185 });
186
187 it('testDefaultValues', function() {
188 var defaultString = "default<>\'\"abc";
189 var response = new proto.jspb.test.DefaultValues();
190
191 // Test toObject
Nikolai Vavilove4b129f2016-07-19 10:34:58 +0300192 var expectedObject = {
193 stringField: defaultString,
194 boolField: true,
195 intField: 11,
196 enumField: 13,
197 emptyField: '',
198 bytesField: 'bW9v'
199 };
Feng Xiaoe841bac2015-12-11 17:09:20 -0800200 assertObjectEquals(expectedObject, response.toObject());
201
202
203 // Test getters
204 response = new proto.jspb.test.DefaultValues();
205 assertEquals(defaultString, response.getStringField());
206 assertEquals(true, response.getBoolField());
207 assertEquals(11, response.getIntField());
208 assertEquals(13, response.getEnumField());
209 assertEquals('', response.getEmptyField());
210 assertEquals('bW9v', response.getBytesField());
211
212 function makeDefault(values) {
213 return new proto.jspb.test.DefaultValues(values);
214 }
215
216 // Test with undefined values,
217 // Use push to workaround IE treating undefined array elements as holes.
218 response = makeDefault([undefined, undefined, undefined, undefined]);
219 assertEquals(defaultString, response.getStringField());
220 assertEquals(true, response.getBoolField());
221 assertEquals(11, response.getIntField());
222 assertEquals(13, response.getEnumField());
Feng Xiao9086d962016-07-13 13:47:51 -0700223 assertFalse(response.hasStringField());
224 assertFalse(response.hasBoolField());
225 assertFalse(response.hasIntField());
226 assertFalse(response.hasEnumField());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800227
228 // Test with null values, as would be returned by a JSON serializer.
229 response = makeDefault([null, null, null, null]);
230 assertEquals(defaultString, response.getStringField());
231 assertEquals(true, response.getBoolField());
232 assertEquals(11, response.getIntField());
233 assertEquals(13, response.getEnumField());
Feng Xiao9086d962016-07-13 13:47:51 -0700234 assertFalse(response.hasStringField());
235 assertFalse(response.hasBoolField());
236 assertFalse(response.hasIntField());
237 assertFalse(response.hasEnumField());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800238
239 // Test with false-like values.
240 response = makeDefault(['', false, 0, 0]);
241 assertEquals('', response.getStringField());
242 assertEquals(false, response.getBoolField());
243 assertEquals(true, response.getIntField() == 0);
244 assertEquals(true, response.getEnumField() == 0);
Feng Xiao9086d962016-07-13 13:47:51 -0700245 assertTrue(response.hasStringField());
246 assertTrue(response.hasBoolField());
247 assertTrue(response.hasIntField());
248 assertTrue(response.hasEnumField());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800249
250 // Test that clearing the values reverts them to the default state.
251 response = makeDefault(['blah', false, 111, 77]);
252 response.clearStringField(); response.clearBoolField();
253 response.clearIntField(); response.clearEnumField();
254 assertEquals(defaultString, response.getStringField());
255 assertEquals(true, response.getBoolField());
256 assertEquals(11, response.getIntField());
257 assertEquals(13, response.getEnumField());
Feng Xiao9086d962016-07-13 13:47:51 -0700258 assertFalse(response.hasStringField());
259 assertFalse(response.hasBoolField());
260 assertFalse(response.hasIntField());
261 assertFalse(response.hasEnumField());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800262
263 // Test that setFoo(null) clears the values.
264 response = makeDefault(['blah', false, 111, 77]);
265 response.setStringField(null); response.setBoolField(null);
266 response.setIntField(undefined); response.setEnumField(undefined);
267 assertEquals(defaultString, response.getStringField());
268 assertEquals(true, response.getBoolField());
269 assertEquals(11, response.getIntField());
270 assertEquals(13, response.getEnumField());
Feng Xiao9086d962016-07-13 13:47:51 -0700271 assertFalse(response.hasStringField());
272 assertFalse(response.hasBoolField());
273 assertFalse(response.hasIntField());
274 assertFalse(response.hasEnumField());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800275 });
276
Bo Yangcc8ca5b2016-09-19 13:45:07 -0700277 it('testMessageRegistration', /** @suppress {visibility} */ function() {
Feng Xiaoe841bac2015-12-11 17:09:20 -0800278 // goog.require(SomeResponse) will include its library, which will in
279 // turn add SomeResponse to the message registry.
280 assertEquals(jspb.Message.registry_['res'], proto.jspb.test.SomeResponse);
281 });
282
283 it('testClearFields', function() {
Feng Xiaoe841bac2015-12-11 17:09:20 -0800284 var data = ['str', true, [11], [[22], [33]], ['s1', 's2']];
285 var foo = new proto.jspb.test.OptionalFields(data);
286 foo.clearAString();
287 foo.clearABool();
288 foo.clearANestedMessage();
289 foo.clearARepeatedMessageList();
290 foo.clearARepeatedStringList();
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300291 assertEquals('', foo.getAString());
292 assertEquals(false, foo.getABool());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800293 assertUndefined(foo.getANestedMessage());
Feng Xiao9086d962016-07-13 13:47:51 -0700294 assertFalse(foo.hasAString());
295 assertFalse(foo.hasABool());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800296 assertObjectEquals([], foo.getARepeatedMessageList());
297 assertObjectEquals([], foo.getARepeatedStringList());
298 // NOTE: We want the missing fields in 'expected' to be undefined,
299 // but we actually get a sparse array instead. We could use something
300 // like [1,undefined,2] to avoid this, except that this is still
301 // sparse on IE. No comment...
302 var expected = [,,, [], []];
303 expected[0] = expected[1] = expected[2] = undefined;
304 assertObjectEquals(expected, foo.toArray());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800305 });
306
Bo Yangcc8ca5b2016-09-19 13:45:07 -0700307 it('testDifferenceRawObject', /** @suppress {visibility} */ function() {
Feng Xiaoe841bac2015-12-11 17:09:20 -0800308 var p1 = new proto.jspb.test.HasExtensions(['hi', 'diff', {}]);
309 var p2 = new proto.jspb.test.HasExtensions(['hi', 'what',
310 {1000: 'unique'}]);
311 var diff = /** @type {proto.jspb.test.HasExtensions} */
312 (jspb.Message.difference(p1, p2));
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300313 assertEquals('', diff.getStr1());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800314 assertEquals('what', diff.getStr2());
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300315 assertEquals('', diff.getStr3());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800316 assertEquals('unique', diff.extensionObject_[1000]);
317 });
318
319 it('testEqualsSimple', function() {
320 var s1 = new proto.jspb.test.Simple1(['hi']);
321 assertTrue(jspb.Message.equals(s1, new proto.jspb.test.Simple1(['hi'])));
322 assertFalse(jspb.Message.equals(s1, new proto.jspb.test.Simple1(['bye'])));
323 var s1b = new proto.jspb.test.Simple1(['hi', ['hello']]);
324 assertTrue(jspb.Message.equals(s1b,
325 new proto.jspb.test.Simple1(['hi', ['hello']])));
326 assertTrue(jspb.Message.equals(s1b,
327 new proto.jspb.test.Simple1(['hi', ['hello', undefined,
328 undefined, undefined]])));
329 assertFalse(jspb.Message.equals(s1b,
330 new proto.jspb.test.Simple1(['no', ['hello']])));
331 // Test with messages of different types
332 var s2 = new proto.jspb.test.Simple2(['hi']);
333 assertFalse(jspb.Message.equals(s1, s2));
334 });
335
336 it('testEquals_softComparison', function() {
337 var s1 = new proto.jspb.test.Simple1(['hi', [], null]);
338 assertTrue(jspb.Message.equals(s1,
339 new proto.jspb.test.Simple1(['hi', []])));
340
341 var s1b = new proto.jspb.test.Simple1(['hi', [], true]);
342 assertTrue(jspb.Message.equals(s1b,
343 new proto.jspb.test.Simple1(['hi', [], 1])));
344 });
345
346 it('testEqualsComplex', function() {
347 var data1 = ['a',,, [, 11], [[, 22], [, 33]],, ['s1', 's2'],, 1];
348 var data2 = ['a',,, [, 11], [[, 22], [, 34]],, ['s1', 's2'],, 1];
349 var data3 = ['a',,, [, 11], [[, 22]],, ['s1', 's2'],, 1];
350 var data4 = ['hi'];
351 var c1a = new proto.jspb.test.Complex(data1);
352 var c1b = new proto.jspb.test.Complex(data1);
353 var c2 = new proto.jspb.test.Complex(data2);
354 var c3 = new proto.jspb.test.Complex(data3);
355 var s1 = new proto.jspb.test.Simple1(data4);
356
357 assertTrue(jspb.Message.equals(c1a, c1b));
358 assertFalse(jspb.Message.equals(c1a, c2));
359 assertFalse(jspb.Message.equals(c2, c3));
360 assertFalse(jspb.Message.equals(c1a, s1));
361 });
362
363 it('testEqualsExtensionsConstructed', function() {
364 assertTrue(jspb.Message.equals(
365 new proto.jspb.test.HasExtensions([]),
366 new proto.jspb.test.HasExtensions([{}])
367 ));
368 assertTrue(jspb.Message.equals(
369 new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'a'}]}]),
370 new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'a'}]}])
371 ));
372 assertFalse(jspb.Message.equals(
373 new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'a'}]}]),
374 new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'b'}]}])
375 ));
376 assertTrue(jspb.Message.equals(
377 new proto.jspb.test.HasExtensions([{100: [{200: 'a'}]}]),
378 new proto.jspb.test.HasExtensions([{100: [{200: 'a'}]}])
379 ));
380 assertTrue(jspb.Message.equals(
381 new proto.jspb.test.HasExtensions([{100: [{200: 'a'}]}]),
382 new proto.jspb.test.HasExtensions([,,, {100: [{200: 'a'}]}])
383 ));
384 assertTrue(jspb.Message.equals(
385 new proto.jspb.test.HasExtensions([,,, {100: [{200: 'a'}]}]),
386 new proto.jspb.test.HasExtensions([{100: [{200: 'a'}]}])
387 ));
388 assertTrue(jspb.Message.equals(
389 new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'a'}]}]),
390 new proto.jspb.test.HasExtensions(['hi',,, {100: [{200: 'a'}]}])
391 ));
392 assertTrue(jspb.Message.equals(
393 new proto.jspb.test.HasExtensions(['hi',,, {100: [{200: 'a'}]}]),
394 new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'a'}]}])
395 ));
396 });
397
398 it('testEqualsExtensionsUnconstructed', function() {
399 assertTrue(jspb.Message.compareFields([], [{}]));
400 assertTrue(jspb.Message.compareFields([,,, {}], []));
401 assertTrue(jspb.Message.compareFields([,,, {}], [,, {}]));
402 assertTrue(jspb.Message.compareFields(
403 ['hi', {100: [{200: 'a'}]}], ['hi', {100: [{200: 'a'}]}]));
404 assertFalse(jspb.Message.compareFields(
405 ['hi', {100: [{200: 'a'}]}], ['hi', {100: [{200: 'b'}]}]));
406 assertTrue(jspb.Message.compareFields(
407 [{100: [{200: 'a'}]}], [{100: [{200: 'a'}]}]));
408 assertTrue(jspb.Message.compareFields(
409 [{100: [{200: 'a'}]}], [,,, {100: [{200: 'a'}]}]));
410 assertTrue(jspb.Message.compareFields(
411 [,,, {100: [{200: 'a'}]}], [{100: [{200: 'a'}]}]));
412 assertTrue(jspb.Message.compareFields(
413 ['hi', {100: [{200: 'a'}]}], ['hi',,, {100: [{200: 'a'}]}]));
414 assertTrue(jspb.Message.compareFields(
415 ['hi',,, {100: [{200: 'a'}]}], ['hi', {100: [{200: 'a'}]}]));
416 });
417
418 it('testToMap', function() {
419 var p1 = new proto.jspb.test.Simple1(['k', ['v']]);
420 var p2 = new proto.jspb.test.Simple1(['k1', ['v1', 'v2']]);
421 var soymap = jspb.Message.toMap([p1, p2],
422 proto.jspb.test.Simple1.prototype.getAString,
423 proto.jspb.test.Simple1.prototype.toObject);
424 assertEquals('k', soymap['k'].aString);
425 assertArrayEquals(['v'], soymap['k'].aRepeatedStringList);
426 var protomap = jspb.Message.toMap([p1, p2],
427 proto.jspb.test.Simple1.prototype.getAString);
428 assertEquals('k', protomap['k'].getAString());
429 assertArrayEquals(['v'], protomap['k'].getARepeatedStringList());
430 });
431
432 it('testClone', function() {
Adam Cozzetted64a2d92016-06-29 15:23:27 -0700433 var supportsUint8Array =
434 !goog.userAgent.IE || goog.userAgent.isVersionOrHigher('10');
Feng Xiaoe841bac2015-12-11 17:09:20 -0800435 var original = new proto.jspb.test.TestClone();
436 original.setStr('v1');
437 var simple1 = new proto.jspb.test.Simple1(['x1', ['y1', 'z1']]);
438 var simple2 = new proto.jspb.test.Simple1(['x2', ['y2', 'z2']]);
439 var simple3 = new proto.jspb.test.Simple1(['x3', ['y3', 'z3']]);
440 original.setSimple1(simple1);
441 original.setSimple2List([simple2, simple3]);
Adam Cozzetted64a2d92016-06-29 15:23:27 -0700442 var bytes1 = supportsUint8Array ? new Uint8Array([1, 2, 3]) : '123';
443 original.setBytesField(bytes1);
Feng Xiaoe841bac2015-12-11 17:09:20 -0800444 var extension = new proto.jspb.test.CloneExtension();
445 extension.setExt('e1');
446 original.setExtension(proto.jspb.test.IsExtension.extField, extension);
Bo Yangcc8ca5b2016-09-19 13:45:07 -0700447 var clone = original.clone();
Feng Xiaoe841bac2015-12-11 17:09:20 -0800448 assertArrayEquals(['v1',, ['x1', ['y1', 'z1']],,
Adam Cozzetted64a2d92016-06-29 15:23:27 -0700449 [['x2', ['y2', 'z2']], ['x3', ['y3', 'z3']]], bytes1,, { 100: [, 'e1'] }],
Feng Xiaoe841bac2015-12-11 17:09:20 -0800450 clone.toArray());
451 clone.setStr('v2');
452 var simple4 = new proto.jspb.test.Simple1(['a1', ['b1', 'c1']]);
453 var simple5 = new proto.jspb.test.Simple1(['a2', ['b2', 'c2']]);
454 var simple6 = new proto.jspb.test.Simple1(['a3', ['b3', 'c3']]);
455 clone.setSimple1(simple4);
456 clone.setSimple2List([simple5, simple6]);
Adam Cozzetted64a2d92016-06-29 15:23:27 -0700457 if (supportsUint8Array) {
458 clone.getBytesField()[0] = 4;
459 assertObjectEquals(bytes1, original.getBytesField());
460 }
461 var bytes2 = supportsUint8Array ? new Uint8Array([4, 5, 6]) : '456';
462 clone.setBytesField(bytes2);
Feng Xiaoe841bac2015-12-11 17:09:20 -0800463 var newExtension = new proto.jspb.test.CloneExtension();
464 newExtension.setExt('e2');
465 clone.setExtension(proto.jspb.test.CloneExtension.extField, newExtension);
466 assertArrayEquals(['v2',, ['a1', ['b1', 'c1']],,
Adam Cozzetted64a2d92016-06-29 15:23:27 -0700467 [['a2', ['b2', 'c2']], ['a3', ['b3', 'c3']]], bytes2,, { 100: [, 'e2'] }],
Feng Xiaoe841bac2015-12-11 17:09:20 -0800468 clone.toArray());
469 assertArrayEquals(['v1',, ['x1', ['y1', 'z1']],,
Adam Cozzetted64a2d92016-06-29 15:23:27 -0700470 [['x2', ['y2', 'z2']], ['x3', ['y3', 'z3']]], bytes1,, { 100: [, 'e1'] }],
Feng Xiaoe841bac2015-12-11 17:09:20 -0800471 original.toArray());
472 });
473
474 it('testCopyInto', function() {
Adam Cozzetted64a2d92016-06-29 15:23:27 -0700475 var supportsUint8Array =
476 !goog.userAgent.IE || goog.userAgent.isVersionOrHigher('10');
Feng Xiaoe841bac2015-12-11 17:09:20 -0800477 var original = new proto.jspb.test.TestClone();
478 original.setStr('v1');
479 var dest = new proto.jspb.test.TestClone();
480 dest.setStr('override');
481 var simple1 = new proto.jspb.test.Simple1(['x1', ['y1', 'z1']]);
482 var simple2 = new proto.jspb.test.Simple1(['x2', ['y2', 'z2']]);
483 var simple3 = new proto.jspb.test.Simple1(['x3', ['y3', 'z3']]);
484 var destSimple1 = new proto.jspb.test.Simple1(['ox1', ['oy1', 'oz1']]);
485 var destSimple2 = new proto.jspb.test.Simple1(['ox2', ['oy2', 'oz2']]);
486 var destSimple3 = new proto.jspb.test.Simple1(['ox3', ['oy3', 'oz3']]);
487 original.setSimple1(simple1);
488 original.setSimple2List([simple2, simple3]);
489 dest.setSimple1(destSimple1);
490 dest.setSimple2List([destSimple2, destSimple3]);
Adam Cozzetted64a2d92016-06-29 15:23:27 -0700491 var bytes1 = supportsUint8Array ? new Uint8Array([1, 2, 3]) : '123';
492 var bytes2 = supportsUint8Array ? new Uint8Array([4, 5, 6]) : '456';
493 original.setBytesField(bytes1);
494 dest.setBytesField(bytes2);
Feng Xiaoe841bac2015-12-11 17:09:20 -0800495 var extension = new proto.jspb.test.CloneExtension();
496 extension.setExt('e1');
497 original.setExtension(proto.jspb.test.CloneExtension.extField, extension);
498
499 jspb.Message.copyInto(original, dest);
500 assertArrayEquals(original.toArray(), dest.toArray());
501 assertEquals('x1', dest.getSimple1().getAString());
502 assertEquals('e1',
503 dest.getExtension(proto.jspb.test.CloneExtension.extField).getExt());
504 dest.getSimple1().setAString('new value');
505 assertNotEquals(dest.getSimple1().getAString(),
506 original.getSimple1().getAString());
Adam Cozzetted64a2d92016-06-29 15:23:27 -0700507 if (supportsUint8Array) {
508 dest.getBytesField()[0] = 7;
509 assertObjectEquals(bytes1, original.getBytesField());
510 assertObjectEquals(new Uint8Array([7, 2, 3]), dest.getBytesField());
511 } else {
512 dest.setBytesField('789');
513 assertObjectEquals(bytes1, original.getBytesField());
514 assertObjectEquals('789', dest.getBytesField());
515 }
Feng Xiaoe841bac2015-12-11 17:09:20 -0800516 dest.getExtension(proto.jspb.test.CloneExtension.extField).
517 setExt('new value');
518 assertNotEquals(
519 dest.getExtension(proto.jspb.test.CloneExtension.extField).getExt(),
520 original.getExtension(
521 proto.jspb.test.CloneExtension.extField).getExt());
522 });
523
524 it('testCopyInto_notSameType', function() {
525 var a = new proto.jspb.test.TestClone();
526 var b = new proto.jspb.test.Simple1(['str', ['s1', 's2']]);
527
528 var e = assertThrows(function() {
529 jspb.Message.copyInto(a, b);
530 });
531 assertContains('should have the same type', e.message);
532 });
533
534 it('testExtensions', function() {
535 var extension1 = new proto.jspb.test.IsExtension(['ext1field']);
536 var extension2 = new proto.jspb.test.Simple1(['str', ['s1', 's2']]);
537 var extendable = new proto.jspb.test.HasExtensions(['v1', 'v2', 'v3']);
538 extendable.setExtension(proto.jspb.test.IsExtension.extField, extension1);
539 extendable.setExtension(proto.jspb.test.IndirectExtension.simple,
540 extension2);
541 extendable.setExtension(proto.jspb.test.IndirectExtension.str, 'xyzzy');
542 extendable.setExtension(proto.jspb.test.IndirectExtension.repeatedStrList,
543 ['a', 'b']);
544 var s1 = new proto.jspb.test.Simple1(['foo', ['s1', 's2']]);
545 var s2 = new proto.jspb.test.Simple1(['bar', ['t1', 't2']]);
546 extendable.setExtension(
547 proto.jspb.test.IndirectExtension.repeatedSimpleList,
548 [s1, s2]);
549 assertObjectEquals(extension1,
550 extendable.getExtension(proto.jspb.test.IsExtension.extField));
551 assertObjectEquals(extension2,
552 extendable.getExtension(proto.jspb.test.IndirectExtension.simple));
553 assertObjectEquals('xyzzy',
554 extendable.getExtension(proto.jspb.test.IndirectExtension.str));
555 assertObjectEquals(['a', 'b'], extendable.getExtension(
556 proto.jspb.test.IndirectExtension.repeatedStrList));
557 assertObjectEquals([s1, s2], extendable.getExtension(
558 proto.jspb.test.IndirectExtension.repeatedSimpleList));
559 // Not supported yet, but it should work...
560 extendable.setExtension(proto.jspb.test.IndirectExtension.simple, null);
561 assertNull(
562 extendable.getExtension(proto.jspb.test.IndirectExtension.simple));
563 extendable.setExtension(proto.jspb.test.IndirectExtension.str, null);
564 assertNull(extendable.getExtension(proto.jspb.test.IndirectExtension.str));
565
Josh Habermane9cf31e2015-12-21 15:18:17 -0800566
Feng Xiaoe841bac2015-12-11 17:09:20 -0800567 // Extension fields with jspb.ignore = true are ignored.
568 assertUndefined(proto.jspb.test.IndirectExtension['ignored']);
569 assertUndefined(proto.jspb.test.HasExtensions['ignoredFloating']);
570 });
571
572 it('testFloatingExtensions', function() {
573 // From an autogenerated container.
574 var extendable = new proto.jspb.test.HasExtensions(['v1', 'v2', 'v3']);
575 var extension = new proto.jspb.test.Simple1(['foo', ['s1', 's2']]);
576 extendable.setExtension(proto.jspb.test.simple1, extension);
577 assertObjectEquals(extension,
578 extendable.getExtension(proto.jspb.test.simple1));
579
580 // From _lib mode.
581 extension = new proto.jspb.test.ExtensionMessage(['s1']);
582 extendable = new proto.jspb.test.TestExtensionsMessage([16]);
583 extendable.setExtension(proto.jspb.test.floatingMsgField, extension);
584 extendable.setExtension(proto.jspb.test.floatingStrField, 's2');
585 assertObjectEquals(extension,
586 extendable.getExtension(proto.jspb.test.floatingMsgField));
587 assertObjectEquals('s2',
588 extendable.getExtension(proto.jspb.test.floatingStrField));
589 assertNotUndefined(proto.jspb.exttest.floatingMsgField);
590 assertNotUndefined(proto.jspb.exttest.floatingMsgFieldTwo);
591 assertNotUndefined(proto.jspb.exttest.beta.floatingStrField);
592 });
593
Marcus Longmuir2c16f692017-01-07 00:59:47 +0000594 it('testNestedExtensions', function() {
595 var extendable = new proto.jspb.exttest.nested.TestNestedExtensionsMessage();
596 var extension = new proto.jspb.exttest.nested.TestOuterMessage.NestedExtensionMessage(['s1']);
597 extendable.setExtension(proto.jspb.exttest.nested.TestOuterMessage.innerExtension, extension);
598 assertObjectEquals(extension,
599 extendable.getExtension(proto.jspb.exttest.nested.TestOuterMessage.innerExtension));
600 });
601
Feng Xiaoe841bac2015-12-11 17:09:20 -0800602 it('testToObject_extendedObject', function() {
603 var extension1 = new proto.jspb.test.IsExtension(['ext1field']);
604 var extension2 = new proto.jspb.test.Simple1(['str', ['s1', 's2'], true]);
605 var extendable = new proto.jspb.test.HasExtensions(['v1', 'v2', 'v3']);
606 extendable.setExtension(proto.jspb.test.IsExtension.extField, extension1);
607 extendable.setExtension(proto.jspb.test.IndirectExtension.simple,
608 extension2);
609 extendable.setExtension(proto.jspb.test.IndirectExtension.str, 'xyzzy');
610 extendable.setExtension(proto.jspb.test.IndirectExtension.repeatedStrList,
611 ['a', 'b']);
612 var s1 = new proto.jspb.test.Simple1(['foo', ['s1', 's2'], true]);
613 var s2 = new proto.jspb.test.Simple1(['bar', ['t1', 't2'], false]);
614 extendable.setExtension(
615 proto.jspb.test.IndirectExtension.repeatedSimpleList,
616 [s1, s2]);
617 assertObjectEquals({
618 str1: 'v1', str2: 'v2', str3: 'v3',
619 extField: { ext1: 'ext1field' },
620 simple: {
621 aString: 'str', aRepeatedStringList: ['s1', 's2'], aBoolean: true
622 },
623 str: 'xyzzy',
624 repeatedStrList: ['a', 'b'],
625 repeatedSimpleList: [
626 { aString: 'foo', aRepeatedStringList: ['s1', 's2'], aBoolean: true},
627 { aString: 'bar', aRepeatedStringList: ['t1', 't2'], aBoolean: false}
628 ]
629 }, extendable.toObject());
630
631 // Now, with instances included.
632 assertObjectEquals({
633 str1: 'v1', str2: 'v2', str3: 'v3',
634 extField: {
635 ext1: 'ext1field',
636 $jspbMessageInstance:
637 extendable.getExtension(proto.jspb.test.IsExtension.extField)
638 },
639 simple: {
640 aString: 'str',
641 aRepeatedStringList: ['s1', 's2'],
642 aBoolean: true,
643 $jspbMessageInstance:
644 extendable.getExtension(proto.jspb.test.IndirectExtension.simple)
645 },
646 str: 'xyzzy',
647 repeatedStrList: ['a', 'b'],
648 repeatedSimpleList: [{
649 aString: 'foo',
650 aRepeatedStringList: ['s1', 's2'],
651 aBoolean: true,
652 $jspbMessageInstance: s1
653 }, {
654 aString: 'bar',
655 aRepeatedStringList: ['t1', 't2'],
656 aBoolean: false,
657 $jspbMessageInstance: s2
658 }],
659 $jspbMessageInstance: extendable
660 }, extendable.toObject(true /* opt_includeInstance */));
661 });
662
663 it('testInitialization_emptyArray', function() {
664 var msg = new proto.jspb.test.HasExtensions([]);
665 if (jspb.Message.MINIMIZE_MEMORY_ALLOCATIONS) {
666 assertArrayEquals([], msg.toArray());
667 } else {
668 // Extension object is created past all regular fields.
669 assertArrayEquals([,,, {}], msg.toArray());
670 }
671 });
672
673 it('testInitialization_justExtensionObject', function() {
674 var msg = new proto.jspb.test.Empty([{1: 'hi'}]);
675 // The extensionObject is not moved from its original location.
676 assertArrayEquals([{1: 'hi'}], msg.toArray());
677 });
678
679 it('testInitialization_incompleteList', function() {
680 var msg = new proto.jspb.test.Empty([1, {4: 'hi'}]);
681 // The extensionObject is not moved from its original location.
682 assertArrayEquals([1, {4: 'hi'}], msg.toArray());
683 });
684
685 it('testInitialization_forwardCompatible', function() {
686 var msg = new proto.jspb.test.Empty([1, 2, 3, {1: 'hi'}]);
687 assertArrayEquals([1, 2, 3, {1: 'hi'}], msg.toArray());
688 });
689
Bo Yangcc8ca5b2016-09-19 13:45:07 -0700690 it('testExtendedMessageEnsureObject',
691 /** @suppress {visibility} */ function() {
692 var data =
693 new proto.jspb.test.HasExtensions(['str1', {'a_key': 'an_object'}]);
694 assertEquals('an_object', data.extensionObject_['a_key']);
695 });
Feng Xiaoe841bac2015-12-11 17:09:20 -0800696
697 it('testToObject_hasExtensionField', function() {
698 var data = new proto.jspb.test.HasExtensions(['str1', {100: ['ext1']}]);
699 var obj = data.toObject();
700 assertEquals('str1', obj.str1);
701 assertEquals('ext1', obj.extField.ext1);
702 });
703
704 it('testGetExtension', function() {
705 var data = new proto.jspb.test.HasExtensions(['str1', {100: ['ext1']}]);
706 assertEquals('str1', data.getStr1());
707 var extension = data.getExtension(proto.jspb.test.IsExtension.extField);
708 assertNotNull(extension);
709 assertEquals('ext1', extension.getExt1());
710 });
711
712 it('testSetExtension', function() {
713 var data = new proto.jspb.test.HasExtensions();
714 var extensionMessage = new proto.jspb.test.IsExtension(['is_extension']);
715 data.setExtension(proto.jspb.test.IsExtension.extField, extensionMessage);
716 var obj = data.toObject();
717 assertNotNull(
718 data.getExtension(proto.jspb.test.IsExtension.extField));
719 assertEquals('is_extension', obj.extField.ext1);
720 });
721
722 /**
723 * Note that group is long deprecated, we only support it because JsPb has
724 * a goal of being able to generate JS classes for all proto descriptors.
725 */
726 it('testGroups', function() {
727 var group = new proto.jspb.test.TestGroup();
728 var someGroup = new proto.jspb.test.TestGroup.RepeatedGroup();
729 someGroup.setId('g1');
730 someGroup.setSomeBoolList([true, false]);
731 group.setRepeatedGroupList([someGroup]);
732 var groups = group.getRepeatedGroupList();
733 assertEquals('g1', groups[0].getId());
734 assertObjectEquals([true, false], groups[0].getSomeBoolList());
735 assertObjectEquals({id: 'g1', someBoolList: [true, false]},
736 groups[0].toObject());
737 assertObjectEquals({
Nikolai Vavilove4b129f2016-07-19 10:34:58 +0300738 repeatedGroupList: [{id: 'g1', someBoolList: [true, false]}],
739 requiredGroup: {id: undefined},
740 optionalGroup: undefined,
741 requiredSimple: {aRepeatedStringList: [], aString: undefined},
742 optionalSimple: undefined,
743 id: undefined
Feng Xiaoe841bac2015-12-11 17:09:20 -0800744 }, group.toObject());
745 var group1 = new proto.jspb.test.TestGroup1();
746 group1.setGroup(someGroup);
747 assertEquals(someGroup, group1.getGroup());
748 });
749
750 it('testNonExtensionFieldsAfterExtensionRange', function() {
751 var data = [{'1': 'a_string'}];
752 var message = new proto.jspb.test.Complex(data);
753 assertArrayEquals([], message.getARepeatedStringList());
754 });
755
756 it('testReservedGetterNames', function() {
757 var message = new proto.jspb.test.TestReservedNames();
758 message.setExtension$(11);
759 message.setExtension(proto.jspb.test.TestReservedNamesExtension.foo, 12);
760 assertEquals(11, message.getExtension$());
761 assertEquals(12, message.getExtension(
762 proto.jspb.test.TestReservedNamesExtension.foo));
763 assertObjectEquals({extension: 11, foo: 12}, message.toObject());
764 });
765
766 it('testInitializeMessageWithUnsetOneof', function() {
767 var message = new proto.jspb.test.TestMessageWithOneof([]);
768 assertEquals(
769 proto.jspb.test.TestMessageWithOneof.PartialOneofCase.
770 PARTIAL_ONEOF_NOT_SET,
771 message.getPartialOneofCase());
772 assertEquals(
773 proto.jspb.test.TestMessageWithOneof.RecursiveOneofCase.
774 RECURSIVE_ONEOF_NOT_SET,
775 message.getRecursiveOneofCase());
776 });
777
778 it('testInitializeMessageWithSingleValueSetInOneof', function() {
779 var message = new proto.jspb.test.TestMessageWithOneof([,, 'x']);
780
781 assertEquals('x', message.getPone());
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300782 assertEquals('', message.getPthree());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800783 assertEquals(
784 proto.jspb.test.TestMessageWithOneof.PartialOneofCase.PONE,
785 message.getPartialOneofCase());
786 });
787
788 it('testKeepsLastWireValueSetInUnion_multipleValues', function() {
789 var message = new proto.jspb.test.TestMessageWithOneof([,, 'x',, 'y']);
790
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300791 assertEquals('', message.getPone());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800792 assertEquals('y', message.getPthree());
793 assertEquals(
794 proto.jspb.test.TestMessageWithOneof.PartialOneofCase.PTHREE,
795 message.getPartialOneofCase());
796 });
797
798 it('testSettingOneofFieldClearsOthers', function() {
799 var message = new proto.jspb.test.TestMessageWithOneof;
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300800 assertEquals('', message.getPone());
801 assertEquals('', message.getPthree());
Feng Xiao9086d962016-07-13 13:47:51 -0700802 assertFalse(message.hasPone());
803 assertFalse(message.hasPthree());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800804
805 message.setPone('hi');
806 assertEquals('hi', message.getPone());
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300807 assertEquals('', message.getPthree());
Feng Xiao9086d962016-07-13 13:47:51 -0700808 assertTrue(message.hasPone());
809 assertFalse(message.hasPthree());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800810
811 message.setPthree('bye');
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300812 assertEquals('', message.getPone());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800813 assertEquals('bye', message.getPthree());
Feng Xiao9086d962016-07-13 13:47:51 -0700814 assertFalse(message.hasPone());
815 assertTrue(message.hasPthree());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800816 });
817
818 it('testSettingOneofFieldDoesNotClearFieldsFromOtherUnions', function() {
819 var other = new proto.jspb.test.TestMessageWithOneof;
820 var message = new proto.jspb.test.TestMessageWithOneof;
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300821 assertEquals('', message.getPone());
822 assertEquals('', message.getPthree());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800823 assertUndefined(message.getRone());
Feng Xiao9086d962016-07-13 13:47:51 -0700824 assertFalse(message.hasPone());
825 assertFalse(message.hasPthree());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800826
827 message.setPone('hi');
828 message.setRone(other);
829 assertEquals('hi', message.getPone());
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300830 assertEquals('', message.getPthree());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800831 assertEquals(other, message.getRone());
Feng Xiao9086d962016-07-13 13:47:51 -0700832 assertTrue(message.hasPone());
833 assertFalse(message.hasPthree());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800834
835 message.setPthree('bye');
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300836 assertEquals('', message.getPone());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800837 assertEquals('bye', message.getPthree());
838 assertEquals(other, message.getRone());
Feng Xiao9086d962016-07-13 13:47:51 -0700839 assertFalse(message.hasPone());
840 assertTrue(message.hasPthree());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800841 });
842
843 it('testUnsetsOneofCaseWhenFieldIsCleared', function() {
844 var message = new proto.jspb.test.TestMessageWithOneof;
845 assertEquals(
846 proto.jspb.test.TestMessageWithOneof.PartialOneofCase.
847 PARTIAL_ONEOF_NOT_SET,
848 message.getPartialOneofCase());
849
850 message.setPone('hi');
851 assertEquals(
852 proto.jspb.test.TestMessageWithOneof.PartialOneofCase.PONE,
853 message.getPartialOneofCase());
854
855 message.clearPone();
856 assertEquals(
857 proto.jspb.test.TestMessageWithOneof.PartialOneofCase.
858 PARTIAL_ONEOF_NOT_SET,
859 message.getPartialOneofCase());
860 });
861
862 it('testMessageWithDefaultOneofValues', function() {
863 var message = new proto.jspb.test.TestMessageWithOneof;
864 assertEquals(1234, message.getAone());
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300865 assertEquals(0, message.getAtwo());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800866 assertEquals(
867 proto.jspb.test.TestMessageWithOneof.DefaultOneofACase
868 .DEFAULT_ONEOF_A_NOT_SET,
869 message.getDefaultOneofACase());
870
871 message.setAone(567);
872 assertEquals(567, message.getAone());
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300873 assertEquals(0, message.getAtwo());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800874 assertEquals(
875 proto.jspb.test.TestMessageWithOneof.DefaultOneofACase.AONE,
876 message.getDefaultOneofACase());
877
878 message.setAtwo(890);
879 assertEquals(1234, message.getAone());
880 assertEquals(890, message.getAtwo());
881 assertEquals(
882 proto.jspb.test.TestMessageWithOneof.DefaultOneofACase.ATWO,
883 message.getDefaultOneofACase());
884
885 message.clearAtwo();
886 assertEquals(1234, message.getAone());
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300887 assertEquals(0, message.getAtwo());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800888 assertEquals(
889 proto.jspb.test.TestMessageWithOneof.DefaultOneofACase
890 .DEFAULT_ONEOF_A_NOT_SET,
891 message.getDefaultOneofACase());
892 });
893
894 it('testMessageWithDefaultOneofValues_defaultNotOnFirstField', function() {
895 var message = new proto.jspb.test.TestMessageWithOneof;
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300896 assertEquals(0, message.getBone());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800897 assertEquals(1234, message.getBtwo());
Feng Xiao9086d962016-07-13 13:47:51 -0700898 assertFalse(message.hasBone());
899 assertFalse(message.hasBtwo());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800900 assertEquals(
901 proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase
902 .DEFAULT_ONEOF_B_NOT_SET,
903 message.getDefaultOneofBCase());
904
905 message.setBone(2);
906 assertEquals(2, message.getBone());
907 assertEquals(1234, message.getBtwo());
Feng Xiao9086d962016-07-13 13:47:51 -0700908 assertTrue(message.hasBone());
909 assertFalse(message.hasBtwo());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800910 assertEquals(
911 proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase.BONE,
912 message.getDefaultOneofBCase());
913
914 message.setBtwo(3);
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300915 assertEquals(0, message.getBone());
Feng Xiao9086d962016-07-13 13:47:51 -0700916 assertFalse(message.hasBone());
917 assertTrue(message.hasBtwo());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800918 assertEquals(3, message.getBtwo());
919 assertEquals(
920 proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase.BTWO,
921 message.getDefaultOneofBCase());
922
923 message.clearBtwo();
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300924 assertEquals(0, message.getBone());
Feng Xiao9086d962016-07-13 13:47:51 -0700925 assertFalse(message.hasBone());
926 assertFalse(message.hasBtwo());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800927 assertEquals(1234, message.getBtwo());
928 assertEquals(
929 proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase
930 .DEFAULT_ONEOF_B_NOT_SET,
931 message.getDefaultOneofBCase());
932 });
933
934 it('testInitializeMessageWithOneofDefaults', function() {
935 var message =
936 new proto.jspb.test.TestMessageWithOneof(new Array(9).concat(567));
937 assertEquals(567, message.getAone());
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300938 assertEquals(0, message.getAtwo());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800939 assertEquals(
940 proto.jspb.test.TestMessageWithOneof.DefaultOneofACase.AONE,
941 message.getDefaultOneofACase());
942
943 message =
944 new proto.jspb.test.TestMessageWithOneof(new Array(10).concat(890));
945 assertEquals(1234, message.getAone());
946 assertEquals(890, message.getAtwo());
947 assertEquals(
948 proto.jspb.test.TestMessageWithOneof.DefaultOneofACase.ATWO,
949 message.getDefaultOneofACase());
950
951 message =
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700952 new proto.jspb.test.TestMessageWithOneof(new Array(9).concat(567, 890));
Feng Xiaoe841bac2015-12-11 17:09:20 -0800953 assertEquals(1234, message.getAone());
954 assertEquals(890, message.getAtwo());
955 assertEquals(
956 proto.jspb.test.TestMessageWithOneof.DefaultOneofACase.ATWO,
957 message.getDefaultOneofACase());
958 });
959
960 it('testInitializeMessageWithOneofDefaults_defaultNotSetOnFirstField',
961 function() {
962 var message;
963
964 message =
965 new proto.jspb.test.TestMessageWithOneof(new Array(11).concat(567));
966 assertEquals(567, message.getBone());
967 assertEquals(1234, message.getBtwo());
968 assertEquals(
969 proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase.BONE,
970 message.getDefaultOneofBCase());
971
972 message =
973 new proto.jspb.test.TestMessageWithOneof(new Array(12).concat(890));
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300974 assertEquals(0, message.getBone());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800975 assertEquals(890, message.getBtwo());
976 assertEquals(
977 proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase.BTWO,
978 message.getDefaultOneofBCase());
979
980 message = new proto.jspb.test.TestMessageWithOneof(
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700981 new Array(11).concat(567, 890));
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300982 assertEquals(0, message.getBone());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800983 assertEquals(890, message.getBtwo());
984 assertEquals(
985 proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase.BTWO,
986 message.getDefaultOneofBCase());
987 });
988
989 it('testOneofContainingAnotherMessage', function() {
990 var message = new proto.jspb.test.TestMessageWithOneof;
991 assertEquals(
992 proto.jspb.test.TestMessageWithOneof.RecursiveOneofCase.
993 RECURSIVE_ONEOF_NOT_SET,
994 message.getRecursiveOneofCase());
995
996 var other = new proto.jspb.test.TestMessageWithOneof;
997 message.setRone(other);
998 assertEquals(other, message.getRone());
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300999 assertEquals('', message.getRtwo());
Feng Xiaoe841bac2015-12-11 17:09:20 -08001000 assertEquals(
1001 proto.jspb.test.TestMessageWithOneof.RecursiveOneofCase.RONE,
1002 message.getRecursiveOneofCase());
1003
1004 message.setRtwo('hi');
1005 assertUndefined(message.getRone());
1006 assertEquals('hi', message.getRtwo());
1007 assertEquals(
1008 proto.jspb.test.TestMessageWithOneof.RecursiveOneofCase.RTWO,
1009 message.getRecursiveOneofCase());
1010 });
1011
1012 it('testQueryingOneofCaseEnsuresOnlyOneFieldIsSetInUnderlyingArray',
1013 function() {
1014 var message = new proto.jspb.test.TestMessageWithOneof;
1015 message.setPone('x');
1016 assertEquals('x', message.getPone());
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +03001017 assertEquals('', message.getPthree());
Feng Xiaoe841bac2015-12-11 17:09:20 -08001018 assertEquals(
1019 proto.jspb.test.TestMessageWithOneof.PartialOneofCase.PONE,
1020 message.getPartialOneofCase());
1021
1022 var array = message.toArray();
1023 assertEquals('x', array[2]);
1024 assertUndefined(array[4]);
1025 array[4] = 'y';
1026
1027 assertEquals(
1028 proto.jspb.test.TestMessageWithOneof.PartialOneofCase.PTHREE,
1029 message.getPartialOneofCase());
1030 assertUndefined(array[2]);
1031 assertEquals('y', array[4]);
1032 });
1033
Jisi Liu3b3c8ab2016-03-30 11:39:59 -07001034 it('testFloatingPointFieldsSupportNan', function() {
1035 var assertNan = function(x) {
1036 assertTrue('Expected ' + x + ' (' + goog.typeOf(x) + ') to be NaN.',
1037 goog.isNumber(x) && isNaN(x));
1038 };
1039
1040 var message = new proto.jspb.test.FloatingPointFields([
1041 'NaN', 'NaN', ['NaN', 'NaN'], 'NaN',
1042 'NaN', 'NaN', ['NaN', 'NaN'], 'NaN'
1043 ]);
1044 assertNan(message.getOptionalFloatField());
1045 assertNan(message.getRequiredFloatField());
1046 assertNan(message.getRepeatedFloatFieldList()[0]);
1047 assertNan(message.getRepeatedFloatFieldList()[1]);
1048 assertNan(message.getDefaultFloatField());
1049 assertNan(message.getOptionalDoubleField());
1050 assertNan(message.getRequiredDoubleField());
1051 assertNan(message.getRepeatedDoubleFieldList()[0]);
1052 assertNan(message.getRepeatedDoubleFieldList()[1]);
1053 assertNan(message.getDefaultDoubleField());
1054 });
1055
Adam Cozzette4a4a1622016-09-27 15:36:41 -07001056 // Verify that we can successfully use a field referring to a nested message
1057 // from a different .proto file.
1058 it('testForeignNestedMessage', function() {
1059 var msg = new proto.jspb.test.ForeignNestedFieldMessage();
1060 var nested = new proto.jspb.test.Deeply.Nested.Message();
1061 nested.setCount(5);
1062 msg.setDeeplyNestedMessage(nested);
Adam Cozzette315350b2016-11-18 11:19:30 -08001063 assertEquals(5, msg.getDeeplyNestedMessage().getCount());
Adam Cozzettefda876a2016-11-21 12:14:42 -08001064
1065 // After a serialization-deserialization round trip we should get back the
1066 // same data we started with.
1067 var serialized = msg.serializeBinary();
1068 var deserialized =
1069 proto.jspb.test.ForeignNestedFieldMessage.deserializeBinary(serialized);
1070 assertEquals(5, deserialized.getDeeplyNestedMessage().getCount());
Adam Cozzette4a4a1622016-09-27 15:36:41 -07001071 });
Adam Cozzette5a76e632016-11-17 16:48:38 -08001072
Feng Xiaoe841bac2015-12-11 17:09:20 -08001073});