message_test.js 42 KB

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