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