message_test.js 41 KB

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