message_test.js 41 KB

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