message_test.js 40 KB

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