message_test.js 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // Test suite is written using Jasmine -- see http://jasmine.github.io/
  31. goog.setTestOnly();
  32. goog.require('goog.json');
  33. goog.require('goog.testing.asserts');
  34. goog.require('goog.userAgent');
  35. // CommonJS-LoadFromFile: google-protobuf jspb
  36. goog.require('jspb.Message');
  37. // CommonJS-LoadFromFile: test5_pb proto.jspb.exttest.beta
  38. goog.require('proto.jspb.exttest.beta.floatingStrField');
  39. // CommonJS-LoadFromFile: test3_pb proto.jspb.exttest
  40. goog.require('proto.jspb.exttest.floatingMsgField');
  41. // CommonJS-LoadFromFile: test4_pb proto.jspb.exttest
  42. goog.require('proto.jspb.exttest.floatingMsgFieldTwo');
  43. // CommonJS-LoadFromFile: test_pb proto.jspb.test
  44. goog.require('proto.jspb.test.CloneExtension');
  45. goog.require('proto.jspb.test.Complex');
  46. goog.require('proto.jspb.test.DefaultValues');
  47. goog.require('proto.jspb.test.Empty');
  48. goog.require('proto.jspb.test.EnumContainer');
  49. goog.require('proto.jspb.test.floatingMsgField');
  50. goog.require('proto.jspb.test.FloatingPointFields');
  51. goog.require('proto.jspb.test.floatingStrField');
  52. goog.require('proto.jspb.test.HasExtensions');
  53. goog.require('proto.jspb.test.IndirectExtension');
  54. goog.require('proto.jspb.test.IsExtension');
  55. goog.require('proto.jspb.test.OptionalFields');
  56. goog.require('proto.jspb.test.OuterEnum');
  57. goog.require('proto.jspb.test.OuterMessage.Complex');
  58. goog.require('proto.jspb.test.Simple1');
  59. goog.require('proto.jspb.test.Simple2');
  60. goog.require('proto.jspb.test.SpecialCases');
  61. goog.require('proto.jspb.test.TestClone');
  62. goog.require('proto.jspb.test.TestGroup');
  63. goog.require('proto.jspb.test.TestGroup1');
  64. goog.require('proto.jspb.test.TestMessageWithOneof');
  65. goog.require('proto.jspb.test.TestReservedNames');
  66. goog.require('proto.jspb.test.TestReservedNamesExtension');
  67. // CommonJS-LoadFromFile: test2_pb proto.jspb.test
  68. goog.require('proto.jspb.test.ExtensionMessage');
  69. goog.require('proto.jspb.test.TestExtensionsMessage');
  70. goog.require('proto.jspb.test.simple1');
  71. describe('Message test suite', function() {
  72. it('testEmptyProto', function() {
  73. var empty1 = new proto.jspb.test.Empty([]);
  74. var empty2 = new proto.jspb.test.Empty([]);
  75. assertObjectEquals({}, empty1.toObject());
  76. assertObjectEquals('Message should not be corrupted:', empty2, empty1);
  77. });
  78. it('testTopLevelEnum', function() {
  79. var response = new proto.jspb.test.EnumContainer([]);
  80. response.setOuterEnum(proto.jspb.test.OuterEnum.FOO);
  81. assertEquals(proto.jspb.test.OuterEnum.FOO, response.getOuterEnum());
  82. });
  83. it('testByteStrings', function() {
  84. var data = new proto.jspb.test.DefaultValues([]);
  85. data.setBytesField('some_bytes');
  86. assertEquals('some_bytes', data.getBytesField());
  87. });
  88. it('testComplexConversion', function() {
  89. var data1 = ['a', , , [, 11], [[, 22], [, 33]], , ['s1', 's2'], , true];
  90. var data2 = ['a', , , [, 11], [[, 22], [, 33]], , ['s1', 's2'], , true];
  91. var foo = new proto.jspb.test.Complex(data1);
  92. var bar = new proto.jspb.test.Complex(data2);
  93. var result = foo.toObject();
  94. assertObjectEquals(
  95. {
  96. aString: 'a',
  97. anOutOfOrderBool: true,
  98. aNestedMessage: {anInt: 11},
  99. aRepeatedMessageList: [{anInt: 22}, {anInt: 33}],
  100. aRepeatedStringList: ['s1', 's2']
  101. },
  102. result);
  103. // Now test with the jspb instances included.
  104. result = foo.toObject(true /* opt_includeInstance */);
  105. assertObjectEquals(
  106. {
  107. aString: 'a',
  108. anOutOfOrderBool: true,
  109. aNestedMessage:
  110. {anInt: 11, $jspbMessageInstance: foo.getANestedMessage()},
  111. aRepeatedMessageList: [
  112. {anInt: 22, $jspbMessageInstance: foo.getARepeatedMessageList()[0]},
  113. {anInt: 33, $jspbMessageInstance: foo.getARepeatedMessageList()[1]}
  114. ],
  115. aRepeatedStringList: ['s1', 's2'],
  116. $jspbMessageInstance: foo
  117. },
  118. result);
  119. });
  120. it('testMissingFields', function() {
  121. var foo = new proto.jspb.test.Complex([
  122. undefined, undefined, undefined, [], undefined, undefined, undefined,
  123. undefined
  124. ]);
  125. var bar = new proto.jspb.test.Complex([
  126. undefined, undefined, undefined, [], undefined, undefined, undefined,
  127. undefined
  128. ]);
  129. var result = foo.toObject();
  130. assertObjectEquals(
  131. {
  132. aString: undefined,
  133. anOutOfOrderBool: undefined,
  134. aNestedMessage: {anInt: undefined},
  135. // Note: JsPb converts undefined repeated fields to empty arrays.
  136. aRepeatedMessageList: [],
  137. aRepeatedStringList: []
  138. },
  139. result);
  140. });
  141. it('testNestedComplexMessage', function() {
  142. // Instantiate the message and set a unique field, just to ensure that we
  143. // are not getting jspb.test.Complex instead.
  144. var msg = new proto.jspb.test.OuterMessage.Complex();
  145. msg.setInnerComplexField(5);
  146. });
  147. it('testSpecialCases', function() {
  148. // Note: Some property names are reserved in JavaScript.
  149. // These names are converted to the Js property named pb_<reserved_name>.
  150. var special = new proto.jspb.test.SpecialCases(
  151. ['normal', 'default', 'function', 'var']);
  152. var result = special.toObject();
  153. assertObjectEquals(
  154. {
  155. normal: 'normal',
  156. pb_default: 'default',
  157. pb_function: 'function',
  158. pb_var: 'var'
  159. },
  160. result);
  161. });
  162. it('testDefaultValues', function() {
  163. var defaultString = 'default<>\'"abc';
  164. var response = new proto.jspb.test.DefaultValues();
  165. // Test toObject
  166. var expectedObject = {
  167. stringField: defaultString,
  168. boolField: true,
  169. intField: 11,
  170. enumField: 13,
  171. emptyField: '',
  172. bytesField: 'bW9v'
  173. };
  174. assertObjectEquals(expectedObject, response.toObject());
  175. // Test getters
  176. response = new proto.jspb.test.DefaultValues();
  177. assertEquals(defaultString, response.getStringField());
  178. assertEquals(true, response.getBoolField());
  179. assertEquals(11, response.getIntField());
  180. assertEquals(13, response.getEnumField());
  181. assertEquals('', response.getEmptyField());
  182. assertEquals('bW9v', response.getBytesField());
  183. function makeDefault(values) {
  184. return new proto.jspb.test.DefaultValues(values);
  185. }
  186. // Test with undefined values,
  187. // Use push to workaround IE treating undefined array elements as holes.
  188. response = makeDefault([undefined, undefined, undefined, undefined]);
  189. assertEquals(defaultString, response.getStringField());
  190. assertEquals(true, response.getBoolField());
  191. assertEquals(11, response.getIntField());
  192. assertEquals(13, response.getEnumField());
  193. assertFalse(response.hasStringField());
  194. assertFalse(response.hasBoolField());
  195. assertFalse(response.hasIntField());
  196. assertFalse(response.hasEnumField());
  197. // Test with null values, as would be returned by a JSON serializer.
  198. response = makeDefault([null, null, null, null]);
  199. assertEquals(defaultString, response.getStringField());
  200. assertEquals(true, response.getBoolField());
  201. assertEquals(11, response.getIntField());
  202. assertEquals(13, response.getEnumField());
  203. assertFalse(response.hasStringField());
  204. assertFalse(response.hasBoolField());
  205. assertFalse(response.hasIntField());
  206. assertFalse(response.hasEnumField());
  207. // Test with false-like values.
  208. response = makeDefault(['', false, 0, 0]);
  209. assertEquals('', response.getStringField());
  210. assertEquals(false, response.getBoolField());
  211. assertEquals(true, response.getIntField() == 0);
  212. assertEquals(true, response.getEnumField() == 0);
  213. assertTrue(response.hasStringField());
  214. assertTrue(response.hasBoolField());
  215. assertTrue(response.hasIntField());
  216. assertTrue(response.hasEnumField());
  217. // Test that clearing the values reverts them to the default state.
  218. response = makeDefault(['blah', false, 111, 77]);
  219. response.clearStringField();
  220. response.clearBoolField();
  221. response.clearIntField();
  222. response.clearEnumField();
  223. assertEquals(defaultString, response.getStringField());
  224. assertEquals(true, response.getBoolField());
  225. assertEquals(11, response.getIntField());
  226. assertEquals(13, response.getEnumField());
  227. assertFalse(response.hasStringField());
  228. assertFalse(response.hasBoolField());
  229. assertFalse(response.hasIntField());
  230. assertFalse(response.hasEnumField());
  231. // Test that setFoo(null) clears the values.
  232. response = makeDefault(['blah', false, 111, 77]);
  233. response.setStringField(null);
  234. response.setBoolField(null);
  235. response.setIntField(undefined);
  236. response.setEnumField(undefined);
  237. assertEquals(defaultString, response.getStringField());
  238. assertEquals(true, response.getBoolField());
  239. assertEquals(11, response.getIntField());
  240. assertEquals(13, response.getEnumField());
  241. assertFalse(response.hasStringField());
  242. assertFalse(response.hasBoolField());
  243. assertFalse(response.hasIntField());
  244. assertFalse(response.hasEnumField());
  245. });
  246. it('testClearFields', function() {
  247. var data = ['str', true, [11], [[22], [33]], ['s1', 's2']];
  248. var foo = new proto.jspb.test.OptionalFields(data);
  249. foo.clearAString();
  250. foo.clearABool();
  251. foo.clearANestedMessage();
  252. foo.clearARepeatedMessageList();
  253. foo.clearARepeatedStringList();
  254. assertEquals('', foo.getAString());
  255. assertEquals(false, foo.getABool());
  256. assertUndefined(foo.getANestedMessage());
  257. assertFalse(foo.hasAString());
  258. assertFalse(foo.hasABool());
  259. assertObjectEquals([], foo.getARepeatedMessageList());
  260. assertObjectEquals([], foo.getARepeatedStringList());
  261. // NOTE: We want the missing fields in 'expected' to be undefined,
  262. // but we actually get a sparse array instead. We could use something
  263. // like [1,undefined,2] to avoid this, except that this is still
  264. // sparse on IE. No comment...
  265. var expected = [, , , [], []];
  266. expected[0] = expected[1] = expected[2] = undefined;
  267. assertObjectEquals(expected, foo.toArray());
  268. });
  269. it('testDifferenceRawObject', /** @suppress {visibility} */ function() {
  270. var p1 = new proto.jspb.test.HasExtensions(['hi', 'diff', {}]);
  271. var p2 =
  272. new proto.jspb.test.HasExtensions(['hi', 'what', {1000: 'unique'}]);
  273. var diff = /** @type {proto.jspb.test.HasExtensions} */
  274. (jspb.Message.difference(p1, p2));
  275. assertEquals('', diff.getStr1());
  276. assertEquals('what', diff.getStr2());
  277. assertEquals('', diff.getStr3());
  278. assertEquals('unique', diff.extensionObject_[1000]);
  279. });
  280. it('testEqualsSimple', function() {
  281. var s1 = new proto.jspb.test.Simple1(['hi']);
  282. assertTrue(jspb.Message.equals(s1, new proto.jspb.test.Simple1(['hi'])));
  283. assertFalse(jspb.Message.equals(s1, new proto.jspb.test.Simple1(['bye'])));
  284. var s1b = new proto.jspb.test.Simple1(['hi', ['hello']]);
  285. assertTrue(jspb.Message.equals(
  286. s1b, new proto.jspb.test.Simple1(['hi', ['hello']])));
  287. assertTrue(jspb.Message.equals(s1b, new proto.jspb.test.Simple1([
  288. 'hi', ['hello', undefined, undefined, undefined]
  289. ])));
  290. assertFalse(jspb.Message.equals(
  291. s1b, new proto.jspb.test.Simple1(['no', ['hello']])));
  292. // Test with messages of different types
  293. var s2 = new proto.jspb.test.Simple2(['hi']);
  294. assertFalse(jspb.Message.equals(s1, s2));
  295. });
  296. it('testEquals_softComparison', function() {
  297. var s1 = new proto.jspb.test.Simple1(['hi', [], null]);
  298. assertTrue(
  299. jspb.Message.equals(s1, new proto.jspb.test.Simple1(['hi', []])));
  300. var s1b = new proto.jspb.test.Simple1(['hi', [], true]);
  301. assertTrue(
  302. jspb.Message.equals(s1b, new proto.jspb.test.Simple1(['hi', [], 1])));
  303. });
  304. it('testEqualsComplex', function() {
  305. var data1 = ['a', , , [, 11], [[, 22], [, 33]], , ['s1', 's2'], , 1];
  306. var data2 = ['a', , , [, 11], [[, 22], [, 34]], , ['s1', 's2'], , 1];
  307. var data3 = ['a', , , [, 11], [[, 22]], , ['s1', 's2'], , 1];
  308. var data4 = ['hi'];
  309. var c1a = new proto.jspb.test.Complex(data1);
  310. var c1b = new proto.jspb.test.Complex(data1);
  311. var c2 = new proto.jspb.test.Complex(data2);
  312. var c3 = new proto.jspb.test.Complex(data3);
  313. var s1 = new proto.jspb.test.Simple1(data4);
  314. assertTrue(jspb.Message.equals(c1a, c1b));
  315. assertFalse(jspb.Message.equals(c1a, c2));
  316. assertFalse(jspb.Message.equals(c2, c3));
  317. assertFalse(jspb.Message.equals(c1a, s1));
  318. });
  319. it('testEqualsExtensionsConstructed', function() {
  320. assertTrue(jspb.Message.equals(
  321. new proto.jspb.test.HasExtensions([]),
  322. new proto.jspb.test.HasExtensions([{}])));
  323. assertTrue(jspb.Message.equals(
  324. new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'a'}]}]),
  325. new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'a'}]}])));
  326. assertFalse(jspb.Message.equals(
  327. new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'a'}]}]),
  328. new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'b'}]}])));
  329. assertTrue(jspb.Message.equals(
  330. new proto.jspb.test.HasExtensions([{100: [{200: 'a'}]}]),
  331. new proto.jspb.test.HasExtensions([{100: [{200: 'a'}]}])));
  332. assertTrue(jspb.Message.equals(
  333. new proto.jspb.test.HasExtensions([{100: [{200: 'a'}]}]),
  334. new proto.jspb.test.HasExtensions([, , , {100: [{200: 'a'}]}])));
  335. assertTrue(jspb.Message.equals(
  336. new proto.jspb.test.HasExtensions([, , , {100: [{200: 'a'}]}]),
  337. new proto.jspb.test.HasExtensions([{100: [{200: 'a'}]}])));
  338. assertTrue(jspb.Message.equals(
  339. new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'a'}]}]),
  340. new proto.jspb.test.HasExtensions(['hi', , , {100: [{200: 'a'}]}])));
  341. assertTrue(jspb.Message.equals(
  342. new proto.jspb.test.HasExtensions(['hi', , , {100: [{200: 'a'}]}]),
  343. new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'a'}]}])));
  344. });
  345. it('testEqualsExtensionsUnconstructed', function() {
  346. assertTrue(jspb.Message.compareFields([], [{}]));
  347. assertTrue(jspb.Message.compareFields([, , , {}], []));
  348. assertTrue(jspb.Message.compareFields([, , , {}], [, , {}]));
  349. assertTrue(jspb.Message.compareFields(
  350. ['hi', {100: [{200: 'a'}]}], ['hi', {100: [{200: 'a'}]}]));
  351. assertFalse(jspb.Message.compareFields(
  352. ['hi', {100: [{200: 'a'}]}], ['hi', {100: [{200: 'b'}]}]));
  353. assertTrue(jspb.Message.compareFields(
  354. [{100: [{200: 'a'}]}], [{100: [{200: 'a'}]}]));
  355. assertTrue(jspb.Message.compareFields(
  356. [{100: [{200: 'a'}]}], [, , , {100: [{200: 'a'}]}]));
  357. assertTrue(jspb.Message.compareFields(
  358. [, , , {100: [{200: 'a'}]}], [{100: [{200: 'a'}]}]));
  359. assertTrue(jspb.Message.compareFields(
  360. ['hi', {100: [{200: 'a'}]}], ['hi', , , {100: [{200: 'a'}]}]));
  361. assertTrue(jspb.Message.compareFields(
  362. ['hi', , , {100: [{200: 'a'}]}], ['hi', {100: [{200: 'a'}]}]));
  363. });
  364. it('testToMap', function() {
  365. var p1 = new proto.jspb.test.Simple1(['k', ['v']]);
  366. var p2 = new proto.jspb.test.Simple1(['k1', ['v1', 'v2']]);
  367. var soymap = jspb.Message.toMap(
  368. [p1, p2], proto.jspb.test.Simple1.prototype.getAString,
  369. proto.jspb.test.Simple1.prototype.toObject);
  370. assertEquals('k', soymap['k'].aString);
  371. assertArrayEquals(['v'], soymap['k'].aRepeatedStringList);
  372. var protomap = jspb.Message.toMap(
  373. [p1, p2], proto.jspb.test.Simple1.prototype.getAString);
  374. assertEquals('k', protomap['k'].getAString());
  375. assertArrayEquals(['v'], protomap['k'].getARepeatedStringList());
  376. });
  377. it('testClone', function() {
  378. var supportsUint8Array =
  379. !goog.userAgent.IE || goog.userAgent.isVersionOrHigher('10');
  380. var original = new proto.jspb.test.TestClone();
  381. original.setStr('v1');
  382. var simple1 = new proto.jspb.test.Simple1(['x1', ['y1', 'z1']]);
  383. var simple2 = new proto.jspb.test.Simple1(['x2', ['y2', 'z2']]);
  384. var simple3 = new proto.jspb.test.Simple1(['x3', ['y3', 'z3']]);
  385. original.setSimple1(simple1);
  386. original.setSimple2List([simple2, simple3]);
  387. var bytes1 = supportsUint8Array ? new Uint8Array([1, 2, 3]) : '123';
  388. original.setBytesField(bytes1);
  389. var extension = new proto.jspb.test.CloneExtension();
  390. extension.setExt('e1');
  391. original.setExtension(proto.jspb.test.IsExtension.extField, extension);
  392. var clone = original.clone();
  393. assertArrayEquals(
  394. [
  395. 'v1', , ['x1', ['y1', 'z1']], ,
  396. [['x2', ['y2', 'z2']], ['x3', ['y3', 'z3']]], bytes1, ,
  397. {100: [, 'e1']}
  398. ],
  399. clone.toArray());
  400. clone.setStr('v2');
  401. var simple4 = new proto.jspb.test.Simple1(['a1', ['b1', 'c1']]);
  402. var simple5 = new proto.jspb.test.Simple1(['a2', ['b2', 'c2']]);
  403. var simple6 = new proto.jspb.test.Simple1(['a3', ['b3', 'c3']]);
  404. clone.setSimple1(simple4);
  405. clone.setSimple2List([simple5, simple6]);
  406. if (supportsUint8Array) {
  407. clone.getBytesField()[0] = 4;
  408. assertObjectEquals(bytes1, original.getBytesField());
  409. }
  410. var bytes2 = supportsUint8Array ? new Uint8Array([4, 5, 6]) : '456';
  411. clone.setBytesField(bytes2);
  412. var newExtension = new proto.jspb.test.CloneExtension();
  413. newExtension.setExt('e2');
  414. clone.setExtension(proto.jspb.test.CloneExtension.extField, newExtension);
  415. assertArrayEquals(
  416. [
  417. 'v2', , ['a1', ['b1', 'c1']], ,
  418. [['a2', ['b2', 'c2']], ['a3', ['b3', 'c3']]], bytes2, ,
  419. {100: [, 'e2']}
  420. ],
  421. clone.toArray());
  422. assertArrayEquals(
  423. [
  424. 'v1', , ['x1', ['y1', 'z1']], ,
  425. [['x2', ['y2', 'z2']], ['x3', ['y3', 'z3']]], bytes1, ,
  426. {100: [, 'e1']}
  427. ],
  428. original.toArray());
  429. });
  430. it('testCopyInto', function() {
  431. var supportsUint8Array =
  432. !goog.userAgent.IE || goog.userAgent.isVersionOrHigher('10');
  433. var original = new proto.jspb.test.TestClone();
  434. original.setStr('v1');
  435. var dest = new proto.jspb.test.TestClone();
  436. dest.setStr('override');
  437. var simple1 = new proto.jspb.test.Simple1(['x1', ['y1', 'z1']]);
  438. var simple2 = new proto.jspb.test.Simple1(['x2', ['y2', 'z2']]);
  439. var simple3 = new proto.jspb.test.Simple1(['x3', ['y3', 'z3']]);
  440. var destSimple1 = new proto.jspb.test.Simple1(['ox1', ['oy1', 'oz1']]);
  441. var destSimple2 = new proto.jspb.test.Simple1(['ox2', ['oy2', 'oz2']]);
  442. var destSimple3 = new proto.jspb.test.Simple1(['ox3', ['oy3', 'oz3']]);
  443. original.setSimple1(simple1);
  444. original.setSimple2List([simple2, simple3]);
  445. dest.setSimple1(destSimple1);
  446. dest.setSimple2List([destSimple2, destSimple3]);
  447. var bytes1 = supportsUint8Array ? new Uint8Array([1, 2, 3]) : '123';
  448. var bytes2 = supportsUint8Array ? new Uint8Array([4, 5, 6]) : '456';
  449. original.setBytesField(bytes1);
  450. dest.setBytesField(bytes2);
  451. var extension = new proto.jspb.test.CloneExtension();
  452. extension.setExt('e1');
  453. original.setExtension(proto.jspb.test.CloneExtension.extField, extension);
  454. jspb.Message.copyInto(original, dest);
  455. assertArrayEquals(original.toArray(), dest.toArray());
  456. assertEquals('x1', dest.getSimple1().getAString());
  457. assertEquals(
  458. 'e1',
  459. dest.getExtension(proto.jspb.test.CloneExtension.extField).getExt());
  460. dest.getSimple1().setAString('new value');
  461. assertNotEquals(
  462. dest.getSimple1().getAString(), original.getSimple1().getAString());
  463. if (supportsUint8Array) {
  464. dest.getBytesField()[0] = 7;
  465. assertObjectEquals(bytes1, original.getBytesField());
  466. assertObjectEquals(new Uint8Array([7, 2, 3]), dest.getBytesField());
  467. } else {
  468. dest.setBytesField('789');
  469. assertObjectEquals(bytes1, original.getBytesField());
  470. assertObjectEquals('789', dest.getBytesField());
  471. }
  472. dest.getExtension(proto.jspb.test.CloneExtension.extField)
  473. .setExt('new value');
  474. assertNotEquals(
  475. dest.getExtension(proto.jspb.test.CloneExtension.extField).getExt(),
  476. original.getExtension(proto.jspb.test.CloneExtension.extField)
  477. .getExt());
  478. });
  479. it('testCopyInto_notSameType', function() {
  480. var a = new proto.jspb.test.TestClone();
  481. var b = new proto.jspb.test.Simple1(['str', ['s1', 's2']]);
  482. var e = assertThrows(function() {
  483. jspb.Message.copyInto(a, b);
  484. });
  485. assertContains('should have the same type', e.message);
  486. });
  487. it('testExtensions', function() {
  488. var extension1 = new proto.jspb.test.IsExtension(['ext1field']);
  489. var extension2 = new proto.jspb.test.Simple1(['str', ['s1', 's2']]);
  490. var extendable = new proto.jspb.test.HasExtensions(['v1', 'v2', 'v3']);
  491. extendable.setExtension(proto.jspb.test.IsExtension.extField, extension1);
  492. extendable.setExtension(
  493. proto.jspb.test.IndirectExtension.simple, extension2);
  494. extendable.setExtension(proto.jspb.test.IndirectExtension.str, 'xyzzy');
  495. extendable.setExtension(
  496. proto.jspb.test.IndirectExtension.repeatedStrList, ['a', 'b']);
  497. var s1 = new proto.jspb.test.Simple1(['foo', ['s1', 's2']]);
  498. var s2 = new proto.jspb.test.Simple1(['bar', ['t1', 't2']]);
  499. extendable.setExtension(
  500. proto.jspb.test.IndirectExtension.repeatedSimpleList, [s1, s2]);
  501. assertObjectEquals(
  502. extension1,
  503. extendable.getExtension(proto.jspb.test.IsExtension.extField));
  504. assertObjectEquals(
  505. extension2,
  506. extendable.getExtension(proto.jspb.test.IndirectExtension.simple));
  507. assertObjectEquals(
  508. 'xyzzy',
  509. extendable.getExtension(proto.jspb.test.IndirectExtension.str));
  510. assertObjectEquals(
  511. ['a', 'b'],
  512. extendable.getExtension(
  513. proto.jspb.test.IndirectExtension.repeatedStrList));
  514. assertObjectEquals(
  515. [s1, s2],
  516. extendable.getExtension(
  517. proto.jspb.test.IndirectExtension.repeatedSimpleList));
  518. // Not supported yet, but it should work...
  519. extendable.setExtension(proto.jspb.test.IndirectExtension.simple, null);
  520. assertNull(
  521. extendable.getExtension(proto.jspb.test.IndirectExtension.simple));
  522. extendable.setExtension(proto.jspb.test.IndirectExtension.str, null);
  523. assertNull(extendable.getExtension(proto.jspb.test.IndirectExtension.str));
  524. // Extension fields with jspb.ignore = true are ignored.
  525. assertUndefined(proto.jspb.test.IndirectExtension['ignored']);
  526. assertUndefined(proto.jspb.test.HasExtensions['ignoredFloating']);
  527. });
  528. it('testFloatingExtensions', function() {
  529. // From an autogenerated container.
  530. var extendable = new proto.jspb.test.HasExtensions(['v1', 'v2', 'v3']);
  531. var extension = new proto.jspb.test.Simple1(['foo', ['s1', 's2']]);
  532. extendable.setExtension(proto.jspb.test.simple1, extension);
  533. assertObjectEquals(
  534. extension, extendable.getExtension(proto.jspb.test.simple1));
  535. // From _lib mode.
  536. extension = new proto.jspb.test.ExtensionMessage(['s1']);
  537. extendable = new proto.jspb.test.TestExtensionsMessage([16]);
  538. extendable.setExtension(proto.jspb.test.floatingMsgField, extension);
  539. extendable.setExtension(proto.jspb.test.floatingStrField, 's2');
  540. assertObjectEquals(
  541. extension, extendable.getExtension(proto.jspb.test.floatingMsgField));
  542. assertObjectEquals(
  543. 's2', extendable.getExtension(proto.jspb.test.floatingStrField));
  544. assertNotUndefined(proto.jspb.exttest.floatingMsgField);
  545. assertNotUndefined(proto.jspb.exttest.floatingMsgFieldTwo);
  546. assertNotUndefined(proto.jspb.exttest.beta.floatingStrField);
  547. });
  548. it('testToObject_extendedObject', function() {
  549. var extension1 = new proto.jspb.test.IsExtension(['ext1field']);
  550. var extension2 = new proto.jspb.test.Simple1(['str', ['s1', 's2'], true]);
  551. var extendable = new proto.jspb.test.HasExtensions(['v1', 'v2', 'v3']);
  552. extendable.setExtension(proto.jspb.test.IsExtension.extField, extension1);
  553. extendable.setExtension(
  554. proto.jspb.test.IndirectExtension.simple, extension2);
  555. extendable.setExtension(proto.jspb.test.IndirectExtension.str, 'xyzzy');
  556. extendable.setExtension(
  557. proto.jspb.test.IndirectExtension.repeatedStrList, ['a', 'b']);
  558. var s1 = new proto.jspb.test.Simple1(['foo', ['s1', 's2'], true]);
  559. var s2 = new proto.jspb.test.Simple1(['bar', ['t1', 't2'], false]);
  560. extendable.setExtension(
  561. proto.jspb.test.IndirectExtension.repeatedSimpleList, [s1, s2]);
  562. assertObjectEquals(
  563. {
  564. str1: 'v1',
  565. str2: 'v2',
  566. str3: 'v3',
  567. extField: {ext1: 'ext1field'},
  568. simple: {
  569. aString: 'str',
  570. aRepeatedStringList: ['s1', 's2'],
  571. aBoolean: true
  572. },
  573. str: 'xyzzy',
  574. repeatedStrList: ['a', 'b'],
  575. repeatedSimpleList: [
  576. {aString: 'foo', aRepeatedStringList: ['s1', 's2'], aBoolean: true},
  577. {aString: 'bar', aRepeatedStringList: ['t1', 't2'], aBoolean: false}
  578. ]
  579. },
  580. extendable.toObject());
  581. // Now, with instances included.
  582. assertObjectEquals(
  583. {
  584. str1: 'v1',
  585. str2: 'v2',
  586. str3: 'v3',
  587. extField: {
  588. ext1: 'ext1field',
  589. $jspbMessageInstance:
  590. extendable.getExtension(proto.jspb.test.IsExtension.extField)
  591. },
  592. simple: {
  593. aString: 'str',
  594. aRepeatedStringList: ['s1', 's2'],
  595. aBoolean: true,
  596. $jspbMessageInstance: extendable.getExtension(
  597. proto.jspb.test.IndirectExtension.simple)
  598. },
  599. str: 'xyzzy',
  600. repeatedStrList: ['a', 'b'],
  601. repeatedSimpleList: [
  602. {
  603. aString: 'foo',
  604. aRepeatedStringList: ['s1', 's2'],
  605. aBoolean: true,
  606. $jspbMessageInstance: s1
  607. },
  608. {
  609. aString: 'bar',
  610. aRepeatedStringList: ['t1', 't2'],
  611. aBoolean: false,
  612. $jspbMessageInstance: s2
  613. }
  614. ],
  615. $jspbMessageInstance: extendable
  616. },
  617. extendable.toObject(true /* opt_includeInstance */));
  618. });
  619. it('testInitialization_emptyArray', function() {
  620. var msg = new proto.jspb.test.HasExtensions([]);
  621. assertArrayEquals([], msg.toArray());
  622. });
  623. it('testInitialization_justExtensionObject', function() {
  624. var msg = new proto.jspb.test.Empty([{1: 'hi'}]);
  625. // The extensionObject is not moved from its original location.
  626. assertArrayEquals([{1: 'hi'}], msg.toArray());
  627. });
  628. it('testInitialization_incompleteList', function() {
  629. var msg = new proto.jspb.test.Empty([1, {4: 'hi'}]);
  630. // The extensionObject is not moved from its original location.
  631. assertArrayEquals([1, {4: 'hi'}], msg.toArray());
  632. });
  633. it('testInitialization_forwardCompatible', function() {
  634. var msg = new proto.jspb.test.Empty([1, 2, 3, {1: 'hi'}]);
  635. assertArrayEquals([1, 2, 3, {1: 'hi'}], msg.toArray());
  636. });
  637. it('testExtendedMessageEnsureObject',
  638. /** @suppress {visibility} */ function() {
  639. var data =
  640. new proto.jspb.test.HasExtensions(['str1', {'a_key': 'an_object'}]);
  641. assertEquals('an_object', data.extensionObject_['a_key']);
  642. });
  643. it('testToObject_hasExtensionField', function() {
  644. var data = new proto.jspb.test.HasExtensions(['str1', {100: ['ext1']}]);
  645. var obj = data.toObject();
  646. assertEquals('str1', obj.str1);
  647. assertEquals('ext1', obj.extField.ext1);
  648. });
  649. it('testGetExtension', function() {
  650. var data = new proto.jspb.test.HasExtensions(['str1', {100: ['ext1']}]);
  651. assertEquals('str1', data.getStr1());
  652. var extension = data.getExtension(proto.jspb.test.IsExtension.extField);
  653. assertNotNull(extension);
  654. assertEquals('ext1', extension.getExt1());
  655. });
  656. it('testSetExtension', function() {
  657. var data = new proto.jspb.test.HasExtensions();
  658. var extensionMessage = new proto.jspb.test.IsExtension(['is_extension']);
  659. data.setExtension(proto.jspb.test.IsExtension.extField, extensionMessage);
  660. var obj = data.toObject();
  661. assertNotNull(data.getExtension(proto.jspb.test.IsExtension.extField));
  662. assertEquals('is_extension', obj.extField.ext1);
  663. });
  664. /**
  665. * Note that group is long deprecated, we only support it because JsPb has
  666. * a goal of being able to generate JS classes for all proto descriptors.
  667. */
  668. it('testGroups', function() {
  669. var group = new proto.jspb.test.TestGroup();
  670. var someGroup = new proto.jspb.test.TestGroup.RepeatedGroup();
  671. someGroup.setId('g1');
  672. someGroup.setSomeBoolList([true, false]);
  673. group.setRepeatedGroupList([someGroup]);
  674. var groups = group.getRepeatedGroupList();
  675. assertEquals('g1', groups[0].getId());
  676. assertObjectEquals([true, false], groups[0].getSomeBoolList());
  677. assertObjectEquals(
  678. {id: 'g1', someBoolList: [true, false]}, groups[0].toObject());
  679. assertObjectEquals(
  680. {
  681. repeatedGroupList: [{id: 'g1', someBoolList: [true, false]}],
  682. requiredGroup: {id: undefined},
  683. optionalGroup: undefined,
  684. requiredSimple: {aRepeatedStringList: [], aString: undefined},
  685. optionalSimple: undefined,
  686. id: undefined
  687. },
  688. group.toObject());
  689. var group1 = new proto.jspb.test.TestGroup1();
  690. group1.setGroup(someGroup);
  691. assertEquals(someGroup, group1.getGroup());
  692. });
  693. it('testNonExtensionFieldsAfterExtensionRange', function() {
  694. var data = [{'1': 'a_string'}];
  695. var message = new proto.jspb.test.Complex(data);
  696. assertArrayEquals([], message.getARepeatedStringList());
  697. });
  698. it('testReservedGetterNames', function() {
  699. var message = new proto.jspb.test.TestReservedNames();
  700. message.setExtension$(11);
  701. message.setExtension(proto.jspb.test.TestReservedNamesExtension.foo, 12);
  702. assertEquals(11, message.getExtension$());
  703. assertEquals(
  704. 12,
  705. message.getExtension(proto.jspb.test.TestReservedNamesExtension.foo));
  706. assertObjectEquals({extension: 11, foo: 12}, message.toObject());
  707. });
  708. it('testInitializeMessageWithUnsetOneof', function() {
  709. var message = new proto.jspb.test.TestMessageWithOneof([]);
  710. assertEquals(
  711. proto.jspb.test.TestMessageWithOneof.PartialOneofCase
  712. .PARTIAL_ONEOF_NOT_SET,
  713. message.getPartialOneofCase());
  714. assertEquals(
  715. proto.jspb.test.TestMessageWithOneof.RecursiveOneofCase
  716. .RECURSIVE_ONEOF_NOT_SET,
  717. message.getRecursiveOneofCase());
  718. });
  719. it('testInitializeMessageWithSingleValueSetInOneof', function() {
  720. var message = new proto.jspb.test.TestMessageWithOneof([, , 'x']);
  721. assertEquals('x', message.getPone());
  722. assertEquals('', message.getPthree());
  723. assertEquals(
  724. proto.jspb.test.TestMessageWithOneof.PartialOneofCase.PONE,
  725. message.getPartialOneofCase());
  726. });
  727. it('testKeepsLastWireValueSetInUnion_multipleValues', function() {
  728. var message = new proto.jspb.test.TestMessageWithOneof([, , 'x', , 'y']);
  729. assertEquals('', message.getPone());
  730. assertEquals('y', message.getPthree());
  731. assertEquals(
  732. proto.jspb.test.TestMessageWithOneof.PartialOneofCase.PTHREE,
  733. message.getPartialOneofCase());
  734. });
  735. it('testSettingOneofFieldClearsOthers', function() {
  736. var message = new proto.jspb.test.TestMessageWithOneof;
  737. assertEquals('', message.getPone());
  738. assertEquals('', message.getPthree());
  739. assertFalse(message.hasPone());
  740. assertFalse(message.hasPthree());
  741. message.setPone('hi');
  742. assertEquals('hi', message.getPone());
  743. assertEquals('', message.getPthree());
  744. assertTrue(message.hasPone());
  745. assertFalse(message.hasPthree());
  746. message.setPthree('bye');
  747. assertEquals('', message.getPone());
  748. assertEquals('bye', message.getPthree());
  749. assertFalse(message.hasPone());
  750. assertTrue(message.hasPthree());
  751. });
  752. it('testSettingOneofFieldDoesNotClearFieldsFromOtherUnions', function() {
  753. var other = new proto.jspb.test.TestMessageWithOneof;
  754. var message = new proto.jspb.test.TestMessageWithOneof;
  755. assertEquals('', message.getPone());
  756. assertEquals('', message.getPthree());
  757. assertUndefined(message.getRone());
  758. assertFalse(message.hasPone());
  759. assertFalse(message.hasPthree());
  760. message.setPone('hi');
  761. message.setRone(other);
  762. assertEquals('hi', message.getPone());
  763. assertEquals('', message.getPthree());
  764. assertEquals(other, message.getRone());
  765. assertTrue(message.hasPone());
  766. assertFalse(message.hasPthree());
  767. message.setPthree('bye');
  768. assertEquals('', message.getPone());
  769. assertEquals('bye', message.getPthree());
  770. assertEquals(other, message.getRone());
  771. assertFalse(message.hasPone());
  772. assertTrue(message.hasPthree());
  773. });
  774. it('testUnsetsOneofCaseWhenFieldIsCleared', function() {
  775. var message = new proto.jspb.test.TestMessageWithOneof;
  776. assertEquals(
  777. proto.jspb.test.TestMessageWithOneof.PartialOneofCase
  778. .PARTIAL_ONEOF_NOT_SET,
  779. message.getPartialOneofCase());
  780. message.setPone('hi');
  781. assertEquals(
  782. proto.jspb.test.TestMessageWithOneof.PartialOneofCase.PONE,
  783. message.getPartialOneofCase());
  784. message.clearPone();
  785. assertEquals(
  786. proto.jspb.test.TestMessageWithOneof.PartialOneofCase
  787. .PARTIAL_ONEOF_NOT_SET,
  788. message.getPartialOneofCase());
  789. });
  790. it('testMessageWithDefaultOneofValues', function() {
  791. var message = new proto.jspb.test.TestMessageWithOneof;
  792. assertEquals(1234, message.getAone());
  793. assertEquals(0, message.getAtwo());
  794. assertEquals(
  795. proto.jspb.test.TestMessageWithOneof.DefaultOneofACase
  796. .DEFAULT_ONEOF_A_NOT_SET,
  797. message.getDefaultOneofACase());
  798. message.setAone(567);
  799. assertEquals(567, message.getAone());
  800. assertEquals(0, message.getAtwo());
  801. assertEquals(
  802. proto.jspb.test.TestMessageWithOneof.DefaultOneofACase.AONE,
  803. message.getDefaultOneofACase());
  804. message.setAtwo(890);
  805. assertEquals(1234, message.getAone());
  806. assertEquals(890, message.getAtwo());
  807. assertEquals(
  808. proto.jspb.test.TestMessageWithOneof.DefaultOneofACase.ATWO,
  809. message.getDefaultOneofACase());
  810. message.clearAtwo();
  811. assertEquals(1234, message.getAone());
  812. assertEquals(0, message.getAtwo());
  813. assertEquals(
  814. proto.jspb.test.TestMessageWithOneof.DefaultOneofACase
  815. .DEFAULT_ONEOF_A_NOT_SET,
  816. message.getDefaultOneofACase());
  817. });
  818. it('testMessageWithDefaultOneofValues_defaultNotOnFirstField', function() {
  819. var message = new proto.jspb.test.TestMessageWithOneof;
  820. assertEquals(0, message.getBone());
  821. assertEquals(1234, message.getBtwo());
  822. assertFalse(message.hasBone());
  823. assertFalse(message.hasBtwo());
  824. assertEquals(
  825. proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase
  826. .DEFAULT_ONEOF_B_NOT_SET,
  827. message.getDefaultOneofBCase());
  828. message.setBone(2);
  829. assertEquals(2, message.getBone());
  830. assertEquals(1234, message.getBtwo());
  831. assertTrue(message.hasBone());
  832. assertFalse(message.hasBtwo());
  833. assertEquals(
  834. proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase.BONE,
  835. message.getDefaultOneofBCase());
  836. message.setBtwo(3);
  837. assertEquals(0, message.getBone());
  838. assertFalse(message.hasBone());
  839. assertTrue(message.hasBtwo());
  840. assertEquals(3, message.getBtwo());
  841. assertEquals(
  842. proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase.BTWO,
  843. message.getDefaultOneofBCase());
  844. message.clearBtwo();
  845. assertEquals(0, message.getBone());
  846. assertFalse(message.hasBone());
  847. assertFalse(message.hasBtwo());
  848. assertEquals(1234, message.getBtwo());
  849. assertEquals(
  850. proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase
  851. .DEFAULT_ONEOF_B_NOT_SET,
  852. message.getDefaultOneofBCase());
  853. });
  854. it('testInitializeMessageWithOneofDefaults', function() {
  855. var message =
  856. new proto.jspb.test.TestMessageWithOneof(new Array(9).concat(567));
  857. assertEquals(567, message.getAone());
  858. assertEquals(0, message.getAtwo());
  859. assertEquals(
  860. proto.jspb.test.TestMessageWithOneof.DefaultOneofACase.AONE,
  861. message.getDefaultOneofACase());
  862. message =
  863. new proto.jspb.test.TestMessageWithOneof(new Array(10).concat(890));
  864. assertEquals(1234, message.getAone());
  865. assertEquals(890, message.getAtwo());
  866. assertEquals(
  867. proto.jspb.test.TestMessageWithOneof.DefaultOneofACase.ATWO,
  868. message.getDefaultOneofACase());
  869. message =
  870. new proto.jspb.test.TestMessageWithOneof(new Array(9).concat(567, 890));
  871. assertEquals(1234, message.getAone());
  872. assertEquals(890, message.getAtwo());
  873. assertEquals(
  874. proto.jspb.test.TestMessageWithOneof.DefaultOneofACase.ATWO,
  875. message.getDefaultOneofACase());
  876. });
  877. it('testInitializeMessageWithOneofDefaults_defaultNotSetOnFirstField',
  878. function() {
  879. var message;
  880. message =
  881. new proto.jspb.test.TestMessageWithOneof(new Array(11).concat(567));
  882. assertEquals(567, message.getBone());
  883. assertEquals(1234, message.getBtwo());
  884. assertEquals(
  885. proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase.BONE,
  886. message.getDefaultOneofBCase());
  887. message =
  888. new proto.jspb.test.TestMessageWithOneof(new Array(12).concat(890));
  889. assertEquals(0, message.getBone());
  890. assertEquals(890, message.getBtwo());
  891. assertEquals(
  892. proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase.BTWO,
  893. message.getDefaultOneofBCase());
  894. message = new proto.jspb.test.TestMessageWithOneof(
  895. new Array(11).concat(567, 890));
  896. assertEquals(0, message.getBone());
  897. assertEquals(890, message.getBtwo());
  898. assertEquals(
  899. proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase.BTWO,
  900. message.getDefaultOneofBCase());
  901. });
  902. it('testOneofContainingAnotherMessage', function() {
  903. var message = new proto.jspb.test.TestMessageWithOneof;
  904. assertEquals(
  905. proto.jspb.test.TestMessageWithOneof.RecursiveOneofCase
  906. .RECURSIVE_ONEOF_NOT_SET,
  907. message.getRecursiveOneofCase());
  908. var other = new proto.jspb.test.TestMessageWithOneof;
  909. message.setRone(other);
  910. assertEquals(other, message.getRone());
  911. assertEquals('', message.getRtwo());
  912. assertEquals(
  913. proto.jspb.test.TestMessageWithOneof.RecursiveOneofCase.RONE,
  914. message.getRecursiveOneofCase());
  915. message.setRtwo('hi');
  916. assertUndefined(message.getRone());
  917. assertEquals('hi', message.getRtwo());
  918. assertEquals(
  919. proto.jspb.test.TestMessageWithOneof.RecursiveOneofCase.RTWO,
  920. message.getRecursiveOneofCase());
  921. });
  922. it('testQueryingOneofCaseEnsuresOnlyOneFieldIsSetInUnderlyingArray',
  923. function() {
  924. var message = new proto.jspb.test.TestMessageWithOneof;
  925. message.setPone('x');
  926. assertEquals('x', message.getPone());
  927. assertEquals('', message.getPthree());
  928. assertEquals(
  929. proto.jspb.test.TestMessageWithOneof.PartialOneofCase.PONE,
  930. message.getPartialOneofCase());
  931. var array = message.toArray();
  932. assertEquals('x', array[2]);
  933. assertUndefined(array[4]);
  934. array[4] = 'y';
  935. assertEquals(
  936. proto.jspb.test.TestMessageWithOneof.PartialOneofCase.PTHREE,
  937. message.getPartialOneofCase());
  938. assertUndefined(array[2]);
  939. assertEquals('y', array[4]);
  940. });
  941. it('testFloatingPointFieldsSupportNan', function() {
  942. var assertNan = function(x) {
  943. assertTrue(
  944. 'Expected ' + x + ' (' + goog.typeOf(x) + ') to be NaN.',
  945. typeof x === 'number' && isNaN(x));
  946. };
  947. var message = new proto.jspb.test.FloatingPointFields([
  948. 'NaN', 'NaN', ['NaN', 'NaN'], 'NaN', 'NaN', 'NaN', ['NaN', 'NaN'], 'NaN'
  949. ]);
  950. assertNan(message.getOptionalFloatField());
  951. assertNan(message.getRequiredFloatField());
  952. assertNan(message.getRepeatedFloatFieldList()[0]);
  953. assertNan(message.getRepeatedFloatFieldList()[1]);
  954. assertNan(message.getDefaultFloatField());
  955. assertNan(message.getOptionalDoubleField());
  956. assertNan(message.getRequiredDoubleField());
  957. assertNan(message.getRepeatedDoubleFieldList()[0]);
  958. assertNan(message.getRepeatedDoubleFieldList()[1]);
  959. assertNan(message.getDefaultDoubleField());
  960. });
  961. });