message_test.js 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  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. // TODO(haberman): codegen changes to properly ignore descriptor.proto
  487. // extensions need to be merged from google3.
  488. // assertUndefined(proto.jspb.test.IsExtension['simpleOption']);
  489. // Extension fields with jspb.ignore = true are ignored.
  490. assertUndefined(proto.jspb.test.IndirectExtension['ignored']);
  491. assertUndefined(proto.jspb.test.HasExtensions['ignoredFloating']);
  492. });
  493. it('testFloatingExtensions', function() {
  494. // From an autogenerated container.
  495. var extendable = new proto.jspb.test.HasExtensions(['v1', 'v2', 'v3']);
  496. var extension = new proto.jspb.test.Simple1(['foo', ['s1', 's2']]);
  497. extendable.setExtension(proto.jspb.test.simple1, extension);
  498. assertObjectEquals(extension,
  499. extendable.getExtension(proto.jspb.test.simple1));
  500. // From _lib mode.
  501. extension = new proto.jspb.test.ExtensionMessage(['s1']);
  502. extendable = new proto.jspb.test.TestExtensionsMessage([16]);
  503. extendable.setExtension(proto.jspb.test.floatingMsgField, extension);
  504. extendable.setExtension(proto.jspb.test.floatingStrField, 's2');
  505. assertObjectEquals(extension,
  506. extendable.getExtension(proto.jspb.test.floatingMsgField));
  507. assertObjectEquals('s2',
  508. extendable.getExtension(proto.jspb.test.floatingStrField));
  509. assertNotUndefined(proto.jspb.exttest.floatingMsgField);
  510. assertNotUndefined(proto.jspb.exttest.floatingMsgFieldTwo);
  511. assertNotUndefined(proto.jspb.exttest.beta.floatingStrField);
  512. });
  513. it('testToObject_extendedObject', function() {
  514. var extension1 = new proto.jspb.test.IsExtension(['ext1field']);
  515. var extension2 = new proto.jspb.test.Simple1(['str', ['s1', 's2'], true]);
  516. var extendable = new proto.jspb.test.HasExtensions(['v1', 'v2', 'v3']);
  517. extendable.setExtension(proto.jspb.test.IsExtension.extField, extension1);
  518. extendable.setExtension(proto.jspb.test.IndirectExtension.simple,
  519. extension2);
  520. extendable.setExtension(proto.jspb.test.IndirectExtension.str, 'xyzzy');
  521. extendable.setExtension(proto.jspb.test.IndirectExtension.repeatedStrList,
  522. ['a', 'b']);
  523. var s1 = new proto.jspb.test.Simple1(['foo', ['s1', 's2'], true]);
  524. var s2 = new proto.jspb.test.Simple1(['bar', ['t1', 't2'], false]);
  525. extendable.setExtension(
  526. proto.jspb.test.IndirectExtension.repeatedSimpleList,
  527. [s1, s2]);
  528. assertObjectEquals({
  529. str1: 'v1', str2: 'v2', str3: 'v3',
  530. extField: { ext1: 'ext1field' },
  531. simple: {
  532. aString: 'str', aRepeatedStringList: ['s1', 's2'], aBoolean: true
  533. },
  534. str: 'xyzzy',
  535. repeatedStrList: ['a', 'b'],
  536. repeatedSimpleList: [
  537. { aString: 'foo', aRepeatedStringList: ['s1', 's2'], aBoolean: true},
  538. { aString: 'bar', aRepeatedStringList: ['t1', 't2'], aBoolean: false}
  539. ]
  540. }, extendable.toObject());
  541. // Now, with instances included.
  542. assertObjectEquals({
  543. str1: 'v1', str2: 'v2', str3: 'v3',
  544. extField: {
  545. ext1: 'ext1field',
  546. $jspbMessageInstance:
  547. extendable.getExtension(proto.jspb.test.IsExtension.extField)
  548. },
  549. simple: {
  550. aString: 'str',
  551. aRepeatedStringList: ['s1', 's2'],
  552. aBoolean: true,
  553. $jspbMessageInstance:
  554. extendable.getExtension(proto.jspb.test.IndirectExtension.simple)
  555. },
  556. str: 'xyzzy',
  557. repeatedStrList: ['a', 'b'],
  558. repeatedSimpleList: [{
  559. aString: 'foo',
  560. aRepeatedStringList: ['s1', 's2'],
  561. aBoolean: true,
  562. $jspbMessageInstance: s1
  563. }, {
  564. aString: 'bar',
  565. aRepeatedStringList: ['t1', 't2'],
  566. aBoolean: false,
  567. $jspbMessageInstance: s2
  568. }],
  569. $jspbMessageInstance: extendable
  570. }, extendable.toObject(true /* opt_includeInstance */));
  571. });
  572. it('testInitialization_emptyArray', function() {
  573. var msg = new proto.jspb.test.HasExtensions([]);
  574. if (jspb.Message.MINIMIZE_MEMORY_ALLOCATIONS) {
  575. assertArrayEquals([], msg.toArray());
  576. } else {
  577. // Extension object is created past all regular fields.
  578. assertArrayEquals([,,, {}], msg.toArray());
  579. }
  580. });
  581. it('testInitialization_justExtensionObject', function() {
  582. var msg = new proto.jspb.test.Empty([{1: 'hi'}]);
  583. // The extensionObject is not moved from its original location.
  584. assertArrayEquals([{1: 'hi'}], msg.toArray());
  585. });
  586. it('testInitialization_incompleteList', function() {
  587. var msg = new proto.jspb.test.Empty([1, {4: 'hi'}]);
  588. // The extensionObject is not moved from its original location.
  589. assertArrayEquals([1, {4: 'hi'}], msg.toArray());
  590. });
  591. it('testInitialization_forwardCompatible', function() {
  592. var msg = new proto.jspb.test.Empty([1, 2, 3, {1: 'hi'}]);
  593. assertArrayEquals([1, 2, 3, {1: 'hi'}], msg.toArray());
  594. });
  595. it('testExtendedMessageEnsureObject', function() {
  596. var data = new proto.jspb.test.HasExtensions(['str1',
  597. {'a_key': 'an_object'}]);
  598. assertEquals('an_object', data.extensionObject_['a_key']);
  599. });
  600. it('testToObject_hasExtensionField', function() {
  601. var data = new proto.jspb.test.HasExtensions(['str1', {100: ['ext1']}]);
  602. var obj = data.toObject();
  603. assertEquals('str1', obj.str1);
  604. assertEquals('ext1', obj.extField.ext1);
  605. });
  606. it('testGetExtension', function() {
  607. var data = new proto.jspb.test.HasExtensions(['str1', {100: ['ext1']}]);
  608. assertEquals('str1', data.getStr1());
  609. var extension = data.getExtension(proto.jspb.test.IsExtension.extField);
  610. assertNotNull(extension);
  611. assertEquals('ext1', extension.getExt1());
  612. });
  613. it('testSetExtension', function() {
  614. var data = new proto.jspb.test.HasExtensions();
  615. var extensionMessage = new proto.jspb.test.IsExtension(['is_extension']);
  616. data.setExtension(proto.jspb.test.IsExtension.extField, extensionMessage);
  617. var obj = data.toObject();
  618. assertNotNull(
  619. data.getExtension(proto.jspb.test.IsExtension.extField));
  620. assertEquals('is_extension', obj.extField.ext1);
  621. });
  622. /**
  623. * Note that group is long deprecated, we only support it because JsPb has
  624. * a goal of being able to generate JS classes for all proto descriptors.
  625. */
  626. it('testGroups', function() {
  627. var group = new proto.jspb.test.TestGroup();
  628. var someGroup = new proto.jspb.test.TestGroup.RepeatedGroup();
  629. someGroup.setId('g1');
  630. someGroup.setSomeBoolList([true, false]);
  631. group.setRepeatedGroupList([someGroup]);
  632. var groups = group.getRepeatedGroupList();
  633. assertEquals('g1', groups[0].getId());
  634. assertObjectEquals([true, false], groups[0].getSomeBoolList());
  635. assertObjectEquals({id: 'g1', someBoolList: [true, false]},
  636. groups[0].toObject());
  637. assertObjectEquals({
  638. repeatedGroupList: [{id: 'g1', someBoolList: [true, false]}],
  639. requiredGroup: {id: undefined},
  640. optionalGroup: undefined,
  641. requiredSimple: {aRepeatedStringList: [], aString: undefined},
  642. optionalSimple: undefined,
  643. id: undefined
  644. }, group.toObject());
  645. var group1 = new proto.jspb.test.TestGroup1();
  646. group1.setGroup(someGroup);
  647. assertEquals(someGroup, group1.getGroup());
  648. });
  649. it('testNonExtensionFieldsAfterExtensionRange', function() {
  650. var data = [{'1': 'a_string'}];
  651. var message = new proto.jspb.test.Complex(data);
  652. assertArrayEquals([], message.getARepeatedStringList());
  653. });
  654. it('testReservedGetterNames', function() {
  655. var message = new proto.jspb.test.TestReservedNames();
  656. message.setExtension$(11);
  657. message.setExtension(proto.jspb.test.TestReservedNamesExtension.foo, 12);
  658. assertEquals(11, message.getExtension$());
  659. assertEquals(12, message.getExtension(
  660. proto.jspb.test.TestReservedNamesExtension.foo));
  661. assertObjectEquals({extension: 11, foo: 12}, message.toObject());
  662. });
  663. it('testInitializeMessageWithUnsetOneof', function() {
  664. var message = new proto.jspb.test.TestMessageWithOneof([]);
  665. assertEquals(
  666. proto.jspb.test.TestMessageWithOneof.PartialOneofCase.
  667. PARTIAL_ONEOF_NOT_SET,
  668. message.getPartialOneofCase());
  669. assertEquals(
  670. proto.jspb.test.TestMessageWithOneof.RecursiveOneofCase.
  671. RECURSIVE_ONEOF_NOT_SET,
  672. message.getRecursiveOneofCase());
  673. });
  674. it('testInitializeMessageWithSingleValueSetInOneof', function() {
  675. var message = new proto.jspb.test.TestMessageWithOneof([,, 'x']);
  676. assertEquals('x', message.getPone());
  677. assertUndefined(message.getPthree());
  678. assertEquals(
  679. proto.jspb.test.TestMessageWithOneof.PartialOneofCase.PONE,
  680. message.getPartialOneofCase());
  681. });
  682. it('testKeepsLastWireValueSetInUnion_multipleValues', function() {
  683. var message = new proto.jspb.test.TestMessageWithOneof([,, 'x',, 'y']);
  684. assertUndefined('x', message.getPone());
  685. assertEquals('y', message.getPthree());
  686. assertEquals(
  687. proto.jspb.test.TestMessageWithOneof.PartialOneofCase.PTHREE,
  688. message.getPartialOneofCase());
  689. });
  690. it('testSettingOneofFieldClearsOthers', function() {
  691. var message = new proto.jspb.test.TestMessageWithOneof;
  692. assertUndefined(message.getPone());
  693. assertUndefined(message.getPthree());
  694. message.setPone('hi');
  695. assertEquals('hi', message.getPone());
  696. assertUndefined(message.getPthree());
  697. message.setPthree('bye');
  698. assertUndefined(message.getPone());
  699. assertEquals('bye', message.getPthree());
  700. });
  701. it('testSettingOneofFieldDoesNotClearFieldsFromOtherUnions', function() {
  702. var other = new proto.jspb.test.TestMessageWithOneof;
  703. var message = new proto.jspb.test.TestMessageWithOneof;
  704. assertUndefined(message.getPone());
  705. assertUndefined(message.getPthree());
  706. assertUndefined(message.getRone());
  707. message.setPone('hi');
  708. message.setRone(other);
  709. assertEquals('hi', message.getPone());
  710. assertUndefined(message.getPthree());
  711. assertEquals(other, message.getRone());
  712. message.setPthree('bye');
  713. assertUndefined(message.getPone());
  714. assertEquals('bye', message.getPthree());
  715. assertEquals(other, message.getRone());
  716. });
  717. it('testUnsetsOneofCaseWhenFieldIsCleared', function() {
  718. var message = new proto.jspb.test.TestMessageWithOneof;
  719. assertEquals(
  720. proto.jspb.test.TestMessageWithOneof.PartialOneofCase.
  721. PARTIAL_ONEOF_NOT_SET,
  722. message.getPartialOneofCase());
  723. message.setPone('hi');
  724. assertEquals(
  725. proto.jspb.test.TestMessageWithOneof.PartialOneofCase.PONE,
  726. message.getPartialOneofCase());
  727. message.clearPone();
  728. assertEquals(
  729. proto.jspb.test.TestMessageWithOneof.PartialOneofCase.
  730. PARTIAL_ONEOF_NOT_SET,
  731. message.getPartialOneofCase());
  732. });
  733. it('testMessageWithDefaultOneofValues', function() {
  734. var message = new proto.jspb.test.TestMessageWithOneof;
  735. assertEquals(1234, message.getAone());
  736. assertUndefined(message.getAtwo());
  737. assertEquals(
  738. proto.jspb.test.TestMessageWithOneof.DefaultOneofACase
  739. .DEFAULT_ONEOF_A_NOT_SET,
  740. message.getDefaultOneofACase());
  741. message.setAone(567);
  742. assertEquals(567, message.getAone());
  743. assertUndefined(message.getAtwo());
  744. assertEquals(
  745. proto.jspb.test.TestMessageWithOneof.DefaultOneofACase.AONE,
  746. message.getDefaultOneofACase());
  747. message.setAtwo(890);
  748. assertEquals(1234, message.getAone());
  749. assertEquals(890, message.getAtwo());
  750. assertEquals(
  751. proto.jspb.test.TestMessageWithOneof.DefaultOneofACase.ATWO,
  752. message.getDefaultOneofACase());
  753. message.clearAtwo();
  754. assertEquals(1234, message.getAone());
  755. assertUndefined(message.getAtwo());
  756. assertEquals(
  757. proto.jspb.test.TestMessageWithOneof.DefaultOneofACase
  758. .DEFAULT_ONEOF_A_NOT_SET,
  759. message.getDefaultOneofACase());
  760. });
  761. it('testMessageWithDefaultOneofValues_defaultNotOnFirstField', function() {
  762. var message = new proto.jspb.test.TestMessageWithOneof;
  763. assertUndefined(message.getBone());
  764. assertEquals(1234, message.getBtwo());
  765. assertEquals(
  766. proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase
  767. .DEFAULT_ONEOF_B_NOT_SET,
  768. message.getDefaultOneofBCase());
  769. message.setBone(2);
  770. assertEquals(2, message.getBone());
  771. assertEquals(1234, message.getBtwo());
  772. assertEquals(
  773. proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase.BONE,
  774. message.getDefaultOneofBCase());
  775. message.setBtwo(3);
  776. assertUndefined(message.getBone());
  777. assertEquals(3, message.getBtwo());
  778. assertEquals(
  779. proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase.BTWO,
  780. message.getDefaultOneofBCase());
  781. message.clearBtwo();
  782. assertUndefined(message.getBone());
  783. assertEquals(1234, message.getBtwo());
  784. assertEquals(
  785. proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase
  786. .DEFAULT_ONEOF_B_NOT_SET,
  787. message.getDefaultOneofBCase());
  788. });
  789. it('testInitializeMessageWithOneofDefaults', function() {
  790. var message =
  791. new proto.jspb.test.TestMessageWithOneof(new Array(9).concat(567));
  792. assertEquals(567, message.getAone());
  793. assertUndefined(message.getAtwo());
  794. assertEquals(
  795. proto.jspb.test.TestMessageWithOneof.DefaultOneofACase.AONE,
  796. message.getDefaultOneofACase());
  797. message =
  798. new proto.jspb.test.TestMessageWithOneof(new Array(10).concat(890));
  799. assertEquals(1234, message.getAone());
  800. assertEquals(890, message.getAtwo());
  801. assertEquals(
  802. proto.jspb.test.TestMessageWithOneof.DefaultOneofACase.ATWO,
  803. message.getDefaultOneofACase());
  804. message =
  805. new proto.jspb.test.TestMessageWithOneof(new Array(9).concat(567,890));
  806. assertEquals(1234, message.getAone());
  807. assertEquals(890, message.getAtwo());
  808. assertEquals(
  809. proto.jspb.test.TestMessageWithOneof.DefaultOneofACase.ATWO,
  810. message.getDefaultOneofACase());
  811. });
  812. it('testInitializeMessageWithOneofDefaults_defaultNotSetOnFirstField',
  813. function() {
  814. var message;
  815. message =
  816. new proto.jspb.test.TestMessageWithOneof(new Array(11).concat(567));
  817. assertEquals(567, message.getBone());
  818. assertEquals(1234, message.getBtwo());
  819. assertEquals(
  820. proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase.BONE,
  821. message.getDefaultOneofBCase());
  822. message =
  823. new proto.jspb.test.TestMessageWithOneof(new Array(12).concat(890));
  824. assertUndefined(message.getBone());
  825. assertEquals(890, message.getBtwo());
  826. assertEquals(
  827. proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase.BTWO,
  828. message.getDefaultOneofBCase());
  829. message = new proto.jspb.test.TestMessageWithOneof(
  830. new Array(11).concat(567,890));
  831. assertUndefined(message.getBone());
  832. assertEquals(890, message.getBtwo());
  833. assertEquals(
  834. proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase.BTWO,
  835. message.getDefaultOneofBCase());
  836. });
  837. it('testOneofContainingAnotherMessage', function() {
  838. var message = new proto.jspb.test.TestMessageWithOneof;
  839. assertEquals(
  840. proto.jspb.test.TestMessageWithOneof.RecursiveOneofCase.
  841. RECURSIVE_ONEOF_NOT_SET,
  842. message.getRecursiveOneofCase());
  843. var other = new proto.jspb.test.TestMessageWithOneof;
  844. message.setRone(other);
  845. assertEquals(other, message.getRone());
  846. assertUndefined(message.getRtwo());
  847. assertEquals(
  848. proto.jspb.test.TestMessageWithOneof.RecursiveOneofCase.RONE,
  849. message.getRecursiveOneofCase());
  850. message.setRtwo('hi');
  851. assertUndefined(message.getRone());
  852. assertEquals('hi', message.getRtwo());
  853. assertEquals(
  854. proto.jspb.test.TestMessageWithOneof.RecursiveOneofCase.RTWO,
  855. message.getRecursiveOneofCase());
  856. });
  857. it('testQueryingOneofCaseEnsuresOnlyOneFieldIsSetInUnderlyingArray',
  858. function() {
  859. var message = new proto.jspb.test.TestMessageWithOneof;
  860. message.setPone('x');
  861. assertEquals('x', message.getPone());
  862. assertUndefined(message.getPthree());
  863. assertEquals(
  864. proto.jspb.test.TestMessageWithOneof.PartialOneofCase.PONE,
  865. message.getPartialOneofCase());
  866. var array = message.toArray();
  867. assertEquals('x', array[2]);
  868. assertUndefined(array[4]);
  869. array[4] = 'y';
  870. assertEquals(
  871. proto.jspb.test.TestMessageWithOneof.PartialOneofCase.PTHREE,
  872. message.getPartialOneofCase());
  873. assertUndefined(array[2]);
  874. assertEquals('y', array[4]);
  875. });
  876. });