message_test.js 42 KB

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