Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(112)

Side by Side Diff: third_party/protobuf/js/message_test.js

Issue 1842653006: Update //third_party/protobuf to version 3. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: merge Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/protobuf/js/message.js ('k') | third_party/protobuf/js/node_loader.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 // Test suite is written using Jasmine -- see http://jasmine.github.io/
32
33 goog.setTestOnly();
34
35 goog.require('goog.json');
36 goog.require('goog.testing.asserts');
37 goog.require('jspb.Message');
38 goog.require('proto.jspb.exttest.beta.floatingStrField');
39 goog.require('proto.jspb.exttest.floatingMsgField');
40 goog.require('proto.jspb.exttest.floatingMsgFieldTwo');
41 goog.require('proto.jspb.test.CloneExtension');
42 goog.require('proto.jspb.test.Complex');
43 goog.require('proto.jspb.test.DefaultValues');
44 goog.require('proto.jspb.test.Empty');
45 goog.require('proto.jspb.test.EnumContainer');
46 goog.require('proto.jspb.test.ExtensionMessage');
47 goog.require('proto.jspb.test.floatingMsgField');
48 goog.require('proto.jspb.test.floatingStrField');
49 goog.require('proto.jspb.test.HasExtensions');
50 goog.require('proto.jspb.test.IndirectExtension');
51 goog.require('proto.jspb.test.IsExtension');
52 goog.require('proto.jspb.test.OptionalFields');
53 goog.require('proto.jspb.test.OuterEnum');
54 goog.require('proto.jspb.test.simple1');
55 goog.require('proto.jspb.test.Simple1');
56 goog.require('proto.jspb.test.Simple2');
57 goog.require('proto.jspb.test.SpecialCases');
58 goog.require('proto.jspb.test.TestClone');
59 goog.require('proto.jspb.test.TestExtensionsMessage');
60 goog.require('proto.jspb.test.TestGroup');
61 goog.require('proto.jspb.test.TestGroup1');
62 goog.require('proto.jspb.test.TestMessageWithOneof');
63 goog.require('proto.jspb.test.TestReservedNames');
64 goog.require('proto.jspb.test.TestReservedNamesExtension');
65
66
67
68
69 describe('Message test suite', function() {
70 it('testEmptyProto', function() {
71 var empty1 = new proto.jspb.test.Empty([]);
72 var empty2 = new proto.jspb.test.Empty([]);
73 assertObjectEquals({}, empty1.toObject());
74 assertObjectEquals('Message should not be corrupted:', empty2, empty1);
75 });
76
77 it('testTopLevelEnum', function() {
78 var response = new proto.jspb.test.EnumContainer([]);
79 response.setOuterEnum(proto.jspb.test.OuterEnum.FOO);
80 assertEquals(proto.jspb.test.OuterEnum.FOO, response.getOuterEnum());
81 });
82
83 it('testByteStrings', function() {
84 var data = new proto.jspb.test.DefaultValues([]);
85 data.setBytesField('some_bytes');
86 assertEquals('some_bytes', data.getBytesField());
87 });
88
89 it('testComplexConversion', function() {
90 var data1 = ['a',,, [, 11], [[, 22], [, 33]],, ['s1', 's2'],, 1];
91 var data2 = ['a',,, [, 11], [[, 22], [, 33]],, ['s1', 's2'],, 1];
92 var foo = new proto.jspb.test.Complex(data1);
93 var bar = new proto.jspb.test.Complex(data2);
94 var result = foo.toObject();
95 assertObjectEquals({
96 aString: 'a',
97 anOutOfOrderBool: 1,
98 aNestedMessage: {
99 anInt: 11
100 },
101 aRepeatedMessageList: [{anInt: 22}, {anInt: 33}],
102 aRepeatedStringList: ['s1', 's2']
103 }, result);
104
105 // Now test with the jspb instances included.
106 result = foo.toObject(true /* opt_includeInstance */);
107 assertObjectEquals({
108 aString: 'a',
109 anOutOfOrderBool: 1,
110 aNestedMessage: {
111 anInt: 11,
112 $jspbMessageInstance: foo.getANestedMessage()
113 },
114 aRepeatedMessageList: [
115 {anInt: 22, $jspbMessageInstance: foo.getARepeatedMessageList()[0]},
116 {anInt: 33, $jspbMessageInstance: foo.getARepeatedMessageList()[1]}
117 ],
118 aRepeatedStringList: ['s1', 's2'],
119 $jspbMessageInstance: foo
120 }, result);
121
122 });
123
124 it('testMissingFields', function() {
125 var foo = new proto.jspb.test.Complex([
126 undefined, undefined, undefined, [],
127 undefined, undefined, undefined, undefined]);
128 var bar = new proto.jspb.test.Complex([
129 undefined, undefined, undefined, [],
130 undefined, undefined, undefined, undefined]);
131 var result = foo.toObject();
132 assertObjectEquals({
133 aString: undefined,
134 anOutOfOrderBool: undefined,
135 aNestedMessage: {
136 anInt: undefined
137 },
138 // Note: JsPb converts undefined repeated fields to empty arrays.
139 aRepeatedMessageList: [],
140 aRepeatedStringList: []
141 }, result);
142
143 });
144
145 it('testSpecialCases', function() {
146 // Note: Some property names are reserved in JavaScript.
147 // These names are converted to the Js property named pb_<reserved_name>.
148 var special =
149 new proto.jspb.test.SpecialCases(['normal', 'default', 'function',
150 'var']);
151 var result = special.toObject();
152 assertObjectEquals({
153 normal: 'normal',
154 pb_default: 'default',
155 pb_function: 'function',
156 pb_var: 'var'
157 }, result);
158 });
159
160 it('testDefaultValues', function() {
161 var defaultString = "default<>\'\"abc";
162 var response = new proto.jspb.test.DefaultValues();
163
164 // Test toObject
165 var expectedObject = {
166 stringField: defaultString,
167 boolField: true,
168 intField: 11,
169 enumField: 13,
170 emptyField: '',
171 bytesField: 'bW9v'
172 };
173 assertObjectEquals(expectedObject, response.toObject());
174
175
176 // Test getters
177 response = new proto.jspb.test.DefaultValues();
178 assertEquals(defaultString, response.getStringField());
179 assertEquals(true, response.getBoolField());
180 assertEquals(11, response.getIntField());
181 assertEquals(13, response.getEnumField());
182 assertEquals('', response.getEmptyField());
183 assertEquals('bW9v', response.getBytesField());
184
185 function makeDefault(values) {
186 return new proto.jspb.test.DefaultValues(values);
187 }
188
189 // Test with undefined values,
190 // Use push to workaround IE treating undefined array elements as holes.
191 response = makeDefault([undefined, undefined, undefined, undefined]);
192 assertEquals(defaultString, response.getStringField());
193 assertEquals(true, response.getBoolField());
194 assertEquals(11, response.getIntField());
195 assertEquals(13, response.getEnumField());
196
197 // Test with null values, as would be returned by a JSON serializer.
198 response = makeDefault([null, null, null, null]);
199 assertEquals(defaultString, response.getStringField());
200 assertEquals(true, response.getBoolField());
201 assertEquals(11, response.getIntField());
202 assertEquals(13, response.getEnumField());
203
204 // Test with false-like values.
205 response = makeDefault(['', false, 0, 0]);
206 assertEquals('', response.getStringField());
207 assertEquals(false, response.getBoolField());
208 assertEquals(true, response.getIntField() == 0);
209 assertEquals(true, response.getEnumField() == 0);
210
211 // Test that clearing the values reverts them to the default state.
212 response = makeDefault(['blah', false, 111, 77]);
213 response.clearStringField(); response.clearBoolField();
214 response.clearIntField(); response.clearEnumField();
215 assertEquals(defaultString, response.getStringField());
216 assertEquals(true, response.getBoolField());
217 assertEquals(11, response.getIntField());
218 assertEquals(13, response.getEnumField());
219
220 // Test that setFoo(null) clears the values.
221 response = makeDefault(['blah', false, 111, 77]);
222 response.setStringField(null); response.setBoolField(null);
223 response.setIntField(undefined); response.setEnumField(undefined);
224 assertEquals(defaultString, response.getStringField());
225 assertEquals(true, response.getBoolField());
226 assertEquals(11, response.getIntField());
227 assertEquals(13, response.getEnumField());
228 });
229
230 it('testMessageRegistration', function() {
231 // goog.require(SomeResponse) will include its library, which will in
232 // turn add SomeResponse to the message registry.
233 assertEquals(jspb.Message.registry_['res'], proto.jspb.test.SomeResponse);
234 });
235
236 it('testClearFields', function() {
237 // We don't set 'proper' defaults, rather, bools, strings,
238 // etc, are cleared to undefined or null and take on the Javascript
239 // meaning for that value. Repeated fields are set to [] when cleared.
240 var data = ['str', true, [11], [[22], [33]], ['s1', 's2']];
241 var foo = new proto.jspb.test.OptionalFields(data);
242 foo.clearAString();
243 foo.clearABool();
244 foo.clearANestedMessage();
245 foo.clearARepeatedMessageList();
246 foo.clearARepeatedStringList();
247 assertUndefined(foo.getAString());
248 assertUndefined(foo.getABool());
249 assertUndefined(foo.getANestedMessage());
250 assertObjectEquals([], foo.getARepeatedMessageList());
251 assertObjectEquals([], foo.getARepeatedStringList());
252 // NOTE: We want the missing fields in 'expected' to be undefined,
253 // but we actually get a sparse array instead. We could use something
254 // like [1,undefined,2] to avoid this, except that this is still
255 // sparse on IE. No comment...
256 var expected = [,,, [], []];
257 expected[0] = expected[1] = expected[2] = undefined;
258 assertObjectEquals(expected, foo.toArray());
259
260 // Test set(null). We could deprecated this in favor of clear(), but
261 // it's also convenient to have.
262 data = ['str', true, [11], [[22], [33]], ['s1', 's2']];
263 foo = new proto.jspb.test.OptionalFields(data);
264 foo.setAString(null);
265 foo.setABool(null);
266 foo.setANestedMessage(null);
267 foo.setARepeatedMessageList(null);
268 foo.setARepeatedStringList(null);
269 assertNull(foo.getAString());
270 assertNull(foo.getABool());
271 assertNull(foo.getANestedMessage());
272 assertObjectEquals([], foo.getARepeatedMessageList());
273 assertObjectEquals([], foo.getARepeatedStringList());
274 assertObjectEquals([null, null, null, [], []], foo.toArray());
275
276 // Test set(undefined). Again, not something we really need, and not
277 // supported directly by our typing, but it should 'do the right thing'.
278 data = ['str', true, [11], [[22], [33]], ['s1', 's2']];
279 foo = new proto.jspb.test.OptionalFields(data);
280 foo.setAString(undefined);
281 foo.setABool(undefined);
282 foo.setANestedMessage(undefined);
283 foo.setARepeatedMessageList(undefined);
284 foo.setARepeatedStringList(undefined);
285 assertUndefined(foo.getAString());
286 assertUndefined(foo.getABool());
287 assertUndefined(foo.getANestedMessage());
288 assertObjectEquals([], foo.getARepeatedMessageList());
289 assertObjectEquals([], foo.getARepeatedStringList());
290 expected = [,,, [], []];
291 expected[0] = expected[1] = expected[2] = undefined;
292 assertObjectEquals(expected, foo.toArray());
293 });
294
295 it('testDifferenceRawObject', function() {
296 var p1 = new proto.jspb.test.HasExtensions(['hi', 'diff', {}]);
297 var p2 = new proto.jspb.test.HasExtensions(['hi', 'what',
298 {1000: 'unique'}]);
299 var diff = /** @type {proto.jspb.test.HasExtensions} */
300 (jspb.Message.difference(p1, p2));
301 assertUndefined(diff.getStr1());
302 assertEquals('what', diff.getStr2());
303 assertUndefined(diff.getStr3());
304 assertEquals('unique', diff.extensionObject_[1000]);
305 });
306
307 it('testEqualsSimple', function() {
308 var s1 = new proto.jspb.test.Simple1(['hi']);
309 assertTrue(jspb.Message.equals(s1, new proto.jspb.test.Simple1(['hi'])));
310 assertFalse(jspb.Message.equals(s1, new proto.jspb.test.Simple1(['bye'])));
311 var s1b = new proto.jspb.test.Simple1(['hi', ['hello']]);
312 assertTrue(jspb.Message.equals(s1b,
313 new proto.jspb.test.Simple1(['hi', ['hello']])));
314 assertTrue(jspb.Message.equals(s1b,
315 new proto.jspb.test.Simple1(['hi', ['hello', undefined,
316 undefined, undefined]])));
317 assertFalse(jspb.Message.equals(s1b,
318 new proto.jspb.test.Simple1(['no', ['hello']])));
319 // Test with messages of different types
320 var s2 = new proto.jspb.test.Simple2(['hi']);
321 assertFalse(jspb.Message.equals(s1, s2));
322 });
323
324 it('testEquals_softComparison', function() {
325 var s1 = new proto.jspb.test.Simple1(['hi', [], null]);
326 assertTrue(jspb.Message.equals(s1,
327 new proto.jspb.test.Simple1(['hi', []])));
328
329 var s1b = new proto.jspb.test.Simple1(['hi', [], true]);
330 assertTrue(jspb.Message.equals(s1b,
331 new proto.jspb.test.Simple1(['hi', [], 1])));
332 });
333
334 it('testEqualsComplex', function() {
335 var data1 = ['a',,, [, 11], [[, 22], [, 33]],, ['s1', 's2'],, 1];
336 var data2 = ['a',,, [, 11], [[, 22], [, 34]],, ['s1', 's2'],, 1];
337 var data3 = ['a',,, [, 11], [[, 22]],, ['s1', 's2'],, 1];
338 var data4 = ['hi'];
339 var c1a = new proto.jspb.test.Complex(data1);
340 var c1b = new proto.jspb.test.Complex(data1);
341 var c2 = new proto.jspb.test.Complex(data2);
342 var c3 = new proto.jspb.test.Complex(data3);
343 var s1 = new proto.jspb.test.Simple1(data4);
344
345 assertTrue(jspb.Message.equals(c1a, c1b));
346 assertFalse(jspb.Message.equals(c1a, c2));
347 assertFalse(jspb.Message.equals(c2, c3));
348 assertFalse(jspb.Message.equals(c1a, s1));
349 });
350
351 it('testEqualsExtensionsConstructed', function() {
352 assertTrue(jspb.Message.equals(
353 new proto.jspb.test.HasExtensions([]),
354 new proto.jspb.test.HasExtensions([{}])
355 ));
356 assertTrue(jspb.Message.equals(
357 new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'a'}]}]),
358 new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'a'}]}])
359 ));
360 assertFalse(jspb.Message.equals(
361 new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'a'}]}]),
362 new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'b'}]}])
363 ));
364 assertTrue(jspb.Message.equals(
365 new proto.jspb.test.HasExtensions([{100: [{200: 'a'}]}]),
366 new proto.jspb.test.HasExtensions([{100: [{200: 'a'}]}])
367 ));
368 assertTrue(jspb.Message.equals(
369 new proto.jspb.test.HasExtensions([{100: [{200: 'a'}]}]),
370 new proto.jspb.test.HasExtensions([,,, {100: [{200: 'a'}]}])
371 ));
372 assertTrue(jspb.Message.equals(
373 new proto.jspb.test.HasExtensions([,,, {100: [{200: 'a'}]}]),
374 new proto.jspb.test.HasExtensions([{100: [{200: 'a'}]}])
375 ));
376 assertTrue(jspb.Message.equals(
377 new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'a'}]}]),
378 new proto.jspb.test.HasExtensions(['hi',,, {100: [{200: 'a'}]}])
379 ));
380 assertTrue(jspb.Message.equals(
381 new proto.jspb.test.HasExtensions(['hi',,, {100: [{200: 'a'}]}]),
382 new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'a'}]}])
383 ));
384 });
385
386 it('testEqualsExtensionsUnconstructed', function() {
387 assertTrue(jspb.Message.compareFields([], [{}]));
388 assertTrue(jspb.Message.compareFields([,,, {}], []));
389 assertTrue(jspb.Message.compareFields([,,, {}], [,, {}]));
390 assertTrue(jspb.Message.compareFields(
391 ['hi', {100: [{200: 'a'}]}], ['hi', {100: [{200: 'a'}]}]));
392 assertFalse(jspb.Message.compareFields(
393 ['hi', {100: [{200: 'a'}]}], ['hi', {100: [{200: 'b'}]}]));
394 assertTrue(jspb.Message.compareFields(
395 [{100: [{200: 'a'}]}], [{100: [{200: 'a'}]}]));
396 assertTrue(jspb.Message.compareFields(
397 [{100: [{200: 'a'}]}], [,,, {100: [{200: 'a'}]}]));
398 assertTrue(jspb.Message.compareFields(
399 [,,, {100: [{200: 'a'}]}], [{100: [{200: 'a'}]}]));
400 assertTrue(jspb.Message.compareFields(
401 ['hi', {100: [{200: 'a'}]}], ['hi',,, {100: [{200: 'a'}]}]));
402 assertTrue(jspb.Message.compareFields(
403 ['hi',,, {100: [{200: 'a'}]}], ['hi', {100: [{200: 'a'}]}]));
404 });
405
406 it('testToMap', function() {
407 var p1 = new proto.jspb.test.Simple1(['k', ['v']]);
408 var p2 = new proto.jspb.test.Simple1(['k1', ['v1', 'v2']]);
409 var soymap = jspb.Message.toMap([p1, p2],
410 proto.jspb.test.Simple1.prototype.getAString,
411 proto.jspb.test.Simple1.prototype.toObject);
412 assertEquals('k', soymap['k'].aString);
413 assertArrayEquals(['v'], soymap['k'].aRepeatedStringList);
414 var protomap = jspb.Message.toMap([p1, p2],
415 proto.jspb.test.Simple1.prototype.getAString);
416 assertEquals('k', protomap['k'].getAString());
417 assertArrayEquals(['v'], protomap['k'].getARepeatedStringList());
418 });
419
420 it('testClone', function() {
421 var original = new proto.jspb.test.TestClone();
422 original.setStr('v1');
423 var simple1 = new proto.jspb.test.Simple1(['x1', ['y1', 'z1']]);
424 var simple2 = new proto.jspb.test.Simple1(['x2', ['y2', 'z2']]);
425 var simple3 = new proto.jspb.test.Simple1(['x3', ['y3', 'z3']]);
426 original.setSimple1(simple1);
427 original.setSimple2List([simple2, simple3]);
428 var extension = new proto.jspb.test.CloneExtension();
429 extension.setExt('e1');
430 original.setExtension(proto.jspb.test.IsExtension.extField, extension);
431 var clone = original.cloneMessage();
432 assertArrayEquals(['v1',, ['x1', ['y1', 'z1']],,
433 [['x2', ['y2', 'z2']], ['x3', ['y3', 'z3']]],,, { 100: [, 'e1'] }],
434 clone.toArray());
435 clone.setStr('v2');
436 var simple4 = new proto.jspb.test.Simple1(['a1', ['b1', 'c1']]);
437 var simple5 = new proto.jspb.test.Simple1(['a2', ['b2', 'c2']]);
438 var simple6 = new proto.jspb.test.Simple1(['a3', ['b3', 'c3']]);
439 clone.setSimple1(simple4);
440 clone.setSimple2List([simple5, simple6]);
441 var newExtension = new proto.jspb.test.CloneExtension();
442 newExtension.setExt('e2');
443 clone.setExtension(proto.jspb.test.CloneExtension.extField, newExtension);
444 assertArrayEquals(['v2',, ['a1', ['b1', 'c1']],,
445 [['a2', ['b2', 'c2']], ['a3', ['b3', 'c3']]],,, { 100: [, 'e2'] }],
446 clone.toArray());
447 assertArrayEquals(['v1',, ['x1', ['y1', 'z1']],,
448 [['x2', ['y2', 'z2']], ['x3', ['y3', 'z3']]],,, { 100: [, 'e1'] }],
449 original.toArray());
450 });
451
452 it('testCopyInto', function() {
453 var original = new proto.jspb.test.TestClone();
454 original.setStr('v1');
455 var dest = new proto.jspb.test.TestClone();
456 dest.setStr('override');
457 var simple1 = new proto.jspb.test.Simple1(['x1', ['y1', 'z1']]);
458 var simple2 = new proto.jspb.test.Simple1(['x2', ['y2', 'z2']]);
459 var simple3 = new proto.jspb.test.Simple1(['x3', ['y3', 'z3']]);
460 var destSimple1 = new proto.jspb.test.Simple1(['ox1', ['oy1', 'oz1']]);
461 var destSimple2 = new proto.jspb.test.Simple1(['ox2', ['oy2', 'oz2']]);
462 var destSimple3 = new proto.jspb.test.Simple1(['ox3', ['oy3', 'oz3']]);
463 original.setSimple1(simple1);
464 original.setSimple2List([simple2, simple3]);
465 dest.setSimple1(destSimple1);
466 dest.setSimple2List([destSimple2, destSimple3]);
467 var extension = new proto.jspb.test.CloneExtension();
468 extension.setExt('e1');
469 original.setExtension(proto.jspb.test.CloneExtension.extField, extension);
470
471 jspb.Message.copyInto(original, dest);
472 assertArrayEquals(original.toArray(), dest.toArray());
473 assertEquals('x1', dest.getSimple1().getAString());
474 assertEquals('e1',
475 dest.getExtension(proto.jspb.test.CloneExtension.extField).getExt());
476 dest.getSimple1().setAString('new value');
477 assertNotEquals(dest.getSimple1().getAString(),
478 original.getSimple1().getAString());
479 dest.getExtension(proto.jspb.test.CloneExtension.extField).
480 setExt('new value');
481 assertNotEquals(
482 dest.getExtension(proto.jspb.test.CloneExtension.extField).getExt(),
483 original.getExtension(
484 proto.jspb.test.CloneExtension.extField).getExt());
485 });
486
487 it('testCopyInto_notSameType', function() {
488 var a = new proto.jspb.test.TestClone();
489 var b = new proto.jspb.test.Simple1(['str', ['s1', 's2']]);
490
491 var e = assertThrows(function() {
492 jspb.Message.copyInto(a, b);
493 });
494 assertContains('should have the same type', e.message);
495 });
496
497 it('testExtensions', function() {
498 var extension1 = new proto.jspb.test.IsExtension(['ext1field']);
499 var extension2 = new proto.jspb.test.Simple1(['str', ['s1', 's2']]);
500 var extendable = new proto.jspb.test.HasExtensions(['v1', 'v2', 'v3']);
501 extendable.setExtension(proto.jspb.test.IsExtension.extField, extension1);
502 extendable.setExtension(proto.jspb.test.IndirectExtension.simple,
503 extension2);
504 extendable.setExtension(proto.jspb.test.IndirectExtension.str, 'xyzzy');
505 extendable.setExtension(proto.jspb.test.IndirectExtension.repeatedStrList,
506 ['a', 'b']);
507 var s1 = new proto.jspb.test.Simple1(['foo', ['s1', 's2']]);
508 var s2 = new proto.jspb.test.Simple1(['bar', ['t1', 't2']]);
509 extendable.setExtension(
510 proto.jspb.test.IndirectExtension.repeatedSimpleList,
511 [s1, s2]);
512 assertObjectEquals(extension1,
513 extendable.getExtension(proto.jspb.test.IsExtension.extField));
514 assertObjectEquals(extension2,
515 extendable.getExtension(proto.jspb.test.IndirectExtension.simple));
516 assertObjectEquals('xyzzy',
517 extendable.getExtension(proto.jspb.test.IndirectExtension.str));
518 assertObjectEquals(['a', 'b'], extendable.getExtension(
519 proto.jspb.test.IndirectExtension.repeatedStrList));
520 assertObjectEquals([s1, s2], extendable.getExtension(
521 proto.jspb.test.IndirectExtension.repeatedSimpleList));
522 // Not supported yet, but it should work...
523 extendable.setExtension(proto.jspb.test.IndirectExtension.simple, null);
524 assertNull(
525 extendable.getExtension(proto.jspb.test.IndirectExtension.simple));
526 extendable.setExtension(proto.jspb.test.IndirectExtension.str, null);
527 assertNull(extendable.getExtension(proto.jspb.test.IndirectExtension.str));
528
529 // These assertions will only work properly in uncompiled mode.
530 // Extension fields defined on proto2 Descriptor messages are filtered out.
531
532 // TODO(haberman): codegen changes to properly ignore descriptor.proto
533 // extensions need to be merged from google3.
534 // assertUndefined(proto.jspb.test.IsExtension['simpleOption']);
535
536 // Extension fields with jspb.ignore = true are ignored.
537 assertUndefined(proto.jspb.test.IndirectExtension['ignored']);
538 assertUndefined(proto.jspb.test.HasExtensions['ignoredFloating']);
539 });
540
541 it('testFloatingExtensions', function() {
542 // From an autogenerated container.
543 var extendable = new proto.jspb.test.HasExtensions(['v1', 'v2', 'v3']);
544 var extension = new proto.jspb.test.Simple1(['foo', ['s1', 's2']]);
545 extendable.setExtension(proto.jspb.test.simple1, extension);
546 assertObjectEquals(extension,
547 extendable.getExtension(proto.jspb.test.simple1));
548
549 // From _lib mode.
550 extension = new proto.jspb.test.ExtensionMessage(['s1']);
551 extendable = new proto.jspb.test.TestExtensionsMessage([16]);
552 extendable.setExtension(proto.jspb.test.floatingMsgField, extension);
553 extendable.setExtension(proto.jspb.test.floatingStrField, 's2');
554 assertObjectEquals(extension,
555 extendable.getExtension(proto.jspb.test.floatingMsgField));
556 assertObjectEquals('s2',
557 extendable.getExtension(proto.jspb.test.floatingStrField));
558 assertNotUndefined(proto.jspb.exttest.floatingMsgField);
559 assertNotUndefined(proto.jspb.exttest.floatingMsgFieldTwo);
560 assertNotUndefined(proto.jspb.exttest.beta.floatingStrField);
561 });
562
563 it('testToObject_extendedObject', function() {
564 var extension1 = new proto.jspb.test.IsExtension(['ext1field']);
565 var extension2 = new proto.jspb.test.Simple1(['str', ['s1', 's2'], true]);
566 var extendable = new proto.jspb.test.HasExtensions(['v1', 'v2', 'v3']);
567 extendable.setExtension(proto.jspb.test.IsExtension.extField, extension1);
568 extendable.setExtension(proto.jspb.test.IndirectExtension.simple,
569 extension2);
570 extendable.setExtension(proto.jspb.test.IndirectExtension.str, 'xyzzy');
571 extendable.setExtension(proto.jspb.test.IndirectExtension.repeatedStrList,
572 ['a', 'b']);
573 var s1 = new proto.jspb.test.Simple1(['foo', ['s1', 's2'], true]);
574 var s2 = new proto.jspb.test.Simple1(['bar', ['t1', 't2'], false]);
575 extendable.setExtension(
576 proto.jspb.test.IndirectExtension.repeatedSimpleList,
577 [s1, s2]);
578 assertObjectEquals({
579 str1: 'v1', str2: 'v2', str3: 'v3',
580 extField: { ext1: 'ext1field' },
581 simple: {
582 aString: 'str', aRepeatedStringList: ['s1', 's2'], aBoolean: true
583 },
584 str: 'xyzzy',
585 repeatedStrList: ['a', 'b'],
586 repeatedSimpleList: [
587 { aString: 'foo', aRepeatedStringList: ['s1', 's2'], aBoolean: true},
588 { aString: 'bar', aRepeatedStringList: ['t1', 't2'], aBoolean: false}
589 ]
590 }, extendable.toObject());
591
592 // Now, with instances included.
593 assertObjectEquals({
594 str1: 'v1', str2: 'v2', str3: 'v3',
595 extField: {
596 ext1: 'ext1field',
597 $jspbMessageInstance:
598 extendable.getExtension(proto.jspb.test.IsExtension.extField)
599 },
600 simple: {
601 aString: 'str',
602 aRepeatedStringList: ['s1', 's2'],
603 aBoolean: true,
604 $jspbMessageInstance:
605 extendable.getExtension(proto.jspb.test.IndirectExtension.simple)
606 },
607 str: 'xyzzy',
608 repeatedStrList: ['a', 'b'],
609 repeatedSimpleList: [{
610 aString: 'foo',
611 aRepeatedStringList: ['s1', 's2'],
612 aBoolean: true,
613 $jspbMessageInstance: s1
614 }, {
615 aString: 'bar',
616 aRepeatedStringList: ['t1', 't2'],
617 aBoolean: false,
618 $jspbMessageInstance: s2
619 }],
620 $jspbMessageInstance: extendable
621 }, extendable.toObject(true /* opt_includeInstance */));
622 });
623
624 it('testInitialization_emptyArray', function() {
625 var msg = new proto.jspb.test.HasExtensions([]);
626 if (jspb.Message.MINIMIZE_MEMORY_ALLOCATIONS) {
627 assertArrayEquals([], msg.toArray());
628 } else {
629 // Extension object is created past all regular fields.
630 assertArrayEquals([,,, {}], msg.toArray());
631 }
632 });
633
634 it('testInitialization_justExtensionObject', function() {
635 var msg = new proto.jspb.test.Empty([{1: 'hi'}]);
636 // The extensionObject is not moved from its original location.
637 assertArrayEquals([{1: 'hi'}], msg.toArray());
638 });
639
640 it('testInitialization_incompleteList', function() {
641 var msg = new proto.jspb.test.Empty([1, {4: 'hi'}]);
642 // The extensionObject is not moved from its original location.
643 assertArrayEquals([1, {4: 'hi'}], msg.toArray());
644 });
645
646 it('testInitialization_forwardCompatible', function() {
647 var msg = new proto.jspb.test.Empty([1, 2, 3, {1: 'hi'}]);
648 assertArrayEquals([1, 2, 3, {1: 'hi'}], msg.toArray());
649 });
650
651 it('testExtendedMessageEnsureObject', function() {
652 var data = new proto.jspb.test.HasExtensions(['str1',
653 {'a_key': 'an_object'}]);
654 assertEquals('an_object', data.extensionObject_['a_key']);
655 });
656
657 it('testToObject_hasExtensionField', function() {
658 var data = new proto.jspb.test.HasExtensions(['str1', {100: ['ext1']}]);
659 var obj = data.toObject();
660 assertEquals('str1', obj.str1);
661 assertEquals('ext1', obj.extField.ext1);
662 });
663
664 it('testGetExtension', function() {
665 var data = new proto.jspb.test.HasExtensions(['str1', {100: ['ext1']}]);
666 assertEquals('str1', data.getStr1());
667 var extension = data.getExtension(proto.jspb.test.IsExtension.extField);
668 assertNotNull(extension);
669 assertEquals('ext1', extension.getExt1());
670 });
671
672 it('testSetExtension', function() {
673 var data = new proto.jspb.test.HasExtensions();
674 var extensionMessage = new proto.jspb.test.IsExtension(['is_extension']);
675 data.setExtension(proto.jspb.test.IsExtension.extField, extensionMessage);
676 var obj = data.toObject();
677 assertNotNull(
678 data.getExtension(proto.jspb.test.IsExtension.extField));
679 assertEquals('is_extension', obj.extField.ext1);
680 });
681
682 /**
683 * Note that group is long deprecated, we only support it because JsPb has
684 * a goal of being able to generate JS classes for all proto descriptors.
685 */
686 it('testGroups', function() {
687 var group = new proto.jspb.test.TestGroup();
688 var someGroup = new proto.jspb.test.TestGroup.RepeatedGroup();
689 someGroup.setId('g1');
690 someGroup.setSomeBoolList([true, false]);
691 group.setRepeatedGroupList([someGroup]);
692 var groups = group.getRepeatedGroupList();
693 assertEquals('g1', groups[0].getId());
694 assertObjectEquals([true, false], groups[0].getSomeBoolList());
695 assertObjectEquals({id: 'g1', someBoolList: [true, false]},
696 groups[0].toObject());
697 assertObjectEquals({
698 repeatedGroupList: [{id: 'g1', someBoolList: [true, false]}],
699 requiredGroup: {id: undefined},
700 optionalGroup: undefined,
701 requiredSimple: {aRepeatedStringList: [], aString: undefined},
702 optionalSimple: undefined,
703 id: undefined
704 }, group.toObject());
705 var group1 = new proto.jspb.test.TestGroup1();
706 group1.setGroup(someGroup);
707 assertEquals(someGroup, group1.getGroup());
708 });
709
710 it('testNonExtensionFieldsAfterExtensionRange', function() {
711 var data = [{'1': 'a_string'}];
712 var message = new proto.jspb.test.Complex(data);
713 assertArrayEquals([], message.getARepeatedStringList());
714 });
715
716 it('testReservedGetterNames', function() {
717 var message = new proto.jspb.test.TestReservedNames();
718 message.setExtension$(11);
719 message.setExtension(proto.jspb.test.TestReservedNamesExtension.foo, 12);
720 assertEquals(11, message.getExtension$());
721 assertEquals(12, message.getExtension(
722 proto.jspb.test.TestReservedNamesExtension.foo));
723 assertObjectEquals({extension: 11, foo: 12}, message.toObject());
724 });
725
726 it('testInitializeMessageWithUnsetOneof', function() {
727 var message = new proto.jspb.test.TestMessageWithOneof([]);
728 assertEquals(
729 proto.jspb.test.TestMessageWithOneof.PartialOneofCase.
730 PARTIAL_ONEOF_NOT_SET,
731 message.getPartialOneofCase());
732 assertEquals(
733 proto.jspb.test.TestMessageWithOneof.RecursiveOneofCase.
734 RECURSIVE_ONEOF_NOT_SET,
735 message.getRecursiveOneofCase());
736 });
737
738 it('testInitializeMessageWithSingleValueSetInOneof', function() {
739 var message = new proto.jspb.test.TestMessageWithOneof([,, 'x']);
740
741 assertEquals('x', message.getPone());
742 assertUndefined(message.getPthree());
743 assertEquals(
744 proto.jspb.test.TestMessageWithOneof.PartialOneofCase.PONE,
745 message.getPartialOneofCase());
746 });
747
748 it('testKeepsLastWireValueSetInUnion_multipleValues', function() {
749 var message = new proto.jspb.test.TestMessageWithOneof([,, 'x',, 'y']);
750
751 assertUndefined('x', message.getPone());
752 assertEquals('y', message.getPthree());
753 assertEquals(
754 proto.jspb.test.TestMessageWithOneof.PartialOneofCase.PTHREE,
755 message.getPartialOneofCase());
756 });
757
758 it('testSettingOneofFieldClearsOthers', function() {
759 var message = new proto.jspb.test.TestMessageWithOneof;
760 assertUndefined(message.getPone());
761 assertUndefined(message.getPthree());
762
763 message.setPone('hi');
764 assertEquals('hi', message.getPone());
765 assertUndefined(message.getPthree());
766
767 message.setPthree('bye');
768 assertUndefined(message.getPone());
769 assertEquals('bye', message.getPthree());
770 });
771
772 it('testSettingOneofFieldDoesNotClearFieldsFromOtherUnions', function() {
773 var other = new proto.jspb.test.TestMessageWithOneof;
774 var message = new proto.jspb.test.TestMessageWithOneof;
775 assertUndefined(message.getPone());
776 assertUndefined(message.getPthree());
777 assertUndefined(message.getRone());
778
779 message.setPone('hi');
780 message.setRone(other);
781 assertEquals('hi', message.getPone());
782 assertUndefined(message.getPthree());
783 assertEquals(other, message.getRone());
784
785 message.setPthree('bye');
786 assertUndefined(message.getPone());
787 assertEquals('bye', message.getPthree());
788 assertEquals(other, message.getRone());
789 });
790
791 it('testUnsetsOneofCaseWhenFieldIsCleared', function() {
792 var message = new proto.jspb.test.TestMessageWithOneof;
793 assertEquals(
794 proto.jspb.test.TestMessageWithOneof.PartialOneofCase.
795 PARTIAL_ONEOF_NOT_SET,
796 message.getPartialOneofCase());
797
798 message.setPone('hi');
799 assertEquals(
800 proto.jspb.test.TestMessageWithOneof.PartialOneofCase.PONE,
801 message.getPartialOneofCase());
802
803 message.clearPone();
804 assertEquals(
805 proto.jspb.test.TestMessageWithOneof.PartialOneofCase.
806 PARTIAL_ONEOF_NOT_SET,
807 message.getPartialOneofCase());
808 });
809
810 it('testMessageWithDefaultOneofValues', function() {
811 var message = new proto.jspb.test.TestMessageWithOneof;
812 assertEquals(1234, message.getAone());
813 assertUndefined(message.getAtwo());
814 assertEquals(
815 proto.jspb.test.TestMessageWithOneof.DefaultOneofACase
816 .DEFAULT_ONEOF_A_NOT_SET,
817 message.getDefaultOneofACase());
818
819 message.setAone(567);
820 assertEquals(567, message.getAone());
821 assertUndefined(message.getAtwo());
822 assertEquals(
823 proto.jspb.test.TestMessageWithOneof.DefaultOneofACase.AONE,
824 message.getDefaultOneofACase());
825
826 message.setAtwo(890);
827 assertEquals(1234, message.getAone());
828 assertEquals(890, message.getAtwo());
829 assertEquals(
830 proto.jspb.test.TestMessageWithOneof.DefaultOneofACase.ATWO,
831 message.getDefaultOneofACase());
832
833 message.clearAtwo();
834 assertEquals(1234, message.getAone());
835 assertUndefined(message.getAtwo());
836 assertEquals(
837 proto.jspb.test.TestMessageWithOneof.DefaultOneofACase
838 .DEFAULT_ONEOF_A_NOT_SET,
839 message.getDefaultOneofACase());
840 });
841
842 it('testMessageWithDefaultOneofValues_defaultNotOnFirstField', function() {
843 var message = new proto.jspb.test.TestMessageWithOneof;
844 assertUndefined(message.getBone());
845 assertEquals(1234, message.getBtwo());
846 assertEquals(
847 proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase
848 .DEFAULT_ONEOF_B_NOT_SET,
849 message.getDefaultOneofBCase());
850
851 message.setBone(2);
852 assertEquals(2, message.getBone());
853 assertEquals(1234, message.getBtwo());
854 assertEquals(
855 proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase.BONE,
856 message.getDefaultOneofBCase());
857
858 message.setBtwo(3);
859 assertUndefined(message.getBone());
860 assertEquals(3, message.getBtwo());
861 assertEquals(
862 proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase.BTWO,
863 message.getDefaultOneofBCase());
864
865 message.clearBtwo();
866 assertUndefined(message.getBone());
867 assertEquals(1234, message.getBtwo());
868 assertEquals(
869 proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase
870 .DEFAULT_ONEOF_B_NOT_SET,
871 message.getDefaultOneofBCase());
872 });
873
874 it('testInitializeMessageWithOneofDefaults', function() {
875 var message =
876 new proto.jspb.test.TestMessageWithOneof(new Array(9).concat(567));
877 assertEquals(567, message.getAone());
878 assertUndefined(message.getAtwo());
879 assertEquals(
880 proto.jspb.test.TestMessageWithOneof.DefaultOneofACase.AONE,
881 message.getDefaultOneofACase());
882
883 message =
884 new proto.jspb.test.TestMessageWithOneof(new Array(10).concat(890));
885 assertEquals(1234, message.getAone());
886 assertEquals(890, message.getAtwo());
887 assertEquals(
888 proto.jspb.test.TestMessageWithOneof.DefaultOneofACase.ATWO,
889 message.getDefaultOneofACase());
890
891 message =
892 new proto.jspb.test.TestMessageWithOneof(new Array(9).concat(567,890));
893 assertEquals(1234, message.getAone());
894 assertEquals(890, message.getAtwo());
895 assertEquals(
896 proto.jspb.test.TestMessageWithOneof.DefaultOneofACase.ATWO,
897 message.getDefaultOneofACase());
898 });
899
900 it('testInitializeMessageWithOneofDefaults_defaultNotSetOnFirstField',
901 function() {
902 var message;
903
904 message =
905 new proto.jspb.test.TestMessageWithOneof(new Array(11).concat(567));
906 assertEquals(567, message.getBone());
907 assertEquals(1234, message.getBtwo());
908 assertEquals(
909 proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase.BONE,
910 message.getDefaultOneofBCase());
911
912 message =
913 new proto.jspb.test.TestMessageWithOneof(new Array(12).concat(890));
914 assertUndefined(message.getBone());
915 assertEquals(890, message.getBtwo());
916 assertEquals(
917 proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase.BTWO,
918 message.getDefaultOneofBCase());
919
920 message = new proto.jspb.test.TestMessageWithOneof(
921 new Array(11).concat(567,890));
922 assertUndefined(message.getBone());
923 assertEquals(890, message.getBtwo());
924 assertEquals(
925 proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase.BTWO,
926 message.getDefaultOneofBCase());
927 });
928
929 it('testOneofContainingAnotherMessage', function() {
930 var message = new proto.jspb.test.TestMessageWithOneof;
931 assertEquals(
932 proto.jspb.test.TestMessageWithOneof.RecursiveOneofCase.
933 RECURSIVE_ONEOF_NOT_SET,
934 message.getRecursiveOneofCase());
935
936 var other = new proto.jspb.test.TestMessageWithOneof;
937 message.setRone(other);
938 assertEquals(other, message.getRone());
939 assertUndefined(message.getRtwo());
940 assertEquals(
941 proto.jspb.test.TestMessageWithOneof.RecursiveOneofCase.RONE,
942 message.getRecursiveOneofCase());
943
944 message.setRtwo('hi');
945 assertUndefined(message.getRone());
946 assertEquals('hi', message.getRtwo());
947 assertEquals(
948 proto.jspb.test.TestMessageWithOneof.RecursiveOneofCase.RTWO,
949 message.getRecursiveOneofCase());
950 });
951
952 it('testQueryingOneofCaseEnsuresOnlyOneFieldIsSetInUnderlyingArray',
953 function() {
954 var message = new proto.jspb.test.TestMessageWithOneof;
955 message.setPone('x');
956 assertEquals('x', message.getPone());
957 assertUndefined(message.getPthree());
958 assertEquals(
959 proto.jspb.test.TestMessageWithOneof.PartialOneofCase.PONE,
960 message.getPartialOneofCase());
961
962 var array = message.toArray();
963 assertEquals('x', array[2]);
964 assertUndefined(array[4]);
965 array[4] = 'y';
966
967 assertEquals(
968 proto.jspb.test.TestMessageWithOneof.PartialOneofCase.PTHREE,
969 message.getPartialOneofCase());
970 assertUndefined(array[2]);
971 assertEquals('y', array[4]);
972 });
973
974 });
OLDNEW
« no previous file with comments | « third_party/protobuf/js/message.js ('k') | third_party/protobuf/js/node_loader.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698