message_test.js 42 KB

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