proto_test.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. // Copyright 2008 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. goog.provide('goog.proto2.messageTest');
  15. goog.setTestOnly('goog.proto2.messageTest');
  16. goog.require('goog.proto2.FieldDescriptor');
  17. goog.require('goog.testing.jsunit');
  18. goog.require('proto2.TestAllTypes');
  19. goog.require('proto2.TestDefaultParent');
  20. goog.require('someprotopackage.TestPackageTypes');
  21. function testPackage() {
  22. var message = new someprotopackage.TestPackageTypes();
  23. message.setOptionalInt32(45);
  24. message.setOtherAll(new proto2.TestAllTypes());
  25. }
  26. function testFields() {
  27. var message = new proto2.TestAllTypes();
  28. // Ensure that the fields are not set.
  29. assertFalse(message.hasOptionalInt32());
  30. assertFalse(message.hasOptionalInt64());
  31. assertFalse(message.hasOptionalUint32());
  32. assertFalse(message.hasOptionalUint64());
  33. assertFalse(message.hasOptionalSint32());
  34. assertFalse(message.hasOptionalSint64());
  35. assertFalse(message.hasOptionalFixed32());
  36. assertFalse(message.hasOptionalFixed64());
  37. assertFalse(message.hasOptionalSfixed32());
  38. assertFalse(message.hasOptionalSfixed64());
  39. assertFalse(message.hasOptionalFloat());
  40. assertFalse(message.hasOptionalDouble());
  41. assertFalse(message.hasOptionalBool());
  42. assertFalse(message.hasOptionalString());
  43. assertFalse(message.hasOptionalBytes());
  44. assertFalse(message.hasOptionalgroup());
  45. assertFalse(message.hasOptionalNestedMessage());
  46. assertFalse(message.hasOptionalNestedEnum());
  47. // Check non-set values.
  48. assertNull(message.getOptionalInt32());
  49. assertNull(message.getOptionalInt64());
  50. assertNull(message.getOptionalFloat());
  51. assertNull(message.getOptionalString());
  52. assertNull(message.getOptionalBytes());
  53. assertNull(message.getOptionalNestedMessage());
  54. assertNull(message.getOptionalNestedEnum());
  55. // Check default values.
  56. assertEquals(0, message.getOptionalInt32OrDefault());
  57. assertEquals('1', message.getOptionalInt64OrDefault());
  58. assertEquals(1.5, message.getOptionalFloatOrDefault());
  59. assertEquals('', message.getOptionalStringOrDefault());
  60. assertEquals('moo', message.getOptionalBytesOrDefault());
  61. // Set the fields.
  62. message.setOptionalInt32(101);
  63. message.setOptionalInt64('102');
  64. message.setOptionalUint32(103);
  65. message.setOptionalUint64('104');
  66. message.setOptionalSint32(105);
  67. message.setOptionalSint64('106');
  68. message.setOptionalFixed32(107);
  69. message.setOptionalFixed64('108');
  70. message.setOptionalSfixed32(109);
  71. message.setOptionalSfixed64('110');
  72. message.setOptionalFloat(111.5);
  73. message.setOptionalDouble(112.5);
  74. message.setOptionalBool(true);
  75. message.setOptionalString('test');
  76. message.setOptionalBytes('abcd');
  77. var group = new proto2.TestAllTypes.OptionalGroup();
  78. group.setA(111);
  79. message.setOptionalgroup(group);
  80. var nestedMessage = new proto2.TestAllTypes.NestedMessage();
  81. nestedMessage.setB(112);
  82. message.setOptionalNestedMessage(nestedMessage);
  83. message.setOptionalNestedEnum(proto2.TestAllTypes.NestedEnum.FOO);
  84. // Ensure that the fields are set.
  85. assertTrue(message.hasOptionalInt32());
  86. assertTrue(message.hasOptionalInt64());
  87. assertTrue(message.hasOptionalUint32());
  88. assertTrue(message.hasOptionalUint64());
  89. assertTrue(message.hasOptionalSint32());
  90. assertTrue(message.hasOptionalSint64());
  91. assertTrue(message.hasOptionalFixed32());
  92. assertTrue(message.hasOptionalFixed64());
  93. assertTrue(message.hasOptionalSfixed32());
  94. assertTrue(message.hasOptionalSfixed64());
  95. assertTrue(message.hasOptionalFloat());
  96. assertTrue(message.hasOptionalDouble());
  97. assertTrue(message.hasOptionalBool());
  98. assertTrue(message.hasOptionalString());
  99. assertTrue(message.hasOptionalBytes());
  100. assertTrue(message.hasOptionalgroup());
  101. assertTrue(message.hasOptionalNestedMessage());
  102. assertTrue(message.hasOptionalNestedEnum());
  103. // Ensure that there is a count of 1 for each of the fields.
  104. assertEquals(1, message.optionalInt32Count());
  105. assertEquals(1, message.optionalInt64Count());
  106. assertEquals(1, message.optionalUint32Count());
  107. assertEquals(1, message.optionalUint64Count());
  108. assertEquals(1, message.optionalSint32Count());
  109. assertEquals(1, message.optionalSint64Count());
  110. assertEquals(1, message.optionalFixed32Count());
  111. assertEquals(1, message.optionalFixed64Count());
  112. assertEquals(1, message.optionalSfixed32Count());
  113. assertEquals(1, message.optionalSfixed64Count());
  114. assertEquals(1, message.optionalFloatCount());
  115. assertEquals(1, message.optionalDoubleCount());
  116. assertEquals(1, message.optionalBoolCount());
  117. assertEquals(1, message.optionalStringCount());
  118. assertEquals(1, message.optionalBytesCount());
  119. assertEquals(1, message.optionalgroupCount());
  120. assertEquals(1, message.optionalNestedMessageCount());
  121. assertEquals(1, message.optionalNestedEnumCount());
  122. // Ensure that the fields have the values expected.
  123. assertEquals(101, message.getOptionalInt32());
  124. assertEquals('102', message.getOptionalInt64());
  125. assertEquals(103, message.getOptionalUint32());
  126. assertEquals('104', message.getOptionalUint64());
  127. assertEquals(105, message.getOptionalSint32());
  128. assertEquals('106', message.getOptionalSint64());
  129. assertEquals(107, message.getOptionalFixed32());
  130. assertEquals('108', message.getOptionalFixed64());
  131. assertEquals(109, message.getOptionalSfixed32());
  132. assertEquals('110', message.getOptionalSfixed64());
  133. assertEquals(111.5, message.getOptionalFloat());
  134. assertEquals(112.5, message.getOptionalDouble());
  135. assertEquals(true, message.getOptionalBool());
  136. assertEquals('test', message.getOptionalString());
  137. assertEquals('abcd', message.getOptionalBytes());
  138. assertEquals(group, message.getOptionalgroup());
  139. assertEquals(nestedMessage, message.getOptionalNestedMessage());
  140. assertEquals(
  141. proto2.TestAllTypes.NestedEnum.FOO, message.getOptionalNestedEnum());
  142. }
  143. function testRepeated() {
  144. var message = new proto2.TestAllTypes();
  145. // Ensure that the fields are not set.
  146. assertFalse(message.hasRepeatedInt32());
  147. assertFalse(message.hasRepeatedInt64());
  148. assertFalse(message.hasRepeatedUint32());
  149. assertFalse(message.hasRepeatedUint64());
  150. assertFalse(message.hasRepeatedSint32());
  151. assertFalse(message.hasRepeatedSint64());
  152. assertFalse(message.hasRepeatedFixed32());
  153. assertFalse(message.hasRepeatedFixed64());
  154. assertFalse(message.hasRepeatedSfixed32());
  155. assertFalse(message.hasRepeatedSfixed64());
  156. assertFalse(message.hasRepeatedFloat());
  157. assertFalse(message.hasRepeatedDouble());
  158. assertFalse(message.hasRepeatedBool());
  159. assertFalse(message.hasRepeatedString());
  160. assertFalse(message.hasRepeatedBytes());
  161. assertFalse(message.hasRepeatedgroup());
  162. assertFalse(message.hasRepeatedNestedMessage());
  163. assertFalse(message.hasRepeatedNestedEnum());
  164. // Expect the arrays to be empty.
  165. assertEquals(0, message.repeatedInt32Array().length);
  166. assertEquals(0, message.repeatedInt64Array().length);
  167. assertEquals(0, message.repeatedUint32Array().length);
  168. assertEquals(0, message.repeatedUint64Array().length);
  169. assertEquals(0, message.repeatedSint32Array().length);
  170. assertEquals(0, message.repeatedSint64Array().length);
  171. assertEquals(0, message.repeatedFixed32Array().length);
  172. assertEquals(0, message.repeatedFixed64Array().length);
  173. assertEquals(0, message.repeatedSfixed32Array().length);
  174. assertEquals(0, message.repeatedSfixed64Array().length);
  175. assertEquals(0, message.repeatedFloatArray().length);
  176. assertEquals(0, message.repeatedDoubleArray().length);
  177. assertEquals(0, message.repeatedBoolArray().length);
  178. assertEquals(0, message.repeatedStringArray().length);
  179. assertEquals(0, message.repeatedBytesArray().length);
  180. assertEquals(0, message.repeatedgroupArray().length);
  181. assertEquals(0, message.repeatedNestedMessageArray().length);
  182. assertEquals(0, message.repeatedNestedEnumArray().length);
  183. // Set the fields.
  184. message.addRepeatedInt32(101);
  185. message.addRepeatedInt64('102');
  186. message.addRepeatedUint32(103);
  187. message.addRepeatedUint64('104');
  188. message.addRepeatedSint32(105);
  189. message.addRepeatedSint64('106');
  190. message.addRepeatedFixed32(107);
  191. message.addRepeatedFixed64('108');
  192. message.addRepeatedSfixed32(109);
  193. message.addRepeatedSfixed64('110');
  194. message.addRepeatedFloat(111.5);
  195. message.addRepeatedDouble(112.5);
  196. message.addRepeatedBool(true);
  197. message.addRepeatedString('test');
  198. message.addRepeatedBytes('abcd');
  199. message.addRepeatedInt32(201);
  200. message.addRepeatedInt64('202');
  201. message.addRepeatedUint32(203);
  202. message.addRepeatedUint64('204');
  203. message.addRepeatedSint32(205);
  204. message.addRepeatedSint64('206');
  205. message.addRepeatedFixed32(207);
  206. message.addRepeatedFixed64('208');
  207. message.addRepeatedSfixed32(209);
  208. message.addRepeatedSfixed64('210');
  209. message.addRepeatedFloat(211.5);
  210. message.addRepeatedDouble(212.5);
  211. message.addRepeatedBool(true);
  212. message.addRepeatedString('test#2');
  213. message.addRepeatedBytes('efgh');
  214. var group1 = new proto2.TestAllTypes.RepeatedGroup();
  215. group1.addA(111);
  216. message.addRepeatedgroup(group1);
  217. var group2 = new proto2.TestAllTypes.RepeatedGroup();
  218. group2.addA(211);
  219. message.addRepeatedgroup(group2);
  220. var nestedMessage1 = new proto2.TestAllTypes.NestedMessage();
  221. nestedMessage1.setB(112);
  222. message.addRepeatedNestedMessage(nestedMessage1);
  223. var nestedMessage2 = new proto2.TestAllTypes.NestedMessage();
  224. nestedMessage2.setB(212);
  225. message.addRepeatedNestedMessage(nestedMessage2);
  226. message.addRepeatedNestedEnum(proto2.TestAllTypes.NestedEnum.FOO);
  227. message.addRepeatedNestedEnum(proto2.TestAllTypes.NestedEnum.BAR);
  228. // Ensure that the fields are set.
  229. assertTrue(message.hasRepeatedInt32());
  230. assertTrue(message.hasRepeatedInt64());
  231. assertTrue(message.hasRepeatedUint32());
  232. assertTrue(message.hasRepeatedUint64());
  233. assertTrue(message.hasRepeatedSint32());
  234. assertTrue(message.hasRepeatedSint64());
  235. assertTrue(message.hasRepeatedFixed32());
  236. assertTrue(message.hasRepeatedFixed64());
  237. assertTrue(message.hasRepeatedSfixed32());
  238. assertTrue(message.hasRepeatedSfixed64());
  239. assertTrue(message.hasRepeatedFloat());
  240. assertTrue(message.hasRepeatedDouble());
  241. assertTrue(message.hasRepeatedBool());
  242. assertTrue(message.hasRepeatedString());
  243. assertTrue(message.hasRepeatedBytes());
  244. assertTrue(message.hasRepeatedgroup());
  245. assertTrue(message.hasRepeatedNestedMessage());
  246. assertTrue(message.hasRepeatedNestedEnum());
  247. // Ensure that there is a count of 2 for each of the fields.
  248. assertEquals(2, message.repeatedInt32Count());
  249. assertEquals(2, message.repeatedInt64Count());
  250. assertEquals(2, message.repeatedUint32Count());
  251. assertEquals(2, message.repeatedUint64Count());
  252. assertEquals(2, message.repeatedSint32Count());
  253. assertEquals(2, message.repeatedSint64Count());
  254. assertEquals(2, message.repeatedFixed32Count());
  255. assertEquals(2, message.repeatedFixed64Count());
  256. assertEquals(2, message.repeatedSfixed32Count());
  257. assertEquals(2, message.repeatedSfixed64Count());
  258. assertEquals(2, message.repeatedFloatCount());
  259. assertEquals(2, message.repeatedDoubleCount());
  260. assertEquals(2, message.repeatedBoolCount());
  261. assertEquals(2, message.repeatedStringCount());
  262. assertEquals(2, message.repeatedBytesCount());
  263. assertEquals(2, message.repeatedgroupCount());
  264. assertEquals(2, message.repeatedNestedMessageCount());
  265. assertEquals(2, message.repeatedNestedEnumCount());
  266. // Ensure that the fields have the values expected.
  267. assertEquals(101, message.getRepeatedInt32(0));
  268. assertEquals('102', message.getRepeatedInt64(0));
  269. assertEquals(103, message.getRepeatedUint32(0));
  270. assertEquals('104', message.getRepeatedUint64(0));
  271. assertEquals(105, message.getRepeatedSint32(0));
  272. assertEquals('106', message.getRepeatedSint64(0));
  273. assertEquals(107, message.getRepeatedFixed32(0));
  274. assertEquals('108', message.getRepeatedFixed64(0));
  275. assertEquals(109, message.getRepeatedSfixed32(0));
  276. assertEquals('110', message.getRepeatedSfixed64(0));
  277. assertEquals(111.5, message.getRepeatedFloat(0));
  278. assertEquals(112.5, message.getRepeatedDouble(0));
  279. assertEquals(true, message.getRepeatedBool(0));
  280. assertEquals('test', message.getRepeatedString(0));
  281. assertEquals('abcd', message.getRepeatedBytes(0));
  282. assertEquals(group1, message.getRepeatedgroup(0));
  283. assertEquals(nestedMessage1, message.getRepeatedNestedMessage(0));
  284. assertEquals(
  285. proto2.TestAllTypes.NestedEnum.FOO, message.getRepeatedNestedEnum(0));
  286. assertEquals(201, message.getRepeatedInt32(1));
  287. assertEquals('202', message.getRepeatedInt64(1));
  288. assertEquals(203, message.getRepeatedUint32(1));
  289. assertEquals('204', message.getRepeatedUint64(1));
  290. assertEquals(205, message.getRepeatedSint32(1));
  291. assertEquals('206', message.getRepeatedSint64(1));
  292. assertEquals(207, message.getRepeatedFixed32(1));
  293. assertEquals('208', message.getRepeatedFixed64(1));
  294. assertEquals(209, message.getRepeatedSfixed32(1));
  295. assertEquals('210', message.getRepeatedSfixed64(1));
  296. assertEquals(211.5, message.getRepeatedFloat(1));
  297. assertEquals(212.5, message.getRepeatedDouble(1));
  298. assertEquals(true, message.getRepeatedBool(1));
  299. assertEquals('test#2', message.getRepeatedString(1));
  300. assertEquals('efgh', message.getRepeatedBytes(1));
  301. assertEquals(group2, message.getRepeatedgroup(1));
  302. assertEquals(nestedMessage2, message.getRepeatedNestedMessage(1));
  303. assertEquals(
  304. proto2.TestAllTypes.NestedEnum.BAR, message.getRepeatedNestedEnum(1));
  305. // Check the array lengths.
  306. assertEquals(2, message.repeatedInt32Array().length);
  307. assertEquals(2, message.repeatedInt64Array().length);
  308. assertEquals(2, message.repeatedUint32Array().length);
  309. assertEquals(2, message.repeatedUint64Array().length);
  310. assertEquals(2, message.repeatedSint32Array().length);
  311. assertEquals(2, message.repeatedSint64Array().length);
  312. assertEquals(2, message.repeatedFixed32Array().length);
  313. assertEquals(2, message.repeatedFixed64Array().length);
  314. assertEquals(2, message.repeatedSfixed32Array().length);
  315. assertEquals(2, message.repeatedSfixed64Array().length);
  316. assertEquals(2, message.repeatedFloatArray().length);
  317. assertEquals(2, message.repeatedDoubleArray().length);
  318. assertEquals(2, message.repeatedBoolArray().length);
  319. assertEquals(2, message.repeatedStringArray().length);
  320. assertEquals(2, message.repeatedBytesArray().length);
  321. assertEquals(2, message.repeatedgroupArray().length);
  322. assertEquals(2, message.repeatedNestedMessageArray().length);
  323. assertEquals(2, message.repeatedNestedEnumArray().length);
  324. // Check the array values.
  325. assertEquals(message.getRepeatedInt32(0), message.repeatedInt32Array()[0]);
  326. assertEquals(message.getRepeatedInt64(0), message.repeatedInt64Array()[0]);
  327. assertEquals(message.getRepeatedUint32(0), message.repeatedUint32Array()[0]);
  328. assertEquals(message.getRepeatedUint64(0), message.repeatedUint64Array()[0]);
  329. assertEquals(message.getRepeatedSint32(0), message.repeatedSint32Array()[0]);
  330. assertEquals(message.getRepeatedSint64(0), message.repeatedSint64Array()[0]);
  331. assertEquals(
  332. message.getRepeatedFixed32(0), message.repeatedFixed32Array()[0]);
  333. assertEquals(
  334. message.getRepeatedFixed64(0), message.repeatedFixed64Array()[0]);
  335. assertEquals(
  336. message.getRepeatedSfixed32(0), message.repeatedSfixed32Array()[0]);
  337. assertEquals(
  338. message.getRepeatedSfixed64(0), message.repeatedSfixed64Array()[0]);
  339. assertEquals(message.getRepeatedFloat(0), message.repeatedFloatArray()[0]);
  340. assertEquals(message.getRepeatedDouble(0), message.repeatedDoubleArray()[0]);
  341. assertEquals(message.getRepeatedBool(0), message.repeatedBoolArray()[0]);
  342. assertEquals(message.getRepeatedString(0), message.repeatedStringArray()[0]);
  343. assertEquals(message.getRepeatedBytes(0), message.repeatedBytesArray()[0]);
  344. assertEquals(message.getRepeatedgroup(0), message.repeatedgroupArray()[0]);
  345. assertEquals(
  346. message.getRepeatedNestedMessage(0),
  347. message.repeatedNestedMessageArray()[0]);
  348. assertEquals(
  349. message.getRepeatedNestedEnum(0), message.repeatedNestedEnumArray()[0]);
  350. assertEquals(message.getRepeatedInt32(1), message.repeatedInt32Array()[1]);
  351. assertEquals(message.getRepeatedInt64(1), message.repeatedInt64Array()[1]);
  352. assertEquals(message.getRepeatedUint32(1), message.repeatedUint32Array()[1]);
  353. assertEquals(message.getRepeatedUint64(1), message.repeatedUint64Array()[1]);
  354. assertEquals(message.getRepeatedSint32(1), message.repeatedSint32Array()[1]);
  355. assertEquals(message.getRepeatedSint64(1), message.repeatedSint64Array()[1]);
  356. assertEquals(
  357. message.getRepeatedFixed32(1), message.repeatedFixed32Array()[1]);
  358. assertEquals(
  359. message.getRepeatedFixed64(1), message.repeatedFixed64Array()[1]);
  360. assertEquals(
  361. message.getRepeatedSfixed32(1), message.repeatedSfixed32Array()[1]);
  362. assertEquals(
  363. message.getRepeatedSfixed64(1), message.repeatedSfixed64Array()[1]);
  364. assertEquals(message.getRepeatedFloat(1), message.repeatedFloatArray()[1]);
  365. assertEquals(message.getRepeatedDouble(1), message.repeatedDoubleArray()[1]);
  366. assertEquals(message.getRepeatedBool(1), message.repeatedBoolArray()[1]);
  367. assertEquals(message.getRepeatedString(1), message.repeatedStringArray()[1]);
  368. assertEquals(message.getRepeatedBytes(1), message.repeatedBytesArray()[1]);
  369. assertEquals(message.getRepeatedgroup(1), message.repeatedgroupArray()[1]);
  370. assertEquals(
  371. message.getRepeatedNestedMessage(1),
  372. message.repeatedNestedMessageArray()[1]);
  373. assertEquals(
  374. message.getRepeatedNestedEnum(1), message.repeatedNestedEnumArray()[1]);
  375. }
  376. function testDescriptor() {
  377. var message = new proto2.TestAllTypes();
  378. var descriptor = message.getDescriptor();
  379. assertEquals('TestAllTypes', descriptor.getName());
  380. assertEquals('TestAllTypes', descriptor.getFullName());
  381. assertEquals(null, descriptor.getContainingType());
  382. var nestedMessage = new proto2.TestAllTypes.NestedMessage();
  383. var nestedDescriptor = nestedMessage.getDescriptor();
  384. assertEquals('NestedMessage', nestedDescriptor.getName());
  385. assertEquals('TestAllTypes.NestedMessage', nestedDescriptor.getFullName());
  386. assertEquals(descriptor, nestedDescriptor.getContainingType());
  387. }
  388. function testFieldDescriptor() {
  389. var message = new proto2.TestAllTypes();
  390. var descriptor = message.getDescriptor();
  391. var fields = descriptor.getFields();
  392. assertEquals(53, fields.length);
  393. // Check the containing types.
  394. for (var i = 0; i < fields.length; ++i) {
  395. assertEquals(descriptor, fields[i].getContainingType());
  396. }
  397. // Check the field names.
  398. assertEquals('optional_int32', fields[0].getName());
  399. assertEquals('optional_int64', fields[1].getName());
  400. assertEquals('optional_uint32', fields[2].getName());
  401. assertEquals('optional_uint64', fields[3].getName());
  402. assertEquals('optional_sint32', fields[4].getName());
  403. assertEquals('optional_sint64', fields[5].getName());
  404. assertEquals('optional_fixed32', fields[6].getName());
  405. assertEquals('optional_fixed64', fields[7].getName());
  406. assertEquals('optional_sfixed32', fields[8].getName());
  407. assertEquals('optional_sfixed64', fields[9].getName());
  408. assertEquals('optional_float', fields[10].getName());
  409. assertEquals('optional_double', fields[11].getName());
  410. assertEquals('optional_bool', fields[12].getName());
  411. assertEquals('optional_string', fields[13].getName());
  412. assertEquals('optional_bytes', fields[14].getName());
  413. assertEquals('optionalgroup', fields[15].getName());
  414. assertEquals('optional_nested_message', fields[16].getName());
  415. assertEquals('optional_nested_enum', fields[17].getName());
  416. assertEquals('repeated_int32', fields[18].getName());
  417. assertEquals('repeated_int64', fields[19].getName());
  418. assertEquals('repeated_uint32', fields[20].getName());
  419. assertEquals('repeated_uint64', fields[21].getName());
  420. assertEquals('repeated_sint32', fields[22].getName());
  421. assertEquals('repeated_sint64', fields[23].getName());
  422. assertEquals('repeated_fixed32', fields[24].getName());
  423. assertEquals('repeated_fixed64', fields[25].getName());
  424. assertEquals('repeated_sfixed32', fields[26].getName());
  425. assertEquals('repeated_sfixed64', fields[27].getName());
  426. assertEquals('repeated_float', fields[28].getName());
  427. assertEquals('repeated_double', fields[29].getName());
  428. assertEquals('repeated_bool', fields[30].getName());
  429. assertEquals('repeated_string', fields[31].getName());
  430. assertEquals('repeated_bytes', fields[32].getName());
  431. assertEquals('repeatedgroup', fields[33].getName());
  432. assertEquals('repeated_nested_message', fields[34].getName());
  433. assertEquals('repeated_nested_enum', fields[35].getName());
  434. assertEquals('optional_int64_number', fields[36].getName());
  435. assertEquals('optional_int64_string', fields[37].getName());
  436. assertEquals('repeated_int64_number', fields[38].getName());
  437. assertEquals('repeated_int64_string', fields[39].getName());
  438. assertEquals('packed_int32', fields[40].getName());
  439. assertEquals('packed_int64', fields[41].getName());
  440. assertEquals('packed_uint32', fields[42].getName());
  441. assertEquals('packed_uint64', fields[43].getName());
  442. assertEquals('packed_sint32', fields[44].getName());
  443. assertEquals('packed_sint64', fields[45].getName());
  444. assertEquals('packed_fixed32', fields[46].getName());
  445. assertEquals('packed_fixed64', fields[47].getName());
  446. assertEquals('packed_sfixed32', fields[48].getName());
  447. assertEquals('packed_sfixed64', fields[49].getName());
  448. assertEquals('packed_float', fields[50].getName());
  449. assertEquals('packed_double', fields[51].getName());
  450. assertEquals('packed_bool', fields[52].getName());
  451. // Check the field types.
  452. var FieldType = goog.proto2.FieldDescriptor.FieldType;
  453. assertEquals(FieldType.INT32, fields[0].getFieldType());
  454. assertEquals(FieldType.INT64, fields[1].getFieldType());
  455. assertEquals(FieldType.UINT32, fields[2].getFieldType());
  456. assertEquals(FieldType.UINT64, fields[3].getFieldType());
  457. assertEquals(FieldType.SINT32, fields[4].getFieldType());
  458. assertEquals(FieldType.SINT64, fields[5].getFieldType());
  459. assertEquals(FieldType.FIXED32, fields[6].getFieldType());
  460. assertEquals(FieldType.FIXED64, fields[7].getFieldType());
  461. assertEquals(FieldType.SFIXED32, fields[8].getFieldType());
  462. assertEquals(FieldType.SFIXED64, fields[9].getFieldType());
  463. assertEquals(FieldType.FLOAT, fields[10].getFieldType());
  464. assertEquals(FieldType.DOUBLE, fields[11].getFieldType());
  465. assertEquals(FieldType.BOOL, fields[12].getFieldType());
  466. assertEquals(FieldType.STRING, fields[13].getFieldType());
  467. assertEquals(FieldType.BYTES, fields[14].getFieldType());
  468. assertEquals(FieldType.GROUP, fields[15].getFieldType());
  469. assertEquals(FieldType.MESSAGE, fields[16].getFieldType());
  470. assertEquals(FieldType.ENUM, fields[17].getFieldType());
  471. assertEquals(FieldType.INT32, fields[18].getFieldType());
  472. assertEquals(FieldType.INT64, fields[19].getFieldType());
  473. assertEquals(FieldType.UINT32, fields[20].getFieldType());
  474. assertEquals(FieldType.UINT64, fields[21].getFieldType());
  475. assertEquals(FieldType.SINT32, fields[22].getFieldType());
  476. assertEquals(FieldType.SINT64, fields[23].getFieldType());
  477. assertEquals(FieldType.FIXED32, fields[24].getFieldType());
  478. assertEquals(FieldType.FIXED64, fields[25].getFieldType());
  479. assertEquals(FieldType.SFIXED32, fields[26].getFieldType());
  480. assertEquals(FieldType.SFIXED64, fields[27].getFieldType());
  481. assertEquals(FieldType.FLOAT, fields[28].getFieldType());
  482. assertEquals(FieldType.DOUBLE, fields[29].getFieldType());
  483. assertEquals(FieldType.BOOL, fields[30].getFieldType());
  484. assertEquals(FieldType.STRING, fields[31].getFieldType());
  485. assertEquals(FieldType.BYTES, fields[32].getFieldType());
  486. assertEquals(FieldType.GROUP, fields[33].getFieldType());
  487. assertEquals(FieldType.MESSAGE, fields[34].getFieldType());
  488. assertEquals(FieldType.ENUM, fields[35].getFieldType());
  489. assertEquals(FieldType.INT64, fields[36].getFieldType());
  490. assertEquals(FieldType.INT64, fields[37].getFieldType());
  491. assertEquals(FieldType.INT64, fields[38].getFieldType());
  492. assertEquals(FieldType.INT64, fields[39].getFieldType());
  493. assertEquals(FieldType.INT32, fields[40].getFieldType());
  494. assertEquals(FieldType.INT64, fields[41].getFieldType());
  495. assertEquals(FieldType.UINT32, fields[42].getFieldType());
  496. assertEquals(FieldType.UINT64, fields[43].getFieldType());
  497. assertEquals(FieldType.SINT32, fields[44].getFieldType());
  498. assertEquals(FieldType.SINT64, fields[45].getFieldType());
  499. assertEquals(FieldType.FIXED32, fields[46].getFieldType());
  500. assertEquals(FieldType.FIXED64, fields[47].getFieldType());
  501. assertEquals(FieldType.SFIXED32, fields[48].getFieldType());
  502. assertEquals(FieldType.SFIXED64, fields[49].getFieldType());
  503. assertEquals(FieldType.FLOAT, fields[50].getFieldType());
  504. assertEquals(FieldType.DOUBLE, fields[51].getFieldType());
  505. assertEquals(FieldType.BOOL, fields[52].getFieldType());
  506. // Check the field native types.
  507. // Singular.
  508. assertEquals(Number, fields[0].getNativeType());
  509. assertEquals(
  510. String, fields[1].getNativeType()); // 64 bit values are strings.
  511. assertEquals(Number, fields[2].getNativeType());
  512. assertEquals(String, fields[3].getNativeType());
  513. assertEquals(Number, fields[4].getNativeType());
  514. assertEquals(String, fields[5].getNativeType());
  515. assertEquals(Number, fields[6].getNativeType());
  516. assertEquals(String, fields[7].getNativeType());
  517. assertEquals(Number, fields[8].getNativeType());
  518. assertEquals(String, fields[9].getNativeType());
  519. assertEquals(Number, fields[10].getNativeType());
  520. assertEquals(Number, fields[11].getNativeType());
  521. assertEquals(Boolean, fields[12].getNativeType());
  522. assertEquals(String, fields[13].getNativeType());
  523. assertEquals(String, fields[14].getNativeType());
  524. assertEquals(proto2.TestAllTypes.OptionalGroup, fields[15].getNativeType());
  525. assertEquals(proto2.TestAllTypes.NestedMessage, fields[16].getNativeType());
  526. assertEquals(proto2.TestAllTypes.NestedEnum, fields[17].getNativeType());
  527. assertEquals(Number, fields[36].getNativeType()); // [jstype="number"]
  528. assertEquals(String, fields[37].getNativeType());
  529. // Repeated.
  530. assertEquals(Number, fields[18].getNativeType());
  531. assertEquals(String, fields[19].getNativeType());
  532. assertEquals(Number, fields[20].getNativeType());
  533. assertEquals(String, fields[21].getNativeType());
  534. assertEquals(Number, fields[22].getNativeType());
  535. assertEquals(String, fields[23].getNativeType());
  536. assertEquals(Number, fields[24].getNativeType());
  537. assertEquals(String, fields[25].getNativeType());
  538. assertEquals(Number, fields[26].getNativeType());
  539. assertEquals(String, fields[27].getNativeType());
  540. assertEquals(Number, fields[28].getNativeType());
  541. assertEquals(Number, fields[29].getNativeType());
  542. assertEquals(Boolean, fields[30].getNativeType());
  543. assertEquals(String, fields[31].getNativeType());
  544. assertEquals(String, fields[32].getNativeType());
  545. assertEquals(proto2.TestAllTypes.RepeatedGroup, fields[33].getNativeType());
  546. assertEquals(proto2.TestAllTypes.NestedMessage, fields[34].getNativeType());
  547. assertEquals(proto2.TestAllTypes.NestedEnum, fields[35].getNativeType());
  548. assertEquals(Number, fields[38].getNativeType()); // [jstype="number"]
  549. assertEquals(String, fields[39].getNativeType());
  550. // Packed (only numeric types can be packed).
  551. assertEquals(Number, fields[40].getNativeType());
  552. assertEquals(Number, fields[41].getNativeType());
  553. assertEquals(Number, fields[42].getNativeType());
  554. assertEquals(Number, fields[43].getNativeType());
  555. assertEquals(Number, fields[44].getNativeType());
  556. assertEquals(Number, fields[45].getNativeType());
  557. assertEquals(Number, fields[46].getNativeType());
  558. assertEquals(Number, fields[47].getNativeType());
  559. assertEquals(Number, fields[48].getNativeType());
  560. assertEquals(Number, fields[49].getNativeType());
  561. assertEquals(Number, fields[50].getNativeType());
  562. assertEquals(Number, fields[51].getNativeType());
  563. assertEquals(Boolean, fields[52].getNativeType());
  564. }
  565. function testUnknown() {
  566. var message = new proto2.TestAllTypes();
  567. // Set some unknown fields.
  568. message.setUnknown(1000, 101);
  569. message.setUnknown(1001, -102);
  570. message.setUnknown(1002, true);
  571. message.setUnknown(1003, 'abcd');
  572. message.setUnknown(1004, ['he', 'llo']);
  573. // Ensure we find them all.
  574. var count = 0;
  575. message.forEachUnknown(function(tag, value) {
  576. if (tag == 1000) {
  577. assertEquals(101, value);
  578. }
  579. if (tag == 1001) {
  580. assertEquals(-102, value);
  581. }
  582. if (tag == 1002) {
  583. assertEquals(true, value);
  584. }
  585. if (tag == 1003) {
  586. assertEquals('abcd', value);
  587. }
  588. if (tag == 1004) {
  589. assertEquals('he', value[0]);
  590. assertEquals('llo', value[1]);
  591. }
  592. count++;
  593. });
  594. assertEquals(5, count);
  595. }
  596. function testReflection() {
  597. var message = new proto2.TestAllTypes();
  598. var descriptor = message.getDescriptor();
  599. var optionalInt = descriptor.findFieldByName('optional_int32');
  600. var optionalString = descriptor.findFieldByName('optional_string');
  601. var repeatedInt64 = descriptor.findFieldByName('repeated_int64');
  602. var optionalWrong = descriptor.findFieldByName('foo_bar');
  603. assertFalse(optionalInt == null);
  604. assertFalse(optionalString == null);
  605. assertFalse(repeatedInt64 == null);
  606. assertTrue(optionalWrong == null);
  607. // Check to ensure the fields are empty.
  608. assertFalse(message.has(optionalInt));
  609. assertFalse(message.has(optionalString));
  610. assertFalse(message.has(repeatedInt64));
  611. assertEquals(0, message.arrayOf(repeatedInt64).length);
  612. // Check default values.
  613. assertEquals(0, message.getOrDefault(optionalInt));
  614. assertEquals('', message.getOrDefault(optionalString));
  615. // Set some of the fields.
  616. message.set(optionalString, 'hello!');
  617. message.add(repeatedInt64, '101');
  618. message.add(repeatedInt64, '102');
  619. // Check the fields.
  620. assertFalse(message.has(optionalInt));
  621. assertTrue(message.has(optionalString));
  622. assertTrue(message.hasOptionalString());
  623. assertTrue(message.has(repeatedInt64));
  624. assertTrue(message.hasRepeatedInt64());
  625. // Check the values.
  626. assertEquals('hello!', message.get(optionalString));
  627. assertEquals('hello!', message.getOptionalString());
  628. assertEquals('101', message.get(repeatedInt64, 0));
  629. assertEquals('102', message.get(repeatedInt64, 1));
  630. assertEquals('101', message.getRepeatedInt64(0));
  631. assertEquals('102', message.getRepeatedInt64(1));
  632. // Check the count.
  633. assertEquals(0, message.countOf(optionalInt));
  634. assertEquals(1, message.countOf(optionalString));
  635. assertEquals(1, message.optionalStringCount());
  636. assertEquals(2, message.countOf(repeatedInt64));
  637. assertEquals(2, message.repeatedInt64Count());
  638. // Check the array.
  639. assertEquals(2, message.arrayOf(repeatedInt64).length);
  640. assertEquals(
  641. message.get(repeatedInt64, 0), message.arrayOf(repeatedInt64)[0]);
  642. assertEquals(
  643. message.get(repeatedInt64, 1), message.arrayOf(repeatedInt64)[1]);
  644. }
  645. function testDefaultValuesForMessages() {
  646. var message = new proto2.TestDefaultParent();
  647. // Ideally this object would be immutable, but the current API does not
  648. // enforce that behavior, so get**OrDefault returns a new instance every time.
  649. var child = message.getChildOrDefault();
  650. child.setFoo(false);
  651. // Changing the value returned by get**OrDefault does not actually change
  652. // the value stored in the parent message.
  653. assertFalse(message.hasChild());
  654. assertNull(message.getChild());
  655. var message2 = new proto2.TestDefaultParent();
  656. var child2 = message2.getChildOrDefault();
  657. assertNull(message2.getChild());
  658. // The parent message returns a different object for the default.
  659. assertNotEquals(child, child2);
  660. // You've only changed the value of child, so child2 should be unaffected.
  661. assertFalse(child2.hasFoo());
  662. assertTrue(child2.getFooOrDefault());
  663. }