message_test.js 41 KB

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