generated_message_reflection_unittest.cc 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // Author: kenton@google.com (Kenton Varda)
  31. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. //
  34. // To test GeneratedMessageReflection, we actually let the protocol compiler
  35. // generate a full protocol message implementation and then test its
  36. // reflection interface. This is much easier and more maintainable than
  37. // trying to create our own Message class for GeneratedMessageReflection
  38. // to wrap.
  39. //
  40. // The tests here closely mirror some of the tests in
  41. // compiler/cpp/unittest, except using the reflection interface
  42. // rather than generated accessors.
  43. #include <google/protobuf/generated_message_reflection.h>
  44. #include <memory>
  45. #include <google/protobuf/test_util.h>
  46. #include <google/protobuf/unittest.pb.h>
  47. #include <google/protobuf/arena.h>
  48. #include <google/protobuf/descriptor.h>
  49. #include <google/protobuf/stubs/logging.h>
  50. #include <google/protobuf/stubs/common.h>
  51. #include <google/protobuf/testing/googletest.h>
  52. #include <gtest/gtest.h>
  53. namespace google {
  54. namespace protobuf {
  55. namespace {
  56. // Shorthand to get a FieldDescriptor for a field of unittest::TestAllTypes.
  57. const FieldDescriptor* F(const string& name) {
  58. const FieldDescriptor* result =
  59. unittest::TestAllTypes::descriptor()->FindFieldByName(name);
  60. GOOGLE_CHECK(result != NULL);
  61. return result;
  62. }
  63. TEST(GeneratedMessageReflectionTest, Defaults) {
  64. // Check that all default values are set correctly in the initial message.
  65. unittest::TestAllTypes message;
  66. TestUtil::ReflectionTester reflection_tester(
  67. unittest::TestAllTypes::descriptor());
  68. reflection_tester.ExpectClearViaReflection(message);
  69. const Reflection* reflection = message.GetReflection();
  70. // Messages should return pointers to default instances until first use.
  71. // (This is not checked by ExpectClear() since it is not actually true after
  72. // the fields have been set and then cleared.)
  73. EXPECT_EQ(&unittest::TestAllTypes::OptionalGroup::default_instance(),
  74. &reflection->GetMessage(message, F("optionalgroup")));
  75. EXPECT_EQ(&unittest::TestAllTypes::NestedMessage::default_instance(),
  76. &reflection->GetMessage(message, F("optional_nested_message")));
  77. EXPECT_EQ(&unittest::ForeignMessage::default_instance(),
  78. &reflection->GetMessage(message, F("optional_foreign_message")));
  79. EXPECT_EQ(&unittest_import::ImportMessage::default_instance(),
  80. &reflection->GetMessage(message, F("optional_import_message")));
  81. }
  82. TEST(GeneratedMessageReflectionTest, Accessors) {
  83. // Set every field to a unique value then go back and check all those
  84. // values.
  85. unittest::TestAllTypes message;
  86. TestUtil::ReflectionTester reflection_tester(
  87. unittest::TestAllTypes::descriptor());
  88. reflection_tester.SetAllFieldsViaReflection(&message);
  89. TestUtil::ExpectAllFieldsSet(message);
  90. reflection_tester.ExpectAllFieldsSetViaReflection(message);
  91. reflection_tester.ModifyRepeatedFieldsViaReflection(&message);
  92. TestUtil::ExpectRepeatedFieldsModified(message);
  93. }
  94. TEST(GeneratedMessageReflectionTest, GetStringReference) {
  95. // Test that GetStringReference() returns the underlying string when it is
  96. // a normal string field.
  97. unittest::TestAllTypes message;
  98. message.set_optional_string("foo");
  99. message.add_repeated_string("foo");
  100. const Reflection* reflection = message.GetReflection();
  101. string scratch;
  102. EXPECT_EQ(&message.optional_string(),
  103. &reflection->GetStringReference(message, F("optional_string"), &scratch))
  104. << "For simple string fields, GetStringReference() should return a "
  105. "reference to the underlying string.";
  106. EXPECT_EQ(&message.repeated_string(0),
  107. &reflection->GetRepeatedStringReference(message, F("repeated_string"),
  108. 0, &scratch))
  109. << "For simple string fields, GetRepeatedStringReference() should return "
  110. "a reference to the underlying string.";
  111. }
  112. TEST(GeneratedMessageReflectionTest, DefaultsAfterClear) {
  113. // Check that after setting all fields and then clearing, getting an
  114. // embedded message does NOT return the default instance.
  115. unittest::TestAllTypes message;
  116. TestUtil::ReflectionTester reflection_tester(
  117. unittest::TestAllTypes::descriptor());
  118. TestUtil::SetAllFields(&message);
  119. message.Clear();
  120. const Reflection* reflection = message.GetReflection();
  121. EXPECT_NE(&unittest::TestAllTypes::OptionalGroup::default_instance(),
  122. &reflection->GetMessage(message, F("optionalgroup")));
  123. EXPECT_NE(&unittest::TestAllTypes::NestedMessage::default_instance(),
  124. &reflection->GetMessage(message, F("optional_nested_message")));
  125. EXPECT_NE(&unittest::ForeignMessage::default_instance(),
  126. &reflection->GetMessage(message, F("optional_foreign_message")));
  127. EXPECT_NE(&unittest_import::ImportMessage::default_instance(),
  128. &reflection->GetMessage(message, F("optional_import_message")));
  129. }
  130. TEST(GeneratedMessageReflectionTest, Swap) {
  131. unittest::TestAllTypes message1;
  132. unittest::TestAllTypes message2;
  133. TestUtil::SetAllFields(&message1);
  134. const Reflection* reflection = message1.GetReflection();
  135. reflection->Swap(&message1, &message2);
  136. TestUtil::ExpectClear(message1);
  137. TestUtil::ExpectAllFieldsSet(message2);
  138. }
  139. TEST(GeneratedMessageReflectionTest, SwapWithBothSet) {
  140. unittest::TestAllTypes message1;
  141. unittest::TestAllTypes message2;
  142. TestUtil::SetAllFields(&message1);
  143. TestUtil::SetAllFields(&message2);
  144. TestUtil::ModifyRepeatedFields(&message2);
  145. const Reflection* reflection = message1.GetReflection();
  146. reflection->Swap(&message1, &message2);
  147. TestUtil::ExpectRepeatedFieldsModified(message1);
  148. TestUtil::ExpectAllFieldsSet(message2);
  149. message1.set_optional_int32(532819);
  150. reflection->Swap(&message1, &message2);
  151. EXPECT_EQ(532819, message2.optional_int32());
  152. }
  153. TEST(GeneratedMessageReflectionTest, SwapExtensions) {
  154. unittest::TestAllExtensions message1;
  155. unittest::TestAllExtensions message2;
  156. TestUtil::SetAllExtensions(&message1);
  157. const Reflection* reflection = message1.GetReflection();
  158. reflection->Swap(&message1, &message2);
  159. TestUtil::ExpectExtensionsClear(message1);
  160. TestUtil::ExpectAllExtensionsSet(message2);
  161. }
  162. TEST(GeneratedMessageReflectionTest, SwapUnknown) {
  163. unittest::TestEmptyMessage message1, message2;
  164. message1.mutable_unknown_fields()->AddVarint(1234, 1);
  165. EXPECT_EQ(1, message1.unknown_fields().field_count());
  166. EXPECT_EQ(0, message2.unknown_fields().field_count());
  167. const Reflection* reflection = message1.GetReflection();
  168. reflection->Swap(&message1, &message2);
  169. EXPECT_EQ(0, message1.unknown_fields().field_count());
  170. EXPECT_EQ(1, message2.unknown_fields().field_count());
  171. }
  172. TEST(GeneratedMessageReflectionTest, SwapFields) {
  173. unittest::TestAllTypes message1, message2;
  174. message1.set_optional_double(12.3);
  175. message1.mutable_repeated_int32()->Add(10);
  176. message1.mutable_repeated_int32()->Add(20);
  177. message2.set_optional_string("hello");
  178. message2.mutable_repeated_int64()->Add(30);
  179. std::vector<const FieldDescriptor*> fields;
  180. const Descriptor* descriptor = message1.GetDescriptor();
  181. fields.push_back(descriptor->FindFieldByName("optional_double"));
  182. fields.push_back(descriptor->FindFieldByName("repeated_int32"));
  183. fields.push_back(descriptor->FindFieldByName("optional_string"));
  184. fields.push_back(descriptor->FindFieldByName("optional_uint64"));
  185. const Reflection* reflection = message1.GetReflection();
  186. reflection->SwapFields(&message1, &message2, fields);
  187. EXPECT_FALSE(message1.has_optional_double());
  188. EXPECT_EQ(0, message1.repeated_int32_size());
  189. EXPECT_TRUE(message1.has_optional_string());
  190. EXPECT_EQ("hello", message1.optional_string());
  191. EXPECT_EQ(0, message1.repeated_int64_size());
  192. EXPECT_FALSE(message1.has_optional_uint64());
  193. EXPECT_TRUE(message2.has_optional_double());
  194. EXPECT_EQ(12.3, message2.optional_double());
  195. EXPECT_EQ(2, message2.repeated_int32_size());
  196. EXPECT_EQ(10, message2.repeated_int32(0));
  197. EXPECT_EQ(20, message2.repeated_int32(1));
  198. EXPECT_FALSE(message2.has_optional_string());
  199. EXPECT_EQ(1, message2.repeated_int64_size());
  200. EXPECT_FALSE(message2.has_optional_uint64());
  201. }
  202. TEST(GeneratedMessageReflectionTest, SwapFieldsAll) {
  203. unittest::TestAllTypes message1;
  204. unittest::TestAllTypes message2;
  205. TestUtil::SetAllFields(&message2);
  206. std::vector<const FieldDescriptor*> fields;
  207. const Reflection* reflection = message1.GetReflection();
  208. reflection->ListFields(message2, &fields);
  209. reflection->SwapFields(&message1, &message2, fields);
  210. TestUtil::ExpectAllFieldsSet(message1);
  211. TestUtil::ExpectClear(message2);
  212. }
  213. TEST(GeneratedMessageReflectionTest, SwapFieldsAllExtension) {
  214. unittest::TestAllExtensions message1;
  215. unittest::TestAllExtensions message2;
  216. TestUtil::SetAllExtensions(&message1);
  217. std::vector<const FieldDescriptor*> fields;
  218. const Reflection* reflection = message1.GetReflection();
  219. reflection->ListFields(message1, &fields);
  220. reflection->SwapFields(&message1, &message2, fields);
  221. TestUtil::ExpectExtensionsClear(message1);
  222. TestUtil::ExpectAllExtensionsSet(message2);
  223. }
  224. TEST(GeneratedMessageReflectionTest, SwapOneof) {
  225. unittest::TestOneof2 message1, message2;
  226. TestUtil::SetOneof1(&message1);
  227. const Reflection* reflection = message1.GetReflection();
  228. reflection->Swap(&message1, &message2);
  229. TestUtil::ExpectOneofClear(message1);
  230. TestUtil::ExpectOneofSet1(message2);
  231. }
  232. TEST(GeneratedMessageReflectionTest, SwapOneofBothSet) {
  233. unittest::TestOneof2 message1, message2;
  234. TestUtil::SetOneof1(&message1);
  235. TestUtil::SetOneof2(&message2);
  236. const Reflection* reflection = message1.GetReflection();
  237. reflection->Swap(&message1, &message2);
  238. TestUtil::ExpectOneofSet2(message1);
  239. TestUtil::ExpectOneofSet1(message2);
  240. }
  241. TEST(GeneratedMessageReflectionTest, SwapFieldsOneof) {
  242. unittest::TestOneof2 message1, message2;
  243. TestUtil::SetOneof1(&message1);
  244. std::vector<const FieldDescriptor*> fields;
  245. const Descriptor* descriptor = message1.GetDescriptor();
  246. for (int i = 0; i < descriptor->field_count(); i++) {
  247. fields.push_back(descriptor->field(i));
  248. }
  249. const Reflection* reflection = message1.GetReflection();
  250. reflection->SwapFields(&message1, &message2, fields);
  251. TestUtil::ExpectOneofClear(message1);
  252. TestUtil::ExpectOneofSet1(message2);
  253. }
  254. TEST(GeneratedMessageReflectionTest, RemoveLast) {
  255. unittest::TestAllTypes message;
  256. TestUtil::ReflectionTester reflection_tester(
  257. unittest::TestAllTypes::descriptor());
  258. TestUtil::SetAllFields(&message);
  259. reflection_tester.RemoveLastRepeatedsViaReflection(&message);
  260. TestUtil::ExpectLastRepeatedsRemoved(message);
  261. }
  262. TEST(GeneratedMessageReflectionTest, RemoveLastExtensions) {
  263. unittest::TestAllExtensions message;
  264. TestUtil::ReflectionTester reflection_tester(
  265. unittest::TestAllExtensions::descriptor());
  266. TestUtil::SetAllExtensions(&message);
  267. reflection_tester.RemoveLastRepeatedsViaReflection(&message);
  268. TestUtil::ExpectLastRepeatedExtensionsRemoved(message);
  269. }
  270. TEST(GeneratedMessageReflectionTest, ReleaseLast) {
  271. unittest::TestAllTypes message;
  272. const Descriptor* descriptor = message.GetDescriptor();
  273. TestUtil::ReflectionTester reflection_tester(descriptor);
  274. TestUtil::SetAllFields(&message);
  275. reflection_tester.ReleaseLastRepeatedsViaReflection(&message, false);
  276. TestUtil::ExpectLastRepeatedsReleased(message);
  277. // Now test that we actually release the right message.
  278. message.Clear();
  279. TestUtil::SetAllFields(&message);
  280. ASSERT_EQ(2, message.repeated_foreign_message_size());
  281. const protobuf_unittest::ForeignMessage* expected =
  282. message.mutable_repeated_foreign_message(1);
  283. std::unique_ptr<Message> released(message.GetReflection()->ReleaseLast(
  284. &message, descriptor->FindFieldByName("repeated_foreign_message")));
  285. EXPECT_EQ(expected, released.get());
  286. }
  287. TEST(GeneratedMessageReflectionTest, ReleaseLastExtensions) {
  288. unittest::TestAllExtensions message;
  289. const Descriptor* descriptor = message.GetDescriptor();
  290. TestUtil::ReflectionTester reflection_tester(descriptor);
  291. TestUtil::SetAllExtensions(&message);
  292. reflection_tester.ReleaseLastRepeatedsViaReflection(&message, true);
  293. TestUtil::ExpectLastRepeatedExtensionsReleased(message);
  294. // Now test that we actually release the right message.
  295. message.Clear();
  296. TestUtil::SetAllExtensions(&message);
  297. ASSERT_EQ(2, message.ExtensionSize(
  298. unittest::repeated_foreign_message_extension));
  299. const protobuf_unittest::ForeignMessage* expected = message.MutableExtension(
  300. unittest::repeated_foreign_message_extension, 1);
  301. std::unique_ptr<Message> released(message.GetReflection()->ReleaseLast(
  302. &message, descriptor->file()->FindExtensionByName(
  303. "repeated_foreign_message_extension")));
  304. EXPECT_EQ(expected, released.get());
  305. }
  306. TEST(GeneratedMessageReflectionTest, SwapRepeatedElements) {
  307. unittest::TestAllTypes message;
  308. TestUtil::ReflectionTester reflection_tester(
  309. unittest::TestAllTypes::descriptor());
  310. TestUtil::SetAllFields(&message);
  311. // Swap and test that fields are all swapped.
  312. reflection_tester.SwapRepeatedsViaReflection(&message);
  313. TestUtil::ExpectRepeatedsSwapped(message);
  314. // Swap back and test that fields are all back to original values.
  315. reflection_tester.SwapRepeatedsViaReflection(&message);
  316. TestUtil::ExpectAllFieldsSet(message);
  317. }
  318. TEST(GeneratedMessageReflectionTest, SwapRepeatedElementsExtension) {
  319. unittest::TestAllExtensions message;
  320. TestUtil::ReflectionTester reflection_tester(
  321. unittest::TestAllExtensions::descriptor());
  322. TestUtil::SetAllExtensions(&message);
  323. // Swap and test that fields are all swapped.
  324. reflection_tester.SwapRepeatedsViaReflection(&message);
  325. TestUtil::ExpectRepeatedExtensionsSwapped(message);
  326. // Swap back and test that fields are all back to original values.
  327. reflection_tester.SwapRepeatedsViaReflection(&message);
  328. TestUtil::ExpectAllExtensionsSet(message);
  329. }
  330. TEST(GeneratedMessageReflectionTest, Extensions) {
  331. // Set every extension to a unique value then go back and check all those
  332. // values.
  333. unittest::TestAllExtensions message;
  334. TestUtil::ReflectionTester reflection_tester(
  335. unittest::TestAllExtensions::descriptor());
  336. reflection_tester.SetAllFieldsViaReflection(&message);
  337. TestUtil::ExpectAllExtensionsSet(message);
  338. reflection_tester.ExpectAllFieldsSetViaReflection(message);
  339. reflection_tester.ModifyRepeatedFieldsViaReflection(&message);
  340. TestUtil::ExpectRepeatedExtensionsModified(message);
  341. }
  342. TEST(GeneratedMessageReflectionTest, FindExtensionTypeByNumber) {
  343. const Reflection* reflection =
  344. unittest::TestAllExtensions::default_instance().GetReflection();
  345. const FieldDescriptor* extension1 =
  346. unittest::TestAllExtensions::descriptor()->file()->FindExtensionByName(
  347. "optional_int32_extension");
  348. const FieldDescriptor* extension2 =
  349. unittest::TestAllExtensions::descriptor()->file()->FindExtensionByName(
  350. "repeated_string_extension");
  351. EXPECT_EQ(extension1,
  352. reflection->FindKnownExtensionByNumber(extension1->number()));
  353. EXPECT_EQ(extension2,
  354. reflection->FindKnownExtensionByNumber(extension2->number()));
  355. // Non-existent extension.
  356. EXPECT_TRUE(reflection->FindKnownExtensionByNumber(62341) == NULL);
  357. // Extensions of TestAllExtensions should not show up as extensions of
  358. // other types.
  359. EXPECT_TRUE(unittest::TestAllTypes::default_instance().GetReflection()->
  360. FindKnownExtensionByNumber(extension1->number()) == NULL);
  361. }
  362. TEST(GeneratedMessageReflectionTest, FindKnownExtensionByName) {
  363. const Reflection* reflection =
  364. unittest::TestAllExtensions::default_instance().GetReflection();
  365. const FieldDescriptor* extension1 =
  366. unittest::TestAllExtensions::descriptor()->file()->FindExtensionByName(
  367. "optional_int32_extension");
  368. const FieldDescriptor* extension2 =
  369. unittest::TestAllExtensions::descriptor()->file()->FindExtensionByName(
  370. "repeated_string_extension");
  371. EXPECT_EQ(extension1,
  372. reflection->FindKnownExtensionByName(extension1->full_name()));
  373. EXPECT_EQ(extension2,
  374. reflection->FindKnownExtensionByName(extension2->full_name()));
  375. // Non-existent extension.
  376. EXPECT_TRUE(reflection->FindKnownExtensionByName("no_such_ext") == NULL);
  377. // Extensions of TestAllExtensions should not show up as extensions of
  378. // other types.
  379. EXPECT_TRUE(unittest::TestAllTypes::default_instance().GetReflection()->
  380. FindKnownExtensionByName(extension1->full_name()) == NULL);
  381. }
  382. TEST(GeneratedMessageReflectionTest, SetAllocatedMessageTest) {
  383. unittest::TestAllTypes from_message1;
  384. unittest::TestAllTypes from_message2;
  385. unittest::TestAllTypes to_message;
  386. TestUtil::ReflectionTester reflection_tester(
  387. unittest::TestAllTypes::descriptor());
  388. reflection_tester.SetAllFieldsViaReflection(&from_message1);
  389. reflection_tester.SetAllFieldsViaReflection(&from_message2);
  390. // Before moving fields, we expect the nested messages to be NULL.
  391. reflection_tester.ExpectMessagesReleasedViaReflection(
  392. &to_message, TestUtil::ReflectionTester::IS_NULL);
  393. // After fields are moved we should get non-NULL releases.
  394. reflection_tester.SetAllocatedOptionalMessageFieldsToMessageViaReflection(
  395. &from_message1, &to_message);
  396. reflection_tester.ExpectMessagesReleasedViaReflection(
  397. &to_message, TestUtil::ReflectionTester::NOT_NULL);
  398. // Another move to make sure that we can SetAllocated several times.
  399. reflection_tester.SetAllocatedOptionalMessageFieldsToMessageViaReflection(
  400. &from_message2, &to_message);
  401. reflection_tester.ExpectMessagesReleasedViaReflection(
  402. &to_message, TestUtil::ReflectionTester::NOT_NULL);
  403. // After SetAllocatedOptionalMessageFieldsToNullViaReflection() we expect the
  404. // releases to be NULL again.
  405. reflection_tester.SetAllocatedOptionalMessageFieldsToNullViaReflection(
  406. &to_message);
  407. reflection_tester.ExpectMessagesReleasedViaReflection(
  408. &to_message, TestUtil::ReflectionTester::IS_NULL);
  409. }
  410. TEST(GeneratedMessageReflectionTest, SetAllocatedMessageOnArenaTest) {
  411. unittest::TestAllTypes from_message1;
  412. unittest::TestAllTypes from_message2;
  413. ::google::protobuf::Arena arena;
  414. unittest::TestAllTypes* to_message =
  415. ::google::protobuf::Arena::CreateMessage<unittest::TestAllTypes>(&arena);
  416. TestUtil::ReflectionTester reflection_tester(
  417. unittest::TestAllTypes::descriptor());
  418. reflection_tester.SetAllFieldsViaReflection(&from_message1);
  419. reflection_tester.SetAllFieldsViaReflection(&from_message2);
  420. // Before moving fields, we expect the nested messages to be NULL.
  421. reflection_tester.ExpectMessagesReleasedViaReflection(
  422. to_message, TestUtil::ReflectionTester::IS_NULL);
  423. // After fields are moved we should get non-NULL releases.
  424. reflection_tester.SetAllocatedOptionalMessageFieldsToMessageViaReflection(
  425. &from_message1, to_message);
  426. reflection_tester.ExpectMessagesReleasedViaReflection(
  427. to_message, TestUtil::ReflectionTester::NOT_NULL);
  428. // Another move to make sure that we can SetAllocated several times.
  429. reflection_tester.SetAllocatedOptionalMessageFieldsToMessageViaReflection(
  430. &from_message2, to_message);
  431. reflection_tester.ExpectMessagesReleasedViaReflection(
  432. to_message, TestUtil::ReflectionTester::NOT_NULL);
  433. // After SetAllocatedOptionalMessageFieldsToNullViaReflection() we expect the
  434. // releases to be NULL again.
  435. reflection_tester.SetAllocatedOptionalMessageFieldsToNullViaReflection(
  436. to_message);
  437. reflection_tester.ExpectMessagesReleasedViaReflection(
  438. to_message, TestUtil::ReflectionTester::IS_NULL);
  439. }
  440. TEST(GeneratedMessageReflectionTest, SetAllocatedExtensionMessageTest) {
  441. unittest::TestAllExtensions from_message1;
  442. unittest::TestAllExtensions from_message2;
  443. unittest::TestAllExtensions to_message;
  444. TestUtil::ReflectionTester reflection_tester(
  445. unittest::TestAllExtensions::descriptor());
  446. reflection_tester.SetAllFieldsViaReflection(&from_message1);
  447. reflection_tester.SetAllFieldsViaReflection(&from_message2);
  448. // Before moving fields, we expect the nested messages to be NULL.
  449. reflection_tester.ExpectMessagesReleasedViaReflection(
  450. &to_message, TestUtil::ReflectionTester::IS_NULL);
  451. // After fields are moved we should get non-NULL releases.
  452. reflection_tester.SetAllocatedOptionalMessageFieldsToMessageViaReflection(
  453. &from_message1, &to_message);
  454. reflection_tester.ExpectMessagesReleasedViaReflection(
  455. &to_message, TestUtil::ReflectionTester::NOT_NULL);
  456. // Another move to make sure that we can SetAllocated several times.
  457. reflection_tester.SetAllocatedOptionalMessageFieldsToMessageViaReflection(
  458. &from_message2, &to_message);
  459. reflection_tester.ExpectMessagesReleasedViaReflection(
  460. &to_message, TestUtil::ReflectionTester::NOT_NULL);
  461. // After SetAllocatedOptionalMessageFieldsToNullViaReflection() we expect the
  462. // releases to be NULL again.
  463. reflection_tester.SetAllocatedOptionalMessageFieldsToNullViaReflection(
  464. &to_message);
  465. reflection_tester.ExpectMessagesReleasedViaReflection(
  466. &to_message, TestUtil::ReflectionTester::IS_NULL);
  467. }
  468. TEST(GeneratedMessageReflectionTest, SetAllocatedExtensionMessageOnArenaTest) {
  469. ::google::protobuf::Arena arena;
  470. unittest::TestAllExtensions* to_message =
  471. ::google::protobuf::Arena::CreateMessage<unittest::TestAllExtensions>(&arena);
  472. unittest::TestAllExtensions from_message1;
  473. unittest::TestAllExtensions from_message2;
  474. TestUtil::ReflectionTester reflection_tester(
  475. unittest::TestAllExtensions::descriptor());
  476. reflection_tester.SetAllFieldsViaReflection(&from_message1);
  477. reflection_tester.SetAllFieldsViaReflection(&from_message2);
  478. // Before moving fields, we expect the nested messages to be NULL.
  479. reflection_tester.ExpectMessagesReleasedViaReflection(
  480. to_message, TestUtil::ReflectionTester::IS_NULL);
  481. // After fields are moved we should get non-NULL releases.
  482. reflection_tester.SetAllocatedOptionalMessageFieldsToMessageViaReflection(
  483. &from_message1, to_message);
  484. reflection_tester.ExpectMessagesReleasedViaReflection(
  485. to_message, TestUtil::ReflectionTester::NOT_NULL);
  486. // Another move to make sure that we can SetAllocated several times.
  487. reflection_tester.SetAllocatedOptionalMessageFieldsToMessageViaReflection(
  488. &from_message2, to_message);
  489. reflection_tester.ExpectMessagesReleasedViaReflection(
  490. to_message, TestUtil::ReflectionTester::NOT_NULL);
  491. // After SetAllocatedOptionalMessageFieldsToNullViaReflection() we expect the
  492. // releases to be NULL again.
  493. reflection_tester.SetAllocatedOptionalMessageFieldsToNullViaReflection(
  494. to_message);
  495. reflection_tester.ExpectMessagesReleasedViaReflection(
  496. to_message, TestUtil::ReflectionTester::IS_NULL);
  497. }
  498. TEST(GeneratedMessageReflectionTest, AddRepeatedMessage) {
  499. unittest::TestAllTypes message;
  500. const Reflection* reflection = message.GetReflection();
  501. const Reflection* nested_reflection =
  502. unittest::TestAllTypes::NestedMessage::default_instance().GetReflection();
  503. const FieldDescriptor* nested_bb =
  504. unittest::TestAllTypes::NestedMessage::descriptor()->FindFieldByName(
  505. "bb");
  506. Message* nested = reflection->AddMessage(
  507. &message, F("repeated_nested_message"));
  508. nested_reflection->SetInt32(nested, nested_bb, 11);
  509. EXPECT_EQ(11, message.repeated_nested_message(0).bb());
  510. }
  511. TEST(GeneratedMessageReflectionTest, MutableRepeatedMessage) {
  512. unittest::TestAllTypes message;
  513. const Reflection* reflection = message.GetReflection();
  514. const Reflection* nested_reflection =
  515. unittest::TestAllTypes::NestedMessage::default_instance().GetReflection();
  516. const FieldDescriptor* nested_bb =
  517. unittest::TestAllTypes::NestedMessage::descriptor()->FindFieldByName(
  518. "bb");
  519. message.add_repeated_nested_message()->set_bb(12);
  520. Message* nested = reflection->MutableRepeatedMessage(
  521. &message, F("repeated_nested_message"), 0);
  522. EXPECT_EQ(12, nested_reflection->GetInt32(*nested, nested_bb));
  523. nested_reflection->SetInt32(nested, nested_bb, 13);
  524. EXPECT_EQ(13, message.repeated_nested_message(0).bb());
  525. }
  526. TEST(GeneratedMessageReflectionTest, AddAllocatedMessage) {
  527. unittest::TestAllTypes message;
  528. const Reflection* reflection = message.GetReflection();
  529. unittest::TestAllTypes::NestedMessage* nested =
  530. new unittest::TestAllTypes::NestedMessage();
  531. nested->set_bb(11);
  532. reflection->AddAllocatedMessage(&message, F("repeated_nested_message"), nested);
  533. EXPECT_EQ(1, message.repeated_nested_message_size());
  534. EXPECT_EQ(11, message.repeated_nested_message(0).bb());
  535. }
  536. TEST(GeneratedMessageReflectionTest, ListFieldsOneOf) {
  537. unittest::TestOneof2 message;
  538. TestUtil::SetOneof1(&message);
  539. const Reflection* reflection = message.GetReflection();
  540. std::vector<const FieldDescriptor*> fields;
  541. reflection->ListFields(message, &fields);
  542. EXPECT_EQ(4, fields.size());
  543. }
  544. TEST(GeneratedMessageReflectionTest, Oneof) {
  545. unittest::TestOneof2 message;
  546. const Descriptor* descriptor = message.GetDescriptor();
  547. const Reflection* reflection = message.GetReflection();
  548. // Check default values.
  549. EXPECT_EQ(0, reflection->GetInt32(
  550. message, descriptor->FindFieldByName("foo_int")));
  551. EXPECT_EQ("", reflection->GetString(
  552. message, descriptor->FindFieldByName("foo_string")));
  553. EXPECT_EQ("", reflection->GetString(
  554. message, descriptor->FindFieldByName("foo_cord")));
  555. EXPECT_EQ("", reflection->GetString(
  556. message, descriptor->FindFieldByName("foo_string_piece")));
  557. EXPECT_EQ("", reflection->GetString(
  558. message, descriptor->FindFieldByName("foo_bytes")));
  559. EXPECT_EQ(unittest::TestOneof2::FOO, reflection->GetEnum(
  560. message, descriptor->FindFieldByName("foo_enum"))->number());
  561. EXPECT_EQ(&unittest::TestOneof2::NestedMessage::default_instance(),
  562. &reflection->GetMessage(
  563. message, descriptor->FindFieldByName("foo_message")));
  564. EXPECT_EQ(&unittest::TestOneof2::FooGroup::default_instance(),
  565. &reflection->GetMessage(
  566. message, descriptor->FindFieldByName("foogroup")));
  567. EXPECT_NE(&unittest::TestOneof2::FooGroup::default_instance(),
  568. &reflection->GetMessage(
  569. message, descriptor->FindFieldByName("foo_lazy_message")));
  570. EXPECT_EQ(5, reflection->GetInt32(
  571. message, descriptor->FindFieldByName("bar_int")));
  572. EXPECT_EQ("STRING", reflection->GetString(
  573. message, descriptor->FindFieldByName("bar_string")));
  574. EXPECT_EQ("CORD", reflection->GetString(
  575. message, descriptor->FindFieldByName("bar_cord")));
  576. EXPECT_EQ("SPIECE", reflection->GetString(
  577. message, descriptor->FindFieldByName("bar_string_piece")));
  578. EXPECT_EQ("BYTES", reflection->GetString(
  579. message, descriptor->FindFieldByName("bar_bytes")));
  580. EXPECT_EQ(unittest::TestOneof2::BAR, reflection->GetEnum(
  581. message, descriptor->FindFieldByName("bar_enum"))->number());
  582. // Check Set functions.
  583. reflection->SetInt32(
  584. &message, descriptor->FindFieldByName("foo_int"), 123);
  585. EXPECT_EQ(123, reflection->GetInt32(
  586. message, descriptor->FindFieldByName("foo_int")));
  587. reflection->SetString(
  588. &message, descriptor->FindFieldByName("foo_string"), "abc");
  589. EXPECT_EQ("abc", reflection->GetString(
  590. message, descriptor->FindFieldByName("foo_string")));
  591. reflection->SetString(
  592. &message, descriptor->FindFieldByName("foo_bytes"), "bytes");
  593. EXPECT_EQ("bytes", reflection->GetString(
  594. message, descriptor->FindFieldByName("foo_bytes")));
  595. reflection->SetString(
  596. &message, descriptor->FindFieldByName("bar_cord"), "change_cord");
  597. EXPECT_EQ("change_cord", reflection->GetString(
  598. message, descriptor->FindFieldByName("bar_cord")));
  599. reflection->SetString(
  600. &message, descriptor->FindFieldByName("bar_string_piece"),
  601. "change_spiece");
  602. EXPECT_EQ("change_spiece", reflection->GetString(
  603. message, descriptor->FindFieldByName("bar_string_piece")));
  604. message.clear_foo();
  605. message.clear_bar();
  606. TestUtil::ExpectOneofClear(message);
  607. }
  608. TEST(GeneratedMessageReflectionTest, SetAllocatedOneofMessageTest) {
  609. unittest::TestOneof2 from_message1;
  610. unittest::TestOneof2 from_message2;
  611. unittest::TestOneof2 to_message;
  612. const Descriptor* descriptor = unittest::TestOneof2::descriptor();
  613. const Reflection* reflection = to_message.GetReflection();
  614. Message* released = reflection->ReleaseMessage(
  615. &to_message, descriptor->FindFieldByName("foo_lazy_message"));
  616. EXPECT_TRUE(released == NULL);
  617. released = reflection->ReleaseMessage(
  618. &to_message, descriptor->FindFieldByName("foo_message"));
  619. EXPECT_TRUE(released == NULL);
  620. TestUtil::ReflectionTester::SetOneofViaReflection(&from_message1);
  621. TestUtil::ReflectionTester::ExpectOneofSetViaReflection(from_message1);
  622. TestUtil::ReflectionTester::
  623. SetAllocatedOptionalMessageFieldsToMessageViaReflection(
  624. &from_message1, &to_message);
  625. const Message& sub_message = reflection->GetMessage(
  626. to_message, descriptor->FindFieldByName("foo_lazy_message"));
  627. released = reflection->ReleaseMessage(
  628. &to_message, descriptor->FindFieldByName("foo_lazy_message"));
  629. EXPECT_TRUE(released != NULL);
  630. EXPECT_EQ(&sub_message, released);
  631. delete released;
  632. TestUtil::ReflectionTester::SetOneofViaReflection(&from_message2);
  633. reflection->MutableMessage(
  634. &from_message2, descriptor->FindFieldByName("foo_message"));
  635. TestUtil::ReflectionTester::
  636. SetAllocatedOptionalMessageFieldsToMessageViaReflection(
  637. &from_message2, &to_message);
  638. const Message& sub_message2 = reflection->GetMessage(
  639. to_message, descriptor->FindFieldByName("foo_message"));
  640. released = reflection->ReleaseMessage(
  641. &to_message, descriptor->FindFieldByName("foo_message"));
  642. EXPECT_TRUE(released != NULL);
  643. EXPECT_EQ(&sub_message2, released);
  644. delete released;
  645. }
  646. TEST(GeneratedMessageReflectionTest, SetAllocatedOneofMessageOnArenaTest) {
  647. unittest::TestOneof2 from_message1;
  648. unittest::TestOneof2 from_message2;
  649. ::google::protobuf::Arena arena;
  650. unittest::TestOneof2* to_message =
  651. ::google::protobuf::Arena::CreateMessage<unittest::TestOneof2>(&arena);
  652. const Descriptor* descriptor = unittest::TestOneof2::descriptor();
  653. const Reflection* reflection = to_message->GetReflection();
  654. Message* released = reflection->ReleaseMessage(
  655. to_message, descriptor->FindFieldByName("foo_lazy_message"));
  656. EXPECT_TRUE(released == NULL);
  657. released = reflection->ReleaseMessage(
  658. to_message, descriptor->FindFieldByName("foo_message"));
  659. EXPECT_TRUE(released == NULL);
  660. TestUtil::ReflectionTester::SetOneofViaReflection(&from_message1);
  661. TestUtil::ReflectionTester::ExpectOneofSetViaReflection(from_message1);
  662. TestUtil::ReflectionTester::
  663. SetAllocatedOptionalMessageFieldsToMessageViaReflection(
  664. &from_message1, to_message);
  665. const Message& sub_message = reflection->GetMessage(
  666. *to_message, descriptor->FindFieldByName("foo_lazy_message"));
  667. released = reflection->ReleaseMessage(
  668. to_message, descriptor->FindFieldByName("foo_lazy_message"));
  669. EXPECT_TRUE(released != NULL);
  670. // Since sub_message is arena allocated, releasing it results in copying it
  671. // into new heap-allocated memory.
  672. EXPECT_NE(&sub_message, released);
  673. delete released;
  674. TestUtil::ReflectionTester::SetOneofViaReflection(&from_message2);
  675. reflection->MutableMessage(
  676. &from_message2, descriptor->FindFieldByName("foo_message"));
  677. TestUtil::ReflectionTester::
  678. SetAllocatedOptionalMessageFieldsToMessageViaReflection(
  679. &from_message2, to_message);
  680. const Message& sub_message2 = reflection->GetMessage(
  681. *to_message, descriptor->FindFieldByName("foo_message"));
  682. released = reflection->ReleaseMessage(
  683. to_message, descriptor->FindFieldByName("foo_message"));
  684. EXPECT_TRUE(released != NULL);
  685. // Since sub_message2 is arena allocated, releasing it results in copying it
  686. // into new heap-allocated memory.
  687. EXPECT_NE(&sub_message2, released);
  688. delete released;
  689. }
  690. TEST(GeneratedMessageReflectionTest, ReleaseMessageTest) {
  691. unittest::TestAllTypes message;
  692. TestUtil::ReflectionTester reflection_tester(
  693. unittest::TestAllTypes::descriptor());
  694. // When nothing is set, we expect all released messages to be NULL.
  695. reflection_tester.ExpectMessagesReleasedViaReflection(
  696. &message, TestUtil::ReflectionTester::IS_NULL);
  697. // After fields are set we should get non-NULL releases.
  698. reflection_tester.SetAllFieldsViaReflection(&message);
  699. reflection_tester.ExpectMessagesReleasedViaReflection(
  700. &message, TestUtil::ReflectionTester::NOT_NULL);
  701. // After Clear() we may or may not get a message from ReleaseMessage().
  702. // This is implementation specific.
  703. reflection_tester.SetAllFieldsViaReflection(&message);
  704. message.Clear();
  705. reflection_tester.ExpectMessagesReleasedViaReflection(
  706. &message, TestUtil::ReflectionTester::CAN_BE_NULL);
  707. // Test a different code path for setting after releasing.
  708. TestUtil::SetAllFields(&message);
  709. TestUtil::ExpectAllFieldsSet(message);
  710. }
  711. TEST(GeneratedMessageReflectionTest, ReleaseExtensionMessageTest) {
  712. unittest::TestAllExtensions message;
  713. TestUtil::ReflectionTester reflection_tester(
  714. unittest::TestAllExtensions::descriptor());
  715. // When nothing is set, we expect all released messages to be NULL.
  716. reflection_tester.ExpectMessagesReleasedViaReflection(
  717. &message, TestUtil::ReflectionTester::IS_NULL);
  718. // After fields are set we should get non-NULL releases.
  719. reflection_tester.SetAllFieldsViaReflection(&message);
  720. reflection_tester.ExpectMessagesReleasedViaReflection(
  721. &message, TestUtil::ReflectionTester::NOT_NULL);
  722. // After Clear() we may or may not get a message from ReleaseMessage().
  723. // This is implementation specific.
  724. reflection_tester.SetAllFieldsViaReflection(&message);
  725. message.Clear();
  726. reflection_tester.ExpectMessagesReleasedViaReflection(
  727. &message, TestUtil::ReflectionTester::CAN_BE_NULL);
  728. // Test a different code path for setting after releasing.
  729. TestUtil::SetAllExtensions(&message);
  730. TestUtil::ExpectAllExtensionsSet(message);
  731. }
  732. TEST(GeneratedMessageReflectionTest, ReleaseOneofMessageTest) {
  733. unittest::TestOneof2 message;
  734. TestUtil::ReflectionTester::SetOneofViaReflection(&message);
  735. const Descriptor* descriptor = unittest::TestOneof2::descriptor();
  736. const Reflection* reflection = message.GetReflection();
  737. const Message& sub_message = reflection->GetMessage(
  738. message, descriptor->FindFieldByName("foo_lazy_message"));
  739. Message* released = reflection->ReleaseMessage(
  740. &message, descriptor->FindFieldByName("foo_lazy_message"));
  741. EXPECT_TRUE(released != NULL);
  742. EXPECT_EQ(&sub_message, released);
  743. delete released;
  744. released = reflection->ReleaseMessage(
  745. &message, descriptor->FindFieldByName("foo_lazy_message"));
  746. EXPECT_TRUE(released == NULL);
  747. }
  748. TEST(GeneratedMessageReflectionTest, ArenaReleaseMessageTest) {
  749. ::google::protobuf::Arena arena;
  750. unittest::TestAllTypes* message =
  751. ::google::protobuf::Arena::CreateMessage<unittest::TestAllTypes>(&arena);
  752. TestUtil::ReflectionTester reflection_tester(
  753. unittest::TestAllTypes::descriptor());
  754. // When nothing is set, we expect all released messages to be NULL.
  755. reflection_tester.ExpectMessagesReleasedViaReflection(
  756. message, TestUtil::ReflectionTester::IS_NULL);
  757. // After fields are set we should get non-NULL releases.
  758. reflection_tester.SetAllFieldsViaReflection(message);
  759. reflection_tester.ExpectMessagesReleasedViaReflection(
  760. message, TestUtil::ReflectionTester::NOT_NULL);
  761. // After Clear() we may or may not get a message from ReleaseMessage().
  762. // This is implementation specific.
  763. reflection_tester.SetAllFieldsViaReflection(message);
  764. message->Clear();
  765. reflection_tester.ExpectMessagesReleasedViaReflection(
  766. message, TestUtil::ReflectionTester::CAN_BE_NULL);
  767. }
  768. TEST(GeneratedMessageReflectionTest, ArenaReleaseExtensionMessageTest) {
  769. ::google::protobuf::Arena arena;
  770. unittest::TestAllExtensions* message =
  771. ::google::protobuf::Arena::CreateMessage<unittest::TestAllExtensions>(&arena);
  772. TestUtil::ReflectionTester reflection_tester(
  773. unittest::TestAllExtensions::descriptor());
  774. // When nothing is set, we expect all released messages to be NULL.
  775. reflection_tester.ExpectMessagesReleasedViaReflection(
  776. message, TestUtil::ReflectionTester::IS_NULL);
  777. // After fields are set we should get non-NULL releases.
  778. reflection_tester.SetAllFieldsViaReflection(message);
  779. reflection_tester.ExpectMessagesReleasedViaReflection(
  780. message, TestUtil::ReflectionTester::NOT_NULL);
  781. // After Clear() we may or may not get a message from ReleaseMessage().
  782. // This is implementation specific.
  783. reflection_tester.SetAllFieldsViaReflection(message);
  784. message->Clear();
  785. reflection_tester.ExpectMessagesReleasedViaReflection(
  786. message, TestUtil::ReflectionTester::CAN_BE_NULL);
  787. }
  788. TEST(GeneratedMessageReflectionTest, ArenaReleaseOneofMessageTest) {
  789. ::google::protobuf::Arena arena;
  790. unittest::TestOneof2* message =
  791. ::google::protobuf::Arena::CreateMessage<unittest::TestOneof2>(&arena);
  792. TestUtil::ReflectionTester::SetOneofViaReflection(message);
  793. const Descriptor* descriptor = unittest::TestOneof2::descriptor();
  794. const Reflection* reflection = message->GetReflection();
  795. Message* released = reflection->ReleaseMessage(
  796. message, descriptor->FindFieldByName("foo_lazy_message"));
  797. EXPECT_TRUE(released != NULL);
  798. delete released;
  799. released = reflection->ReleaseMessage(
  800. message, descriptor->FindFieldByName("foo_lazy_message"));
  801. EXPECT_TRUE(released == NULL);
  802. }
  803. #ifdef PROTOBUF_HAS_DEATH_TEST
  804. TEST(GeneratedMessageReflectionTest, UsageErrors) {
  805. unittest::TestAllTypes message;
  806. const Reflection* reflection = message.GetReflection();
  807. const Descriptor* descriptor = message.GetDescriptor();
  808. #define f(NAME) descriptor->FindFieldByName(NAME)
  809. // Testing every single failure mode would be too much work. Let's just
  810. // check a few.
  811. EXPECT_DEATH(
  812. reflection->GetInt32(
  813. message, descriptor->FindFieldByName("optional_int64")),
  814. "Protocol Buffer reflection usage error:\n"
  815. " Method : google::protobuf::Reflection::GetInt32\n"
  816. " Message type: protobuf_unittest\\.TestAllTypes\n"
  817. " Field : protobuf_unittest\\.TestAllTypes\\.optional_int64\n"
  818. " Problem : Field is not the right type for this message:\n"
  819. " Expected : CPPTYPE_INT32\n"
  820. " Field type: CPPTYPE_INT64");
  821. EXPECT_DEATH(
  822. reflection->GetInt32(
  823. message, descriptor->FindFieldByName("repeated_int32")),
  824. "Protocol Buffer reflection usage error:\n"
  825. " Method : google::protobuf::Reflection::GetInt32\n"
  826. " Message type: protobuf_unittest.TestAllTypes\n"
  827. " Field : protobuf_unittest.TestAllTypes.repeated_int32\n"
  828. " Problem : Field is repeated; the method requires a singular field.");
  829. EXPECT_DEATH(
  830. reflection->GetInt32(
  831. message, unittest::ForeignMessage::descriptor()->FindFieldByName("c")),
  832. "Protocol Buffer reflection usage error:\n"
  833. " Method : google::protobuf::Reflection::GetInt32\n"
  834. " Message type: protobuf_unittest.TestAllTypes\n"
  835. " Field : protobuf_unittest.ForeignMessage.c\n"
  836. " Problem : Field does not match message type.");
  837. EXPECT_DEATH(
  838. reflection->HasField(
  839. message, unittest::ForeignMessage::descriptor()->FindFieldByName("c")),
  840. "Protocol Buffer reflection usage error:\n"
  841. " Method : google::protobuf::Reflection::HasField\n"
  842. " Message type: protobuf_unittest.TestAllTypes\n"
  843. " Field : protobuf_unittest.ForeignMessage.c\n"
  844. " Problem : Field does not match message type.");
  845. #undef f
  846. }
  847. #endif // PROTOBUF_HAS_DEATH_TEST
  848. } // namespace
  849. } // namespace protobuf
  850. } // namespace google