message_test.js 41 KB

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