message_test.js 41 KB

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