message_test.js 38 KB

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