message_test.js 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  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('testEqualsNonFinite', function() {
  378. assertTrue(jspb.Message.compareFields(NaN, NaN));
  379. assertTrue(jspb.Message.compareFields(NaN, 'NaN'));
  380. assertTrue(jspb.Message.compareFields('NaN', NaN));
  381. assertTrue(jspb.Message.compareFields(Infinity, Infinity));
  382. assertTrue(jspb.Message.compareFields(Infinity, 'Infinity'));
  383. assertTrue(jspb.Message.compareFields('-Infinity', -Infinity));
  384. assertTrue(jspb.Message.compareFields([NaN], ['NaN']));
  385. assertFalse(jspb.Message.compareFields(undefined, NaN));
  386. assertFalse(jspb.Message.compareFields(NaN, undefined));
  387. });
  388. it('testToMap', function() {
  389. var p1 = new proto.jspb.test.Simple1(['k', ['v']]);
  390. var p2 = new proto.jspb.test.Simple1(['k1', ['v1', 'v2']]);
  391. var soymap = jspb.Message.toMap([p1, p2],
  392. proto.jspb.test.Simple1.prototype.getAString,
  393. proto.jspb.test.Simple1.prototype.toObject);
  394. assertEquals('k', soymap['k'].aString);
  395. assertArrayEquals(['v'], soymap['k'].aRepeatedStringList);
  396. var protomap = jspb.Message.toMap([p1, p2],
  397. proto.jspb.test.Simple1.prototype.getAString);
  398. assertEquals('k', protomap['k'].getAString());
  399. assertArrayEquals(['v'], protomap['k'].getARepeatedStringList());
  400. });
  401. it('testClone', function() {
  402. var supportsUint8Array =
  403. !goog.userAgent.IE || goog.userAgent.isVersionOrHigher('10');
  404. var original = new proto.jspb.test.TestClone();
  405. original.setStr('v1');
  406. var simple1 = new proto.jspb.test.Simple1(['x1', ['y1', 'z1']]);
  407. var simple2 = new proto.jspb.test.Simple1(['x2', ['y2', 'z2']]);
  408. var simple3 = new proto.jspb.test.Simple1(['x3', ['y3', 'z3']]);
  409. original.setSimple1(simple1);
  410. original.setSimple2List([simple2, simple3]);
  411. var bytes1 = supportsUint8Array ? new Uint8Array([1, 2, 3]) : '123';
  412. original.setBytesField(bytes1);
  413. var extension = new proto.jspb.test.CloneExtension();
  414. extension.setExt('e1');
  415. original.setExtension(proto.jspb.test.IsExtension.extField, extension);
  416. var clone = original.clone();
  417. assertArrayEquals(['v1',, ['x1', ['y1', 'z1']],,
  418. [['x2', ['y2', 'z2']], ['x3', ['y3', 'z3']]], bytes1,, { 100: [, 'e1'] }],
  419. clone.toArray());
  420. clone.setStr('v2');
  421. var simple4 = new proto.jspb.test.Simple1(['a1', ['b1', 'c1']]);
  422. var simple5 = new proto.jspb.test.Simple1(['a2', ['b2', 'c2']]);
  423. var simple6 = new proto.jspb.test.Simple1(['a3', ['b3', 'c3']]);
  424. clone.setSimple1(simple4);
  425. clone.setSimple2List([simple5, simple6]);
  426. if (supportsUint8Array) {
  427. clone.getBytesField()[0] = 4;
  428. assertObjectEquals(bytes1, original.getBytesField());
  429. }
  430. var bytes2 = supportsUint8Array ? new Uint8Array([4, 5, 6]) : '456';
  431. clone.setBytesField(bytes2);
  432. var newExtension = new proto.jspb.test.CloneExtension();
  433. newExtension.setExt('e2');
  434. clone.setExtension(proto.jspb.test.CloneExtension.extField, newExtension);
  435. assertArrayEquals(['v2',, ['a1', ['b1', 'c1']],,
  436. [['a2', ['b2', 'c2']], ['a3', ['b3', 'c3']]], bytes2,, { 100: [, 'e2'] }],
  437. clone.toArray());
  438. assertArrayEquals(['v1',, ['x1', ['y1', 'z1']],,
  439. [['x2', ['y2', 'z2']], ['x3', ['y3', 'z3']]], bytes1,, { 100: [, 'e1'] }],
  440. original.toArray());
  441. });
  442. it('testCopyInto', function() {
  443. var supportsUint8Array =
  444. !goog.userAgent.IE || goog.userAgent.isVersionOrHigher('10');
  445. var original = new proto.jspb.test.TestClone();
  446. original.setStr('v1');
  447. var dest = new proto.jspb.test.TestClone();
  448. dest.setStr('override');
  449. var simple1 = new proto.jspb.test.Simple1(['x1', ['y1', 'z1']]);
  450. var simple2 = new proto.jspb.test.Simple1(['x2', ['y2', 'z2']]);
  451. var simple3 = new proto.jspb.test.Simple1(['x3', ['y3', 'z3']]);
  452. var destSimple1 = new proto.jspb.test.Simple1(['ox1', ['oy1', 'oz1']]);
  453. var destSimple2 = new proto.jspb.test.Simple1(['ox2', ['oy2', 'oz2']]);
  454. var destSimple3 = new proto.jspb.test.Simple1(['ox3', ['oy3', 'oz3']]);
  455. original.setSimple1(simple1);
  456. original.setSimple2List([simple2, simple3]);
  457. dest.setSimple1(destSimple1);
  458. dest.setSimple2List([destSimple2, destSimple3]);
  459. var bytes1 = supportsUint8Array ? new Uint8Array([1, 2, 3]) : '123';
  460. var bytes2 = supportsUint8Array ? new Uint8Array([4, 5, 6]) : '456';
  461. original.setBytesField(bytes1);
  462. dest.setBytesField(bytes2);
  463. var extension = new proto.jspb.test.CloneExtension();
  464. extension.setExt('e1');
  465. original.setExtension(proto.jspb.test.CloneExtension.extField, extension);
  466. jspb.Message.copyInto(original, dest);
  467. assertArrayEquals(original.toArray(), dest.toArray());
  468. assertEquals('x1', dest.getSimple1().getAString());
  469. assertEquals('e1',
  470. dest.getExtension(proto.jspb.test.CloneExtension.extField).getExt());
  471. dest.getSimple1().setAString('new value');
  472. assertNotEquals(dest.getSimple1().getAString(),
  473. original.getSimple1().getAString());
  474. if (supportsUint8Array) {
  475. dest.getBytesField()[0] = 7;
  476. assertObjectEquals(bytes1, original.getBytesField());
  477. assertObjectEquals(new Uint8Array([7, 2, 3]), dest.getBytesField());
  478. } else {
  479. dest.setBytesField('789');
  480. assertObjectEquals(bytes1, original.getBytesField());
  481. assertObjectEquals('789', dest.getBytesField());
  482. }
  483. dest.getExtension(proto.jspb.test.CloneExtension.extField).
  484. setExt('new value');
  485. assertNotEquals(
  486. dest.getExtension(proto.jspb.test.CloneExtension.extField).getExt(),
  487. original.getExtension(
  488. proto.jspb.test.CloneExtension.extField).getExt());
  489. });
  490. it('testCopyInto_notSameType', function() {
  491. var a = new proto.jspb.test.TestClone();
  492. var b = new proto.jspb.test.Simple1(['str', ['s1', 's2']]);
  493. var e = assertThrows(function() {
  494. jspb.Message.copyInto(a, b);
  495. });
  496. assertContains('should have the same type', e.message);
  497. });
  498. it('testExtensions', function() {
  499. var extension1 = new proto.jspb.test.IsExtension(['ext1field']);
  500. var extension2 = new proto.jspb.test.Simple1(['str', ['s1', 's2']]);
  501. var extendable = new proto.jspb.test.HasExtensions(['v1', 'v2', 'v3']);
  502. extendable.setExtension(proto.jspb.test.IsExtension.extField, extension1);
  503. extendable.setExtension(proto.jspb.test.IndirectExtension.simple,
  504. extension2);
  505. extendable.setExtension(proto.jspb.test.IndirectExtension.str, 'xyzzy');
  506. extendable.setExtension(proto.jspb.test.IndirectExtension.repeatedStrList,
  507. ['a', 'b']);
  508. var s1 = new proto.jspb.test.Simple1(['foo', ['s1', 's2']]);
  509. var s2 = new proto.jspb.test.Simple1(['bar', ['t1', 't2']]);
  510. extendable.setExtension(
  511. proto.jspb.test.IndirectExtension.repeatedSimpleList,
  512. [s1, s2]);
  513. assertObjectEquals(extension1,
  514. extendable.getExtension(proto.jspb.test.IsExtension.extField));
  515. assertObjectEquals(extension2,
  516. extendable.getExtension(proto.jspb.test.IndirectExtension.simple));
  517. assertObjectEquals('xyzzy',
  518. extendable.getExtension(proto.jspb.test.IndirectExtension.str));
  519. assertObjectEquals(['a', 'b'], extendable.getExtension(
  520. proto.jspb.test.IndirectExtension.repeatedStrList));
  521. assertObjectEquals([s1, s2], extendable.getExtension(
  522. proto.jspb.test.IndirectExtension.repeatedSimpleList));
  523. // Not supported yet, but it should work...
  524. extendable.setExtension(proto.jspb.test.IndirectExtension.simple, null);
  525. assertNull(
  526. extendable.getExtension(proto.jspb.test.IndirectExtension.simple));
  527. extendable.setExtension(proto.jspb.test.IndirectExtension.str, null);
  528. assertNull(extendable.getExtension(proto.jspb.test.IndirectExtension.str));
  529. // Extension fields with jspb.ignore = true are ignored.
  530. assertUndefined(proto.jspb.test.IndirectExtension['ignored']);
  531. assertUndefined(proto.jspb.test.HasExtensions['ignoredFloating']);
  532. });
  533. it('testFloatingExtensions', function() {
  534. // From an autogenerated container.
  535. var extendable = new proto.jspb.test.HasExtensions(['v1', 'v2', 'v3']);
  536. var extension = new proto.jspb.test.Simple1(['foo', ['s1', 's2']]);
  537. extendable.setExtension(proto.jspb.test.simple1, extension);
  538. assertObjectEquals(extension,
  539. extendable.getExtension(proto.jspb.test.simple1));
  540. // From _lib mode.
  541. extension = new proto.jspb.test.ExtensionMessage(['s1']);
  542. extendable = new proto.jspb.test.TestExtensionsMessage([16]);
  543. extendable.setExtension(proto.jspb.test.floatingMsgField, extension);
  544. extendable.setExtension(proto.jspb.test.floatingStrField, 's2');
  545. assertObjectEquals(extension,
  546. extendable.getExtension(proto.jspb.test.floatingMsgField));
  547. assertObjectEquals('s2',
  548. extendable.getExtension(proto.jspb.test.floatingStrField));
  549. assertNotUndefined(proto.jspb.exttest.floatingMsgField);
  550. assertNotUndefined(proto.jspb.exttest.floatingMsgFieldTwo);
  551. assertNotUndefined(proto.jspb.exttest.beta.floatingStrField);
  552. });
  553. it('testNestedExtensions', function() {
  554. var extendable = new proto.jspb.exttest.nested.TestNestedExtensionsMessage();
  555. var extension = new proto.jspb.exttest.nested.TestOuterMessage.NestedExtensionMessage(['s1']);
  556. extendable.setExtension(proto.jspb.exttest.nested.TestOuterMessage.innerExtension, extension);
  557. assertObjectEquals(extension,
  558. extendable.getExtension(proto.jspb.exttest.nested.TestOuterMessage.innerExtension));
  559. });
  560. it('testToObject_extendedObject', function() {
  561. var extension1 = new proto.jspb.test.IsExtension(['ext1field']);
  562. var extension2 = new proto.jspb.test.Simple1(['str', ['s1', 's2'], true]);
  563. var extendable = new proto.jspb.test.HasExtensions(['v1', 'v2', 'v3']);
  564. extendable.setExtension(proto.jspb.test.IsExtension.extField, extension1);
  565. extendable.setExtension(proto.jspb.test.IndirectExtension.simple,
  566. extension2);
  567. extendable.setExtension(proto.jspb.test.IndirectExtension.str, 'xyzzy');
  568. extendable.setExtension(proto.jspb.test.IndirectExtension.repeatedStrList,
  569. ['a', 'b']);
  570. var s1 = new proto.jspb.test.Simple1(['foo', ['s1', 's2'], true]);
  571. var s2 = new proto.jspb.test.Simple1(['bar', ['t1', 't2'], false]);
  572. extendable.setExtension(
  573. proto.jspb.test.IndirectExtension.repeatedSimpleList,
  574. [s1, s2]);
  575. assertObjectEquals({
  576. str1: 'v1', str2: 'v2', str3: 'v3',
  577. extField: { ext1: 'ext1field' },
  578. simple: {
  579. aString: 'str', aRepeatedStringList: ['s1', 's2'], aBoolean: true
  580. },
  581. str: 'xyzzy',
  582. repeatedStrList: ['a', 'b'],
  583. repeatedSimpleList: [
  584. { aString: 'foo', aRepeatedStringList: ['s1', 's2'], aBoolean: true},
  585. { aString: 'bar', aRepeatedStringList: ['t1', 't2'], aBoolean: false}
  586. ]
  587. }, extendable.toObject());
  588. // Now, with instances included.
  589. assertObjectEquals({
  590. str1: 'v1', str2: 'v2', str3: 'v3',
  591. extField: {
  592. ext1: 'ext1field',
  593. $jspbMessageInstance:
  594. extendable.getExtension(proto.jspb.test.IsExtension.extField)
  595. },
  596. simple: {
  597. aString: 'str',
  598. aRepeatedStringList: ['s1', 's2'],
  599. aBoolean: true,
  600. $jspbMessageInstance:
  601. extendable.getExtension(proto.jspb.test.IndirectExtension.simple)
  602. },
  603. str: 'xyzzy',
  604. repeatedStrList: ['a', 'b'],
  605. repeatedSimpleList: [{
  606. aString: 'foo',
  607. aRepeatedStringList: ['s1', 's2'],
  608. aBoolean: true,
  609. $jspbMessageInstance: s1
  610. }, {
  611. aString: 'bar',
  612. aRepeatedStringList: ['t1', 't2'],
  613. aBoolean: false,
  614. $jspbMessageInstance: s2
  615. }],
  616. $jspbMessageInstance: extendable
  617. }, extendable.toObject(true /* opt_includeInstance */));
  618. });
  619. it('testInitialization_emptyArray', function() {
  620. var msg = new proto.jspb.test.HasExtensions([]);
  621. assertArrayEquals([], msg.toArray());
  622. });
  623. it('testInitialization_justExtensionObject', function() {
  624. var msg = new proto.jspb.test.Empty([{1: 'hi'}]);
  625. // The extensionObject is not moved from its original location.
  626. assertArrayEquals([{1: 'hi'}], msg.toArray());
  627. });
  628. it('testInitialization_incompleteList', function() {
  629. var msg = new proto.jspb.test.Empty([1, {4: 'hi'}]);
  630. // The extensionObject is not moved from its original location.
  631. assertArrayEquals([1, {4: 'hi'}], msg.toArray());
  632. });
  633. it('testInitialization_forwardCompatible', function() {
  634. var msg = new proto.jspb.test.Empty([1, 2, 3, {1: 'hi'}]);
  635. assertArrayEquals([1, 2, 3, {1: 'hi'}], msg.toArray());
  636. });
  637. it('testExtendedMessageEnsureObject',
  638. /** @suppress {visibility} */ function() {
  639. var data =
  640. new proto.jspb.test.HasExtensions(['str1', {'a_key': 'an_object'}]);
  641. assertEquals('an_object', data.extensionObject_['a_key']);
  642. });
  643. it('testToObject_hasExtensionField', function() {
  644. var data = new proto.jspb.test.HasExtensions(['str1', {100: ['ext1'], 102: ''}]);
  645. var obj = data.toObject();
  646. assertEquals('str1', obj.str1);
  647. assertEquals('ext1', obj.extField.ext1);
  648. assertEquals('', obj.str);
  649. });
  650. it('testGetExtension', function() {
  651. var data = new proto.jspb.test.HasExtensions(['str1', {100: ['ext1']}]);
  652. assertEquals('str1', data.getStr1());
  653. var extension = data.getExtension(proto.jspb.test.IsExtension.extField);
  654. assertNotNull(extension);
  655. assertEquals('ext1', extension.getExt1());
  656. });
  657. it('testSetExtension', function() {
  658. var data = new proto.jspb.test.HasExtensions();
  659. var extensionMessage = new proto.jspb.test.IsExtension(['is_extension']);
  660. data.setExtension(proto.jspb.test.IsExtension.extField, extensionMessage);
  661. var obj = data.toObject();
  662. assertNotNull(
  663. data.getExtension(proto.jspb.test.IsExtension.extField));
  664. assertEquals('is_extension', obj.extField.ext1);
  665. });
  666. /**
  667. * Note that group is long deprecated, we only support it because JsPb has
  668. * a goal of being able to generate JS classes for all proto descriptors.
  669. */
  670. it('testGroups', function() {
  671. var group = new proto.jspb.test.TestGroup();
  672. var someGroup = new proto.jspb.test.TestGroup.RepeatedGroup();
  673. someGroup.setId('g1');
  674. someGroup.setSomeBoolList([true, false]);
  675. group.setRepeatedGroupList([someGroup]);
  676. var groups = group.getRepeatedGroupList();
  677. assertEquals('g1', groups[0].getId());
  678. assertObjectEquals([true, false], groups[0].getSomeBoolList());
  679. assertObjectEquals({id: 'g1', someBoolList: [true, false]},
  680. groups[0].toObject());
  681. assertObjectEquals({
  682. repeatedGroupList: [{id: 'g1', someBoolList: [true, false]}],
  683. requiredGroup: {id: undefined},
  684. optionalGroup: undefined,
  685. requiredSimple: {aRepeatedStringList: [], aString: undefined},
  686. optionalSimple: undefined,
  687. id: undefined
  688. }, group.toObject());
  689. var group1 = new proto.jspb.test.TestGroup1();
  690. group1.setGroup(someGroup);
  691. assertEquals(someGroup, group1.getGroup());
  692. });
  693. it('testNonExtensionFieldsAfterExtensionRange', function() {
  694. var data = [{'1': 'a_string'}];
  695. var message = new proto.jspb.test.Complex(data);
  696. assertArrayEquals([], message.getARepeatedStringList());
  697. });
  698. it('testReservedGetterNames', function() {
  699. var message = new proto.jspb.test.TestReservedNames();
  700. message.setExtension$(11);
  701. message.setExtension(proto.jspb.test.TestReservedNamesExtension.foo, 12);
  702. assertEquals(11, message.getExtension$());
  703. assertEquals(12, message.getExtension(
  704. proto.jspb.test.TestReservedNamesExtension.foo));
  705. assertObjectEquals({extension: 11, foo: 12}, message.toObject());
  706. });
  707. it('testInitializeMessageWithUnsetOneof', function() {
  708. var message = new proto.jspb.test.TestMessageWithOneof([]);
  709. assertEquals(
  710. proto.jspb.test.TestMessageWithOneof.PartialOneofCase.
  711. PARTIAL_ONEOF_NOT_SET,
  712. message.getPartialOneofCase());
  713. assertEquals(
  714. proto.jspb.test.TestMessageWithOneof.RecursiveOneofCase.
  715. RECURSIVE_ONEOF_NOT_SET,
  716. message.getRecursiveOneofCase());
  717. });
  718. it('testInitializeMessageWithSingleValueSetInOneof', function() {
  719. var message = new proto.jspb.test.TestMessageWithOneof([,, 'x']);
  720. assertEquals('x', message.getPone());
  721. assertEquals('', message.getPthree());
  722. assertEquals(
  723. proto.jspb.test.TestMessageWithOneof.PartialOneofCase.PONE,
  724. message.getPartialOneofCase());
  725. });
  726. it('testKeepsLastWireValueSetInUnion_multipleValues', function() {
  727. var message = new proto.jspb.test.TestMessageWithOneof([,, 'x',, 'y']);
  728. assertEquals('', message.getPone());
  729. assertEquals('y', message.getPthree());
  730. assertEquals(
  731. proto.jspb.test.TestMessageWithOneof.PartialOneofCase.PTHREE,
  732. message.getPartialOneofCase());
  733. });
  734. it('testSettingOneofFieldClearsOthers', function() {
  735. var message = new proto.jspb.test.TestMessageWithOneof;
  736. assertEquals('', message.getPone());
  737. assertEquals('', message.getPthree());
  738. assertFalse(message.hasPone());
  739. assertFalse(message.hasPthree());
  740. message.setPone('hi');
  741. assertEquals('hi', message.getPone());
  742. assertEquals('', message.getPthree());
  743. assertTrue(message.hasPone());
  744. assertFalse(message.hasPthree());
  745. message.setPthree('bye');
  746. assertEquals('', message.getPone());
  747. assertEquals('bye', message.getPthree());
  748. assertFalse(message.hasPone());
  749. assertTrue(message.hasPthree());
  750. });
  751. it('testSettingOneofFieldDoesNotClearFieldsFromOtherUnions', function() {
  752. var other = new proto.jspb.test.TestMessageWithOneof;
  753. var message = new proto.jspb.test.TestMessageWithOneof;
  754. assertEquals('', message.getPone());
  755. assertEquals('', message.getPthree());
  756. assertUndefined(message.getRone());
  757. assertFalse(message.hasPone());
  758. assertFalse(message.hasPthree());
  759. message.setPone('hi');
  760. message.setRone(other);
  761. assertEquals('hi', message.getPone());
  762. assertEquals('', message.getPthree());
  763. assertEquals(other, message.getRone());
  764. assertTrue(message.hasPone());
  765. assertFalse(message.hasPthree());
  766. message.setPthree('bye');
  767. assertEquals('', message.getPone());
  768. assertEquals('bye', message.getPthree());
  769. assertEquals(other, message.getRone());
  770. assertFalse(message.hasPone());
  771. assertTrue(message.hasPthree());
  772. });
  773. it('testUnsetsOneofCaseWhenFieldIsCleared', function() {
  774. var message = new proto.jspb.test.TestMessageWithOneof;
  775. assertEquals(
  776. proto.jspb.test.TestMessageWithOneof.PartialOneofCase.
  777. PARTIAL_ONEOF_NOT_SET,
  778. message.getPartialOneofCase());
  779. message.setPone('hi');
  780. assertEquals(
  781. proto.jspb.test.TestMessageWithOneof.PartialOneofCase.PONE,
  782. message.getPartialOneofCase());
  783. message.clearPone();
  784. assertEquals(
  785. proto.jspb.test.TestMessageWithOneof.PartialOneofCase.
  786. PARTIAL_ONEOF_NOT_SET,
  787. message.getPartialOneofCase());
  788. });
  789. it('testMessageWithDefaultOneofValues', function() {
  790. var message = new proto.jspb.test.TestMessageWithOneof;
  791. assertEquals(1234, message.getAone());
  792. assertEquals(0, message.getAtwo());
  793. assertEquals(
  794. proto.jspb.test.TestMessageWithOneof.DefaultOneofACase
  795. .DEFAULT_ONEOF_A_NOT_SET,
  796. message.getDefaultOneofACase());
  797. message.setAone(567);
  798. assertEquals(567, message.getAone());
  799. assertEquals(0, message.getAtwo());
  800. assertEquals(
  801. proto.jspb.test.TestMessageWithOneof.DefaultOneofACase.AONE,
  802. message.getDefaultOneofACase());
  803. message.setAtwo(890);
  804. assertEquals(1234, message.getAone());
  805. assertEquals(890, message.getAtwo());
  806. assertEquals(
  807. proto.jspb.test.TestMessageWithOneof.DefaultOneofACase.ATWO,
  808. message.getDefaultOneofACase());
  809. message.clearAtwo();
  810. assertEquals(1234, message.getAone());
  811. assertEquals(0, message.getAtwo());
  812. assertEquals(
  813. proto.jspb.test.TestMessageWithOneof.DefaultOneofACase
  814. .DEFAULT_ONEOF_A_NOT_SET,
  815. message.getDefaultOneofACase());
  816. });
  817. it('testMessageWithDefaultOneofValues_defaultNotOnFirstField', function() {
  818. var message = new proto.jspb.test.TestMessageWithOneof;
  819. assertEquals(0, message.getBone());
  820. assertEquals(1234, message.getBtwo());
  821. assertFalse(message.hasBone());
  822. assertFalse(message.hasBtwo());
  823. assertEquals(
  824. proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase
  825. .DEFAULT_ONEOF_B_NOT_SET,
  826. message.getDefaultOneofBCase());
  827. message.setBone(2);
  828. assertEquals(2, message.getBone());
  829. assertEquals(1234, message.getBtwo());
  830. assertTrue(message.hasBone());
  831. assertFalse(message.hasBtwo());
  832. assertEquals(
  833. proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase.BONE,
  834. message.getDefaultOneofBCase());
  835. message.setBtwo(3);
  836. assertEquals(0, message.getBone());
  837. assertFalse(message.hasBone());
  838. assertTrue(message.hasBtwo());
  839. assertEquals(3, message.getBtwo());
  840. assertEquals(
  841. proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase.BTWO,
  842. message.getDefaultOneofBCase());
  843. message.clearBtwo();
  844. assertEquals(0, message.getBone());
  845. assertFalse(message.hasBone());
  846. assertFalse(message.hasBtwo());
  847. assertEquals(1234, message.getBtwo());
  848. assertEquals(
  849. proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase
  850. .DEFAULT_ONEOF_B_NOT_SET,
  851. message.getDefaultOneofBCase());
  852. });
  853. it('testInitializeMessageWithOneofDefaults', function() {
  854. var message =
  855. new proto.jspb.test.TestMessageWithOneof(new Array(9).concat(567));
  856. assertEquals(567, message.getAone());
  857. assertEquals(0, message.getAtwo());
  858. assertEquals(
  859. proto.jspb.test.TestMessageWithOneof.DefaultOneofACase.AONE,
  860. message.getDefaultOneofACase());
  861. message =
  862. new proto.jspb.test.TestMessageWithOneof(new Array(10).concat(890));
  863. assertEquals(1234, message.getAone());
  864. assertEquals(890, message.getAtwo());
  865. assertEquals(
  866. proto.jspb.test.TestMessageWithOneof.DefaultOneofACase.ATWO,
  867. message.getDefaultOneofACase());
  868. message =
  869. new proto.jspb.test.TestMessageWithOneof(new Array(9).concat(567, 890));
  870. assertEquals(1234, message.getAone());
  871. assertEquals(890, message.getAtwo());
  872. assertEquals(
  873. proto.jspb.test.TestMessageWithOneof.DefaultOneofACase.ATWO,
  874. message.getDefaultOneofACase());
  875. });
  876. it('testInitializeMessageWithOneofDefaults_defaultNotSetOnFirstField',
  877. function() {
  878. var message;
  879. message =
  880. new proto.jspb.test.TestMessageWithOneof(new Array(11).concat(567));
  881. assertEquals(567, message.getBone());
  882. assertEquals(1234, message.getBtwo());
  883. assertEquals(
  884. proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase.BONE,
  885. message.getDefaultOneofBCase());
  886. message =
  887. new proto.jspb.test.TestMessageWithOneof(new Array(12).concat(890));
  888. assertEquals(0, message.getBone());
  889. assertEquals(890, message.getBtwo());
  890. assertEquals(
  891. proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase.BTWO,
  892. message.getDefaultOneofBCase());
  893. message = new proto.jspb.test.TestMessageWithOneof(
  894. new Array(11).concat(567, 890));
  895. assertEquals(0, message.getBone());
  896. assertEquals(890, message.getBtwo());
  897. assertEquals(
  898. proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase.BTWO,
  899. message.getDefaultOneofBCase());
  900. });
  901. it('testOneofContainingAnotherMessage', function() {
  902. var message = new proto.jspb.test.TestMessageWithOneof;
  903. assertEquals(
  904. proto.jspb.test.TestMessageWithOneof.RecursiveOneofCase.
  905. RECURSIVE_ONEOF_NOT_SET,
  906. message.getRecursiveOneofCase());
  907. var other = new proto.jspb.test.TestMessageWithOneof;
  908. message.setRone(other);
  909. assertEquals(other, message.getRone());
  910. assertEquals('', message.getRtwo());
  911. assertEquals(
  912. proto.jspb.test.TestMessageWithOneof.RecursiveOneofCase.RONE,
  913. message.getRecursiveOneofCase());
  914. message.setRtwo('hi');
  915. assertUndefined(message.getRone());
  916. assertEquals('hi', message.getRtwo());
  917. assertEquals(
  918. proto.jspb.test.TestMessageWithOneof.RecursiveOneofCase.RTWO,
  919. message.getRecursiveOneofCase());
  920. });
  921. it('testQueryingOneofCaseEnsuresOnlyOneFieldIsSetInUnderlyingArray',
  922. function() {
  923. var message = new proto.jspb.test.TestMessageWithOneof;
  924. message.setPone('x');
  925. assertEquals('x', message.getPone());
  926. assertEquals('', message.getPthree());
  927. assertEquals(
  928. proto.jspb.test.TestMessageWithOneof.PartialOneofCase.PONE,
  929. message.getPartialOneofCase());
  930. var array = message.toArray();
  931. assertEquals('x', array[2]);
  932. assertUndefined(array[4]);
  933. array[4] = 'y';
  934. assertEquals(
  935. proto.jspb.test.TestMessageWithOneof.PartialOneofCase.PTHREE,
  936. message.getPartialOneofCase());
  937. assertUndefined(array[2]);
  938. assertEquals('y', array[4]);
  939. });
  940. it('testFloatingPointFieldsSupportNan', function() {
  941. var assertNan = function(x) {
  942. assertTrue('Expected ' + x + ' (' + goog.typeOf(x) + ') to be NaN.',
  943. goog.isNumber(x) && isNaN(x));
  944. };
  945. var message = new proto.jspb.test.FloatingPointFields([
  946. 'NaN', 'NaN', ['NaN', 'NaN'], 'NaN',
  947. 'NaN', 'NaN', ['NaN', 'NaN'], 'NaN'
  948. ]);
  949. assertNan(message.getOptionalFloatField());
  950. assertNan(message.getRequiredFloatField());
  951. assertNan(message.getRepeatedFloatFieldList()[0]);
  952. assertNan(message.getRepeatedFloatFieldList()[1]);
  953. assertNan(message.getDefaultFloatField());
  954. assertNan(message.getOptionalDoubleField());
  955. assertNan(message.getRequiredDoubleField());
  956. assertNan(message.getRepeatedDoubleFieldList()[0]);
  957. assertNan(message.getRepeatedDoubleFieldList()[1]);
  958. assertNan(message.getDefaultDoubleField());
  959. });
  960. });