message_test.js 40 KB

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