message_test.js 42.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc.  All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// Test suite is written using Jasmine -- see http://jasmine.github.io/

goog.setTestOnly();

goog.require('goog.json');
36
goog.require('goog.testing.PropertyReplacer');
37
goog.require('goog.testing.asserts');
38
goog.require('goog.userAgent');
39

40
// CommonJS-LoadFromFile: google-protobuf jspb
41
goog.require('jspb.Message');
42

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
// CommonJS-LoadFromFile: test15_pb proto.jspb.filenametest.package1
goog.require('proto.jspb.filenametest.package1.b');

// CommonJS-LoadFromFile: test14_pb proto.jspb.filenametest.package2
goog.require('proto.jspb.filenametest.package2.TestMessage');

// CommonJS-LoadFromFile: test13_pb proto.jspb.filenametest.package1
goog.require('proto.jspb.filenametest.package1.a');
goog.require('proto.jspb.filenametest.package1.TestMessage');

// CommonJS-LoadFromFile: test12_pb proto.jspb.circulartest
goog.require('proto.jspb.circulartest.ExtensionContainingType1');
goog.require('proto.jspb.circulartest.ExtensionContainingType2');
goog.require('proto.jspb.circulartest.ExtensionField1');
goog.require('proto.jspb.circulartest.ExtensionField2');
goog.require('proto.jspb.circulartest.ExtensionField3');
goog.require('proto.jspb.circulartest.MapField1');
goog.require('proto.jspb.circulartest.MapField2');
goog.require('proto.jspb.circulartest.MessageField1');
goog.require('proto.jspb.circulartest.MessageField2');
goog.require('proto.jspb.circulartest.NestedEnum1');
goog.require('proto.jspb.circulartest.NestedEnum2');
goog.require('proto.jspb.circulartest.NestedMessage1');
goog.require('proto.jspb.circulartest.NestedMessage2');
goog.require('proto.jspb.circulartest.RepeatedMessageField1');
goog.require('proto.jspb.circulartest.RepeatedMessageField2');

// CommonJS-LoadFromFile: test11_pb proto.jspb.exttest.reverse
goog.require('proto.jspb.exttest.reverse.TestExtensionReverseOrderMessage1');
goog.require('proto.jspb.exttest.reverse.TestExtensionReverseOrderMessage2');
goog.require('proto.jspb.exttest.reverse.c');

75
// CommonJS-LoadFromFile: test8_pb proto.jspb.exttest.nested
76
goog.require('proto.jspb.exttest.nested.TestNestedExtensionsMessage');
77 78
goog.require('proto.jspb.exttest.nested.TestOuterMessage');

79
// CommonJS-LoadFromFile: test5_pb proto.jspb.exttest.beta
80
goog.require('proto.jspb.exttest.beta.floatingStrField');
81

82
// CommonJS-LoadFromFile: test3_pb proto.jspb.exttest
83
goog.require('proto.jspb.exttest.floatingMsgField');
84

85
// CommonJS-LoadFromFile: test4_pb proto.jspb.exttest
86
goog.require('proto.jspb.exttest.floatingMsgFieldTwo');
87

88
// CommonJS-LoadFromFile: test_pb proto.jspb.test
89
goog.require('proto.jspb.test.BooleanFields');
90 91 92 93 94
goog.require('proto.jspb.test.CloneExtension');
goog.require('proto.jspb.test.Complex');
goog.require('proto.jspb.test.DefaultValues');
goog.require('proto.jspb.test.Empty');
goog.require('proto.jspb.test.EnumContainer');
95 96
goog.require('proto.jspb.test.floatingMsgField');
goog.require('proto.jspb.test.FloatingPointFields');
97 98 99 100 101 102
goog.require('proto.jspb.test.floatingStrField');
goog.require('proto.jspb.test.HasExtensions');
goog.require('proto.jspb.test.IndirectExtension');
goog.require('proto.jspb.test.IsExtension');
goog.require('proto.jspb.test.OptionalFields');
goog.require('proto.jspb.test.OuterEnum');
103
goog.require('proto.jspb.test.OuterMessage.Complex');
104 105 106 107
goog.require('proto.jspb.test.Simple1');
goog.require('proto.jspb.test.Simple2');
goog.require('proto.jspb.test.SpecialCases');
goog.require('proto.jspb.test.TestClone');
108
goog.require('proto.jspb.test.TestCloneExtension');
109
goog.require('proto.jspb.test.TestEndsWithBytes');
110 111
goog.require('proto.jspb.test.TestGroup');
goog.require('proto.jspb.test.TestGroup1');
112
goog.require('proto.jspb.test.TestLastFieldBeforePivot');
113 114 115 116
goog.require('proto.jspb.test.TestMessageWithOneof');
goog.require('proto.jspb.test.TestReservedNames');
goog.require('proto.jspb.test.TestReservedNamesExtension');

117
// CommonJS-LoadFromFile: test2_pb proto.jspb.test
118 119
goog.require('proto.jspb.test.ExtensionMessage');
goog.require('proto.jspb.test.TestExtensionsMessage');
120

121
goog.require('proto.jspb.test.TestAllowAliasEnum');
122 123

describe('Message test suite', function() {
124 125 126 127 128 129 130 131 132 133
  var stubs = new goog.testing.PropertyReplacer();

  beforeEach(function() {
    stubs.set(jspb.Message, 'SERIALIZE_EMPTY_TRAILING_FIELDS', false);
  });

  afterEach(function() {
    stubs.reset();
  });

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
  it('testEmptyProto', function() {
    var empty1 = new proto.jspb.test.Empty([]);
    var empty2 = new proto.jspb.test.Empty([]);
    assertObjectEquals({}, empty1.toObject());
    assertObjectEquals('Message should not be corrupted:', empty2, empty1);
  });

  it('testTopLevelEnum', function() {
    var response = new proto.jspb.test.EnumContainer([]);
    response.setOuterEnum(proto.jspb.test.OuterEnum.FOO);
    assertEquals(proto.jspb.test.OuterEnum.FOO, response.getOuterEnum());
  });

  it('testByteStrings', function() {
    var data = new proto.jspb.test.DefaultValues([]);
    data.setBytesField('some_bytes');
    assertEquals('some_bytes', data.getBytesField());
  });

  it('testComplexConversion', function() {
154 155
    var data1 = ['a', , , [, 11], [[, 22], [, 33]], , ['s1', 's2'], , 1];
    var data2 = ['a', , , [, 11], [[, 22], [, 33]], , ['s1', 's2'], , 1];
156 157 158
    var foo = new proto.jspb.test.Complex(data1);
    var bar = new proto.jspb.test.Complex(data2);
    var result = foo.toObject();
159 160 161
    assertObjectEquals(
        {
          aString: 'a',
162
          anOutOfOrderBool: true,
163 164
          aNestedMessage: {anInt: 11},
          aRepeatedMessageList: [{anInt: 22}, {anInt: 33}],
165 166
          aRepeatedStringList: ['s1', 's2'],
          aFloatingPointField: undefined,
167 168
        },
        result);
169 170 171

    // Now test with the jspb instances included.
    result = foo.toObject(true /* opt_includeInstance */);
172 173 174
    assertObjectEquals(
        {
          aString: 'a',
175
          anOutOfOrderBool: true,
176 177 178 179 180 181 182
          aNestedMessage:
              {anInt: 11, $jspbMessageInstance: foo.getANestedMessage()},
          aRepeatedMessageList: [
            {anInt: 22, $jspbMessageInstance: foo.getARepeatedMessageList()[0]},
            {anInt: 33, $jspbMessageInstance: foo.getARepeatedMessageList()[1]}
          ],
          aRepeatedStringList: ['s1', 's2'],
183
          aFloatingPointField: undefined,
184 185 186
          $jspbMessageInstance: foo
        },
        result);
187 188 189 190 191

  });

  it('testMissingFields', function() {
    var foo = new proto.jspb.test.Complex([
192 193 194
      undefined, undefined, undefined, [], undefined, undefined, undefined,
      undefined
    ]);
195
    var bar = new proto.jspb.test.Complex([
196 197 198
      undefined, undefined, undefined, [], undefined, undefined, undefined,
      undefined
    ]);
199
    var result = foo.toObject();
200 201 202 203 204 205 206
    assertObjectEquals(
        {
          aString: undefined,
          anOutOfOrderBool: undefined,
          aNestedMessage: {anInt: undefined},
          // Note: JsPb converts undefined repeated fields to empty arrays.
          aRepeatedMessageList: [],
207 208
          aRepeatedStringList: [],
          aFloatingPointField: undefined,
209 210
        },
        result);
211 212 213

  });

214 215 216 217 218 219 220
  it('testNestedComplexMessage', function() {
    // Instantiate the message and set a unique field, just to ensure that we
    // are not getting jspb.test.Complex instead.
    var msg = new proto.jspb.test.OuterMessage.Complex();
    msg.setInnerComplexField(5);
  });

221 222 223
  it('testSpecialCases', function() {
    // Note: Some property names are reserved in JavaScript.
    // These names are converted to the Js property named pb_<reserved_name>.
224 225
    var special = new proto.jspb.test.SpecialCases(
        ['normal', 'default', 'function', 'var']);
226
    var result = special.toObject();
227 228 229 230 231 232 233 234
    assertObjectEquals(
        {
          normal: 'normal',
          pb_default: 'default',
          pb_function: 'function',
          pb_var: 'var'
        },
        result);
235 236 237
  });

  it('testDefaultValues', function() {
238
    var defaultString = 'default<>\'"abc';
239 240 241
    var response = new proto.jspb.test.DefaultValues();

    // Test toObject
242 243 244 245 246 247 248 249
    var expectedObject = {
      stringField: defaultString,
      boolField: true,
      intField: 11,
      enumField: 13,
      emptyField: '',
      bytesField: 'bW9v'
    };
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
    assertObjectEquals(expectedObject, response.toObject());


    // Test getters
    response = new proto.jspb.test.DefaultValues();
    assertEquals(defaultString, response.getStringField());
    assertEquals(true, response.getBoolField());
    assertEquals(11, response.getIntField());
    assertEquals(13, response.getEnumField());
    assertEquals('', response.getEmptyField());
    assertEquals('bW9v', response.getBytesField());

    function makeDefault(values) {
      return new proto.jspb.test.DefaultValues(values);
    }

    // Test with undefined values,
    // Use push to workaround IE treating undefined array elements as holes.
    response = makeDefault([undefined, undefined, undefined, undefined]);
    assertEquals(defaultString, response.getStringField());
    assertEquals(true, response.getBoolField());
    assertEquals(11, response.getIntField());
    assertEquals(13, response.getEnumField());
273 274 275 276
    assertFalse(response.hasStringField());
    assertFalse(response.hasBoolField());
    assertFalse(response.hasIntField());
    assertFalse(response.hasEnumField());
277 278 279 280 281 282 283

    // Test with null values, as would be returned by a JSON serializer.
    response = makeDefault([null, null, null, null]);
    assertEquals(defaultString, response.getStringField());
    assertEquals(true, response.getBoolField());
    assertEquals(11, response.getIntField());
    assertEquals(13, response.getEnumField());
284 285 286 287
    assertFalse(response.hasStringField());
    assertFalse(response.hasBoolField());
    assertFalse(response.hasIntField());
    assertFalse(response.hasEnumField());
288 289 290 291 292 293 294

    // Test with false-like values.
    response = makeDefault(['', false, 0, 0]);
    assertEquals('', response.getStringField());
    assertEquals(false, response.getBoolField());
    assertEquals(true, response.getIntField() == 0);
    assertEquals(true, response.getEnumField() == 0);
295 296 297 298
    assertTrue(response.hasStringField());
    assertTrue(response.hasBoolField());
    assertTrue(response.hasIntField());
    assertTrue(response.hasEnumField());
299 300 301

    // Test that clearing the values reverts them to the default state.
    response = makeDefault(['blah', false, 111, 77]);
302 303 304 305
    response.clearStringField();
    response.clearBoolField();
    response.clearIntField();
    response.clearEnumField();
306 307 308 309
    assertEquals(defaultString, response.getStringField());
    assertEquals(true, response.getBoolField());
    assertEquals(11, response.getIntField());
    assertEquals(13, response.getEnumField());
310 311 312 313
    assertFalse(response.hasStringField());
    assertFalse(response.hasBoolField());
    assertFalse(response.hasIntField());
    assertFalse(response.hasEnumField());
314 315 316

    // Test that setFoo(null) clears the values.
    response = makeDefault(['blah', false, 111, 77]);
317 318 319 320
    response.setStringField(null);
    response.setBoolField(null);
    response.setIntField(undefined);
    response.setEnumField(undefined);
321 322 323 324
    assertEquals(defaultString, response.getStringField());
    assertEquals(true, response.getBoolField());
    assertEquals(11, response.getIntField());
    assertEquals(13, response.getEnumField());
325 326 327 328
    assertFalse(response.hasStringField());
    assertFalse(response.hasBoolField());
    assertFalse(response.hasIntField());
    assertFalse(response.hasEnumField());
329 330 331 332 333 334 335
  });

  it('testEqualsSimple', function() {
    var s1 = new proto.jspb.test.Simple1(['hi']);
    assertTrue(jspb.Message.equals(s1, new proto.jspb.test.Simple1(['hi'])));
    assertFalse(jspb.Message.equals(s1, new proto.jspb.test.Simple1(['bye'])));
    var s1b = new proto.jspb.test.Simple1(['hi', ['hello']]);
336 337 338 339 340 341 342
    assertTrue(jspb.Message.equals(
        s1b, new proto.jspb.test.Simple1(['hi', ['hello']])));
    assertTrue(jspb.Message.equals(s1b, new proto.jspb.test.Simple1([
      'hi', ['hello', undefined, undefined, undefined]
    ])));
    assertFalse(jspb.Message.equals(
        s1b, new proto.jspb.test.Simple1(['no', ['hello']])));
343 344 345 346 347 348 349
    // Test with messages of different types
    var s2 = new proto.jspb.test.Simple2(['hi']);
    assertFalse(jspb.Message.equals(s1, s2));
  });

  it('testEquals_softComparison', function() {
    var s1 = new proto.jspb.test.Simple1(['hi', [], null]);
350 351
    assertTrue(
        jspb.Message.equals(s1, new proto.jspb.test.Simple1(['hi', []])));
352 353

    var s1b = new proto.jspb.test.Simple1(['hi', [], true]);
354 355
    assertTrue(
        jspb.Message.equals(s1b, new proto.jspb.test.Simple1(['hi', [], 1])));
356 357 358
  });

  it('testEqualsComplex', function() {
359 360 361
    var data1 = ['a', , , [, 11], [[, 22], [, 33]], , ['s1', 's2'], , 1];
    var data2 = ['a', , , [, 11], [[, 22], [, 34]], , ['s1', 's2'], , 1];
    var data3 = ['a', , , [, 11], [[, 22]], , ['s1', 's2'], , 1];
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
    var data4 = ['hi'];
    var c1a = new proto.jspb.test.Complex(data1);
    var c1b = new proto.jspb.test.Complex(data1);
    var c2 = new proto.jspb.test.Complex(data2);
    var c3 = new proto.jspb.test.Complex(data3);
    var s1 = new proto.jspb.test.Simple1(data4);

    assertTrue(jspb.Message.equals(c1a, c1b));
    assertFalse(jspb.Message.equals(c1a, c2));
    assertFalse(jspb.Message.equals(c2, c3));
    assertFalse(jspb.Message.equals(c1a, s1));
  });

  it('testEqualsExtensionsConstructed', function() {
    assertTrue(jspb.Message.equals(
        new proto.jspb.test.HasExtensions([]),
378
        new proto.jspb.test.HasExtensions([{}])));
379 380
    assertTrue(jspb.Message.equals(
        new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'a'}]}]),
381
        new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'a'}]}])));
382 383
    assertFalse(jspb.Message.equals(
        new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'a'}]}]),
384
        new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'b'}]}])));
385 386
    assertTrue(jspb.Message.equals(
        new proto.jspb.test.HasExtensions([{100: [{200: 'a'}]}]),
387
        new proto.jspb.test.HasExtensions([{100: [{200: 'a'}]}])));
388 389
    assertTrue(jspb.Message.equals(
        new proto.jspb.test.HasExtensions([{100: [{200: 'a'}]}]),
390
        new proto.jspb.test.HasExtensions([, , , {100: [{200: 'a'}]}])));
391
    assertTrue(jspb.Message.equals(
392 393
        new proto.jspb.test.HasExtensions([, , , {100: [{200: 'a'}]}]),
        new proto.jspb.test.HasExtensions([{100: [{200: 'a'}]}])));
394 395
    assertTrue(jspb.Message.equals(
        new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'a'}]}]),
396
        new proto.jspb.test.HasExtensions(['hi', , , {100: [{200: 'a'}]}])));
397
    assertTrue(jspb.Message.equals(
398 399
        new proto.jspb.test.HasExtensions(['hi', , , {100: [{200: 'a'}]}]),
        new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'a'}]}])));
400 401 402 403
  });

  it('testEqualsExtensionsUnconstructed', function() {
    assertTrue(jspb.Message.compareFields([], [{}]));
404 405
    assertTrue(jspb.Message.compareFields([, , , {}], []));
    assertTrue(jspb.Message.compareFields([, , , {}], [, , {}]));
406 407 408 409 410 411 412
    assertTrue(jspb.Message.compareFields(
        ['hi', {100: [{200: 'a'}]}], ['hi', {100: [{200: 'a'}]}]));
    assertFalse(jspb.Message.compareFields(
        ['hi', {100: [{200: 'a'}]}], ['hi', {100: [{200: 'b'}]}]));
    assertTrue(jspb.Message.compareFields(
        [{100: [{200: 'a'}]}], [{100: [{200: 'a'}]}]));
    assertTrue(jspb.Message.compareFields(
413
        [{100: [{200: 'a'}]}], [, , , {100: [{200: 'a'}]}]));
414
    assertTrue(jspb.Message.compareFields(
415
        [, , , {100: [{200: 'a'}]}], [{100: [{200: 'a'}]}]));
416
    assertTrue(jspb.Message.compareFields(
417
        ['hi', {100: [{200: 'a'}]}], ['hi', , , {100: [{200: 'a'}]}]));
418
    assertTrue(jspb.Message.compareFields(
419
        ['hi', , , {100: [{200: 'a'}]}], ['hi', {100: [{200: 'a'}]}]));
420 421
  });

422 423 424 425 426 427
  it('testInitializeMessageWithLastFieldNull', function() {
    // This tests for regression to bug http://b/117298778
    var msg = new proto.jspb.test.TestLastFieldBeforePivot([null]);
    assertNotUndefined(msg.getLastFieldBeforePivot());
  });

428 429 430 431 432 433 434 435 436 437 438 439
  it('testEqualsNonFinite', function() {
    assertTrue(jspb.Message.compareFields(NaN, NaN));
    assertTrue(jspb.Message.compareFields(NaN, 'NaN'));
    assertTrue(jspb.Message.compareFields('NaN', NaN));
    assertTrue(jspb.Message.compareFields(Infinity, Infinity));
    assertTrue(jspb.Message.compareFields(Infinity, 'Infinity'));
    assertTrue(jspb.Message.compareFields('-Infinity', -Infinity));
    assertTrue(jspb.Message.compareFields([NaN], ['NaN']));
    assertFalse(jspb.Message.compareFields(undefined, NaN));
    assertFalse(jspb.Message.compareFields(NaN, undefined));
  });

440 441 442
  it('testToMap', function() {
    var p1 = new proto.jspb.test.Simple1(['k', ['v']]);
    var p2 = new proto.jspb.test.Simple1(['k1', ['v1', 'v2']]);
443 444
    var soymap = jspb.Message.toMap(
        [p1, p2], proto.jspb.test.Simple1.prototype.getAString,
445 446 447
        proto.jspb.test.Simple1.prototype.toObject);
    assertEquals('k', soymap['k'].aString);
    assertArrayEquals(['v'], soymap['k'].aRepeatedStringList);
448 449
    var protomap = jspb.Message.toMap(
        [p1, p2], proto.jspb.test.Simple1.prototype.getAString);
450 451 452 453 454
    assertEquals('k', protomap['k'].getAString());
    assertArrayEquals(['v'], protomap['k'].getARepeatedStringList());
  });

  it('testClone', function() {
455 456
    var supportsUint8Array =
        !goog.userAgent.IE || goog.userAgent.isVersionOrHigher('10');
457 458 459 460 461 462 463
    var original = new proto.jspb.test.TestClone();
    original.setStr('v1');
    var simple1 = new proto.jspb.test.Simple1(['x1', ['y1', 'z1']]);
    var simple2 = new proto.jspb.test.Simple1(['x2', ['y2', 'z2']]);
    var simple3 = new proto.jspb.test.Simple1(['x3', ['y3', 'z3']]);
    original.setSimple1(simple1);
    original.setSimple2List([simple2, simple3]);
464 465
    var bytes1 = supportsUint8Array ? new Uint8Array([1, 2, 3]) : '123';
    original.setBytesField(bytes1);
466 467 468
    var extension = new proto.jspb.test.CloneExtension();
    extension.setExt('e1');
    original.setExtension(proto.jspb.test.IsExtension.extField, extension);
Bo Yang's avatar
Bo Yang committed
469
    var clone = original.clone();
470 471 472 473 474 475
    assertArrayEquals(
        [
          'v1', , ['x1', ['y1', 'z1']], ,
          [['x2', ['y2', 'z2']], ['x3', ['y3', 'z3']]], bytes1, ,
          {100: [, 'e1']}
        ],
476 477 478 479 480 481 482
        clone.toArray());
    clone.setStr('v2');
    var simple4 = new proto.jspb.test.Simple1(['a1', ['b1', 'c1']]);
    var simple5 = new proto.jspb.test.Simple1(['a2', ['b2', 'c2']]);
    var simple6 = new proto.jspb.test.Simple1(['a3', ['b3', 'c3']]);
    clone.setSimple1(simple4);
    clone.setSimple2List([simple5, simple6]);
483 484 485 486 487 488
    if (supportsUint8Array) {
      clone.getBytesField()[0] = 4;
      assertObjectEquals(bytes1, original.getBytesField());
    }
    var bytes2 = supportsUint8Array ? new Uint8Array([4, 5, 6]) : '456';
    clone.setBytesField(bytes2);
489 490 491
    var newExtension = new proto.jspb.test.CloneExtension();
    newExtension.setExt('e2');
    clone.setExtension(proto.jspb.test.CloneExtension.extField, newExtension);
492 493 494 495 496 497
    assertArrayEquals(
        [
          'v2', , ['a1', ['b1', 'c1']], ,
          [['a2', ['b2', 'c2']], ['a3', ['b3', 'c3']]], bytes2, ,
          {100: [, 'e2']}
        ],
498
        clone.toArray());
499 500 501 502 503 504
    assertArrayEquals(
        [
          'v1', , ['x1', ['y1', 'z1']], ,
          [['x2', ['y2', 'z2']], ['x3', ['y3', 'z3']]], bytes1, ,
          {100: [, 'e1']}
        ],
505 506 507 508
        original.toArray());
  });

  it('testCopyInto', function() {
509 510
    var supportsUint8Array =
        !goog.userAgent.IE || goog.userAgent.isVersionOrHigher('10');
511 512 513 514 515 516 517 518 519 520 521 522 523 524
    var original = new proto.jspb.test.TestClone();
    original.setStr('v1');
    var dest = new proto.jspb.test.TestClone();
    dest.setStr('override');
    var simple1 = new proto.jspb.test.Simple1(['x1', ['y1', 'z1']]);
    var simple2 = new proto.jspb.test.Simple1(['x2', ['y2', 'z2']]);
    var simple3 = new proto.jspb.test.Simple1(['x3', ['y3', 'z3']]);
    var destSimple1 = new proto.jspb.test.Simple1(['ox1', ['oy1', 'oz1']]);
    var destSimple2 = new proto.jspb.test.Simple1(['ox2', ['oy2', 'oz2']]);
    var destSimple3 = new proto.jspb.test.Simple1(['ox3', ['oy3', 'oz3']]);
    original.setSimple1(simple1);
    original.setSimple2List([simple2, simple3]);
    dest.setSimple1(destSimple1);
    dest.setSimple2List([destSimple2, destSimple3]);
525 526 527 528
    var bytes1 = supportsUint8Array ? new Uint8Array([1, 2, 3]) : '123';
    var bytes2 = supportsUint8Array ? new Uint8Array([4, 5, 6]) : '456';
    original.setBytesField(bytes1);
    dest.setBytesField(bytes2);
529 530 531 532 533 534 535
    var extension = new proto.jspb.test.CloneExtension();
    extension.setExt('e1');
    original.setExtension(proto.jspb.test.CloneExtension.extField, extension);

    jspb.Message.copyInto(original, dest);
    assertArrayEquals(original.toArray(), dest.toArray());
    assertEquals('x1', dest.getSimple1().getAString());
536 537
    assertEquals(
        'e1',
538 539
        dest.getExtension(proto.jspb.test.CloneExtension.extField).getExt());
    dest.getSimple1().setAString('new value');
540 541
    assertNotEquals(
        dest.getSimple1().getAString(), original.getSimple1().getAString());
542 543 544 545 546 547 548 549 550
    if (supportsUint8Array) {
      dest.getBytesField()[0] = 7;
      assertObjectEquals(bytes1, original.getBytesField());
      assertObjectEquals(new Uint8Array([7, 2, 3]), dest.getBytesField());
    } else {
      dest.setBytesField('789');
      assertObjectEquals(bytes1, original.getBytesField());
      assertObjectEquals('789', dest.getBytesField());
    }
551 552
    dest.getExtension(proto.jspb.test.CloneExtension.extField)
        .setExt('new value');
553 554
    assertNotEquals(
        dest.getExtension(proto.jspb.test.CloneExtension.extField).getExt(),
555 556
        original.getExtension(proto.jspb.test.CloneExtension.extField)
            .getExt());
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
  });

  it('testCopyInto_notSameType', function() {
    var a = new proto.jspb.test.TestClone();
    var b = new proto.jspb.test.Simple1(['str', ['s1', 's2']]);

    var e = assertThrows(function() {
      jspb.Message.copyInto(a, b);
    });
    assertContains('should have the same type', e.message);
  });

  it('testExtensions', function() {
    var extension1 = new proto.jspb.test.IsExtension(['ext1field']);
    var extension2 = new proto.jspb.test.Simple1(['str', ['s1', 's2']]);
    var extendable = new proto.jspb.test.HasExtensions(['v1', 'v2', 'v3']);
    extendable.setExtension(proto.jspb.test.IsExtension.extField, extension1);
574 575
    extendable.setExtension(
        proto.jspb.test.IndirectExtension.simple, extension2);
576
    extendable.setExtension(proto.jspb.test.IndirectExtension.str, 'xyzzy');
577 578
    extendable.setExtension(
        proto.jspb.test.IndirectExtension.repeatedStrList, ['a', 'b']);
579 580 581
    var s1 = new proto.jspb.test.Simple1(['foo', ['s1', 's2']]);
    var s2 = new proto.jspb.test.Simple1(['bar', ['t1', 't2']]);
    extendable.setExtension(
582 583 584
        proto.jspb.test.IndirectExtension.repeatedSimpleList, [s1, s2]);
    assertObjectEquals(
        extension1,
585
        extendable.getExtension(proto.jspb.test.IsExtension.extField));
586 587
    assertObjectEquals(
        extension2,
588
        extendable.getExtension(proto.jspb.test.IndirectExtension.simple));
589 590
    assertObjectEquals(
        'xyzzy',
591
        extendable.getExtension(proto.jspb.test.IndirectExtension.str));
592 593 594 595 596 597 598 599
    assertObjectEquals(
        ['a', 'b'],
        extendable.getExtension(
            proto.jspb.test.IndirectExtension.repeatedStrList));
    assertObjectEquals(
        [s1, s2],
        extendable.getExtension(
            proto.jspb.test.IndirectExtension.repeatedSimpleList));
600 601 602 603 604 605 606
    // Not supported yet, but it should work...
    extendable.setExtension(proto.jspb.test.IndirectExtension.simple, null);
    assertNull(
        extendable.getExtension(proto.jspb.test.IndirectExtension.simple));
    extendable.setExtension(proto.jspb.test.IndirectExtension.str, null);
    assertNull(extendable.getExtension(proto.jspb.test.IndirectExtension.str));

607

608 609 610 611 612 613 614 615 616 617
    // Extension fields with jspb.ignore = true are ignored.
    assertUndefined(proto.jspb.test.IndirectExtension['ignored']);
    assertUndefined(proto.jspb.test.HasExtensions['ignoredFloating']);
  });

  it('testFloatingExtensions', function() {
    // From an autogenerated container.
    var extendable = new proto.jspb.test.HasExtensions(['v1', 'v2', 'v3']);
    var extension = new proto.jspb.test.Simple1(['foo', ['s1', 's2']]);
    extendable.setExtension(proto.jspb.test.simple1, extension);
618 619
    assertObjectEquals(
        extension, extendable.getExtension(proto.jspb.test.simple1));
620 621 622 623 624 625

    // From _lib mode.
    extension = new proto.jspb.test.ExtensionMessage(['s1']);
    extendable = new proto.jspb.test.TestExtensionsMessage([16]);
    extendable.setExtension(proto.jspb.test.floatingMsgField, extension);
    extendable.setExtension(proto.jspb.test.floatingStrField, 's2');
626 627 628 629
    assertObjectEquals(
        extension, extendable.getExtension(proto.jspb.test.floatingMsgField));
    assertObjectEquals(
        's2', extendable.getExtension(proto.jspb.test.floatingStrField));
630 631 632 633 634
    assertNotUndefined(proto.jspb.exttest.floatingMsgField);
    assertNotUndefined(proto.jspb.exttest.floatingMsgFieldTwo);
    assertNotUndefined(proto.jspb.exttest.beta.floatingStrField);
  });

635
  it('testNestedExtensions', function() {
636 637 638 639 640 641 642 643 644 645 646
    var extendable =
        new proto.jspb.exttest.nested.TestNestedExtensionsMessage();
    var extension =
        new proto.jspb.exttest.nested.TestOuterMessage.NestedExtensionMessage(
            ['s1']);
    extendable.setExtension(
        proto.jspb.exttest.nested.TestOuterMessage.innerExtension, extension);
    assertObjectEquals(
        extension,
        extendable.getExtension(
            proto.jspb.exttest.nested.TestOuterMessage.innerExtension));
647 648
  });

649 650 651 652 653
  it('testToObject_extendedObject', function() {
    var extension1 = new proto.jspb.test.IsExtension(['ext1field']);
    var extension2 = new proto.jspb.test.Simple1(['str', ['s1', 's2'], true]);
    var extendable = new proto.jspb.test.HasExtensions(['v1', 'v2', 'v3']);
    extendable.setExtension(proto.jspb.test.IsExtension.extField, extension1);
654 655
    extendable.setExtension(
        proto.jspb.test.IndirectExtension.simple, extension2);
656
    extendable.setExtension(proto.jspb.test.IndirectExtension.str, 'xyzzy');
657 658
    extendable.setExtension(
        proto.jspb.test.IndirectExtension.repeatedStrList, ['a', 'b']);
659 660 661
    var s1 = new proto.jspb.test.Simple1(['foo', ['s1', 's2'], true]);
    var s2 = new proto.jspb.test.Simple1(['bar', ['t1', 't2'], false]);
    extendable.setExtension(
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
        proto.jspb.test.IndirectExtension.repeatedSimpleList, [s1, s2]);
    assertObjectEquals(
        {
          str1: 'v1',
          str2: 'v2',
          str3: 'v3',
          extField: {ext1: 'ext1field'},
          simple: {
            aString: 'str',
            aRepeatedStringList: ['s1', 's2'],
            aBoolean: true
          },
          str: 'xyzzy',
          repeatedStrList: ['a', 'b'],
          repeatedSimpleList: [
            {aString: 'foo', aRepeatedStringList: ['s1', 's2'], aBoolean: true},
            {aString: 'bar', aRepeatedStringList: ['t1', 't2'], aBoolean: false}
          ]
        },
        extendable.toObject());
682 683

    // Now, with instances included.
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
    assertObjectEquals(
        {
          str1: 'v1',
          str2: 'v2',
          str3: 'v3',
          extField: {
            ext1: 'ext1field',
            $jspbMessageInstance:
                extendable.getExtension(proto.jspb.test.IsExtension.extField)
          },
          simple: {
            aString: 'str',
            aRepeatedStringList: ['s1', 's2'],
            aBoolean: true,
            $jspbMessageInstance: extendable.getExtension(
                proto.jspb.test.IndirectExtension.simple)
          },
          str: 'xyzzy',
          repeatedStrList: ['a', 'b'],
          repeatedSimpleList: [
            {
              aString: 'foo',
              aRepeatedStringList: ['s1', 's2'],
              aBoolean: true,
              $jspbMessageInstance: s1
            },
            {
              aString: 'bar',
              aRepeatedStringList: ['t1', 't2'],
              aBoolean: false,
              $jspbMessageInstance: s2
            }
          ],
          $jspbMessageInstance: extendable
        },
        extendable.toObject(true /* opt_includeInstance */));
720 721 722 723
  });

  it('testInitialization_emptyArray', function() {
    var msg = new proto.jspb.test.HasExtensions([]);
724
    assertArrayEquals([], msg.toArray());
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
  });

  it('testInitialization_justExtensionObject', function() {
    var msg = new proto.jspb.test.Empty([{1: 'hi'}]);
    // The extensionObject is not moved from its original location.
    assertArrayEquals([{1: 'hi'}], msg.toArray());
  });

  it('testInitialization_incompleteList', function() {
    var msg = new proto.jspb.test.Empty([1, {4: 'hi'}]);
    // The extensionObject is not moved from its original location.
    assertArrayEquals([1, {4: 'hi'}], msg.toArray());
  });

  it('testInitialization_forwardCompatible', function() {
    var msg = new proto.jspb.test.Empty([1, 2, 3, {1: 'hi'}]);
    assertArrayEquals([1, 2, 3, {1: 'hi'}], msg.toArray());
  });

Bo Yang's avatar
Bo Yang committed
744 745 746 747 748 749
  it('testExtendedMessageEnsureObject',
     /** @suppress {visibility} */ function() {
       var data =
           new proto.jspb.test.HasExtensions(['str1', {'a_key': 'an_object'}]);
       assertEquals('an_object', data.extensionObject_['a_key']);
     });
750 751

  it('testToObject_hasExtensionField', function() {
752 753
    var data =
        new proto.jspb.test.HasExtensions(['str1', {100: ['ext1'], 102: ''}]);
754 755 756
    var obj = data.toObject();
    assertEquals('str1', obj.str1);
    assertEquals('ext1', obj.extField.ext1);
757
    assertEquals('', obj.str);
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
  });

  it('testGetExtension', function() {
    var data = new proto.jspb.test.HasExtensions(['str1', {100: ['ext1']}]);
    assertEquals('str1', data.getStr1());
    var extension = data.getExtension(proto.jspb.test.IsExtension.extField);
    assertNotNull(extension);
    assertEquals('ext1', extension.getExt1());
  });

  it('testSetExtension', function() {
    var data = new proto.jspb.test.HasExtensions();
    var extensionMessage = new proto.jspb.test.IsExtension(['is_extension']);
    data.setExtension(proto.jspb.test.IsExtension.extField, extensionMessage);
    var obj = data.toObject();
773
    assertNotNull(data.getExtension(proto.jspb.test.IsExtension.extField));
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
    assertEquals('is_extension', obj.extField.ext1);
  });

  /**
   * Note that group is long deprecated, we only support it because JsPb has
   * a goal of being able to generate JS classes for all proto descriptors.
   */
  it('testGroups', function() {
    var group = new proto.jspb.test.TestGroup();
    var someGroup = new proto.jspb.test.TestGroup.RepeatedGroup();
    someGroup.setId('g1');
    someGroup.setSomeBoolList([true, false]);
    group.setRepeatedGroupList([someGroup]);
    var groups = group.getRepeatedGroupList();
    assertEquals('g1', groups[0].getId());
    assertObjectEquals([true, false], groups[0].getSomeBoolList());
790 791 792 793 794 795 796 797 798 799 800 801
    assertObjectEquals(
        {id: 'g1', someBoolList: [true, false]}, groups[0].toObject());
    assertObjectEquals(
        {
          repeatedGroupList: [{id: 'g1', someBoolList: [true, false]}],
          requiredGroup: {id: undefined},
          optionalGroup: undefined,
          requiredSimple: {aRepeatedStringList: [], aString: undefined},
          optionalSimple: undefined,
          id: undefined
        },
        group.toObject());
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
    var group1 = new proto.jspb.test.TestGroup1();
    group1.setGroup(someGroup);
    assertEquals(someGroup, group1.getGroup());
  });

  it('testNonExtensionFieldsAfterExtensionRange', function() {
    var data = [{'1': 'a_string'}];
    var message = new proto.jspb.test.Complex(data);
    assertArrayEquals([], message.getARepeatedStringList());
  });

  it('testReservedGetterNames', function() {
    var message = new proto.jspb.test.TestReservedNames();
    message.setExtension$(11);
    message.setExtension(proto.jspb.test.TestReservedNamesExtension.foo, 12);
    assertEquals(11, message.getExtension$());
818 819 820
    assertEquals(
        12,
        message.getExtension(proto.jspb.test.TestReservedNamesExtension.foo));
821 822 823 824 825 826
    assertObjectEquals({extension: 11, foo: 12}, message.toObject());
  });

  it('testInitializeMessageWithUnsetOneof', function() {
    var message = new proto.jspb.test.TestMessageWithOneof([]);
    assertEquals(
827 828
        proto.jspb.test.TestMessageWithOneof.PartialOneofCase
            .PARTIAL_ONEOF_NOT_SET,
829 830
        message.getPartialOneofCase());
    assertEquals(
831 832
        proto.jspb.test.TestMessageWithOneof.RecursiveOneofCase
            .RECURSIVE_ONEOF_NOT_SET,
833 834 835 836 837 838
        message.getRecursiveOneofCase());
  });

  it('testUnsetsOneofCaseWhenFieldIsCleared', function() {
    var message = new proto.jspb.test.TestMessageWithOneof;
    assertEquals(
839 840
        proto.jspb.test.TestMessageWithOneof.PartialOneofCase
            .PARTIAL_ONEOF_NOT_SET,
841 842 843 844 845 846 847 848 849
        message.getPartialOneofCase());

    message.setPone('hi');
    assertEquals(
        proto.jspb.test.TestMessageWithOneof.PartialOneofCase.PONE,
        message.getPartialOneofCase());

    message.clearPone();
    assertEquals(
850 851
        proto.jspb.test.TestMessageWithOneof.PartialOneofCase
            .PARTIAL_ONEOF_NOT_SET,
852 853 854
        message.getPartialOneofCase());
  });

855 856
  it('testFloatingPointFieldsSupportNan', function() {
    var assertNan = function(x) {
857 858
      assertTrue(
          'Expected ' + x + ' (' + goog.typeOf(x) + ') to be NaN.',
859 860
          goog.isNumber(x) && isNaN(x));
    };
861

862
    var message = new proto.jspb.test.FloatingPointFields([
863
      'NaN', 'NaN', ['NaN', 'NaN'], 'NaN', 'NaN', 'NaN', ['NaN', 'NaN'], 'NaN'
864 865 866 867 868 869 870 871 872 873 874 875
    ]);
    assertNan(message.getOptionalFloatField());
    assertNan(message.getRequiredFloatField());
    assertNan(message.getRepeatedFloatFieldList()[0]);
    assertNan(message.getRepeatedFloatFieldList()[1]);
    assertNan(message.getDefaultFloatField());
    assertNan(message.getOptionalDoubleField());
    assertNan(message.getRequiredDoubleField());
    assertNan(message.getRepeatedDoubleFieldList()[0]);
    assertNan(message.getRepeatedDoubleFieldList()[1]);
    assertNan(message.getDefaultDoubleField());
  });
876

877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923
  it('testFloatingPointsAreConvertedFromStringInput', function() {
    var assertInf = function(x) {
      assertTrue(
          'Expected ' + x + ' (' + goog.typeOf(x) + ') to be Infinity.',
          x === Infinity);
    };
    var message = new proto.jspb.test.FloatingPointFields([
      Infinity, 'Infinity', ['Infinity', Infinity], 'Infinity', 'Infinity',
      'Infinity', ['Infinity', Infinity], 'Infinity'
    ]);
    assertInf(message.getOptionalFloatField());
    assertInf(message.getRequiredFloatField());
    assertInf(message.getRepeatedFloatFieldList()[0]);
    assertInf(message.getRepeatedFloatFieldList()[1]);
    assertInf(message.getDefaultFloatField());
    assertInf(message.getOptionalDoubleField());
    assertInf(message.getRequiredDoubleField());
    assertInf(message.getRepeatedDoubleFieldList()[0]);
    assertInf(message.getRepeatedDoubleFieldList()[1]);
    assertInf(message.getDefaultDoubleField());
  });

  it('testBooleansAreConvertedFromNumberInput', function() {
    var assertBooleanFieldTrue = function(x) {
      assertTrue(
          'Expected ' + x + ' (' + goog.typeOf(x) + ') to be True.',
          x === true);
    };
    var message = new proto.jspb.test.BooleanFields([1, 1, [true, 1]]);
    assertBooleanFieldTrue(message.getOptionalBooleanField());
    assertBooleanFieldTrue(message.getRequiredBooleanField());
    assertBooleanFieldTrue(message.getRepeatedBooleanFieldList()[0]);
    assertBooleanFieldTrue(message.getRepeatedBooleanFieldList()[1]);
    assertBooleanFieldTrue(message.getDefaultBooleanField());

    var assertBooleanFieldFalse = function(x) {
      assertTrue(
          'Expected ' + x + ' (' + goog.typeOf(x) + ') to be False.',
          x === false);
    };
    message = new proto.jspb.test.BooleanFields([0, 0, [0, 0]]);
    assertBooleanFieldFalse(message.getOptionalBooleanField());
    assertBooleanFieldFalse(message.getRequiredBooleanField());
    assertBooleanFieldFalse(message.getRepeatedBooleanFieldList()[0]);
    assertBooleanFieldFalse(message.getRepeatedBooleanFieldList()[1]);
  });

924 925 926
  it('testExtensionReverseOrder', function() {
    var message2 =
        new proto.jspb.exttest.reverse.TestExtensionReverseOrderMessage2;
927

928 929 930
    message2.setExtension(
        proto.jspb.exttest.reverse.TestExtensionReverseOrderMessage1.a, 233);
    message2.setExtension(
931 932 933
        proto.jspb.exttest.reverse.TestExtensionReverseOrderMessage1
            .TestExtensionReverseOrderNestedMessage1.b,
        2333);
934
    message2.setExtension(proto.jspb.exttest.reverse.c, 23333);
935 936

    assertEquals(
937 938 939
        233,
        message2.getExtension(
            proto.jspb.exttest.reverse.TestExtensionReverseOrderMessage1.a));
940
    assertEquals(
941 942
        2333,
        message2.getExtension(
943
            proto.jspb.exttest.reverse.TestExtensionReverseOrderMessage1
944
                .TestExtensionReverseOrderNestedMessage1.b));
945
    assertEquals(23333, message2.getExtension(proto.jspb.exttest.reverse.c));
946 947
  });

948 949 950 951 952
  it('testCircularDepsBaseOnMessageField', function() {
    var nestMessage1 = new proto.jspb.circulartest.MessageField1;
    var nestMessage2 = new proto.jspb.circulartest.MessageField2;
    var message1 = new proto.jspb.circulartest.MessageField1;
    var message2 = new proto.jspb.circulartest.MessageField2;
953

954 955 956 957
    nestMessage1.setA(1);
    nestMessage2.setA(2);
    message1.setB(nestMessage2);
    message2.setB(nestMessage1);
958

959 960 961

    assertEquals(2, message1.getB().getA());
    assertEquals(1, message2.getB().getA());
962 963 964
  });


965 966 967 968 969
  it('testCircularDepsBaseOnRepeatedMessageField', function() {
    var nestMessage1 = new proto.jspb.circulartest.RepeatedMessageField1;
    var nestMessage2 = new proto.jspb.circulartest.RepeatedMessageField2;
    var message1 = new proto.jspb.circulartest.RepeatedMessageField1;
    var message2 = new proto.jspb.circulartest.RepeatedMessageField2;
970

971 972 973 974 975 976 977 978
    nestMessage1.setA(1);
    nestMessage2.setA(2);
    message1.setB(nestMessage2);
    message2.addB(nestMessage1);


    assertEquals(2, message1.getB().getA());
    assertEquals(1, message2.getBList()[0].getA());
979 980
  });

981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
  it('testCircularDepsBaseOnMapField', function() {
    var nestMessage1 = new proto.jspb.circulartest.MapField1;
    var nestMessage2 = new proto.jspb.circulartest.MapField2;
    var message1 = new proto.jspb.circulartest.MapField1;
    var message2 = new proto.jspb.circulartest.MapField2;

    nestMessage1.setA(1);
    nestMessage2.setA(2);
    message1.setB(nestMessage2);
    message2.getBMap().set(1, nestMessage1);


    assertEquals(2, message1.getB().getA());
    assertEquals(1, message2.getBMap().get(1).getA());
  });

  it('testCircularDepsBaseOnNestedMessage', function() {
    var nestMessage1 =
        new proto.jspb.circulartest.NestedMessage1.NestedNestedMessage;
    var nestMessage2 = new proto.jspb.circulartest.NestedMessage2;
    var message1 = new proto.jspb.circulartest.NestedMessage1;
    var message2 = new proto.jspb.circulartest.NestedMessage2;

    nestMessage1.setA(1);
    nestMessage2.setA(2);
    message1.setB(nestMessage2);
    message2.setB(nestMessage1);


    assertEquals(2, message1.getB().getA());
    assertEquals(1, message2.getB().getA());
  });

  it('testCircularDepsBaseOnNestedEnum', function() {
    var nestMessage2 = new proto.jspb.circulartest.NestedEnum2;
    var message1 = new proto.jspb.circulartest.NestedEnum1;
    var message2 = new proto.jspb.circulartest.NestedEnum2;

    nestMessage2.setA(2);
    message1.setB(nestMessage2);
    message2.setB(proto.jspb.circulartest.NestedEnum1.NestedNestedEnum.VALUE_1);


    assertEquals(2, message1.getB().getA());
1025
    assertEquals(
1026 1027 1028 1029 1030 1031 1032
        proto.jspb.circulartest.NestedEnum1.NestedNestedEnum.VALUE_1,
        message2.getB());
  });

  it('testCircularDepsBaseOnExtensionContainingType', function() {
    var nestMessage2 = new proto.jspb.circulartest.ExtensionContainingType2;
    var message1 = new proto.jspb.circulartest.ExtensionContainingType1;
1033

1034 1035 1036 1037
    nestMessage2.setA(2);
    message1.setB(nestMessage2);
    message1.setExtension(
        proto.jspb.circulartest.ExtensionContainingType2.c, 1);
1038

1039 1040

    assertEquals(2, message1.getB().getA());
1041
    assertEquals(
1042 1043 1044
        1,
        message1.getExtension(
            proto.jspb.circulartest.ExtensionContainingType2.c));
1045 1046
  });

1047 1048 1049 1050
  it('testCircularDepsBaseOnExtensionField', function() {
    var nestMessage2 = new proto.jspb.circulartest.ExtensionField2;
    var message1 = new proto.jspb.circulartest.ExtensionField1;
    var message3 = new proto.jspb.circulartest.ExtensionField3;
1051

1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
    nestMessage2.setA(2);
    message1.setB(nestMessage2);
    message3.setExtension(proto.jspb.circulartest.ExtensionField2.c, message1);


    assertEquals(
        2,
        message3.getExtension(proto.jspb.circulartest.ExtensionField2.c)
            .getB()
            .getA());
  });

  it('testSameMessageNameOuputs', function() {
    var package1Message = new proto.jspb.filenametest.package1.TestMessage;
    var package2Message = new proto.jspb.filenametest.package2.TestMessage;

1068 1069
    package1Message.setExtension(proto.jspb.filenametest.package1.a, 10);
    package1Message.setExtension(proto.jspb.filenametest.package1.b, 11);
1070 1071
    package2Message.setA(12);

1072 1073 1074 1075
    assertEquals(
        10, package1Message.getExtension(proto.jspb.filenametest.package1.a));
    assertEquals(
        11, package1Message.getExtension(proto.jspb.filenametest.package1.b));
1076
    assertEquals(12, package2Message.getA());
1077 1078
  });

1079
});