message_test.js 42 KB

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