message_test.js 42 KB

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