message_test.js 42 KB

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