message_test.js 42 KB

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