message_test.js 42 KB

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