message_test.js 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  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. // Test suite is written using Jasmine -- see http://jasmine.github.io/
  31. goog.setTestOnly();
  32. goog.require('goog.json');
  33. goog.require('goog.testing.PropertyReplacer');
  34. goog.require('goog.testing.asserts');
  35. goog.require('goog.userAgent');
  36. // CommonJS-LoadFromFile: google-protobuf jspb
  37. goog.require('jspb.Message');
  38. // CommonJS-LoadFromFile: test15_pb proto.jspb.filenametest.package1
  39. goog.require('proto.jspb.filenametest.package1.b');
  40. // CommonJS-LoadFromFile: test14_pb proto.jspb.filenametest.package2
  41. goog.require('proto.jspb.filenametest.package2.TestMessage');
  42. // CommonJS-LoadFromFile: test13_pb proto.jspb.filenametest.package1
  43. goog.require('proto.jspb.filenametest.package1.a');
  44. goog.require('proto.jspb.filenametest.package1.TestMessage');
  45. // CommonJS-LoadFromFile: test12_pb proto.jspb.circulartest
  46. goog.require('proto.jspb.circulartest.ExtensionContainingType1');
  47. goog.require('proto.jspb.circulartest.ExtensionContainingType2');
  48. goog.require('proto.jspb.circulartest.ExtensionField1');
  49. goog.require('proto.jspb.circulartest.ExtensionField2');
  50. goog.require('proto.jspb.circulartest.ExtensionField3');
  51. goog.require('proto.jspb.circulartest.MapField1');
  52. goog.require('proto.jspb.circulartest.MapField2');
  53. goog.require('proto.jspb.circulartest.MessageField1');
  54. goog.require('proto.jspb.circulartest.MessageField2');
  55. goog.require('proto.jspb.circulartest.NestedEnum1');
  56. goog.require('proto.jspb.circulartest.NestedEnum2');
  57. goog.require('proto.jspb.circulartest.NestedMessage1');
  58. goog.require('proto.jspb.circulartest.NestedMessage2');
  59. goog.require('proto.jspb.circulartest.RepeatedMessageField1');
  60. goog.require('proto.jspb.circulartest.RepeatedMessageField2');
  61. // CommonJS-LoadFromFile: test11_pb proto.jspb.exttest.reverse
  62. goog.require('proto.jspb.exttest.reverse.TestExtensionReverseOrderMessage1');
  63. goog.require('proto.jspb.exttest.reverse.TestExtensionReverseOrderMessage2');
  64. goog.require('proto.jspb.exttest.reverse.c');
  65. // CommonJS-LoadFromFile: test8_pb proto.jspb.exttest.nested
  66. goog.require('proto.jspb.exttest.nested.TestNestedExtensionsMessage');
  67. goog.require('proto.jspb.exttest.nested.TestOuterMessage');
  68. // CommonJS-LoadFromFile: test5_pb proto.jspb.exttest.beta
  69. goog.require('proto.jspb.exttest.beta.floatingStrField');
  70. // CommonJS-LoadFromFile: test3_pb proto.jspb.exttest
  71. goog.require('proto.jspb.exttest.floatingMsgField');
  72. // CommonJS-LoadFromFile: test4_pb proto.jspb.exttest
  73. goog.require('proto.jspb.exttest.floatingMsgFieldTwo');
  74. // CommonJS-LoadFromFile: test_pb proto.jspb.test
  75. goog.require('proto.jspb.test.CloneExtension');
  76. goog.require('proto.jspb.test.Complex');
  77. goog.require('proto.jspb.test.DefaultValues');
  78. goog.require('proto.jspb.test.Empty');
  79. goog.require('proto.jspb.test.EnumContainer');
  80. goog.require('proto.jspb.test.floatingMsgField');
  81. goog.require('proto.jspb.test.FloatingPointFields');
  82. goog.require('proto.jspb.test.floatingStrField');
  83. goog.require('proto.jspb.test.HasExtensions');
  84. goog.require('proto.jspb.test.IndirectExtension');
  85. goog.require('proto.jspb.test.IsExtension');
  86. goog.require('proto.jspb.test.OptionalFields');
  87. goog.require('proto.jspb.test.OuterEnum');
  88. goog.require('proto.jspb.test.OuterMessage.Complex');
  89. goog.require('proto.jspb.test.Simple1');
  90. goog.require('proto.jspb.test.Simple2');
  91. goog.require('proto.jspb.test.SpecialCases');
  92. goog.require('proto.jspb.test.TestClone');
  93. goog.require('proto.jspb.test.TestCloneExtension');
  94. goog.require('proto.jspb.test.TestEndsWithBytes');
  95. goog.require('proto.jspb.test.TestGroup');
  96. goog.require('proto.jspb.test.TestGroup1');
  97. goog.require('proto.jspb.test.TestLastFieldBeforePivot');
  98. goog.require('proto.jspb.test.TestMessageWithOneof');
  99. goog.require('proto.jspb.test.TestReservedNames');
  100. goog.require('proto.jspb.test.TestReservedNamesExtension');
  101. // CommonJS-LoadFromFile: test2_pb proto.jspb.test
  102. goog.require('proto.jspb.test.ExtensionMessage');
  103. goog.require('proto.jspb.test.TestExtensionsMessage');
  104. describe('Message test suite', function() {
  105. var stubs = new goog.testing.PropertyReplacer();
  106. beforeEach(function() {
  107. stubs.set(jspb.Message, 'SERIALIZE_EMPTY_TRAILING_FIELDS', false);
  108. });
  109. afterEach(function() {
  110. stubs.reset();
  111. });
  112. it('testEmptyProto', function() {
  113. var empty1 = new proto.jspb.test.Empty([]);
  114. var empty2 = new proto.jspb.test.Empty([]);
  115. assertObjectEquals({}, empty1.toObject());
  116. assertObjectEquals('Message should not be corrupted:', empty2, empty1);
  117. });
  118. it('testTopLevelEnum', function() {
  119. var response = new proto.jspb.test.EnumContainer([]);
  120. response.setOuterEnum(proto.jspb.test.OuterEnum.FOO);
  121. assertEquals(proto.jspb.test.OuterEnum.FOO, response.getOuterEnum());
  122. });
  123. it('testByteStrings', function() {
  124. var data = new proto.jspb.test.DefaultValues([]);
  125. data.setBytesField('some_bytes');
  126. assertEquals('some_bytes', data.getBytesField());
  127. });
  128. it('testComplexConversion', function() {
  129. var data1 = ['a',,, [, 11], [[, 22], [, 33]],, ['s1', 's2'],, 1];
  130. var data2 = ['a',,, [, 11], [[, 22], [, 33]],, ['s1', 's2'],, 1];
  131. var foo = new proto.jspb.test.Complex(data1);
  132. var bar = new proto.jspb.test.Complex(data2);
  133. var result = foo.toObject();
  134. assertObjectEquals({
  135. aString: 'a',
  136. anOutOfOrderBool: 1,
  137. aNestedMessage: {
  138. anInt: 11
  139. },
  140. aRepeatedMessageList: [{anInt: 22}, {anInt: 33}],
  141. aRepeatedStringList: ['s1', 's2']
  142. }, result);
  143. // Now test with the jspb instances included.
  144. result = foo.toObject(true /* opt_includeInstance */);
  145. assertObjectEquals({
  146. aString: 'a',
  147. anOutOfOrderBool: 1,
  148. aNestedMessage: {
  149. anInt: 11,
  150. $jspbMessageInstance: foo.getANestedMessage()
  151. },
  152. aRepeatedMessageList: [
  153. {anInt: 22, $jspbMessageInstance: foo.getARepeatedMessageList()[0]},
  154. {anInt: 33, $jspbMessageInstance: foo.getARepeatedMessageList()[1]}
  155. ],
  156. aRepeatedStringList: ['s1', 's2'],
  157. $jspbMessageInstance: foo
  158. }, result);
  159. });
  160. it('testMissingFields', function() {
  161. var foo = new proto.jspb.test.Complex([
  162. undefined, undefined, undefined, [],
  163. undefined, undefined, undefined, undefined]);
  164. var bar = new proto.jspb.test.Complex([
  165. undefined, undefined, undefined, [],
  166. undefined, undefined, undefined, undefined]);
  167. var result = foo.toObject();
  168. assertObjectEquals({
  169. aString: undefined,
  170. anOutOfOrderBool: undefined,
  171. aNestedMessage: {
  172. anInt: undefined
  173. },
  174. // Note: JsPb converts undefined repeated fields to empty arrays.
  175. aRepeatedMessageList: [],
  176. aRepeatedStringList: []
  177. }, result);
  178. });
  179. it('testNestedComplexMessage', function() {
  180. // Instantiate the message and set a unique field, just to ensure that we
  181. // are not getting jspb.test.Complex instead.
  182. var msg = new proto.jspb.test.OuterMessage.Complex();
  183. msg.setInnerComplexField(5);
  184. });
  185. it('testSpecialCases', function() {
  186. // Note: Some property names are reserved in JavaScript.
  187. // These names are converted to the Js property named pb_<reserved_name>.
  188. var special =
  189. new proto.jspb.test.SpecialCases(['normal', 'default', 'function',
  190. 'var']);
  191. var result = special.toObject();
  192. assertObjectEquals({
  193. normal: 'normal',
  194. pb_default: 'default',
  195. pb_function: 'function',
  196. pb_var: 'var'
  197. }, result);
  198. });
  199. it('testDefaultValues', function() {
  200. var defaultString = "default<>\'\"abc";
  201. var response = new proto.jspb.test.DefaultValues();
  202. // Test toObject
  203. var expectedObject = {
  204. stringField: defaultString,
  205. boolField: true,
  206. intField: 11,
  207. enumField: 13,
  208. emptyField: '',
  209. bytesField: 'bW9v'
  210. };
  211. assertObjectEquals(expectedObject, response.toObject());
  212. // Test getters
  213. response = new proto.jspb.test.DefaultValues();
  214. assertEquals(defaultString, response.getStringField());
  215. assertEquals(true, response.getBoolField());
  216. assertEquals(11, response.getIntField());
  217. assertEquals(13, response.getEnumField());
  218. assertEquals('', response.getEmptyField());
  219. assertEquals('bW9v', response.getBytesField());
  220. function makeDefault(values) {
  221. return new proto.jspb.test.DefaultValues(values);
  222. }
  223. // Test with undefined values,
  224. // Use push to workaround IE treating undefined array elements as holes.
  225. response = makeDefault([undefined, undefined, undefined, undefined]);
  226. assertEquals(defaultString, response.getStringField());
  227. assertEquals(true, response.getBoolField());
  228. assertEquals(11, response.getIntField());
  229. assertEquals(13, response.getEnumField());
  230. assertFalse(response.hasStringField());
  231. assertFalse(response.hasBoolField());
  232. assertFalse(response.hasIntField());
  233. assertFalse(response.hasEnumField());
  234. // Test with null values, as would be returned by a JSON serializer.
  235. response = makeDefault([null, null, null, null]);
  236. assertEquals(defaultString, response.getStringField());
  237. assertEquals(true, response.getBoolField());
  238. assertEquals(11, response.getIntField());
  239. assertEquals(13, response.getEnumField());
  240. assertFalse(response.hasStringField());
  241. assertFalse(response.hasBoolField());
  242. assertFalse(response.hasIntField());
  243. assertFalse(response.hasEnumField());
  244. // Test with false-like values.
  245. response = makeDefault(['', false, 0, 0]);
  246. assertEquals('', response.getStringField());
  247. assertEquals(false, response.getBoolField());
  248. assertEquals(true, response.getIntField() == 0);
  249. assertEquals(true, response.getEnumField() == 0);
  250. assertTrue(response.hasStringField());
  251. assertTrue(response.hasBoolField());
  252. assertTrue(response.hasIntField());
  253. assertTrue(response.hasEnumField());
  254. // Test that clearing the values reverts them to the default state.
  255. response = makeDefault(['blah', false, 111, 77]);
  256. response.clearStringField(); response.clearBoolField();
  257. response.clearIntField(); response.clearEnumField();
  258. assertEquals(defaultString, response.getStringField());
  259. assertEquals(true, response.getBoolField());
  260. assertEquals(11, response.getIntField());
  261. assertEquals(13, response.getEnumField());
  262. assertFalse(response.hasStringField());
  263. assertFalse(response.hasBoolField());
  264. assertFalse(response.hasIntField());
  265. assertFalse(response.hasEnumField());
  266. // Test that setFoo(null) clears the values.
  267. response = makeDefault(['blah', false, 111, 77]);
  268. response.setStringField(null); response.setBoolField(null);
  269. response.setIntField(undefined); response.setEnumField(undefined);
  270. assertEquals(defaultString, response.getStringField());
  271. assertEquals(true, response.getBoolField());
  272. assertEquals(11, response.getIntField());
  273. assertEquals(13, response.getEnumField());
  274. assertFalse(response.hasStringField());
  275. assertFalse(response.hasBoolField());
  276. assertFalse(response.hasIntField());
  277. assertFalse(response.hasEnumField());
  278. });
  279. it('testEqualsSimple', function() {
  280. var s1 = new proto.jspb.test.Simple1(['hi']);
  281. assertTrue(jspb.Message.equals(s1, new proto.jspb.test.Simple1(['hi'])));
  282. assertFalse(jspb.Message.equals(s1, new proto.jspb.test.Simple1(['bye'])));
  283. var s1b = new proto.jspb.test.Simple1(['hi', ['hello']]);
  284. assertTrue(jspb.Message.equals(s1b,
  285. new proto.jspb.test.Simple1(['hi', ['hello']])));
  286. assertTrue(jspb.Message.equals(s1b,
  287. new proto.jspb.test.Simple1(['hi', ['hello', undefined,
  288. undefined, undefined]])));
  289. assertFalse(jspb.Message.equals(s1b,
  290. new proto.jspb.test.Simple1(['no', ['hello']])));
  291. // Test with messages of different types
  292. var s2 = new proto.jspb.test.Simple2(['hi']);
  293. assertFalse(jspb.Message.equals(s1, s2));
  294. });
  295. it('testEquals_softComparison', function() {
  296. var s1 = new proto.jspb.test.Simple1(['hi', [], null]);
  297. assertTrue(jspb.Message.equals(s1,
  298. new proto.jspb.test.Simple1(['hi', []])));
  299. var s1b = new proto.jspb.test.Simple1(['hi', [], true]);
  300. assertTrue(jspb.Message.equals(s1b,
  301. new proto.jspb.test.Simple1(['hi', [], 1])));
  302. });
  303. it('testEqualsComplex', function() {
  304. var data1 = ['a',,, [, 11], [[, 22], [, 33]],, ['s1', 's2'],, 1];
  305. var data2 = ['a',,, [, 11], [[, 22], [, 34]],, ['s1', 's2'],, 1];
  306. var data3 = ['a',,, [, 11], [[, 22]],, ['s1', 's2'],, 1];
  307. var data4 = ['hi'];
  308. var c1a = new proto.jspb.test.Complex(data1);
  309. var c1b = new proto.jspb.test.Complex(data1);
  310. var c2 = new proto.jspb.test.Complex(data2);
  311. var c3 = new proto.jspb.test.Complex(data3);
  312. var s1 = new proto.jspb.test.Simple1(data4);
  313. assertTrue(jspb.Message.equals(c1a, c1b));
  314. assertFalse(jspb.Message.equals(c1a, c2));
  315. assertFalse(jspb.Message.equals(c2, c3));
  316. assertFalse(jspb.Message.equals(c1a, s1));
  317. });
  318. it('testEqualsExtensionsConstructed', function() {
  319. assertTrue(jspb.Message.equals(
  320. new proto.jspb.test.HasExtensions([]),
  321. new proto.jspb.test.HasExtensions([{}])
  322. ));
  323. assertTrue(jspb.Message.equals(
  324. new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'a'}]}]),
  325. new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'a'}]}])
  326. ));
  327. assertFalse(jspb.Message.equals(
  328. new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'a'}]}]),
  329. new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'b'}]}])
  330. ));
  331. assertTrue(jspb.Message.equals(
  332. new proto.jspb.test.HasExtensions([{100: [{200: 'a'}]}]),
  333. new proto.jspb.test.HasExtensions([{100: [{200: 'a'}]}])
  334. ));
  335. assertTrue(jspb.Message.equals(
  336. new proto.jspb.test.HasExtensions([{100: [{200: 'a'}]}]),
  337. new proto.jspb.test.HasExtensions([,,, {100: [{200: 'a'}]}])
  338. ));
  339. assertTrue(jspb.Message.equals(
  340. new proto.jspb.test.HasExtensions([,,, {100: [{200: 'a'}]}]),
  341. new proto.jspb.test.HasExtensions([{100: [{200: 'a'}]}])
  342. ));
  343. assertTrue(jspb.Message.equals(
  344. new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'a'}]}]),
  345. new proto.jspb.test.HasExtensions(['hi',,, {100: [{200: 'a'}]}])
  346. ));
  347. assertTrue(jspb.Message.equals(
  348. new proto.jspb.test.HasExtensions(['hi',,, {100: [{200: 'a'}]}]),
  349. new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'a'}]}])
  350. ));
  351. });
  352. it('testEqualsExtensionsUnconstructed', function() {
  353. assertTrue(jspb.Message.compareFields([], [{}]));
  354. assertTrue(jspb.Message.compareFields([,,, {}], []));
  355. assertTrue(jspb.Message.compareFields([,,, {}], [,, {}]));
  356. assertTrue(jspb.Message.compareFields(
  357. ['hi', {100: [{200: 'a'}]}], ['hi', {100: [{200: 'a'}]}]));
  358. assertFalse(jspb.Message.compareFields(
  359. ['hi', {100: [{200: 'a'}]}], ['hi', {100: [{200: 'b'}]}]));
  360. assertTrue(jspb.Message.compareFields(
  361. [{100: [{200: 'a'}]}], [{100: [{200: 'a'}]}]));
  362. assertTrue(jspb.Message.compareFields(
  363. [{100: [{200: 'a'}]}], [,,, {100: [{200: 'a'}]}]));
  364. assertTrue(jspb.Message.compareFields(
  365. [,,, {100: [{200: 'a'}]}], [{100: [{200: 'a'}]}]));
  366. assertTrue(jspb.Message.compareFields(
  367. ['hi', {100: [{200: 'a'}]}], ['hi',,, {100: [{200: 'a'}]}]));
  368. assertTrue(jspb.Message.compareFields(
  369. ['hi',,, {100: [{200: 'a'}]}], ['hi', {100: [{200: 'a'}]}]));
  370. });
  371. it('testInitializeMessageWithLastFieldNull', function() {
  372. // This tests for regression to bug http://b/117298778
  373. var msg = new proto.jspb.test.TestLastFieldBeforePivot([null]);
  374. assertNotUndefined(msg.getLastFieldBeforePivot());
  375. });
  376. it('testEqualsNonFinite', function() {
  377. assertTrue(jspb.Message.compareFields(NaN, NaN));
  378. assertTrue(jspb.Message.compareFields(NaN, 'NaN'));
  379. assertTrue(jspb.Message.compareFields('NaN', NaN));
  380. assertTrue(jspb.Message.compareFields(Infinity, Infinity));
  381. assertTrue(jspb.Message.compareFields(Infinity, 'Infinity'));
  382. assertTrue(jspb.Message.compareFields('-Infinity', -Infinity));
  383. assertTrue(jspb.Message.compareFields([NaN], ['NaN']));
  384. assertFalse(jspb.Message.compareFields(undefined, NaN));
  385. assertFalse(jspb.Message.compareFields(NaN, undefined));
  386. });
  387. it('testToMap', function() {
  388. var p1 = new proto.jspb.test.Simple1(['k', ['v']]);
  389. var p2 = new proto.jspb.test.Simple1(['k1', ['v1', 'v2']]);
  390. var soymap = jspb.Message.toMap([p1, p2],
  391. proto.jspb.test.Simple1.prototype.getAString,
  392. proto.jspb.test.Simple1.prototype.toObject);
  393. assertEquals('k', soymap['k'].aString);
  394. assertArrayEquals(['v'], soymap['k'].aRepeatedStringList);
  395. var protomap = jspb.Message.toMap([p1, p2],
  396. proto.jspb.test.Simple1.prototype.getAString);
  397. assertEquals('k', protomap['k'].getAString());
  398. assertArrayEquals(['v'], protomap['k'].getARepeatedStringList());
  399. });
  400. it('testClone', function() {
  401. var supportsUint8Array =
  402. !goog.userAgent.IE || goog.userAgent.isVersionOrHigher('10');
  403. var original = new proto.jspb.test.TestClone();
  404. original.setStr('v1');
  405. var simple1 = new proto.jspb.test.Simple1(['x1', ['y1', 'z1']]);
  406. var simple2 = new proto.jspb.test.Simple1(['x2', ['y2', 'z2']]);
  407. var simple3 = new proto.jspb.test.Simple1(['x3', ['y3', 'z3']]);
  408. original.setSimple1(simple1);
  409. original.setSimple2List([simple2, simple3]);
  410. var bytes1 = supportsUint8Array ? new Uint8Array([1, 2, 3]) : '123';
  411. original.setBytesField(bytes1);
  412. var extension = new proto.jspb.test.CloneExtension();
  413. extension.setExt('e1');
  414. original.setExtension(proto.jspb.test.IsExtension.extField, extension);
  415. var clone = original.clone();
  416. assertArrayEquals(['v1',, ['x1', ['y1', 'z1']],,
  417. [['x2', ['y2', 'z2']], ['x3', ['y3', 'z3']]], bytes1,, { 100: [, 'e1'] }],
  418. clone.toArray());
  419. clone.setStr('v2');
  420. var simple4 = new proto.jspb.test.Simple1(['a1', ['b1', 'c1']]);
  421. var simple5 = new proto.jspb.test.Simple1(['a2', ['b2', 'c2']]);
  422. var simple6 = new proto.jspb.test.Simple1(['a3', ['b3', 'c3']]);
  423. clone.setSimple1(simple4);
  424. clone.setSimple2List([simple5, simple6]);
  425. if (supportsUint8Array) {
  426. clone.getBytesField()[0] = 4;
  427. assertObjectEquals(bytes1, original.getBytesField());
  428. }
  429. var bytes2 = supportsUint8Array ? new Uint8Array([4, 5, 6]) : '456';
  430. clone.setBytesField(bytes2);
  431. var newExtension = new proto.jspb.test.CloneExtension();
  432. newExtension.setExt('e2');
  433. clone.setExtension(proto.jspb.test.CloneExtension.extField, newExtension);
  434. assertArrayEquals(['v2',, ['a1', ['b1', 'c1']],,
  435. [['a2', ['b2', 'c2']], ['a3', ['b3', 'c3']]], bytes2,, { 100: [, 'e2'] }],
  436. clone.toArray());
  437. assertArrayEquals(['v1',, ['x1', ['y1', 'z1']],,
  438. [['x2', ['y2', 'z2']], ['x3', ['y3', 'z3']]], bytes1,, { 100: [, 'e1'] }],
  439. original.toArray());
  440. });
  441. it('testCopyInto', function() {
  442. var supportsUint8Array =
  443. !goog.userAgent.IE || goog.userAgent.isVersionOrHigher('10');
  444. var original = new proto.jspb.test.TestClone();
  445. original.setStr('v1');
  446. var dest = new proto.jspb.test.TestClone();
  447. dest.setStr('override');
  448. var simple1 = new proto.jspb.test.Simple1(['x1', ['y1', 'z1']]);
  449. var simple2 = new proto.jspb.test.Simple1(['x2', ['y2', 'z2']]);
  450. var simple3 = new proto.jspb.test.Simple1(['x3', ['y3', 'z3']]);
  451. var destSimple1 = new proto.jspb.test.Simple1(['ox1', ['oy1', 'oz1']]);
  452. var destSimple2 = new proto.jspb.test.Simple1(['ox2', ['oy2', 'oz2']]);
  453. var destSimple3 = new proto.jspb.test.Simple1(['ox3', ['oy3', 'oz3']]);
  454. original.setSimple1(simple1);
  455. original.setSimple2List([simple2, simple3]);
  456. dest.setSimple1(destSimple1);
  457. dest.setSimple2List([destSimple2, destSimple3]);
  458. var bytes1 = supportsUint8Array ? new Uint8Array([1, 2, 3]) : '123';
  459. var bytes2 = supportsUint8Array ? new Uint8Array([4, 5, 6]) : '456';
  460. original.setBytesField(bytes1);
  461. dest.setBytesField(bytes2);
  462. var extension = new proto.jspb.test.CloneExtension();
  463. extension.setExt('e1');
  464. original.setExtension(proto.jspb.test.CloneExtension.extField, extension);
  465. jspb.Message.copyInto(original, dest);
  466. assertArrayEquals(original.toArray(), dest.toArray());
  467. assertEquals('x1', dest.getSimple1().getAString());
  468. assertEquals('e1',
  469. dest.getExtension(proto.jspb.test.CloneExtension.extField).getExt());
  470. dest.getSimple1().setAString('new value');
  471. assertNotEquals(dest.getSimple1().getAString(),
  472. original.getSimple1().getAString());
  473. if (supportsUint8Array) {
  474. dest.getBytesField()[0] = 7;
  475. assertObjectEquals(bytes1, original.getBytesField());
  476. assertObjectEquals(new Uint8Array([7, 2, 3]), dest.getBytesField());
  477. } else {
  478. dest.setBytesField('789');
  479. assertObjectEquals(bytes1, original.getBytesField());
  480. assertObjectEquals('789', dest.getBytesField());
  481. }
  482. dest.getExtension(proto.jspb.test.CloneExtension.extField).
  483. setExt('new value');
  484. assertNotEquals(
  485. dest.getExtension(proto.jspb.test.CloneExtension.extField).getExt(),
  486. original.getExtension(
  487. proto.jspb.test.CloneExtension.extField).getExt());
  488. });
  489. it('testCopyInto_notSameType', function() {
  490. var a = new proto.jspb.test.TestClone();
  491. var b = new proto.jspb.test.Simple1(['str', ['s1', 's2']]);
  492. var e = assertThrows(function() {
  493. jspb.Message.copyInto(a, b);
  494. });
  495. assertContains('should have the same type', e.message);
  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. // Extension fields with jspb.ignore = true are ignored.
  529. assertUndefined(proto.jspb.test.IndirectExtension['ignored']);
  530. assertUndefined(proto.jspb.test.HasExtensions['ignoredFloating']);
  531. });
  532. it('testFloatingExtensions', function() {
  533. // From an autogenerated container.
  534. var extendable = new proto.jspb.test.HasExtensions(['v1', 'v2', 'v3']);
  535. var extension = new proto.jspb.test.Simple1(['foo', ['s1', 's2']]);
  536. extendable.setExtension(proto.jspb.test.simple1, extension);
  537. assertObjectEquals(extension,
  538. extendable.getExtension(proto.jspb.test.simple1));
  539. // From _lib mode.
  540. extension = new proto.jspb.test.ExtensionMessage(['s1']);
  541. extendable = new proto.jspb.test.TestExtensionsMessage([16]);
  542. extendable.setExtension(proto.jspb.test.floatingMsgField, extension);
  543. extendable.setExtension(proto.jspb.test.floatingStrField, 's2');
  544. assertObjectEquals(extension,
  545. extendable.getExtension(proto.jspb.test.floatingMsgField));
  546. assertObjectEquals('s2',
  547. extendable.getExtension(proto.jspb.test.floatingStrField));
  548. assertNotUndefined(proto.jspb.exttest.floatingMsgField);
  549. assertNotUndefined(proto.jspb.exttest.floatingMsgFieldTwo);
  550. assertNotUndefined(proto.jspb.exttest.beta.floatingStrField);
  551. });
  552. it('testNestedExtensions', function() {
  553. var extendable = new proto.jspb.exttest.nested.TestNestedExtensionsMessage();
  554. var extension = new proto.jspb.exttest.nested.TestOuterMessage.NestedExtensionMessage(['s1']);
  555. extendable.setExtension(proto.jspb.exttest.nested.TestOuterMessage.innerExtension, extension);
  556. assertObjectEquals(extension,
  557. extendable.getExtension(proto.jspb.exttest.nested.TestOuterMessage.innerExtension));
  558. });
  559. it('testToObject_extendedObject', function() {
  560. var extension1 = new proto.jspb.test.IsExtension(['ext1field']);
  561. var extension2 = new proto.jspb.test.Simple1(['str', ['s1', 's2'], true]);
  562. var extendable = new proto.jspb.test.HasExtensions(['v1', 'v2', 'v3']);
  563. extendable.setExtension(proto.jspb.test.IsExtension.extField, extension1);
  564. extendable.setExtension(proto.jspb.test.IndirectExtension.simple,
  565. extension2);
  566. extendable.setExtension(proto.jspb.test.IndirectExtension.str, 'xyzzy');
  567. extendable.setExtension(proto.jspb.test.IndirectExtension.repeatedStrList,
  568. ['a', 'b']);
  569. var s1 = new proto.jspb.test.Simple1(['foo', ['s1', 's2'], true]);
  570. var s2 = new proto.jspb.test.Simple1(['bar', ['t1', 't2'], false]);
  571. extendable.setExtension(
  572. proto.jspb.test.IndirectExtension.repeatedSimpleList,
  573. [s1, s2]);
  574. assertObjectEquals({
  575. str1: 'v1', str2: 'v2', str3: 'v3',
  576. extField: { ext1: 'ext1field' },
  577. simple: {
  578. aString: 'str', aRepeatedStringList: ['s1', 's2'], aBoolean: true
  579. },
  580. str: 'xyzzy',
  581. repeatedStrList: ['a', 'b'],
  582. repeatedSimpleList: [
  583. { aString: 'foo', aRepeatedStringList: ['s1', 's2'], aBoolean: true},
  584. { aString: 'bar', aRepeatedStringList: ['t1', 't2'], aBoolean: false}
  585. ]
  586. }, extendable.toObject());
  587. // Now, with instances included.
  588. assertObjectEquals({
  589. str1: 'v1', str2: 'v2', str3: 'v3',
  590. extField: {
  591. ext1: 'ext1field',
  592. $jspbMessageInstance:
  593. extendable.getExtension(proto.jspb.test.IsExtension.extField)
  594. },
  595. simple: {
  596. aString: 'str',
  597. aRepeatedStringList: ['s1', 's2'],
  598. aBoolean: true,
  599. $jspbMessageInstance:
  600. extendable.getExtension(proto.jspb.test.IndirectExtension.simple)
  601. },
  602. str: 'xyzzy',
  603. repeatedStrList: ['a', 'b'],
  604. repeatedSimpleList: [{
  605. aString: 'foo',
  606. aRepeatedStringList: ['s1', 's2'],
  607. aBoolean: true,
  608. $jspbMessageInstance: s1
  609. }, {
  610. aString: 'bar',
  611. aRepeatedStringList: ['t1', 't2'],
  612. aBoolean: false,
  613. $jspbMessageInstance: s2
  614. }],
  615. $jspbMessageInstance: extendable
  616. }, extendable.toObject(true /* opt_includeInstance */));
  617. });
  618. it('testInitialization_emptyArray', function() {
  619. var msg = new proto.jspb.test.HasExtensions([]);
  620. assertArrayEquals([], msg.toArray());
  621. });
  622. it('testInitialization_justExtensionObject', function() {
  623. var msg = new proto.jspb.test.Empty([{1: 'hi'}]);
  624. // The extensionObject is not moved from its original location.
  625. assertArrayEquals([{1: 'hi'}], msg.toArray());
  626. });
  627. it('testInitialization_incompleteList', function() {
  628. var msg = new proto.jspb.test.Empty([1, {4: 'hi'}]);
  629. // The extensionObject is not moved from its original location.
  630. assertArrayEquals([1, {4: 'hi'}], msg.toArray());
  631. });
  632. it('testInitialization_forwardCompatible', function() {
  633. var msg = new proto.jspb.test.Empty([1, 2, 3, {1: 'hi'}]);
  634. assertArrayEquals([1, 2, 3, {1: 'hi'}], msg.toArray());
  635. });
  636. it('testExtendedMessageEnsureObject',
  637. /** @suppress {visibility} */ function() {
  638. var data =
  639. new proto.jspb.test.HasExtensions(['str1', {'a_key': 'an_object'}]);
  640. assertEquals('an_object', data.extensionObject_['a_key']);
  641. });
  642. it('testToObject_hasExtensionField', function() {
  643. var data = new proto.jspb.test.HasExtensions(['str1', {100: ['ext1'], 102: ''}]);
  644. var obj = data.toObject();
  645. assertEquals('str1', obj.str1);
  646. assertEquals('ext1', obj.extField.ext1);
  647. assertEquals('', obj.str);
  648. });
  649. it('testGetExtension', function() {
  650. var data = new proto.jspb.test.HasExtensions(['str1', {100: ['ext1']}]);
  651. assertEquals('str1', data.getStr1());
  652. var extension = data.getExtension(proto.jspb.test.IsExtension.extField);
  653. assertNotNull(extension);
  654. assertEquals('ext1', extension.getExt1());
  655. });
  656. it('testSetExtension', function() {
  657. var data = new proto.jspb.test.HasExtensions();
  658. var extensionMessage = new proto.jspb.test.IsExtension(['is_extension']);
  659. data.setExtension(proto.jspb.test.IsExtension.extField, extensionMessage);
  660. var obj = data.toObject();
  661. assertNotNull(
  662. data.getExtension(proto.jspb.test.IsExtension.extField));
  663. assertEquals('is_extension', obj.extField.ext1);
  664. });
  665. /**
  666. * Note that group is long deprecated, we only support it because JsPb has
  667. * a goal of being able to generate JS classes for all proto descriptors.
  668. */
  669. it('testGroups', function() {
  670. var group = new proto.jspb.test.TestGroup();
  671. var someGroup = new proto.jspb.test.TestGroup.RepeatedGroup();
  672. someGroup.setId('g1');
  673. someGroup.setSomeBoolList([true, false]);
  674. group.setRepeatedGroupList([someGroup]);
  675. var groups = group.getRepeatedGroupList();
  676. assertEquals('g1', groups[0].getId());
  677. assertObjectEquals([true, false], groups[0].getSomeBoolList());
  678. assertObjectEquals({id: 'g1', someBoolList: [true, false]},
  679. groups[0].toObject());
  680. assertObjectEquals({
  681. repeatedGroupList: [{id: 'g1', someBoolList: [true, false]}],
  682. requiredGroup: {id: undefined},
  683. optionalGroup: undefined,
  684. requiredSimple: {aRepeatedStringList: [], aString: undefined},
  685. optionalSimple: undefined,
  686. id: undefined
  687. }, group.toObject());
  688. var group1 = new proto.jspb.test.TestGroup1();
  689. group1.setGroup(someGroup);
  690. assertEquals(someGroup, group1.getGroup());
  691. });
  692. it('testNonExtensionFieldsAfterExtensionRange', function() {
  693. var data = [{'1': 'a_string'}];
  694. var message = new proto.jspb.test.Complex(data);
  695. assertArrayEquals([], message.getARepeatedStringList());
  696. });
  697. it('testReservedGetterNames', function() {
  698. var message = new proto.jspb.test.TestReservedNames();
  699. message.setExtension$(11);
  700. message.setExtension(proto.jspb.test.TestReservedNamesExtension.foo, 12);
  701. assertEquals(11, message.getExtension$());
  702. assertEquals(12, message.getExtension(
  703. proto.jspb.test.TestReservedNamesExtension.foo));
  704. assertObjectEquals({extension: 11, foo: 12}, message.toObject());
  705. });
  706. it('testInitializeMessageWithUnsetOneof', function() {
  707. var message = new proto.jspb.test.TestMessageWithOneof([]);
  708. assertEquals(
  709. proto.jspb.test.TestMessageWithOneof.PartialOneofCase.
  710. PARTIAL_ONEOF_NOT_SET,
  711. message.getPartialOneofCase());
  712. assertEquals(
  713. proto.jspb.test.TestMessageWithOneof.RecursiveOneofCase.
  714. RECURSIVE_ONEOF_NOT_SET,
  715. message.getRecursiveOneofCase());
  716. });
  717. it('testUnsetsOneofCaseWhenFieldIsCleared', function() {
  718. var message = new proto.jspb.test.TestMessageWithOneof;
  719. assertEquals(
  720. proto.jspb.test.TestMessageWithOneof.PartialOneofCase.
  721. PARTIAL_ONEOF_NOT_SET,
  722. message.getPartialOneofCase());
  723. message.setPone('hi');
  724. assertEquals(
  725. proto.jspb.test.TestMessageWithOneof.PartialOneofCase.PONE,
  726. message.getPartialOneofCase());
  727. message.clearPone();
  728. assertEquals(
  729. proto.jspb.test.TestMessageWithOneof.PartialOneofCase.
  730. PARTIAL_ONEOF_NOT_SET,
  731. message.getPartialOneofCase());
  732. });
  733. it('testFloatingPointFieldsSupportNan', function() {
  734. var assertNan = function(x) {
  735. assertTrue('Expected ' + x + ' (' + goog.typeOf(x) + ') to be NaN.',
  736. goog.isNumber(x) && isNaN(x));
  737. };
  738. var message = new proto.jspb.test.FloatingPointFields([
  739. 'NaN', 'NaN', ['NaN', 'NaN'], 'NaN',
  740. 'NaN', 'NaN', ['NaN', 'NaN'], 'NaN'
  741. ]);
  742. assertNan(message.getOptionalFloatField());
  743. assertNan(message.getRequiredFloatField());
  744. assertNan(message.getRepeatedFloatFieldList()[0]);
  745. assertNan(message.getRepeatedFloatFieldList()[1]);
  746. assertNan(message.getDefaultFloatField());
  747. assertNan(message.getOptionalDoubleField());
  748. assertNan(message.getRequiredDoubleField());
  749. assertNan(message.getRepeatedDoubleFieldList()[0]);
  750. assertNan(message.getRepeatedDoubleFieldList()[1]);
  751. assertNan(message.getDefaultDoubleField());
  752. });
  753. it('testExtensionReverseOrder', function() {
  754. var message2 =
  755. new proto.jspb.exttest.reverse.TestExtensionReverseOrderMessage2;
  756. message2.setExtension(
  757. proto.jspb.exttest.reverse.TestExtensionReverseOrderMessage1.a, 233);
  758. message2.setExtension(
  759. proto
  760. .jspb
  761. .exttest
  762. .reverse
  763. .TestExtensionReverseOrderMessage1
  764. .TestExtensionReverseOrderNestedMessage1.b, 2333);
  765. message2.setExtension(proto.jspb.exttest.reverse.c, 23333);
  766. assertEquals(
  767. 233,
  768. message2.getExtension(
  769. proto.jspb.exttest.reverse.TestExtensionReverseOrderMessage1.a));
  770. assertEquals(
  771. 2333,
  772. message2.getExtension(
  773. proto
  774. .jspb
  775. .exttest
  776. .reverse
  777. .TestExtensionReverseOrderMessage1
  778. .TestExtensionReverseOrderNestedMessage1.b));
  779. assertEquals(
  780. 23333,
  781. message2.getExtension(proto.jspb.exttest.reverse.c));
  782. });
  783. it('testCircularDepsBaseOnMessageField', function() {
  784. var nestMessage1 = new proto.jspb.circulartest.MessageField1;
  785. var nestMessage2 = new proto.jspb.circulartest.MessageField2;
  786. var message1 = new proto.jspb.circulartest.MessageField1;
  787. var message2 = new proto.jspb.circulartest.MessageField2;
  788. nestMessage1.setA(1);
  789. nestMessage2.setA(2);
  790. message1.setB(nestMessage2);
  791. message2.setB(nestMessage1);
  792. assertEquals(2, message1.getB().getA());
  793. assertEquals(1, message2.getB().getA());
  794. });
  795. it('testCircularDepsBaseOnRepeatedMessageField', function() {
  796. var nestMessage1 = new proto.jspb.circulartest.RepeatedMessageField1;
  797. var nestMessage2 = new proto.jspb.circulartest.RepeatedMessageField2;
  798. var message1 = new proto.jspb.circulartest.RepeatedMessageField1;
  799. var message2 = new proto.jspb.circulartest.RepeatedMessageField2;
  800. nestMessage1.setA(1);
  801. nestMessage2.setA(2);
  802. message1.setB(nestMessage2);
  803. message2.addB(nestMessage1);
  804. assertEquals(2, message1.getB().getA());
  805. assertEquals(1, message2.getBList()[0].getA());
  806. });
  807. it('testCircularDepsBaseOnMapField', function() {
  808. var nestMessage1 = new proto.jspb.circulartest.MapField1;
  809. var nestMessage2 = new proto.jspb.circulartest.MapField2;
  810. var message1 = new proto.jspb.circulartest.MapField1;
  811. var message2 = new proto.jspb.circulartest.MapField2;
  812. nestMessage1.setA(1);
  813. nestMessage2.setA(2);
  814. message1.setB(nestMessage2);
  815. message2.getBMap().set(1, nestMessage1);
  816. assertEquals(2, message1.getB().getA());
  817. assertEquals(1, message2.getBMap().get(1).getA());
  818. });
  819. it('testCircularDepsBaseOnNestedMessage', function() {
  820. var nestMessage1 =
  821. new proto.jspb.circulartest.NestedMessage1.NestedNestedMessage;
  822. var nestMessage2 = new proto.jspb.circulartest.NestedMessage2;
  823. var message1 = new proto.jspb.circulartest.NestedMessage1;
  824. var message2 = new proto.jspb.circulartest.NestedMessage2;
  825. nestMessage1.setA(1);
  826. nestMessage2.setA(2);
  827. message1.setB(nestMessage2);
  828. message2.setB(nestMessage1);
  829. assertEquals(2, message1.getB().getA());
  830. assertEquals(1, message2.getB().getA());
  831. });
  832. it('testCircularDepsBaseOnNestedEnum', function() {
  833. var nestMessage2 = new proto.jspb.circulartest.NestedEnum2;
  834. var message1 = new proto.jspb.circulartest.NestedEnum1;
  835. var message2 = new proto.jspb.circulartest.NestedEnum2;
  836. nestMessage2.setA(2);
  837. message1.setB(nestMessage2);
  838. message2.setB(proto.jspb.circulartest.NestedEnum1.NestedNestedEnum.VALUE_1);
  839. assertEquals(2, message1.getB().getA());
  840. assertEquals(
  841. proto.jspb.circulartest.NestedEnum1.NestedNestedEnum.VALUE_1,
  842. message2.getB());
  843. });
  844. it('testCircularDepsBaseOnExtensionContainingType', function() {
  845. var nestMessage2 = new proto.jspb.circulartest.ExtensionContainingType2;
  846. var message1 = new proto.jspb.circulartest.ExtensionContainingType1;
  847. nestMessage2.setA(2);
  848. message1.setB(nestMessage2);
  849. message1.setExtension(
  850. proto.jspb.circulartest.ExtensionContainingType2.c, 1);
  851. assertEquals(2, message1.getB().getA());
  852. assertEquals(
  853. 1,
  854. message1.getExtension(
  855. proto.jspb.circulartest.ExtensionContainingType2.c));
  856. });
  857. it('testCircularDepsBaseOnExtensionField', function() {
  858. var nestMessage2 = new proto.jspb.circulartest.ExtensionField2;
  859. var message1 = new proto.jspb.circulartest.ExtensionField1;
  860. var message3 = new proto.jspb.circulartest.ExtensionField3;
  861. nestMessage2.setA(2);
  862. message1.setB(nestMessage2);
  863. message3.setExtension(proto.jspb.circulartest.ExtensionField2.c, message1);
  864. assertEquals(
  865. 2,
  866. message3.getExtension(proto.jspb.circulartest.ExtensionField2.c)
  867. .getB()
  868. .getA());
  869. });
  870. it('testSameMessageNameOuputs', function() {
  871. var package1Message = new proto.jspb.filenametest.package1.TestMessage;
  872. var package2Message = new proto.jspb.filenametest.package2.TestMessage;
  873. package1Message.setExtension(
  874. proto.jspb.filenametest.package1.a, 10);
  875. package1Message.setExtension(
  876. proto.jspb.filenametest.package1.b, 11);
  877. package2Message.setA(12);
  878. assertEquals(10,
  879. package1Message.getExtension(proto.jspb.filenametest.package1.a));
  880. assertEquals(11,
  881. package1Message.getExtension(proto.jspb.filenametest.package1.b));
  882. assertEquals(12, package2Message.getA());
  883. });
  884. });