message_test.js 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  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('testMessageRegistration', /** @suppress {visibility} */ function() {
  240. // goog.require(SomeResponse) will include its library, which will in
  241. // turn add SomeResponse to the message registry.
  242. assertEquals(jspb.Message.registry_['res'], proto.jspb.test.SomeResponse);
  243. });
  244. it('testClearFields', function() {
  245. var data = ['str', true, [11], [[22], [33]], ['s1', 's2']];
  246. var foo = new proto.jspb.test.OptionalFields(data);
  247. foo.clearAString();
  248. foo.clearABool();
  249. foo.clearANestedMessage();
  250. foo.clearARepeatedMessageList();
  251. foo.clearARepeatedStringList();
  252. assertEquals('', foo.getAString());
  253. assertEquals(false, foo.getABool());
  254. assertUndefined(foo.getANestedMessage());
  255. assertFalse(foo.hasAString());
  256. assertFalse(foo.hasABool());
  257. assertObjectEquals([], foo.getARepeatedMessageList());
  258. assertObjectEquals([], foo.getARepeatedStringList());
  259. // NOTE: We want the missing fields in 'expected' to be undefined,
  260. // but we actually get a sparse array instead. We could use something
  261. // like [1,undefined,2] to avoid this, except that this is still
  262. // sparse on IE. No comment...
  263. var expected = [,,, [], []];
  264. expected[0] = expected[1] = expected[2] = undefined;
  265. assertObjectEquals(expected, foo.toArray());
  266. });
  267. it('testDifferenceRawObject', /** @suppress {visibility} */ function() {
  268. var p1 = new proto.jspb.test.HasExtensions(['hi', 'diff', {}]);
  269. var p2 = new proto.jspb.test.HasExtensions(['hi', 'what',
  270. {1000: 'unique'}]);
  271. var diff = /** @type {proto.jspb.test.HasExtensions} */
  272. (jspb.Message.difference(p1, p2));
  273. assertEquals('', diff.getStr1());
  274. assertEquals('what', diff.getStr2());
  275. assertEquals('', diff.getStr3());
  276. assertEquals('unique', diff.extensionObject_[1000]);
  277. });
  278. it('testEqualsSimple', function() {
  279. var s1 = new proto.jspb.test.Simple1(['hi']);
  280. assertTrue(jspb.Message.equals(s1, new proto.jspb.test.Simple1(['hi'])));
  281. assertFalse(jspb.Message.equals(s1, new proto.jspb.test.Simple1(['bye'])));
  282. var s1b = new proto.jspb.test.Simple1(['hi', ['hello']]);
  283. assertTrue(jspb.Message.equals(s1b,
  284. new proto.jspb.test.Simple1(['hi', ['hello']])));
  285. assertTrue(jspb.Message.equals(s1b,
  286. new proto.jspb.test.Simple1(['hi', ['hello', undefined,
  287. undefined, undefined]])));
  288. assertFalse(jspb.Message.equals(s1b,
  289. new proto.jspb.test.Simple1(['no', ['hello']])));
  290. // Test with messages of different types
  291. var s2 = new proto.jspb.test.Simple2(['hi']);
  292. assertFalse(jspb.Message.equals(s1, s2));
  293. });
  294. it('testEquals_softComparison', function() {
  295. var s1 = new proto.jspb.test.Simple1(['hi', [], null]);
  296. assertTrue(jspb.Message.equals(s1,
  297. new proto.jspb.test.Simple1(['hi', []])));
  298. var s1b = new proto.jspb.test.Simple1(['hi', [], true]);
  299. assertTrue(jspb.Message.equals(s1b,
  300. new proto.jspb.test.Simple1(['hi', [], 1])));
  301. });
  302. it('testEqualsComplex', function() {
  303. var data1 = ['a',,, [, 11], [[, 22], [, 33]],, ['s1', 's2'],, 1];
  304. var data2 = ['a',,, [, 11], [[, 22], [, 34]],, ['s1', 's2'],, 1];
  305. var data3 = ['a',,, [, 11], [[, 22]],, ['s1', 's2'],, 1];
  306. var data4 = ['hi'];
  307. var c1a = new proto.jspb.test.Complex(data1);
  308. var c1b = new proto.jspb.test.Complex(data1);
  309. var c2 = new proto.jspb.test.Complex(data2);
  310. var c3 = new proto.jspb.test.Complex(data3);
  311. var s1 = new proto.jspb.test.Simple1(data4);
  312. assertTrue(jspb.Message.equals(c1a, c1b));
  313. assertFalse(jspb.Message.equals(c1a, c2));
  314. assertFalse(jspb.Message.equals(c2, c3));
  315. assertFalse(jspb.Message.equals(c1a, s1));
  316. });
  317. it('testEqualsExtensionsConstructed', function() {
  318. assertTrue(jspb.Message.equals(
  319. new proto.jspb.test.HasExtensions([]),
  320. new proto.jspb.test.HasExtensions([{}])
  321. ));
  322. assertTrue(jspb.Message.equals(
  323. new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'a'}]}]),
  324. new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'a'}]}])
  325. ));
  326. assertFalse(jspb.Message.equals(
  327. new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'a'}]}]),
  328. new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'b'}]}])
  329. ));
  330. assertTrue(jspb.Message.equals(
  331. new proto.jspb.test.HasExtensions([{100: [{200: 'a'}]}]),
  332. new proto.jspb.test.HasExtensions([{100: [{200: 'a'}]}])
  333. ));
  334. assertTrue(jspb.Message.equals(
  335. new proto.jspb.test.HasExtensions([{100: [{200: 'a'}]}]),
  336. new proto.jspb.test.HasExtensions([,,, {100: [{200: 'a'}]}])
  337. ));
  338. assertTrue(jspb.Message.equals(
  339. new proto.jspb.test.HasExtensions([,,, {100: [{200: 'a'}]}]),
  340. new proto.jspb.test.HasExtensions([{100: [{200: 'a'}]}])
  341. ));
  342. assertTrue(jspb.Message.equals(
  343. new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'a'}]}]),
  344. new proto.jspb.test.HasExtensions(['hi',,, {100: [{200: 'a'}]}])
  345. ));
  346. assertTrue(jspb.Message.equals(
  347. new proto.jspb.test.HasExtensions(['hi',,, {100: [{200: 'a'}]}]),
  348. new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'a'}]}])
  349. ));
  350. });
  351. it('testEqualsExtensionsUnconstructed', function() {
  352. assertTrue(jspb.Message.compareFields([], [{}]));
  353. assertTrue(jspb.Message.compareFields([,,, {}], []));
  354. assertTrue(jspb.Message.compareFields([,,, {}], [,, {}]));
  355. assertTrue(jspb.Message.compareFields(
  356. ['hi', {100: [{200: 'a'}]}], ['hi', {100: [{200: 'a'}]}]));
  357. assertFalse(jspb.Message.compareFields(
  358. ['hi', {100: [{200: 'a'}]}], ['hi', {100: [{200: 'b'}]}]));
  359. assertTrue(jspb.Message.compareFields(
  360. [{100: [{200: 'a'}]}], [{100: [{200: 'a'}]}]));
  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. ['hi', {100: [{200: 'a'}]}], ['hi',,, {100: [{200: 'a'}]}]));
  367. assertTrue(jspb.Message.compareFields(
  368. ['hi',,, {100: [{200: 'a'}]}], ['hi', {100: [{200: 'a'}]}]));
  369. });
  370. it('testToMap', function() {
  371. var p1 = new proto.jspb.test.Simple1(['k', ['v']]);
  372. var p2 = new proto.jspb.test.Simple1(['k1', ['v1', 'v2']]);
  373. var soymap = jspb.Message.toMap([p1, p2],
  374. proto.jspb.test.Simple1.prototype.getAString,
  375. proto.jspb.test.Simple1.prototype.toObject);
  376. assertEquals('k', soymap['k'].aString);
  377. assertArrayEquals(['v'], soymap['k'].aRepeatedStringList);
  378. var protomap = jspb.Message.toMap([p1, p2],
  379. proto.jspb.test.Simple1.prototype.getAString);
  380. assertEquals('k', protomap['k'].getAString());
  381. assertArrayEquals(['v'], protomap['k'].getARepeatedStringList());
  382. });
  383. it('testClone', function() {
  384. var supportsUint8Array =
  385. !goog.userAgent.IE || goog.userAgent.isVersionOrHigher('10');
  386. var original = new proto.jspb.test.TestClone();
  387. original.setStr('v1');
  388. var simple1 = new proto.jspb.test.Simple1(['x1', ['y1', 'z1']]);
  389. var simple2 = new proto.jspb.test.Simple1(['x2', ['y2', 'z2']]);
  390. var simple3 = new proto.jspb.test.Simple1(['x3', ['y3', 'z3']]);
  391. original.setSimple1(simple1);
  392. original.setSimple2List([simple2, simple3]);
  393. var bytes1 = supportsUint8Array ? new Uint8Array([1, 2, 3]) : '123';
  394. original.setBytesField(bytes1);
  395. var extension = new proto.jspb.test.CloneExtension();
  396. extension.setExt('e1');
  397. original.setExtension(proto.jspb.test.IsExtension.extField, extension);
  398. var clone = original.clone();
  399. assertArrayEquals(['v1',, ['x1', ['y1', 'z1']],,
  400. [['x2', ['y2', 'z2']], ['x3', ['y3', 'z3']]], bytes1,, { 100: [, 'e1'] }],
  401. clone.toArray());
  402. clone.setStr('v2');
  403. var simple4 = new proto.jspb.test.Simple1(['a1', ['b1', 'c1']]);
  404. var simple5 = new proto.jspb.test.Simple1(['a2', ['b2', 'c2']]);
  405. var simple6 = new proto.jspb.test.Simple1(['a3', ['b3', 'c3']]);
  406. clone.setSimple1(simple4);
  407. clone.setSimple2List([simple5, simple6]);
  408. if (supportsUint8Array) {
  409. clone.getBytesField()[0] = 4;
  410. assertObjectEquals(bytes1, original.getBytesField());
  411. }
  412. var bytes2 = supportsUint8Array ? new Uint8Array([4, 5, 6]) : '456';
  413. clone.setBytesField(bytes2);
  414. var newExtension = new proto.jspb.test.CloneExtension();
  415. newExtension.setExt('e2');
  416. clone.setExtension(proto.jspb.test.CloneExtension.extField, newExtension);
  417. assertArrayEquals(['v2',, ['a1', ['b1', 'c1']],,
  418. [['a2', ['b2', 'c2']], ['a3', ['b3', 'c3']]], bytes2,, { 100: [, 'e2'] }],
  419. clone.toArray());
  420. assertArrayEquals(['v1',, ['x1', ['y1', 'z1']],,
  421. [['x2', ['y2', 'z2']], ['x3', ['y3', 'z3']]], bytes1,, { 100: [, 'e1'] }],
  422. original.toArray());
  423. });
  424. it('testCopyInto', function() {
  425. var supportsUint8Array =
  426. !goog.userAgent.IE || goog.userAgent.isVersionOrHigher('10');
  427. var original = new proto.jspb.test.TestClone();
  428. original.setStr('v1');
  429. var dest = new proto.jspb.test.TestClone();
  430. dest.setStr('override');
  431. var simple1 = new proto.jspb.test.Simple1(['x1', ['y1', 'z1']]);
  432. var simple2 = new proto.jspb.test.Simple1(['x2', ['y2', 'z2']]);
  433. var simple3 = new proto.jspb.test.Simple1(['x3', ['y3', 'z3']]);
  434. var destSimple1 = new proto.jspb.test.Simple1(['ox1', ['oy1', 'oz1']]);
  435. var destSimple2 = new proto.jspb.test.Simple1(['ox2', ['oy2', 'oz2']]);
  436. var destSimple3 = new proto.jspb.test.Simple1(['ox3', ['oy3', 'oz3']]);
  437. original.setSimple1(simple1);
  438. original.setSimple2List([simple2, simple3]);
  439. dest.setSimple1(destSimple1);
  440. dest.setSimple2List([destSimple2, destSimple3]);
  441. var bytes1 = supportsUint8Array ? new Uint8Array([1, 2, 3]) : '123';
  442. var bytes2 = supportsUint8Array ? new Uint8Array([4, 5, 6]) : '456';
  443. original.setBytesField(bytes1);
  444. dest.setBytesField(bytes2);
  445. var extension = new proto.jspb.test.CloneExtension();
  446. extension.setExt('e1');
  447. original.setExtension(proto.jspb.test.CloneExtension.extField, extension);
  448. jspb.Message.copyInto(original, dest);
  449. assertArrayEquals(original.toArray(), dest.toArray());
  450. assertEquals('x1', dest.getSimple1().getAString());
  451. assertEquals('e1',
  452. dest.getExtension(proto.jspb.test.CloneExtension.extField).getExt());
  453. dest.getSimple1().setAString('new value');
  454. assertNotEquals(dest.getSimple1().getAString(),
  455. original.getSimple1().getAString());
  456. if (supportsUint8Array) {
  457. dest.getBytesField()[0] = 7;
  458. assertObjectEquals(bytes1, original.getBytesField());
  459. assertObjectEquals(new Uint8Array([7, 2, 3]), dest.getBytesField());
  460. } else {
  461. dest.setBytesField('789');
  462. assertObjectEquals(bytes1, original.getBytesField());
  463. assertObjectEquals('789', dest.getBytesField());
  464. }
  465. dest.getExtension(proto.jspb.test.CloneExtension.extField).
  466. setExt('new value');
  467. assertNotEquals(
  468. dest.getExtension(proto.jspb.test.CloneExtension.extField).getExt(),
  469. original.getExtension(
  470. proto.jspb.test.CloneExtension.extField).getExt());
  471. });
  472. it('testCopyInto_notSameType', function() {
  473. var a = new proto.jspb.test.TestClone();
  474. var b = new proto.jspb.test.Simple1(['str', ['s1', 's2']]);
  475. var e = assertThrows(function() {
  476. jspb.Message.copyInto(a, b);
  477. });
  478. assertContains('should have the same type', e.message);
  479. });
  480. it('testExtensions', function() {
  481. var extension1 = new proto.jspb.test.IsExtension(['ext1field']);
  482. var extension2 = new proto.jspb.test.Simple1(['str', ['s1', 's2']]);
  483. var extendable = new proto.jspb.test.HasExtensions(['v1', 'v2', 'v3']);
  484. extendable.setExtension(proto.jspb.test.IsExtension.extField, extension1);
  485. extendable.setExtension(proto.jspb.test.IndirectExtension.simple,
  486. extension2);
  487. extendable.setExtension(proto.jspb.test.IndirectExtension.str, 'xyzzy');
  488. extendable.setExtension(proto.jspb.test.IndirectExtension.repeatedStrList,
  489. ['a', 'b']);
  490. var s1 = new proto.jspb.test.Simple1(['foo', ['s1', 's2']]);
  491. var s2 = new proto.jspb.test.Simple1(['bar', ['t1', 't2']]);
  492. extendable.setExtension(
  493. proto.jspb.test.IndirectExtension.repeatedSimpleList,
  494. [s1, s2]);
  495. assertObjectEquals(extension1,
  496. extendable.getExtension(proto.jspb.test.IsExtension.extField));
  497. assertObjectEquals(extension2,
  498. extendable.getExtension(proto.jspb.test.IndirectExtension.simple));
  499. assertObjectEquals('xyzzy',
  500. extendable.getExtension(proto.jspb.test.IndirectExtension.str));
  501. assertObjectEquals(['a', 'b'], extendable.getExtension(
  502. proto.jspb.test.IndirectExtension.repeatedStrList));
  503. assertObjectEquals([s1, s2], extendable.getExtension(
  504. proto.jspb.test.IndirectExtension.repeatedSimpleList));
  505. // Not supported yet, but it should work...
  506. extendable.setExtension(proto.jspb.test.IndirectExtension.simple, null);
  507. assertNull(
  508. extendable.getExtension(proto.jspb.test.IndirectExtension.simple));
  509. extendable.setExtension(proto.jspb.test.IndirectExtension.str, null);
  510. assertNull(extendable.getExtension(proto.jspb.test.IndirectExtension.str));
  511. // Extension fields with jspb.ignore = true are ignored.
  512. assertUndefined(proto.jspb.test.IndirectExtension['ignored']);
  513. assertUndefined(proto.jspb.test.HasExtensions['ignoredFloating']);
  514. });
  515. it('testFloatingExtensions', function() {
  516. // From an autogenerated container.
  517. var extendable = new proto.jspb.test.HasExtensions(['v1', 'v2', 'v3']);
  518. var extension = new proto.jspb.test.Simple1(['foo', ['s1', 's2']]);
  519. extendable.setExtension(proto.jspb.test.simple1, extension);
  520. assertObjectEquals(extension,
  521. extendable.getExtension(proto.jspb.test.simple1));
  522. // From _lib mode.
  523. extension = new proto.jspb.test.ExtensionMessage(['s1']);
  524. extendable = new proto.jspb.test.TestExtensionsMessage([16]);
  525. extendable.setExtension(proto.jspb.test.floatingMsgField, extension);
  526. extendable.setExtension(proto.jspb.test.floatingStrField, 's2');
  527. assertObjectEquals(extension,
  528. extendable.getExtension(proto.jspb.test.floatingMsgField));
  529. assertObjectEquals('s2',
  530. extendable.getExtension(proto.jspb.test.floatingStrField));
  531. assertNotUndefined(proto.jspb.exttest.floatingMsgField);
  532. assertNotUndefined(proto.jspb.exttest.floatingMsgFieldTwo);
  533. assertNotUndefined(proto.jspb.exttest.beta.floatingStrField);
  534. });
  535. it('testToObject_extendedObject', function() {
  536. var extension1 = new proto.jspb.test.IsExtension(['ext1field']);
  537. var extension2 = new proto.jspb.test.Simple1(['str', ['s1', 's2'], true]);
  538. var extendable = new proto.jspb.test.HasExtensions(['v1', 'v2', 'v3']);
  539. extendable.setExtension(proto.jspb.test.IsExtension.extField, extension1);
  540. extendable.setExtension(proto.jspb.test.IndirectExtension.simple,
  541. extension2);
  542. extendable.setExtension(proto.jspb.test.IndirectExtension.str, 'xyzzy');
  543. extendable.setExtension(proto.jspb.test.IndirectExtension.repeatedStrList,
  544. ['a', 'b']);
  545. var s1 = new proto.jspb.test.Simple1(['foo', ['s1', 's2'], true]);
  546. var s2 = new proto.jspb.test.Simple1(['bar', ['t1', 't2'], false]);
  547. extendable.setExtension(
  548. proto.jspb.test.IndirectExtension.repeatedSimpleList,
  549. [s1, s2]);
  550. assertObjectEquals({
  551. str1: 'v1', str2: 'v2', str3: 'v3',
  552. extField: { ext1: 'ext1field' },
  553. simple: {
  554. aString: 'str', aRepeatedStringList: ['s1', 's2'], aBoolean: true
  555. },
  556. str: 'xyzzy',
  557. repeatedStrList: ['a', 'b'],
  558. repeatedSimpleList: [
  559. { aString: 'foo', aRepeatedStringList: ['s1', 's2'], aBoolean: true},
  560. { aString: 'bar', aRepeatedStringList: ['t1', 't2'], aBoolean: false}
  561. ]
  562. }, extendable.toObject());
  563. // Now, with instances included.
  564. assertObjectEquals({
  565. str1: 'v1', str2: 'v2', str3: 'v3',
  566. extField: {
  567. ext1: 'ext1field',
  568. $jspbMessageInstance:
  569. extendable.getExtension(proto.jspb.test.IsExtension.extField)
  570. },
  571. simple: {
  572. aString: 'str',
  573. aRepeatedStringList: ['s1', 's2'],
  574. aBoolean: true,
  575. $jspbMessageInstance:
  576. extendable.getExtension(proto.jspb.test.IndirectExtension.simple)
  577. },
  578. str: 'xyzzy',
  579. repeatedStrList: ['a', 'b'],
  580. repeatedSimpleList: [{
  581. aString: 'foo',
  582. aRepeatedStringList: ['s1', 's2'],
  583. aBoolean: true,
  584. $jspbMessageInstance: s1
  585. }, {
  586. aString: 'bar',
  587. aRepeatedStringList: ['t1', 't2'],
  588. aBoolean: false,
  589. $jspbMessageInstance: s2
  590. }],
  591. $jspbMessageInstance: extendable
  592. }, extendable.toObject(true /* opt_includeInstance */));
  593. });
  594. it('testInitialization_emptyArray', function() {
  595. var msg = new proto.jspb.test.HasExtensions([]);
  596. if (jspb.Message.MINIMIZE_MEMORY_ALLOCATIONS) {
  597. assertArrayEquals([], msg.toArray());
  598. } else {
  599. // Extension object is created past all regular fields.
  600. assertArrayEquals([,,, {}], msg.toArray());
  601. }
  602. });
  603. it('testInitialization_justExtensionObject', function() {
  604. var msg = new proto.jspb.test.Empty([{1: 'hi'}]);
  605. // The extensionObject is not moved from its original location.
  606. assertArrayEquals([{1: 'hi'}], msg.toArray());
  607. });
  608. it('testInitialization_incompleteList', function() {
  609. var msg = new proto.jspb.test.Empty([1, {4: 'hi'}]);
  610. // The extensionObject is not moved from its original location.
  611. assertArrayEquals([1, {4: 'hi'}], msg.toArray());
  612. });
  613. it('testInitialization_forwardCompatible', function() {
  614. var msg = new proto.jspb.test.Empty([1, 2, 3, {1: 'hi'}]);
  615. assertArrayEquals([1, 2, 3, {1: 'hi'}], msg.toArray());
  616. });
  617. it('testExtendedMessageEnsureObject',
  618. /** @suppress {visibility} */ function() {
  619. var data =
  620. new proto.jspb.test.HasExtensions(['str1', {'a_key': 'an_object'}]);
  621. assertEquals('an_object', data.extensionObject_['a_key']);
  622. });
  623. it('testToObject_hasExtensionField', function() {
  624. var data = new proto.jspb.test.HasExtensions(['str1', {100: ['ext1']}]);
  625. var obj = data.toObject();
  626. assertEquals('str1', obj.str1);
  627. assertEquals('ext1', obj.extField.ext1);
  628. });
  629. it('testGetExtension', function() {
  630. var data = new proto.jspb.test.HasExtensions(['str1', {100: ['ext1']}]);
  631. assertEquals('str1', data.getStr1());
  632. var extension = data.getExtension(proto.jspb.test.IsExtension.extField);
  633. assertNotNull(extension);
  634. assertEquals('ext1', extension.getExt1());
  635. });
  636. it('testSetExtension', function() {
  637. var data = new proto.jspb.test.HasExtensions();
  638. var extensionMessage = new proto.jspb.test.IsExtension(['is_extension']);
  639. data.setExtension(proto.jspb.test.IsExtension.extField, extensionMessage);
  640. var obj = data.toObject();
  641. assertNotNull(
  642. data.getExtension(proto.jspb.test.IsExtension.extField));
  643. assertEquals('is_extension', obj.extField.ext1);
  644. });
  645. /**
  646. * Note that group is long deprecated, we only support it because JsPb has
  647. * a goal of being able to generate JS classes for all proto descriptors.
  648. */
  649. it('testGroups', function() {
  650. var group = new proto.jspb.test.TestGroup();
  651. var someGroup = new proto.jspb.test.TestGroup.RepeatedGroup();
  652. someGroup.setId('g1');
  653. someGroup.setSomeBoolList([true, false]);
  654. group.setRepeatedGroupList([someGroup]);
  655. var groups = group.getRepeatedGroupList();
  656. assertEquals('g1', groups[0].getId());
  657. assertObjectEquals([true, false], groups[0].getSomeBoolList());
  658. assertObjectEquals({id: 'g1', someBoolList: [true, false]},
  659. groups[0].toObject());
  660. assertObjectEquals({
  661. repeatedGroupList: [{id: 'g1', someBoolList: [true, false]}],
  662. requiredGroup: {id: undefined},
  663. optionalGroup: undefined,
  664. requiredSimple: {aRepeatedStringList: [], aString: undefined},
  665. optionalSimple: undefined,
  666. id: undefined
  667. }, group.toObject());
  668. var group1 = new proto.jspb.test.TestGroup1();
  669. group1.setGroup(someGroup);
  670. assertEquals(someGroup, group1.getGroup());
  671. });
  672. it('testNonExtensionFieldsAfterExtensionRange', function() {
  673. var data = [{'1': 'a_string'}];
  674. var message = new proto.jspb.test.Complex(data);
  675. assertArrayEquals([], message.getARepeatedStringList());
  676. });
  677. it('testReservedGetterNames', function() {
  678. var message = new proto.jspb.test.TestReservedNames();
  679. message.setExtension$(11);
  680. message.setExtension(proto.jspb.test.TestReservedNamesExtension.foo, 12);
  681. assertEquals(11, message.getExtension$());
  682. assertEquals(12, message.getExtension(
  683. proto.jspb.test.TestReservedNamesExtension.foo));
  684. assertObjectEquals({extension: 11, foo: 12}, message.toObject());
  685. });
  686. it('testInitializeMessageWithUnsetOneof', function() {
  687. var message = new proto.jspb.test.TestMessageWithOneof([]);
  688. assertEquals(
  689. proto.jspb.test.TestMessageWithOneof.PartialOneofCase.
  690. PARTIAL_ONEOF_NOT_SET,
  691. message.getPartialOneofCase());
  692. assertEquals(
  693. proto.jspb.test.TestMessageWithOneof.RecursiveOneofCase.
  694. RECURSIVE_ONEOF_NOT_SET,
  695. message.getRecursiveOneofCase());
  696. });
  697. it('testInitializeMessageWithSingleValueSetInOneof', function() {
  698. var message = new proto.jspb.test.TestMessageWithOneof([,, 'x']);
  699. assertEquals('x', message.getPone());
  700. assertEquals('', message.getPthree());
  701. assertEquals(
  702. proto.jspb.test.TestMessageWithOneof.PartialOneofCase.PONE,
  703. message.getPartialOneofCase());
  704. });
  705. it('testKeepsLastWireValueSetInUnion_multipleValues', function() {
  706. var message = new proto.jspb.test.TestMessageWithOneof([,, 'x',, 'y']);
  707. assertEquals('', message.getPone());
  708. assertEquals('y', message.getPthree());
  709. assertEquals(
  710. proto.jspb.test.TestMessageWithOneof.PartialOneofCase.PTHREE,
  711. message.getPartialOneofCase());
  712. });
  713. it('testSettingOneofFieldClearsOthers', function() {
  714. var message = new proto.jspb.test.TestMessageWithOneof;
  715. assertEquals('', message.getPone());
  716. assertEquals('', message.getPthree());
  717. assertFalse(message.hasPone());
  718. assertFalse(message.hasPthree());
  719. message.setPone('hi');
  720. assertEquals('hi', message.getPone());
  721. assertEquals('', message.getPthree());
  722. assertTrue(message.hasPone());
  723. assertFalse(message.hasPthree());
  724. message.setPthree('bye');
  725. assertEquals('', message.getPone());
  726. assertEquals('bye', message.getPthree());
  727. assertFalse(message.hasPone());
  728. assertTrue(message.hasPthree());
  729. });
  730. it('testSettingOneofFieldDoesNotClearFieldsFromOtherUnions', function() {
  731. var other = new proto.jspb.test.TestMessageWithOneof;
  732. var message = new proto.jspb.test.TestMessageWithOneof;
  733. assertEquals('', message.getPone());
  734. assertEquals('', message.getPthree());
  735. assertUndefined(message.getRone());
  736. assertFalse(message.hasPone());
  737. assertFalse(message.hasPthree());
  738. message.setPone('hi');
  739. message.setRone(other);
  740. assertEquals('hi', message.getPone());
  741. assertEquals('', message.getPthree());
  742. assertEquals(other, message.getRone());
  743. assertTrue(message.hasPone());
  744. assertFalse(message.hasPthree());
  745. message.setPthree('bye');
  746. assertEquals('', message.getPone());
  747. assertEquals('bye', message.getPthree());
  748. assertEquals(other, message.getRone());
  749. assertFalse(message.hasPone());
  750. assertTrue(message.hasPthree());
  751. });
  752. it('testUnsetsOneofCaseWhenFieldIsCleared', function() {
  753. var message = new proto.jspb.test.TestMessageWithOneof;
  754. assertEquals(
  755. proto.jspb.test.TestMessageWithOneof.PartialOneofCase.
  756. PARTIAL_ONEOF_NOT_SET,
  757. message.getPartialOneofCase());
  758. message.setPone('hi');
  759. assertEquals(
  760. proto.jspb.test.TestMessageWithOneof.PartialOneofCase.PONE,
  761. message.getPartialOneofCase());
  762. message.clearPone();
  763. assertEquals(
  764. proto.jspb.test.TestMessageWithOneof.PartialOneofCase.
  765. PARTIAL_ONEOF_NOT_SET,
  766. message.getPartialOneofCase());
  767. });
  768. it('testMessageWithDefaultOneofValues', function() {
  769. var message = new proto.jspb.test.TestMessageWithOneof;
  770. assertEquals(1234, message.getAone());
  771. assertEquals(0, message.getAtwo());
  772. assertEquals(
  773. proto.jspb.test.TestMessageWithOneof.DefaultOneofACase
  774. .DEFAULT_ONEOF_A_NOT_SET,
  775. message.getDefaultOneofACase());
  776. message.setAone(567);
  777. assertEquals(567, message.getAone());
  778. assertEquals(0, message.getAtwo());
  779. assertEquals(
  780. proto.jspb.test.TestMessageWithOneof.DefaultOneofACase.AONE,
  781. message.getDefaultOneofACase());
  782. message.setAtwo(890);
  783. assertEquals(1234, message.getAone());
  784. assertEquals(890, message.getAtwo());
  785. assertEquals(
  786. proto.jspb.test.TestMessageWithOneof.DefaultOneofACase.ATWO,
  787. message.getDefaultOneofACase());
  788. message.clearAtwo();
  789. assertEquals(1234, message.getAone());
  790. assertEquals(0, message.getAtwo());
  791. assertEquals(
  792. proto.jspb.test.TestMessageWithOneof.DefaultOneofACase
  793. .DEFAULT_ONEOF_A_NOT_SET,
  794. message.getDefaultOneofACase());
  795. });
  796. it('testMessageWithDefaultOneofValues_defaultNotOnFirstField', function() {
  797. var message = new proto.jspb.test.TestMessageWithOneof;
  798. assertEquals(0, message.getBone());
  799. assertEquals(1234, message.getBtwo());
  800. assertFalse(message.hasBone());
  801. assertFalse(message.hasBtwo());
  802. assertEquals(
  803. proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase
  804. .DEFAULT_ONEOF_B_NOT_SET,
  805. message.getDefaultOneofBCase());
  806. message.setBone(2);
  807. assertEquals(2, message.getBone());
  808. assertEquals(1234, message.getBtwo());
  809. assertTrue(message.hasBone());
  810. assertFalse(message.hasBtwo());
  811. assertEquals(
  812. proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase.BONE,
  813. message.getDefaultOneofBCase());
  814. message.setBtwo(3);
  815. assertEquals(0, message.getBone());
  816. assertFalse(message.hasBone());
  817. assertTrue(message.hasBtwo());
  818. assertEquals(3, message.getBtwo());
  819. assertEquals(
  820. proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase.BTWO,
  821. message.getDefaultOneofBCase());
  822. message.clearBtwo();
  823. assertEquals(0, message.getBone());
  824. assertFalse(message.hasBone());
  825. assertFalse(message.hasBtwo());
  826. assertEquals(1234, message.getBtwo());
  827. assertEquals(
  828. proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase
  829. .DEFAULT_ONEOF_B_NOT_SET,
  830. message.getDefaultOneofBCase());
  831. });
  832. it('testInitializeMessageWithOneofDefaults', function() {
  833. var message =
  834. new proto.jspb.test.TestMessageWithOneof(new Array(9).concat(567));
  835. assertEquals(567, message.getAone());
  836. assertEquals(0, message.getAtwo());
  837. assertEquals(
  838. proto.jspb.test.TestMessageWithOneof.DefaultOneofACase.AONE,
  839. message.getDefaultOneofACase());
  840. message =
  841. new proto.jspb.test.TestMessageWithOneof(new Array(10).concat(890));
  842. assertEquals(1234, message.getAone());
  843. assertEquals(890, message.getAtwo());
  844. assertEquals(
  845. proto.jspb.test.TestMessageWithOneof.DefaultOneofACase.ATWO,
  846. message.getDefaultOneofACase());
  847. message =
  848. new proto.jspb.test.TestMessageWithOneof(new Array(9).concat(567, 890));
  849. assertEquals(1234, message.getAone());
  850. assertEquals(890, message.getAtwo());
  851. assertEquals(
  852. proto.jspb.test.TestMessageWithOneof.DefaultOneofACase.ATWO,
  853. message.getDefaultOneofACase());
  854. });
  855. it('testInitializeMessageWithOneofDefaults_defaultNotSetOnFirstField',
  856. function() {
  857. var message;
  858. message =
  859. new proto.jspb.test.TestMessageWithOneof(new Array(11).concat(567));
  860. assertEquals(567, message.getBone());
  861. assertEquals(1234, message.getBtwo());
  862. assertEquals(
  863. proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase.BONE,
  864. message.getDefaultOneofBCase());
  865. message =
  866. new proto.jspb.test.TestMessageWithOneof(new Array(12).concat(890));
  867. assertEquals(0, message.getBone());
  868. assertEquals(890, message.getBtwo());
  869. assertEquals(
  870. proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase.BTWO,
  871. message.getDefaultOneofBCase());
  872. message = new proto.jspb.test.TestMessageWithOneof(
  873. new Array(11).concat(567, 890));
  874. assertEquals(0, message.getBone());
  875. assertEquals(890, message.getBtwo());
  876. assertEquals(
  877. proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase.BTWO,
  878. message.getDefaultOneofBCase());
  879. });
  880. it('testOneofContainingAnotherMessage', function() {
  881. var message = new proto.jspb.test.TestMessageWithOneof;
  882. assertEquals(
  883. proto.jspb.test.TestMessageWithOneof.RecursiveOneofCase.
  884. RECURSIVE_ONEOF_NOT_SET,
  885. message.getRecursiveOneofCase());
  886. var other = new proto.jspb.test.TestMessageWithOneof;
  887. message.setRone(other);
  888. assertEquals(other, message.getRone());
  889. assertEquals('', message.getRtwo());
  890. assertEquals(
  891. proto.jspb.test.TestMessageWithOneof.RecursiveOneofCase.RONE,
  892. message.getRecursiveOneofCase());
  893. message.setRtwo('hi');
  894. assertUndefined(message.getRone());
  895. assertEquals('hi', message.getRtwo());
  896. assertEquals(
  897. proto.jspb.test.TestMessageWithOneof.RecursiveOneofCase.RTWO,
  898. message.getRecursiveOneofCase());
  899. });
  900. it('testQueryingOneofCaseEnsuresOnlyOneFieldIsSetInUnderlyingArray',
  901. function() {
  902. var message = new proto.jspb.test.TestMessageWithOneof;
  903. message.setPone('x');
  904. assertEquals('x', message.getPone());
  905. assertEquals('', message.getPthree());
  906. assertEquals(
  907. proto.jspb.test.TestMessageWithOneof.PartialOneofCase.PONE,
  908. message.getPartialOneofCase());
  909. var array = message.toArray();
  910. assertEquals('x', array[2]);
  911. assertUndefined(array[4]);
  912. array[4] = 'y';
  913. assertEquals(
  914. proto.jspb.test.TestMessageWithOneof.PartialOneofCase.PTHREE,
  915. message.getPartialOneofCase());
  916. assertUndefined(array[2]);
  917. assertEquals('y', array[4]);
  918. });
  919. it('testFloatingPointFieldsSupportNan', function() {
  920. var assertNan = function(x) {
  921. assertTrue('Expected ' + x + ' (' + goog.typeOf(x) + ') to be NaN.',
  922. goog.isNumber(x) && isNaN(x));
  923. };
  924. var message = new proto.jspb.test.FloatingPointFields([
  925. 'NaN', 'NaN', ['NaN', 'NaN'], 'NaN',
  926. 'NaN', 'NaN', ['NaN', 'NaN'], 'NaN'
  927. ]);
  928. assertNan(message.getOptionalFloatField());
  929. assertNan(message.getRequiredFloatField());
  930. assertNan(message.getRepeatedFloatFieldList()[0]);
  931. assertNan(message.getRepeatedFloatFieldList()[1]);
  932. assertNan(message.getDefaultFloatField());
  933. assertNan(message.getOptionalDoubleField());
  934. assertNan(message.getRequiredDoubleField());
  935. assertNan(message.getRepeatedDoubleFieldList()[0]);
  936. assertNan(message.getRepeatedDoubleFieldList()[1]);
  937. assertNan(message.getDefaultDoubleField());
  938. });
  939. });