message_test.js 43 KB

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