message_test.js 43 KB

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