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 31// Author: kenton@google.com (Kenton Varda) 32// Based on original Protocol Buffers design by 33// Sanjay Ghemawat, Jeff Dean, and others. 34// 35// To test the code generator, we actually use it to generate code for 36// net/proto2/internal/unittest.proto, then test that. This means that we 37// are actually testing the parser and other parts of the system at the same 38// time, and that problems in the generator may show up as compile-time errors 39// rather than unittest failures, which may be surprising. However, testing 40// the output of the C++ generator directly would be very hard. We can't very 41// well just check it against golden files since those files would have to be 42// updated for any small change; such a test would be very brittle and probably 43// not very helpful. What we really want to test is that the code compiles 44// correctly and produces the interfaces we expect, which is why this test 45// is written this way. 46 47#include <memory> 48#include <vector> 49 50#include <google/protobuf/compiler/cpp/cpp_unittest.h> 51#include <google/protobuf/stubs/strutil.h> 52#if !defined(GOOGLE_PROTOBUF_CMAKE_BUILD) && !defined(_MSC_VER) 53// We exclude this large proto from cmake build because it's too large for 54// visual studio to compile (report internal errors). 55#include <google/protobuf/unittest_enormous_descriptor.pb.h> 56#endif 57#include <google/protobuf/compiler/cpp/cpp_helpers.h> 58#include <google/protobuf/compiler/cpp/cpp_test_bad_identifiers.pb.h> 59#include <google/protobuf/compiler/scc.h> 60#include <google/protobuf/compiler/importer.h> 61#include <google/protobuf/test_util2.h> 62#include <google/protobuf/unittest_no_generic_services.pb.h> 63#include <google/protobuf/io/coded_stream.h> 64#include <google/protobuf/io/zero_copy_stream_impl.h> 65#include <google/protobuf/descriptor.pb.h> 66#include <google/protobuf/arena.h> 67#include <google/protobuf/descriptor.h> 68#include <google/protobuf/dynamic_message.h> 69 70#include <google/protobuf/stubs/callback.h> 71#include <google/protobuf/stubs/common.h> 72#include <google/protobuf/stubs/logging.h> 73#include <google/protobuf/testing/googletest.h> 74#include <gtest/gtest.h> 75#include <google/protobuf/stubs/casts.h> 76#include <google/protobuf/stubs/substitute.h> 77#include <google/protobuf/stubs/stl_util.h> 78 79// Must be included last. 80#include <google/protobuf/port_def.inc> 81 82namespace google { 83namespace protobuf { 84namespace compiler { 85namespace cpp { 86 87// Can't use an anonymous namespace here due to brokenness of Tru64 compiler. 88namespace cpp_unittest { 89 90 91class MockErrorCollector : public MultiFileErrorCollector { 92 public: 93 MockErrorCollector() {} 94 ~MockErrorCollector() {} 95 96 std::string text_; 97 98 // implements ErrorCollector --------------------------------------- 99 void AddError(const std::string& filename, int line, int column, 100 const std::string& message) { 101 strings::SubstituteAndAppend(&text_, "$0:$1:$2: $3\n", filename, line, column, 102 message); 103 } 104}; 105 106#ifndef PROTOBUF_TEST_NO_DESCRIPTORS 107 108// Test that generated code has proper descriptors: 109// Parse a descriptor directly (using google::protobuf::compiler::Importer) and 110// compare it to the one that was produced by generated code. 111TEST(GENERATED_DESCRIPTOR_TEST_NAME, IdenticalDescriptors) { 112 const FileDescriptor* generated_descriptor = 113 UNITTEST::TestAllTypes::descriptor()->file(); 114 115 // Set up the Importer. 116 MockErrorCollector error_collector; 117 DiskSourceTree source_tree; 118 source_tree.MapPath("", TestUtil::TestSourceDir()); 119 Importer importer(&source_tree, &error_collector); 120 121 // Import (parse) unittest.proto. 122 const FileDescriptor* parsed_descriptor = 123 importer.Import(TestUtil::MaybeTranslatePath(UNITTEST_PROTO_PATH)); 124 EXPECT_EQ("", error_collector.text_); 125 ASSERT_TRUE(parsed_descriptor != NULL); 126 127 // Test that descriptors are generated correctly by converting them to 128 // FileDescriptorProtos and comparing. 129 FileDescriptorProto generated_descriptor_proto, parsed_descriptor_proto; 130 generated_descriptor->CopyTo(&generated_descriptor_proto); 131 parsed_descriptor->CopyTo(&parsed_descriptor_proto); 132 133 EXPECT_EQ(parsed_descriptor_proto.DebugString(), 134 generated_descriptor_proto.DebugString()); 135} 136 137#if !defined(GOOGLE_PROTOBUF_CMAKE_BUILD) && !defined(_MSC_VER) 138// Test that generated code has proper descriptors: 139// Touch a descriptor generated from an enormous message to validate special 140// handling for descriptors exceeding the C++ standard's recommended minimum 141// limit for string literal size 142TEST(GENERATED_DESCRIPTOR_TEST_NAME, EnormousDescriptor) { 143 const Descriptor* generated_descriptor = 144 ::protobuf_unittest::TestEnormousDescriptor::descriptor(); 145 146 EXPECT_TRUE(generated_descriptor != NULL); 147} 148#endif 149 150#endif // !PROTOBUF_TEST_NO_DESCRIPTORS 151 152// =================================================================== 153 154TEST(GENERATED_MESSAGE_TEST_NAME, Defaults) { 155 // Check that all default values are set correctly in the initial message. 156 UNITTEST::TestAllTypes message; 157 158 TestUtil::ExpectClear(message); 159 160 // Messages should return pointers to default instances until first use. 161 // (This is not checked by ExpectClear() since it is not actually true after 162 // the fields have been set and then cleared.) 163 EXPECT_EQ(&UNITTEST::TestAllTypes::OptionalGroup::default_instance(), 164 &message.optionalgroup()); 165 EXPECT_EQ(&UNITTEST::TestAllTypes::NestedMessage::default_instance(), 166 &message.optional_nested_message()); 167 EXPECT_EQ(&UNITTEST::ForeignMessage::default_instance(), 168 &message.optional_foreign_message()); 169 EXPECT_EQ(&UNITTEST_IMPORT::ImportMessage::default_instance(), 170 &message.optional_import_message()); 171} 172 173TEST(GENERATED_MESSAGE_TEST_NAME, FloatingPointDefaults) { 174 const UNITTEST::TestExtremeDefaultValues& extreme_default = 175 UNITTEST::TestExtremeDefaultValues::default_instance(); 176 177 EXPECT_EQ(0.0f, extreme_default.zero_float()); 178 EXPECT_EQ(1.0f, extreme_default.one_float()); 179 EXPECT_EQ(1.5f, extreme_default.small_float()); 180 EXPECT_EQ(-1.0f, extreme_default.negative_one_float()); 181 EXPECT_EQ(-1.5f, extreme_default.negative_float()); 182 EXPECT_EQ(2.0e8f, extreme_default.large_float()); 183 EXPECT_EQ(-8e-28f, extreme_default.small_negative_float()); 184 EXPECT_EQ(std::numeric_limits<double>::infinity(), 185 extreme_default.inf_double()); 186 EXPECT_EQ(-std::numeric_limits<double>::infinity(), 187 extreme_default.neg_inf_double()); 188 EXPECT_TRUE(extreme_default.nan_double() != extreme_default.nan_double()); 189 EXPECT_EQ(std::numeric_limits<float>::infinity(), 190 extreme_default.inf_float()); 191 EXPECT_EQ(-std::numeric_limits<float>::infinity(), 192 extreme_default.neg_inf_float()); 193 EXPECT_TRUE(extreme_default.nan_float() != extreme_default.nan_float()); 194} 195 196TEST(GENERATED_MESSAGE_TEST_NAME, Trigraph) { 197 const UNITTEST::TestExtremeDefaultValues& extreme_default = 198 UNITTEST::TestExtremeDefaultValues::default_instance(); 199 200 EXPECT_EQ("? ? ?? ?? ??? ?\?/ ?\?-", extreme_default.cpp_trigraph()); 201} 202 203TEST(GENERATED_MESSAGE_TEST_NAME, ExtremeSmallIntegerDefault) { 204 const UNITTEST::TestExtremeDefaultValues& extreme_default = 205 UNITTEST::TestExtremeDefaultValues::default_instance(); 206 EXPECT_EQ(~0x7fffffff, kint32min); 207 EXPECT_EQ(PROTOBUF_LONGLONG(~0x7fffffffffffffff), kint64min); 208 EXPECT_EQ(kint32min, extreme_default.really_small_int32()); 209 EXPECT_EQ(kint64min, extreme_default.really_small_int64()); 210} 211 212TEST(GENERATED_MESSAGE_TEST_NAME, Accessors) { 213 // Set every field to a unique value then go back and check all those 214 // values. 215 UNITTEST::TestAllTypes message; 216 217 TestUtil::SetAllFields(&message); 218 TestUtil::ExpectAllFieldsSet(message); 219 220 TestUtil::ModifyRepeatedFields(&message); 221 TestUtil::ExpectRepeatedFieldsModified(message); 222} 223 224TEST(GENERATED_MESSAGE_TEST_NAME, MutableStringDefault) { 225 // mutable_foo() for a string should return a string initialized to its 226 // default value. 227 UNITTEST::TestAllTypes message; 228 229 EXPECT_EQ("hello", *message.mutable_default_string()); 230 231 // Note that the first time we call mutable_foo(), we get a newly-allocated 232 // string, but if we clear it and call it again, we get the same object again. 233 // We should verify that it has its default value in both cases. 234 message.set_default_string("blah"); 235 message.Clear(); 236 237 EXPECT_EQ("hello", *message.mutable_default_string()); 238} 239 240TEST(GENERATED_MESSAGE_TEST_NAME, StringDefaults) { 241 UNITTEST::TestExtremeDefaultValues message; 242 // Check if '\000' can be used in default string value. 243 EXPECT_EQ(std::string("hel\000lo", 6), message.string_with_zero()); 244 EXPECT_EQ(std::string("wor\000ld", 6), message.bytes_with_zero()); 245} 246 247TEST(GENERATED_MESSAGE_TEST_NAME, ReleaseString) { 248 // Check that release_foo() starts out NULL, and gives us a value 249 // that we can delete after it's been set. 250 UNITTEST::TestAllTypes message; 251 252 EXPECT_EQ(NULL, message.release_default_string()); 253 EXPECT_FALSE(message.has_default_string()); 254 EXPECT_EQ("hello", message.default_string()); 255 256 message.set_default_string("blah"); 257 EXPECT_TRUE(message.has_default_string()); 258 std::unique_ptr<std::string> str(message.release_default_string()); 259 EXPECT_FALSE(message.has_default_string()); 260 ASSERT_TRUE(str != NULL); 261 EXPECT_EQ("blah", *str); 262 263 EXPECT_EQ(NULL, message.release_default_string()); 264 EXPECT_FALSE(message.has_default_string()); 265 EXPECT_EQ("hello", message.default_string()); 266} 267 268TEST(GENERATED_MESSAGE_TEST_NAME, ReleaseMessage) { 269 // Check that release_foo() starts out NULL, and gives us a value 270 // that we can delete after it's been set. 271 UNITTEST::TestAllTypes message; 272 273 EXPECT_EQ(NULL, message.release_optional_nested_message()); 274 EXPECT_FALSE(message.has_optional_nested_message()); 275 276 message.mutable_optional_nested_message()->set_bb(1); 277 std::unique_ptr<UNITTEST::TestAllTypes::NestedMessage> nest( 278 message.release_optional_nested_message()); 279 EXPECT_FALSE(message.has_optional_nested_message()); 280 ASSERT_TRUE(nest != NULL); 281 EXPECT_EQ(1, nest->bb()); 282 283 EXPECT_EQ(NULL, message.release_optional_nested_message()); 284 EXPECT_FALSE(message.has_optional_nested_message()); 285} 286 287TEST(GENERATED_MESSAGE_TEST_NAME, SetAllocatedString) { 288 // Check that set_allocated_foo() works for strings. 289 UNITTEST::TestAllTypes message; 290 291 EXPECT_FALSE(message.has_optional_string()); 292 const std::string kHello("hello"); 293 message.set_optional_string(kHello); 294 EXPECT_TRUE(message.has_optional_string()); 295 296 message.set_allocated_optional_string(NULL); 297 EXPECT_FALSE(message.has_optional_string()); 298 EXPECT_EQ("", message.optional_string()); 299 300 message.set_allocated_optional_string(new std::string(kHello)); 301 EXPECT_TRUE(message.has_optional_string()); 302 EXPECT_EQ(kHello, message.optional_string()); 303} 304 305TEST(GENERATED_MESSAGE_TEST_NAME, SetAllocatedMessage) { 306 // Check that set_allocated_foo() can be called in all cases. 307 UNITTEST::TestAllTypes message; 308 309 EXPECT_FALSE(message.has_optional_nested_message()); 310 311 message.mutable_optional_nested_message()->set_bb(1); 312 EXPECT_TRUE(message.has_optional_nested_message()); 313 314 message.set_allocated_optional_nested_message(NULL); 315 EXPECT_FALSE(message.has_optional_nested_message()); 316 EXPECT_EQ(&UNITTEST::TestAllTypes::NestedMessage::default_instance(), 317 &message.optional_nested_message()); 318 319 message.mutable_optional_nested_message()->set_bb(1); 320 UNITTEST::TestAllTypes::NestedMessage* nest = 321 message.release_optional_nested_message(); 322 ASSERT_TRUE(nest != NULL); 323 EXPECT_FALSE(message.has_optional_nested_message()); 324 325 message.set_allocated_optional_nested_message(nest); 326 EXPECT_TRUE(message.has_optional_nested_message()); 327 EXPECT_EQ(1, message.optional_nested_message().bb()); 328} 329 330TEST(GENERATED_MESSAGE_TEST_NAME, Clear) { 331 // Set every field to a unique value, clear the message, then check that 332 // it is cleared. 333 UNITTEST::TestAllTypes message; 334 335 TestUtil::SetAllFields(&message); 336 message.Clear(); 337 TestUtil::ExpectClear(message); 338 339 // Unlike with the defaults test, we do NOT expect that requesting embedded 340 // messages will return a pointer to the default instance. Instead, they 341 // should return the objects that were created when mutable_blah() was 342 // called. 343 EXPECT_NE(&UNITTEST::TestAllTypes::OptionalGroup::default_instance(), 344 &message.optionalgroup()); 345 EXPECT_NE(&UNITTEST::TestAllTypes::NestedMessage::default_instance(), 346 &message.optional_nested_message()); 347 EXPECT_NE(&UNITTEST::ForeignMessage::default_instance(), 348 &message.optional_foreign_message()); 349 EXPECT_NE(&UNITTEST_IMPORT::ImportMessage::default_instance(), 350 &message.optional_import_message()); 351} 352 353TEST(GENERATED_MESSAGE_TEST_NAME, EmbeddedNullsInBytesCharStar) { 354 UNITTEST::TestAllTypes message; 355 356 const char* value = "\0lalala\0\0"; 357 message.set_optional_bytes(value, 9); 358 ASSERT_EQ(9, message.optional_bytes().size()); 359 EXPECT_EQ(0, memcmp(value, message.optional_bytes().data(), 9)); 360 361 message.add_repeated_bytes(value, 9); 362 ASSERT_EQ(9, message.repeated_bytes(0).size()); 363 EXPECT_EQ(0, memcmp(value, message.repeated_bytes(0).data(), 9)); 364} 365 366TEST(GENERATED_MESSAGE_TEST_NAME, ClearOneField) { 367 // Set every field to a unique value, then clear one value and insure that 368 // only that one value is cleared. 369 UNITTEST::TestAllTypes message; 370 371 TestUtil::SetAllFields(&message); 372 int64 original_value = message.optional_int64(); 373 374 // Clear the field and make sure it shows up as cleared. 375 message.clear_optional_int64(); 376 EXPECT_FALSE(message.has_optional_int64()); 377 EXPECT_EQ(0, message.optional_int64()); 378 379 // Other adjacent fields should not be cleared. 380 EXPECT_TRUE(message.has_optional_int32()); 381 EXPECT_TRUE(message.has_optional_uint32()); 382 383 // Make sure if we set it again, then all fields are set. 384 message.set_optional_int64(original_value); 385 TestUtil::ExpectAllFieldsSet(message); 386} 387 388TEST(GENERATED_MESSAGE_TEST_NAME, StringCharStarLength) { 389 // Verify that we can use a char*,length to set one of the string fields. 390 UNITTEST::TestAllTypes message; 391 message.set_optional_string("abcdef", 3); 392 EXPECT_EQ("abc", message.optional_string()); 393 394 // Verify that we can use a char*,length to add to a repeated string field. 395 message.add_repeated_string("abcdef", 3); 396 EXPECT_EQ(1, message.repeated_string_size()); 397 EXPECT_EQ("abc", message.repeated_string(0)); 398 399 // Verify that we can use a char*,length to set a repeated string field. 400 message.set_repeated_string(0, "wxyz", 2); 401 EXPECT_EQ("wx", message.repeated_string(0)); 402} 403 404 405TEST(GENERATED_MESSAGE_TEST_NAME, CopyFrom) { 406 UNITTEST::TestAllTypes message1, message2; 407 408 TestUtil::SetAllFields(&message1); 409 message2.CopyFrom(message1); 410 TestUtil::ExpectAllFieldsSet(message2); 411 412 // Copying from self should be a no-op. 413 message2.CopyFrom(message2); 414 TestUtil::ExpectAllFieldsSet(message2); 415} 416 417 418TEST(GENERATED_MESSAGE_TEST_NAME, SwapWithEmpty) { 419 UNITTEST::TestAllTypes message1, message2; 420 TestUtil::SetAllFields(&message1); 421 422 TestUtil::ExpectAllFieldsSet(message1); 423 TestUtil::ExpectClear(message2); 424 message1.Swap(&message2); 425 TestUtil::ExpectAllFieldsSet(message2); 426 TestUtil::ExpectClear(message1); 427} 428 429TEST(GENERATED_MESSAGE_TEST_NAME, SwapWithSelf) { 430 UNITTEST::TestAllTypes message; 431 TestUtil::SetAllFields(&message); 432 TestUtil::ExpectAllFieldsSet(message); 433 message.Swap(&message); 434 TestUtil::ExpectAllFieldsSet(message); 435} 436 437TEST(GENERATED_MESSAGE_TEST_NAME, SwapWithOther) { 438 UNITTEST::TestAllTypes message1, message2; 439 440 message1.set_optional_int32(123); 441 message1.set_optional_string("abc"); 442 message1.mutable_optional_nested_message()->set_bb(1); 443 message1.set_optional_nested_enum(UNITTEST::TestAllTypes::FOO); 444 message1.add_repeated_int32(1); 445 message1.add_repeated_int32(2); 446 message1.add_repeated_string("a"); 447 message1.add_repeated_string("b"); 448 message1.add_repeated_nested_message()->set_bb(7); 449 message1.add_repeated_nested_message()->set_bb(8); 450 message1.add_repeated_nested_enum(UNITTEST::TestAllTypes::FOO); 451 message1.add_repeated_nested_enum(UNITTEST::TestAllTypes::BAR); 452 453 message2.set_optional_int32(456); 454 message2.set_optional_string("def"); 455 message2.mutable_optional_nested_message()->set_bb(2); 456 message2.set_optional_nested_enum(UNITTEST::TestAllTypes::BAR); 457 message2.add_repeated_int32(3); 458 message2.add_repeated_string("c"); 459 message2.add_repeated_nested_message()->set_bb(9); 460 message2.add_repeated_nested_enum(UNITTEST::TestAllTypes::BAZ); 461 462 message1.Swap(&message2); 463 464 EXPECT_EQ(456, message1.optional_int32()); 465 EXPECT_EQ("def", message1.optional_string()); 466 EXPECT_EQ(2, message1.optional_nested_message().bb()); 467 EXPECT_EQ(UNITTEST::TestAllTypes::BAR, message1.optional_nested_enum()); 468 ASSERT_EQ(1, message1.repeated_int32_size()); 469 EXPECT_EQ(3, message1.repeated_int32(0)); 470 ASSERT_EQ(1, message1.repeated_string_size()); 471 EXPECT_EQ("c", message1.repeated_string(0)); 472 ASSERT_EQ(1, message1.repeated_nested_message_size()); 473 EXPECT_EQ(9, message1.repeated_nested_message(0).bb()); 474 ASSERT_EQ(1, message1.repeated_nested_enum_size()); 475 EXPECT_EQ(UNITTEST::TestAllTypes::BAZ, message1.repeated_nested_enum(0)); 476 477 EXPECT_EQ(123, message2.optional_int32()); 478 EXPECT_EQ("abc", message2.optional_string()); 479 EXPECT_EQ(1, message2.optional_nested_message().bb()); 480 EXPECT_EQ(UNITTEST::TestAllTypes::FOO, message2.optional_nested_enum()); 481 ASSERT_EQ(2, message2.repeated_int32_size()); 482 EXPECT_EQ(1, message2.repeated_int32(0)); 483 EXPECT_EQ(2, message2.repeated_int32(1)); 484 ASSERT_EQ(2, message2.repeated_string_size()); 485 EXPECT_EQ("a", message2.repeated_string(0)); 486 EXPECT_EQ("b", message2.repeated_string(1)); 487 ASSERT_EQ(2, message2.repeated_nested_message_size()); 488 EXPECT_EQ(7, message2.repeated_nested_message(0).bb()); 489 EXPECT_EQ(8, message2.repeated_nested_message(1).bb()); 490 ASSERT_EQ(2, message2.repeated_nested_enum_size()); 491 EXPECT_EQ(UNITTEST::TestAllTypes::FOO, message2.repeated_nested_enum(0)); 492 EXPECT_EQ(UNITTEST::TestAllTypes::BAR, message2.repeated_nested_enum(1)); 493} 494 495TEST(GENERATED_MESSAGE_TEST_NAME, ADLSwap) { 496 UNITTEST::TestAllTypes message1, message2; 497 TestUtil::SetAllFields(&message1); 498 499 // Note the address of one of the repeated fields, to verify it was swapped 500 // rather than copied. 501 const int32* addr = &message1.repeated_int32().Get(0); 502 503 using std::swap; 504 swap(message1, message2); 505 506 TestUtil::ExpectAllFieldsSet(message2); 507 TestUtil::ExpectClear(message1); 508 509 EXPECT_EQ(addr, &message2.repeated_int32().Get(0)); 510} 511 512TEST(GENERATED_MESSAGE_TEST_NAME, CopyConstructor) { 513 // All set. 514 { 515 UNITTEST::TestAllTypes message1; 516 TestUtil::SetAllFields(&message1); 517 518 UNITTEST::TestAllTypes message2(message1); 519 TestUtil::ExpectAllFieldsSet(message2); 520 } 521 522 // None set. 523 { 524 UNITTEST::TestAllTypes message1; 525 UNITTEST::TestAllTypes message2(message1); 526 527 EXPECT_FALSE(message1.has_optional_string()); 528 EXPECT_FALSE(message2.has_optional_string()); 529 EXPECT_EQ(message1.optional_string(), message2.optional_string()); 530 531 EXPECT_FALSE(message1.has_optional_bytes()); 532 EXPECT_FALSE(message2.has_optional_bytes()); 533 EXPECT_EQ(message1.optional_bytes(), message2.optional_bytes()); 534 535 EXPECT_FALSE(message1.has_optional_nested_message()); 536 EXPECT_FALSE(message2.has_optional_nested_message()); 537 EXPECT_EQ(&message1.optional_nested_message(), 538 &message2.optional_nested_message()); 539 540 EXPECT_FALSE(message1.has_optional_foreign_message()); 541 EXPECT_FALSE(message2.has_optional_foreign_message()); 542 EXPECT_EQ(&message1.optional_foreign_message(), 543 &message2.optional_foreign_message()); 544 545 EXPECT_FALSE(message1.has_optional_import_message()); 546 EXPECT_FALSE(message2.has_optional_import_message()); 547 EXPECT_EQ(&message1.optional_import_message(), 548 &message2.optional_import_message()); 549 550 EXPECT_FALSE(message1.has_optional_public_import_message()); 551 EXPECT_FALSE(message2.has_optional_public_import_message()); 552 EXPECT_EQ(&message1.optional_public_import_message(), 553 &message2.optional_public_import_message()); 554 555 EXPECT_FALSE(message1.has_optional_lazy_message()); 556 EXPECT_FALSE(message2.has_optional_lazy_message()); 557 EXPECT_EQ(&message1.optional_lazy_message(), 558 &message2.optional_lazy_message()); 559 } 560} 561 562TEST(GENERATED_MESSAGE_TEST_NAME, CopyConstructorWithArenas) { 563 Arena arena; 564 UNITTEST::TestAllTypes* message1 = 565 Arena::CreateMessage<UNITTEST::TestAllTypes>(&arena); 566 TestUtil::SetAllFields(message1); 567 568 UNITTEST::TestAllTypes message2_stack(*message1); 569 TestUtil::ExpectAllFieldsSet(message2_stack); 570 571 std::unique_ptr<UNITTEST::TestAllTypes> message2_heap( 572 new UNITTEST::TestAllTypes(*message1)); 573 TestUtil::ExpectAllFieldsSet(*message2_heap); 574 575 arena.Reset(); 576 577 // Verify that the copies are still intact. 578 TestUtil::ExpectAllFieldsSet(message2_stack); 579 TestUtil::ExpectAllFieldsSet(*message2_heap); 580} 581 582TEST(GENERATED_MESSAGE_TEST_NAME, CopyAssignmentOperator) { 583 UNITTEST::TestAllTypes message1; 584 TestUtil::SetAllFields(&message1); 585 586 UNITTEST::TestAllTypes message2; 587 message2 = message1; 588 TestUtil::ExpectAllFieldsSet(message2); 589 590 // Make sure that self-assignment does something sane. 591 message2.operator=(message2); 592 TestUtil::ExpectAllFieldsSet(message2); 593} 594 595#if !defined(PROTOBUF_TEST_NO_DESCRIPTORS) || PROTOBUF_RTTI 596TEST(GENERATED_MESSAGE_TEST_NAME, UpcastCopyFrom) { 597 // Test the CopyFrom method that takes in the generic const Message& 598 // parameter. 599 UNITTEST::TestAllTypes message1, message2; 600 601 TestUtil::SetAllFields(&message1); 602 603 const Message* source = implicit_cast<const Message*>(&message1); 604 message2.CopyFrom(*source); 605 606 TestUtil::ExpectAllFieldsSet(message2); 607} 608#endif 609 610#ifndef PROTOBUF_TEST_NO_DESCRIPTORS 611 612TEST(GENERATED_MESSAGE_TEST_NAME, DynamicMessageCopyFrom) { 613 // Test copying from a DynamicMessage, which must fall back to using 614 // reflection. 615 UNITTEST::TestAllTypes message2; 616 617 // Construct a new version of the dynamic message via the factory. 618 DynamicMessageFactory factory; 619 std::unique_ptr<Message> message1; 620 message1.reset(factory.GetPrototype( 621 UNITTEST::TestAllTypes::descriptor())->New()); 622 623 TestUtil::ReflectionTester reflection_tester( 624 UNITTEST::TestAllTypes::descriptor()); 625 reflection_tester.SetAllFieldsViaReflection(message1.get()); 626 627 message2.CopyFrom(*message1); 628 629 TestUtil::ExpectAllFieldsSet(message2); 630} 631 632#endif // !PROTOBUF_TEST_NO_DESCRIPTORS 633 634TEST(GENERATED_MESSAGE_TEST_NAME, NonEmptyMergeFrom) { 635 // Test merging with a non-empty message. Code is a modified form 636 // of that found in net/proto2/internal/reflection_ops_unittest.cc. 637 UNITTEST::TestAllTypes message1, message2; 638 639 TestUtil::SetAllFields(&message1); 640 641 // This field will test merging into an empty spot. 642 message2.set_optional_int32(message1.optional_int32()); 643 message1.clear_optional_int32(); 644 645 // This tests overwriting. 646 message2.set_optional_string(message1.optional_string()); 647 message1.set_optional_string("something else"); 648 649 // This tests concatenating. 650 message2.add_repeated_int32(message1.repeated_int32(1)); 651 int32 i = message1.repeated_int32(0); 652 message1.clear_repeated_int32(); 653 message1.add_repeated_int32(i); 654 655 message1.MergeFrom(message2); 656 657 TestUtil::ExpectAllFieldsSet(message1); 658} 659 660 661// Test the generated SerializeWithCachedSizesToArray(), 662TEST(GENERATED_MESSAGE_TEST_NAME, SerializationToArray) { 663 UNITTEST::TestAllTypes message1, message2; 664 std::string data; 665 TestUtil::SetAllFields(&message1); 666 int size = message1.ByteSizeLong(); 667 data.resize(size); 668 uint8* start = reinterpret_cast<uint8*>(::google::protobuf::string_as_array(&data)); 669 uint8* end = message1.SerializeWithCachedSizesToArray(start); 670 EXPECT_EQ(size, end - start); 671 EXPECT_TRUE(message2.ParseFromString(data)); 672 TestUtil::ExpectAllFieldsSet(message2); 673 674} 675 676TEST(GENERATED_MESSAGE_TEST_NAME, PackedFieldsSerializationToArray) { 677 UNITTEST::TestPackedTypes packed_message1, packed_message2; 678 std::string packed_data; 679 TestUtil::SetPackedFields(&packed_message1); 680 int packed_size = packed_message1.ByteSizeLong(); 681 packed_data.resize(packed_size); 682 uint8* start = reinterpret_cast<uint8*>(::google::protobuf::string_as_array(&packed_data)); 683 uint8* end = packed_message1.SerializeWithCachedSizesToArray(start); 684 EXPECT_EQ(packed_size, end - start); 685 EXPECT_TRUE(packed_message2.ParseFromString(packed_data)); 686 TestUtil::ExpectPackedFieldsSet(packed_message2); 687} 688 689// Test the generated SerializeWithCachedSizes() by forcing the buffer to write 690// one byte at a time. 691TEST(GENERATED_MESSAGE_TEST_NAME, SerializationToStream) { 692 UNITTEST::TestAllTypes message1, message2; 693 TestUtil::SetAllFields(&message1); 694 int size = message1.ByteSizeLong(); 695 std::string data; 696 data.resize(size); 697 { 698 // Allow the output stream to buffer only one byte at a time. 699 io::ArrayOutputStream array_stream(::google::protobuf::string_as_array(&data), size, 1); 700 io::CodedOutputStream output_stream(&array_stream); 701 message1.SerializeWithCachedSizes(&output_stream); 702 EXPECT_FALSE(output_stream.HadError()); 703 EXPECT_EQ(size, output_stream.ByteCount()); 704 } 705 EXPECT_TRUE(message2.ParseFromString(data)); 706 TestUtil::ExpectAllFieldsSet(message2); 707 708} 709 710TEST(GENERATED_MESSAGE_TEST_NAME, PackedFieldsSerializationToStream) { 711 UNITTEST::TestPackedTypes message1, message2; 712 TestUtil::SetPackedFields(&message1); 713 int size = message1.ByteSizeLong(); 714 std::string data; 715 data.resize(size); 716 { 717 // Allow the output stream to buffer only one byte at a time. 718 io::ArrayOutputStream array_stream(::google::protobuf::string_as_array(&data), size, 1); 719 io::CodedOutputStream output_stream(&array_stream); 720 message1.SerializeWithCachedSizes(&output_stream); 721 EXPECT_FALSE(output_stream.HadError()); 722 EXPECT_EQ(size, output_stream.ByteCount()); 723 } 724 EXPECT_TRUE(message2.ParseFromString(data)); 725 TestUtil::ExpectPackedFieldsSet(message2); 726} 727 728 729TEST(GENERATED_MESSAGE_TEST_NAME, Required) { 730 // Test that IsInitialized() returns false if required fields are missing. 731 UNITTEST::TestRequired message; 732 733 EXPECT_FALSE(message.IsInitialized()); 734 message.set_a(1); 735 EXPECT_FALSE(message.IsInitialized()); 736 message.set_b(2); 737 EXPECT_FALSE(message.IsInitialized()); 738 message.set_c(3); 739 EXPECT_TRUE(message.IsInitialized()); 740} 741 742TEST(GENERATED_MESSAGE_TEST_NAME, RequiredForeign) { 743 // Test that IsInitialized() returns false if required fields in nested 744 // messages are missing. 745 UNITTEST::TestRequiredForeign message; 746 747 EXPECT_TRUE(message.IsInitialized()); 748 749 message.mutable_optional_message(); 750 EXPECT_FALSE(message.IsInitialized()); 751 752 message.mutable_optional_message()->set_a(1); 753 message.mutable_optional_message()->set_b(2); 754 message.mutable_optional_message()->set_c(3); 755 EXPECT_TRUE(message.IsInitialized()); 756 757 message.add_repeated_message(); 758 EXPECT_FALSE(message.IsInitialized()); 759 760 message.mutable_repeated_message(0)->set_a(1); 761 message.mutable_repeated_message(0)->set_b(2); 762 message.mutable_repeated_message(0)->set_c(3); 763 EXPECT_TRUE(message.IsInitialized()); 764} 765 766TEST(GENERATED_MESSAGE_TEST_NAME, ForeignNested) { 767 // Test that TestAllTypes::NestedMessage can be embedded directly into 768 // another message. 769 UNITTEST::TestForeignNested message; 770 771 // If this compiles and runs without crashing, it must work. We have 772 // nothing more to test. 773 UNITTEST::TestAllTypes::NestedMessage* nested = 774 message.mutable_foreign_nested(); 775 nested->set_bb(1); 776} 777 778TEST(GENERATED_MESSAGE_TEST_NAME, ReallyLargeTagNumber) { 779 // Test that really large tag numbers don't break anything. 780 UNITTEST::TestReallyLargeTagNumber message1, message2; 781 std::string data; 782 783 // For the most part, if this compiles and runs then we're probably good. 784 // (The most likely cause for failure would be if something were attempting 785 // to allocate a lookup table of some sort using tag numbers as the index.) 786 // We'll try serializing just for fun. 787 message1.set_a(1234); 788 message1.set_bb(5678); 789 message1.SerializeToString(&data); 790 EXPECT_TRUE(message2.ParseFromString(data)); 791 EXPECT_EQ(1234, message2.a()); 792 EXPECT_EQ(5678, message2.bb()); 793} 794 795TEST(GENERATED_MESSAGE_TEST_NAME, MutualRecursion) { 796 // Test that mutually-recursive message types work. 797 UNITTEST::TestMutualRecursionA message; 798 UNITTEST::TestMutualRecursionA* nested = message.mutable_bb()->mutable_a(); 799 UNITTEST::TestMutualRecursionA* nested2 = nested->mutable_bb()->mutable_a(); 800 801 // Again, if the above compiles and runs, that's all we really have to 802 // test, but just for run we'll check that the system didn't somehow come 803 // up with a pointer loop... 804 EXPECT_NE(&message, nested); 805 EXPECT_NE(&message, nested2); 806 EXPECT_NE(nested, nested2); 807} 808 809TEST(GENERATED_MESSAGE_TEST_NAME, CamelCaseFieldNames) { 810 // This test is mainly checking that the following compiles, which verifies 811 // that the field names were coerced to lower-case. 812 // 813 // Protocol buffers standard style is to use lowercase-with-underscores for 814 // field names. Some old proto1 .protos unfortunately used camel-case field 815 // names. In proto1, these names were forced to lower-case. So, we do the 816 // same thing in proto2. 817 818 UNITTEST::TestCamelCaseFieldNames message; 819 820 message.set_primitivefield(2); 821 message.set_stringfield("foo"); 822 message.set_enumfield(UNITTEST::FOREIGN_FOO); 823 message.mutable_messagefield()->set_c(6); 824 825 message.add_repeatedprimitivefield(8); 826 message.add_repeatedstringfield("qux"); 827 message.add_repeatedenumfield(UNITTEST::FOREIGN_BAR); 828 message.add_repeatedmessagefield()->set_c(15); 829 830 EXPECT_EQ(2, message.primitivefield()); 831 EXPECT_EQ("foo", message.stringfield()); 832 EXPECT_EQ(UNITTEST::FOREIGN_FOO, message.enumfield()); 833 EXPECT_EQ(6, message.messagefield().c()); 834 835 EXPECT_EQ(8, message.repeatedprimitivefield(0)); 836 EXPECT_EQ("qux", message.repeatedstringfield(0)); 837 EXPECT_EQ(UNITTEST::FOREIGN_BAR, message.repeatedenumfield(0)); 838 EXPECT_EQ(15, message.repeatedmessagefield(0).c()); 839} 840 841#ifndef PROTOBUF_TEST_NO_DESCRIPTORS 842 843TEST(GENERATED_MESSAGE_TEST_NAME, TestOptimizedForSize) { 844 // We rely on the tests in reflection_ops_unittest and wire_format_unittest 845 // to really test that reflection-based methods work. Here we are mostly 846 // just making sure that TestOptimizedForSize actually builds and seems to 847 // function. 848 849 UNITTEST::TestOptimizedForSize message, message2; 850 message.set_i(1); 851 message.mutable_msg()->set_c(2); 852 message2.CopyFrom(message); 853 EXPECT_EQ(1, message2.i()); 854 EXPECT_EQ(2, message2.msg().c()); 855} 856 857TEST(GENERATED_MESSAGE_TEST_NAME, TestEmbedOptimizedForSize) { 858 // Verifies that something optimized for speed can contain something optimized 859 // for size. 860 861 UNITTEST::TestEmbedOptimizedForSize message, message2; 862 message.mutable_optional_message()->set_i(1); 863 message.add_repeated_message()->mutable_msg()->set_c(2); 864 std::string data; 865 message.SerializeToString(&data); 866 ASSERT_TRUE(message2.ParseFromString(data)); 867 EXPECT_EQ(1, message2.optional_message().i()); 868 EXPECT_EQ(2, message2.repeated_message(0).msg().c()); 869} 870 871TEST(GENERATED_MESSAGE_TEST_NAME, TestSpaceUsed) { 872 UNITTEST::TestAllTypes message1; 873 // sizeof provides a lower bound on SpaceUsedLong(). 874 EXPECT_LE(sizeof(UNITTEST::TestAllTypes), message1.SpaceUsedLong()); 875 const size_t empty_message_size = message1.SpaceUsedLong(); 876 877 // Setting primitive types shouldn't affect the space used. 878 message1.set_optional_int32(123); 879 message1.set_optional_int64(12345); 880 message1.set_optional_uint32(123); 881 message1.set_optional_uint64(12345); 882 EXPECT_EQ(empty_message_size, message1.SpaceUsedLong()); 883 884 // On some STL implementations, setting the string to a small value should 885 // only increase SpaceUsedLong() by the size of a string object, though this 886 // is not true everywhere. 887 message1.set_optional_string("abc"); 888 EXPECT_LE(empty_message_size + message1.optional_string().size(), 889 message1.SpaceUsedLong()); 890 891 // Setting a string to a value larger than the string object itself should 892 // increase SpaceUsedLong(), because it cannot store the value internally. 893 message1.set_optional_string(std::string(sizeof(std::string) + 1, 'x')); 894 int min_expected_increase = message1.optional_string().capacity(); 895 EXPECT_LE(empty_message_size + min_expected_increase, 896 message1.SpaceUsedLong()); 897 898 size_t previous_size = message1.SpaceUsedLong(); 899 // Adding an optional message should increase the size by the size of the 900 // nested message type. NestedMessage is simple enough (1 int field) that it 901 // is equal to sizeof(NestedMessage) 902 message1.mutable_optional_nested_message(); 903 ASSERT_EQ(sizeof(UNITTEST::TestAllTypes::NestedMessage), 904 message1.optional_nested_message().SpaceUsedLong()); 905 EXPECT_EQ(previous_size + 906 sizeof(UNITTEST::TestAllTypes::NestedMessage), 907 message1.SpaceUsedLong()); 908} 909 910TEST(GENERATED_MESSAGE_TEST_NAME, TestOneofSpaceUsed) { 911 UNITTEST::TestOneof2 message1; 912 EXPECT_LE(sizeof(UNITTEST::TestOneof2), message1.SpaceUsedLong()); 913 914 const size_t empty_message_size = message1.SpaceUsedLong(); 915 // Setting primitive types shouldn't affect the space used. 916 message1.set_foo_int(123); 917 message1.set_bar_int(12345); 918 EXPECT_EQ(empty_message_size, message1.SpaceUsedLong()); 919 920 // Setting a string in oneof to a small value should only increase 921 // SpaceUsedLong() by the size of a string object. 922 message1.set_foo_string("abc"); 923 EXPECT_LE(empty_message_size + sizeof(std::string), message1.SpaceUsedLong()); 924 925 // Setting a string in oneof to a value larger than the string object itself 926 // should increase SpaceUsedLong(), because it cannot store the value 927 // internally. 928 message1.set_foo_string(std::string(sizeof(std::string) + 1, 'x')); 929 int min_expected_increase = 930 message1.foo_string().capacity() + sizeof(std::string); 931 EXPECT_LE(empty_message_size + min_expected_increase, 932 message1.SpaceUsedLong()); 933 934 // Setting a message in oneof should delete the other fields and increase the 935 // size by the size of the nested message type. NestedMessage is simple enough 936 // that it is equal to sizeof(NestedMessage) 937 message1.mutable_foo_message(); 938 ASSERT_EQ(sizeof(UNITTEST::TestOneof2::NestedMessage), 939 message1.foo_message().SpaceUsedLong()); 940 EXPECT_EQ(empty_message_size + 941 sizeof(UNITTEST::TestOneof2::NestedMessage), 942 message1.SpaceUsedLong()); 943} 944 945#endif // !PROTOBUF_TEST_NO_DESCRIPTORS 946 947 948TEST(GENERATED_MESSAGE_TEST_NAME, FieldConstantValues) { 949 UNITTEST::TestRequired message; 950 EXPECT_EQ(UNITTEST::TestAllTypes_NestedMessage::kBbFieldNumber, 1); 951 EXPECT_EQ(UNITTEST::TestAllTypes::kOptionalInt32FieldNumber, 1); 952 EXPECT_EQ(UNITTEST::TestAllTypes::kOptionalgroupFieldNumber, 16); 953 EXPECT_EQ(UNITTEST::TestAllTypes::kOptionalNestedMessageFieldNumber, 18); 954 EXPECT_EQ(UNITTEST::TestAllTypes::kOptionalNestedEnumFieldNumber, 21); 955 EXPECT_EQ(UNITTEST::TestAllTypes::kRepeatedInt32FieldNumber, 31); 956 EXPECT_EQ(UNITTEST::TestAllTypes::kRepeatedgroupFieldNumber, 46); 957 EXPECT_EQ(UNITTEST::TestAllTypes::kRepeatedNestedMessageFieldNumber, 48); 958 EXPECT_EQ(UNITTEST::TestAllTypes::kRepeatedNestedEnumFieldNumber, 51); 959} 960 961TEST(GENERATED_MESSAGE_TEST_NAME, ExtensionConstantValues) { 962 EXPECT_EQ(UNITTEST::TestRequired::kSingleFieldNumber, 1000); 963 EXPECT_EQ(UNITTEST::TestRequired::kMultiFieldNumber, 1001); 964 EXPECT_EQ(UNITTEST::kOptionalInt32ExtensionFieldNumber, 1); 965 EXPECT_EQ(UNITTEST::kOptionalgroupExtensionFieldNumber, 16); 966 EXPECT_EQ(UNITTEST::kOptionalNestedMessageExtensionFieldNumber, 18); 967 EXPECT_EQ(UNITTEST::kOptionalNestedEnumExtensionFieldNumber, 21); 968 EXPECT_EQ(UNITTEST::kRepeatedInt32ExtensionFieldNumber, 31); 969 EXPECT_EQ(UNITTEST::kRepeatedgroupExtensionFieldNumber, 46); 970 EXPECT_EQ(UNITTEST::kRepeatedNestedMessageExtensionFieldNumber, 48); 971 EXPECT_EQ(UNITTEST::kRepeatedNestedEnumExtensionFieldNumber, 51); 972} 973 974TEST(GENERATED_MESSAGE_TEST_NAME, ParseFromTruncated) { 975 const std::string long_string = std::string(128, 'q'); 976 FileDescriptorProto p; 977 p.add_extension()->set_name(long_string); 978 const std::string msg = p.SerializeAsString(); 979 int successful_count = 0; 980 for (int i = 0; i <= msg.size(); i++) { 981 if (p.ParseFromArray(msg.c_str(), i)) { 982 ++successful_count; 983 } 984 } 985 // We don't really care about how often we succeeded. 986 // As long as we didn't crash, we're happy. 987 EXPECT_GE(successful_count, 1); 988} 989 990// =================================================================== 991 992TEST(GENERATED_ENUM_TEST_NAME, EnumValuesAsSwitchCases) { 993 // Test that our nested enum values can be used as switch cases. This test 994 // doesn't actually do anything, the proof that it works is that it 995 // compiles. 996 int i =0; 997 UNITTEST::TestAllTypes::NestedEnum a = UNITTEST::TestAllTypes::BAR; 998 switch (a) { 999 case UNITTEST::TestAllTypes::FOO: 1000 i = 1; 1001 break; 1002 case UNITTEST::TestAllTypes::BAR: 1003 i = 2; 1004 break; 1005 case UNITTEST::TestAllTypes::BAZ: 1006 i = 3; 1007 break; 1008 case UNITTEST::TestAllTypes::NEG: 1009 i = -1; 1010 break; 1011 // no default case: We want to make sure the compiler recognizes that 1012 // all cases are covered. (GCC warns if you do not cover all cases of 1013 // an enum in a switch.) 1014 } 1015 1016 // Token check just for fun. 1017 EXPECT_EQ(2, i); 1018} 1019 1020TEST(GENERATED_ENUM_TEST_NAME, IsValidValue) { 1021 // Test enum IsValidValue. 1022 EXPECT_TRUE(UNITTEST::TestAllTypes::NestedEnum_IsValid(1)); 1023 EXPECT_TRUE(UNITTEST::TestAllTypes::NestedEnum_IsValid(2)); 1024 EXPECT_TRUE(UNITTEST::TestAllTypes::NestedEnum_IsValid(3)); 1025 1026 EXPECT_FALSE(UNITTEST::TestAllTypes::NestedEnum_IsValid(0)); 1027 EXPECT_FALSE(UNITTEST::TestAllTypes::NestedEnum_IsValid(4)); 1028 1029 // Make sure it also works when there are dups. 1030 EXPECT_TRUE(UNITTEST::TestEnumWithDupValue_IsValid(1)); 1031 EXPECT_TRUE(UNITTEST::TestEnumWithDupValue_IsValid(2)); 1032 EXPECT_TRUE(UNITTEST::TestEnumWithDupValue_IsValid(3)); 1033 1034 EXPECT_FALSE(UNITTEST::TestEnumWithDupValue_IsValid(0)); 1035 EXPECT_FALSE(UNITTEST::TestEnumWithDupValue_IsValid(4)); 1036} 1037 1038TEST(GENERATED_ENUM_TEST_NAME, MinAndMax) { 1039 EXPECT_EQ(UNITTEST::TestAllTypes::NEG, 1040 UNITTEST::TestAllTypes::NestedEnum_MIN); 1041 EXPECT_EQ(UNITTEST::TestAllTypes::BAZ, 1042 UNITTEST::TestAllTypes::NestedEnum_MAX); 1043 EXPECT_EQ(4, UNITTEST::TestAllTypes::NestedEnum_ARRAYSIZE); 1044 1045 EXPECT_EQ(UNITTEST::FOREIGN_FOO, UNITTEST::ForeignEnum_MIN); 1046 EXPECT_EQ(UNITTEST::FOREIGN_BAZ, UNITTEST::ForeignEnum_MAX); 1047 EXPECT_EQ(7, UNITTEST::ForeignEnum_ARRAYSIZE); 1048 1049 EXPECT_EQ(1, UNITTEST::TestEnumWithDupValue_MIN); 1050 EXPECT_EQ(3, UNITTEST::TestEnumWithDupValue_MAX); 1051 EXPECT_EQ(4, UNITTEST::TestEnumWithDupValue_ARRAYSIZE); 1052 1053 EXPECT_EQ(UNITTEST::SPARSE_E, UNITTEST::TestSparseEnum_MIN); 1054 EXPECT_EQ(UNITTEST::SPARSE_C, UNITTEST::TestSparseEnum_MAX); 1055 EXPECT_EQ(12589235, UNITTEST::TestSparseEnum_ARRAYSIZE); 1056 1057 // Make sure we can take the address of _MIN, _MAX and _ARRAYSIZE. 1058 void* null_pointer = 0; // NULL may be integer-type, not pointer-type. 1059 EXPECT_NE(null_pointer, &UNITTEST::TestAllTypes::NestedEnum_MIN); 1060 EXPECT_NE(null_pointer, &UNITTEST::TestAllTypes::NestedEnum_MAX); 1061 EXPECT_NE(null_pointer, &UNITTEST::TestAllTypes::NestedEnum_ARRAYSIZE); 1062 1063 EXPECT_NE(null_pointer, &UNITTEST::ForeignEnum_MIN); 1064 EXPECT_NE(null_pointer, &UNITTEST::ForeignEnum_MAX); 1065 EXPECT_NE(null_pointer, &UNITTEST::ForeignEnum_ARRAYSIZE); 1066 1067 // Make sure we can use _MIN and _MAX as switch cases. 1068 switch (UNITTEST::SPARSE_A) { 1069 case UNITTEST::TestSparseEnum_MIN: 1070 case UNITTEST::TestSparseEnum_MAX: 1071 break; 1072 default: 1073 break; 1074 } 1075} 1076 1077#ifndef PROTOBUF_TEST_NO_DESCRIPTORS 1078 1079TEST(GENERATED_ENUM_TEST_NAME, Name) { 1080 // "Names" in the presence of dup values map to the first alias. 1081 EXPECT_EQ("FOO1", UNITTEST::TestEnumWithDupValue_Name(UNITTEST::FOO1)); 1082 EXPECT_EQ("FOO1", UNITTEST::TestEnumWithDupValue_Name(UNITTEST::FOO2)); 1083 1084 EXPECT_EQ("SPARSE_A", UNITTEST::TestSparseEnum_Name(UNITTEST::SPARSE_A)); 1085 EXPECT_EQ("SPARSE_B", UNITTEST::TestSparseEnum_Name(UNITTEST::SPARSE_B)); 1086 EXPECT_EQ("SPARSE_C", UNITTEST::TestSparseEnum_Name(UNITTEST::SPARSE_C)); 1087 EXPECT_EQ("SPARSE_D", UNITTEST::TestSparseEnum_Name(UNITTEST::SPARSE_D)); 1088 EXPECT_EQ("SPARSE_E", UNITTEST::TestSparseEnum_Name(UNITTEST::SPARSE_E)); 1089 EXPECT_EQ("SPARSE_F", UNITTEST::TestSparseEnum_Name(UNITTEST::SPARSE_F)); 1090 EXPECT_EQ("SPARSE_G", UNITTEST::TestSparseEnum_Name(UNITTEST::SPARSE_G)); 1091} 1092 1093TEST(GENERATED_ENUM_TEST_NAME, Parse) { 1094 UNITTEST::TestEnumWithDupValue dup_value = UNITTEST::FOO1; 1095 EXPECT_TRUE(UNITTEST::TestEnumWithDupValue_Parse("FOO1", &dup_value)); 1096 EXPECT_EQ(UNITTEST::FOO1, dup_value); 1097 EXPECT_TRUE(UNITTEST::TestEnumWithDupValue_Parse("FOO2", &dup_value)); 1098 EXPECT_EQ(UNITTEST::FOO2, dup_value); 1099 EXPECT_FALSE(UNITTEST::TestEnumWithDupValue_Parse("FOO", &dup_value)); 1100} 1101 1102TEST(GENERATED_ENUM_TEST_NAME, GetEnumDescriptor) { 1103 EXPECT_EQ(UNITTEST::TestAllTypes::NestedEnum_descriptor(), 1104 GetEnumDescriptor<UNITTEST::TestAllTypes::NestedEnum>()); 1105 EXPECT_EQ(UNITTEST::ForeignEnum_descriptor(), 1106 GetEnumDescriptor<UNITTEST::ForeignEnum>()); 1107 EXPECT_EQ(UNITTEST::TestEnumWithDupValue_descriptor(), 1108 GetEnumDescriptor<UNITTEST::TestEnumWithDupValue>()); 1109 EXPECT_EQ(UNITTEST::TestSparseEnum_descriptor(), 1110 GetEnumDescriptor<UNITTEST::TestSparseEnum>()); 1111} 1112 1113enum NonProtoEnum { 1114 kFoo = 1, 1115}; 1116 1117TEST(GENERATED_ENUM_TEST_NAME, IsProtoEnumTypeTrait) { 1118 EXPECT_TRUE(is_proto_enum<UNITTEST::TestAllTypes::NestedEnum>::value); 1119 EXPECT_TRUE(is_proto_enum<UNITTEST::ForeignEnum>::value); 1120 EXPECT_TRUE(is_proto_enum<UNITTEST::TestEnumWithDupValue>::value); 1121 EXPECT_TRUE(is_proto_enum<UNITTEST::TestSparseEnum>::value); 1122 1123 EXPECT_FALSE(is_proto_enum<int>::value); 1124 EXPECT_FALSE(is_proto_enum<NonProtoEnum>::value); 1125} 1126 1127#endif // PROTOBUF_TEST_NO_DESCRIPTORS 1128 1129// =================================================================== 1130 1131#ifndef PROTOBUF_TEST_NO_DESCRIPTORS 1132 1133// Support code for testing services. 1134class GENERATED_SERVICE_TEST_NAME : public testing::Test { 1135 protected: 1136 class MockTestService : public UNITTEST::TestService { 1137 public: 1138 MockTestService() 1139 : called_(false), 1140 method_(""), 1141 controller_(NULL), 1142 request_(NULL), 1143 response_(NULL), 1144 done_(NULL) {} 1145 1146 ~MockTestService() {} 1147 1148 void Reset() { called_ = false; } 1149 1150 // implements TestService ---------------------------------------- 1151 1152 void Foo(RpcController* controller, 1153 const UNITTEST::FooRequest* request, 1154 UNITTEST::FooResponse* response, 1155 Closure* done) { 1156 ASSERT_FALSE(called_); 1157 called_ = true; 1158 method_ = "Foo"; 1159 controller_ = controller; 1160 request_ = request; 1161 response_ = response; 1162 done_ = done; 1163 } 1164 1165 void Bar(RpcController* controller, 1166 const UNITTEST::BarRequest* request, 1167 UNITTEST::BarResponse* response, 1168 Closure* done) { 1169 ASSERT_FALSE(called_); 1170 called_ = true; 1171 method_ = "Bar"; 1172 controller_ = controller; 1173 request_ = request; 1174 response_ = response; 1175 done_ = done; 1176 } 1177 1178 // --------------------------------------------------------------- 1179 1180 bool called_; 1181 std::string method_; 1182 RpcController* controller_; 1183 const Message* request_; 1184 Message* response_; 1185 Closure* done_; 1186 }; 1187 1188 class MockRpcChannel : public RpcChannel { 1189 public: 1190 MockRpcChannel() 1191 : called_(false), 1192 method_(NULL), 1193 controller_(NULL), 1194 request_(NULL), 1195 response_(NULL), 1196 done_(NULL), 1197 destroyed_(NULL) {} 1198 1199 ~MockRpcChannel() { 1200 if (destroyed_ != NULL) *destroyed_ = true; 1201 } 1202 1203 void Reset() { called_ = false; } 1204 1205 // implements TestService ---------------------------------------- 1206 1207 void CallMethod(const MethodDescriptor* method, 1208 RpcController* controller, 1209 const Message* request, 1210 Message* response, 1211 Closure* done) { 1212 ASSERT_FALSE(called_); 1213 called_ = true; 1214 method_ = method; 1215 controller_ = controller; 1216 request_ = request; 1217 response_ = response; 1218 done_ = done; 1219 } 1220 1221 // --------------------------------------------------------------- 1222 1223 bool called_; 1224 const MethodDescriptor* method_; 1225 RpcController* controller_; 1226 const Message* request_; 1227 Message* response_; 1228 Closure* done_; 1229 bool* destroyed_; 1230 }; 1231 1232 class MockController : public RpcController { 1233 public: 1234 void Reset() { 1235 ADD_FAILURE() << "Reset() not expected during this test."; 1236 } 1237 bool Failed() const { 1238 ADD_FAILURE() << "Failed() not expected during this test."; 1239 return false; 1240 } 1241 std::string ErrorText() const { 1242 ADD_FAILURE() << "ErrorText() not expected during this test."; 1243 return ""; 1244 } 1245 void StartCancel() { 1246 ADD_FAILURE() << "StartCancel() not expected during this test."; 1247 } 1248 void SetFailed(const std::string& reason) { 1249 ADD_FAILURE() << "SetFailed() not expected during this test."; 1250 } 1251 bool IsCanceled() const { 1252 ADD_FAILURE() << "IsCanceled() not expected during this test."; 1253 return false; 1254 } 1255 void NotifyOnCancel(Closure* callback) { 1256 ADD_FAILURE() << "NotifyOnCancel() not expected during this test."; 1257 } 1258 }; 1259 1260 GENERATED_SERVICE_TEST_NAME() 1261 : descriptor_(UNITTEST::TestService::descriptor()), 1262 foo_(descriptor_->FindMethodByName("Foo")), 1263 bar_(descriptor_->FindMethodByName("Bar")), 1264 stub_(&mock_channel_), 1265 done_(::google::protobuf::NewPermanentCallback(&DoNothing)) {} 1266 1267 virtual void SetUp() { 1268 ASSERT_TRUE(foo_ != NULL); 1269 ASSERT_TRUE(bar_ != NULL); 1270 } 1271 1272 const ServiceDescriptor* descriptor_; 1273 const MethodDescriptor* foo_; 1274 const MethodDescriptor* bar_; 1275 1276 MockTestService mock_service_; 1277 MockController mock_controller_; 1278 1279 MockRpcChannel mock_channel_; 1280 UNITTEST::TestService::Stub stub_; 1281 1282 // Just so we don't have to re-define these with every test. 1283 UNITTEST::FooRequest foo_request_; 1284 UNITTEST::FooResponse foo_response_; 1285 UNITTEST::BarRequest bar_request_; 1286 UNITTEST::BarResponse bar_response_; 1287 std::unique_ptr<Closure> done_; 1288}; 1289 1290TEST_F(GENERATED_SERVICE_TEST_NAME, GetDescriptor) { 1291 // Test that GetDescriptor() works. 1292 1293 EXPECT_EQ(descriptor_, mock_service_.GetDescriptor()); 1294} 1295 1296TEST_F(GENERATED_SERVICE_TEST_NAME, GetChannel) { 1297 EXPECT_EQ(&mock_channel_, stub_.channel()); 1298} 1299 1300TEST_F(GENERATED_SERVICE_TEST_NAME, OwnsChannel) { 1301 MockRpcChannel* channel = new MockRpcChannel; 1302 bool destroyed = false; 1303 channel->destroyed_ = &destroyed; 1304 1305 { 1306 UNITTEST::TestService::Stub owning_stub(channel, 1307 Service::STUB_OWNS_CHANNEL); 1308 EXPECT_FALSE(destroyed); 1309 } 1310 1311 EXPECT_TRUE(destroyed); 1312} 1313 1314TEST_F(GENERATED_SERVICE_TEST_NAME, CallMethod) { 1315 // Test that CallMethod() works. 1316 1317 // Call Foo() via CallMethod(). 1318 mock_service_.CallMethod(foo_, &mock_controller_, 1319 &foo_request_, &foo_response_, done_.get()); 1320 1321 ASSERT_TRUE(mock_service_.called_); 1322 1323 EXPECT_EQ("Foo" , mock_service_.method_ ); 1324 EXPECT_EQ(&mock_controller_, mock_service_.controller_); 1325 EXPECT_EQ(&foo_request_ , mock_service_.request_ ); 1326 EXPECT_EQ(&foo_response_ , mock_service_.response_ ); 1327 EXPECT_EQ(done_.get() , mock_service_.done_ ); 1328 1329 // Try again, but call Bar() instead. 1330 mock_service_.Reset(); 1331 mock_service_.CallMethod(bar_, &mock_controller_, 1332 &bar_request_, &bar_response_, done_.get()); 1333 1334 ASSERT_TRUE(mock_service_.called_); 1335 EXPECT_EQ("Bar", mock_service_.method_); 1336} 1337 1338TEST_F(GENERATED_SERVICE_TEST_NAME, CallMethodTypeFailure) { 1339 // Verify death if we call Foo() with Bar's message types. 1340 1341#ifdef PROTOBUF_HAS_DEATH_TEST // death tests do not work on Windows yet 1342 EXPECT_DEBUG_DEATH( 1343 mock_service_.CallMethod(foo_, &mock_controller_, 1344 &foo_request_, &bar_response_, done_.get()), 1345 "dynamic_cast"); 1346 1347 mock_service_.Reset(); 1348 EXPECT_DEBUG_DEATH( 1349 mock_service_.CallMethod(foo_, &mock_controller_, 1350 &bar_request_, &foo_response_, done_.get()), 1351 "dynamic_cast"); 1352#endif // PROTOBUF_HAS_DEATH_TEST 1353} 1354 1355TEST_F(GENERATED_SERVICE_TEST_NAME, GetPrototypes) { 1356 // Test Get{Request,Response}Prototype() methods. 1357 1358 EXPECT_EQ(&UNITTEST::FooRequest::default_instance(), 1359 &mock_service_.GetRequestPrototype(foo_)); 1360 EXPECT_EQ(&UNITTEST::BarRequest::default_instance(), 1361 &mock_service_.GetRequestPrototype(bar_)); 1362 1363 EXPECT_EQ(&UNITTEST::FooResponse::default_instance(), 1364 &mock_service_.GetResponsePrototype(foo_)); 1365 EXPECT_EQ(&UNITTEST::BarResponse::default_instance(), 1366 &mock_service_.GetResponsePrototype(bar_)); 1367} 1368 1369TEST_F(GENERATED_SERVICE_TEST_NAME, Stub) { 1370 // Test that the stub class works. 1371 1372 // Call Foo() via the stub. 1373 stub_.Foo(&mock_controller_, &foo_request_, &foo_response_, done_.get()); 1374 1375 ASSERT_TRUE(mock_channel_.called_); 1376 1377 EXPECT_EQ(foo_ , mock_channel_.method_ ); 1378 EXPECT_EQ(&mock_controller_, mock_channel_.controller_); 1379 EXPECT_EQ(&foo_request_ , mock_channel_.request_ ); 1380 EXPECT_EQ(&foo_response_ , mock_channel_.response_ ); 1381 EXPECT_EQ(done_.get() , mock_channel_.done_ ); 1382 1383 // Call Bar() via the stub. 1384 mock_channel_.Reset(); 1385 stub_.Bar(&mock_controller_, &bar_request_, &bar_response_, done_.get()); 1386 1387 ASSERT_TRUE(mock_channel_.called_); 1388 EXPECT_EQ(bar_, mock_channel_.method_); 1389} 1390 1391TEST_F(GENERATED_SERVICE_TEST_NAME, NotImplemented) { 1392 // Test that failing to implement a method of a service causes it to fail 1393 // with a "not implemented" error message. 1394 1395 // A service which doesn't implement any methods. 1396 class UnimplementedService : public UNITTEST::TestService { 1397 public: 1398 UnimplementedService() {} 1399 }; 1400 1401 UnimplementedService unimplemented_service; 1402 1403 // And a controller which expects to get a "not implemented" error. 1404 class ExpectUnimplementedController : public MockController { 1405 public: 1406 ExpectUnimplementedController() : called_(false) {} 1407 1408 void SetFailed(const std::string& reason) { 1409 EXPECT_FALSE(called_); 1410 called_ = true; 1411 EXPECT_EQ("Method Foo() not implemented.", reason); 1412 } 1413 1414 bool called_; 1415 }; 1416 1417 ExpectUnimplementedController controller; 1418 1419 // Call Foo. 1420 unimplemented_service.Foo(&controller, &foo_request_, &foo_response_, 1421 done_.get()); 1422 1423 EXPECT_TRUE(controller.called_); 1424} 1425 1426// =================================================================== 1427 1428class OneofTest : public testing::Test { 1429 protected: 1430 virtual void SetUp() { 1431 } 1432 1433 void ExpectEnumCasesWork(const UNITTEST::TestOneof2 &message) { 1434 switch (message.foo_case()) { 1435 case UNITTEST::TestOneof2::kFooInt: 1436 EXPECT_TRUE(message.has_foo_int()); 1437 break; 1438 case UNITTEST::TestOneof2::kFooString: 1439 EXPECT_TRUE(message.has_foo_string()); 1440 break; 1441 case UNITTEST::TestOneof2::kFooCord: 1442 EXPECT_TRUE(message.has_foo_cord()); 1443 break; 1444 case UNITTEST::TestOneof2::kFooStringPiece: 1445 EXPECT_TRUE(message.has_foo_string_piece()); 1446 break; 1447 case UNITTEST::TestOneof2::kFooBytes: 1448 EXPECT_TRUE(message.has_foo_bytes()); 1449 break; 1450 case UNITTEST::TestOneof2::kFooEnum: 1451 EXPECT_TRUE(message.has_foo_enum()); 1452 break; 1453 case UNITTEST::TestOneof2::kFooMessage: 1454 EXPECT_TRUE(message.has_foo_message()); 1455 break; 1456 case UNITTEST::TestOneof2::kFoogroup: 1457 EXPECT_TRUE(message.has_foogroup()); 1458 break; 1459 case UNITTEST::TestOneof2::kFooLazyMessage: 1460 EXPECT_TRUE(message.has_foo_lazy_message()); 1461 break; 1462 case UNITTEST::TestOneof2::FOO_NOT_SET: 1463 break; 1464 } 1465 } 1466}; 1467 1468TEST_F(OneofTest, SettingOneFieldClearsOthers) { 1469 UNITTEST::TestOneof2 message; 1470 1471 message.set_foo_int(123); 1472 EXPECT_TRUE(message.has_foo_int()); 1473 TestUtil::ExpectAtMostOneFieldSetInOneof(message); 1474 1475 message.set_foo_string("foo"); 1476 EXPECT_TRUE(message.has_foo_string()); 1477 TestUtil::ExpectAtMostOneFieldSetInOneof(message); 1478 1479 1480 message.set_foo_bytes("qux"); 1481 EXPECT_TRUE(message.has_foo_bytes()); 1482 TestUtil::ExpectAtMostOneFieldSetInOneof(message); 1483 1484 message.set_foo_enum(UNITTEST::TestOneof2::FOO); 1485 EXPECT_TRUE(message.has_foo_enum()); 1486 TestUtil::ExpectAtMostOneFieldSetInOneof(message); 1487 1488 message.mutable_foo_message()->set_qux_int(234); 1489 EXPECT_TRUE(message.has_foo_message()); 1490 TestUtil::ExpectAtMostOneFieldSetInOneof(message); 1491 1492 message.mutable_foogroup()->set_a(345); 1493 EXPECT_TRUE(message.has_foogroup()); 1494 TestUtil::ExpectAtMostOneFieldSetInOneof(message); 1495 1496 1497 // we repeat this because we didn't test if this properly clears other fields 1498 // at the beginning. 1499 message.set_foo_int(123); 1500 EXPECT_TRUE(message.has_foo_int()); 1501 TestUtil::ExpectAtMostOneFieldSetInOneof(message); 1502} 1503 1504TEST_F(OneofTest, EnumCases) { 1505 UNITTEST::TestOneof2 message; 1506 1507 message.set_foo_int(123); 1508 ExpectEnumCasesWork(message); 1509 message.set_foo_string("foo"); 1510 ExpectEnumCasesWork(message); 1511 message.set_foo_bytes("qux"); 1512 ExpectEnumCasesWork(message); 1513 message.set_foo_enum(UNITTEST::TestOneof2::FOO); 1514 ExpectEnumCasesWork(message); 1515 message.mutable_foo_message()->set_qux_int(234); 1516 ExpectEnumCasesWork(message); 1517 message.mutable_foogroup()->set_a(345); 1518 ExpectEnumCasesWork(message); 1519} 1520 1521TEST_F(OneofTest, PrimitiveType) { 1522 UNITTEST::TestOneof2 message; 1523 // Unset field returns default value 1524 EXPECT_EQ(message.foo_int(), 0); 1525 1526 message.set_foo_int(123); 1527 EXPECT_TRUE(message.has_foo_int()); 1528 EXPECT_EQ(message.foo_int(), 123); 1529 message.clear_foo_int(); 1530 EXPECT_FALSE(message.has_foo_int()); 1531} 1532 1533TEST_F(OneofTest, EnumType) { 1534 UNITTEST::TestOneof2 message; 1535 // Unset field returns default value 1536 EXPECT_EQ(message.foo_enum(), 1); 1537 1538 message.set_foo_enum(UNITTEST::TestOneof2::FOO); 1539 EXPECT_TRUE(message.has_foo_enum()); 1540 EXPECT_EQ(message.foo_enum(), UNITTEST::TestOneof2::FOO); 1541 message.clear_foo_enum(); 1542 EXPECT_FALSE(message.has_foo_enum()); 1543} 1544 1545TEST_F(OneofTest, SetString) { 1546 // Check that setting a string field in various ways works 1547 UNITTEST::TestOneof2 message; 1548 1549 // Unset field returns default value 1550 EXPECT_EQ(message.foo_string(), ""); 1551 1552 message.set_foo_string("foo"); 1553 EXPECT_TRUE(message.has_foo_string()); 1554 EXPECT_EQ(message.foo_string(), "foo"); 1555 message.clear_foo_string(); 1556 EXPECT_FALSE(message.has_foo_string()); 1557 1558 message.set_foo_string(std::string("bar")); 1559 EXPECT_TRUE(message.has_foo_string()); 1560 EXPECT_EQ(message.foo_string(), "bar"); 1561 message.clear_foo_string(); 1562 EXPECT_FALSE(message.has_foo_string()); 1563 1564 1565 message.set_foo_string("qux", 3); 1566 EXPECT_TRUE(message.has_foo_string()); 1567 EXPECT_EQ(message.foo_string(), "qux"); 1568 message.clear_foo_string(); 1569 EXPECT_FALSE(message.has_foo_string()); 1570 1571 message.mutable_foo_string()->assign("quux"); 1572 EXPECT_TRUE(message.has_foo_string()); 1573 EXPECT_EQ(message.foo_string(), "quux"); 1574 message.clear_foo_string(); 1575 EXPECT_FALSE(message.has_foo_string()); 1576 1577 message.set_foo_string("corge"); 1578 EXPECT_TRUE(message.has_foo_string()); 1579 EXPECT_EQ(message.foo_string(), "corge"); 1580 message.clear_foo_string(); 1581 EXPECT_FALSE(message.has_foo_string()); 1582} 1583 1584TEST_F(OneofTest, ReleaseString) { 1585 // Check that release_foo() starts out NULL, and gives us a value 1586 // that we can delete after it's been set. 1587 UNITTEST::TestOneof2 message; 1588 1589 EXPECT_EQ(NULL, message.release_foo_string()); 1590 EXPECT_FALSE(message.has_foo_string()); 1591 1592 message.set_foo_string("blah"); 1593 EXPECT_TRUE(message.has_foo_string()); 1594 std::unique_ptr<std::string> str(message.release_foo_string()); 1595 EXPECT_FALSE(message.has_foo_string()); 1596 ASSERT_TRUE(str != NULL); 1597 EXPECT_EQ("blah", *str); 1598 1599 EXPECT_EQ(NULL, message.release_foo_string()); 1600 EXPECT_FALSE(message.has_foo_string()); 1601} 1602 1603TEST_F(OneofTest, SetAllocatedString) { 1604 // Check that set_allocated_foo() works for strings. 1605 UNITTEST::TestOneof2 message; 1606 1607 EXPECT_FALSE(message.has_foo_string()); 1608 const std::string kHello("hello"); 1609 message.set_foo_string(kHello); 1610 EXPECT_TRUE(message.has_foo_string()); 1611 1612 message.set_allocated_foo_string(NULL); 1613 EXPECT_FALSE(message.has_foo_string()); 1614 EXPECT_EQ("", message.foo_string()); 1615 1616 message.set_allocated_foo_string(new std::string(kHello)); 1617 EXPECT_TRUE(message.has_foo_string()); 1618 EXPECT_EQ(kHello, message.foo_string()); 1619} 1620 1621TEST_F(OneofTest, ArenaSetAllocatedString) { 1622 // Check that set_allocated_foo() works for strings. 1623 Arena arena; 1624 UNITTEST::TestOneof2* message = 1625 Arena::CreateMessage<UNITTEST::TestOneof2>(&arena); 1626 1627 EXPECT_FALSE(message->has_foo_string()); 1628 const std::string kHello("hello"); 1629 message->set_foo_string(kHello); 1630 EXPECT_TRUE(message->has_foo_string()); 1631 1632 message->set_allocated_foo_string(NULL); 1633 EXPECT_FALSE(message->has_foo_string()); 1634 EXPECT_EQ("", message->foo_string()); 1635 1636 message->set_allocated_foo_string(new std::string(kHello)); 1637 EXPECT_TRUE(message->has_foo_string()); 1638 EXPECT_EQ(kHello, message->foo_string()); 1639} 1640 1641 1642TEST_F(OneofTest, SetMessage) { 1643 // Check that setting a message field works 1644 UNITTEST::TestOneof2 message; 1645 1646 // Unset field returns default instance 1647 EXPECT_EQ(&message.foo_message(), 1648 &UNITTEST::TestOneof2_NestedMessage::default_instance()); 1649 EXPECT_EQ(message.foo_message().qux_int(), 0); 1650 1651 message.mutable_foo_message()->set_qux_int(234); 1652 EXPECT_TRUE(message.has_foo_message()); 1653 EXPECT_EQ(message.foo_message().qux_int(), 234); 1654 message.clear_foo_message(); 1655 EXPECT_FALSE(message.has_foo_message()); 1656} 1657 1658TEST_F(OneofTest, ReleaseMessage) { 1659 // Check that release_foo() starts out NULL, and gives us a value 1660 // that we can delete after it's been set. 1661 UNITTEST::TestOneof2 message; 1662 1663 EXPECT_EQ(NULL, message.release_foo_message()); 1664 EXPECT_FALSE(message.has_foo_message()); 1665 1666 message.mutable_foo_message()->set_qux_int(1); 1667 EXPECT_TRUE(message.has_foo_message()); 1668 std::unique_ptr<UNITTEST::TestOneof2_NestedMessage> mes( 1669 message.release_foo_message()); 1670 EXPECT_FALSE(message.has_foo_message()); 1671 ASSERT_TRUE(mes != NULL); 1672 EXPECT_EQ(1, mes->qux_int()); 1673 1674 EXPECT_EQ(NULL, message.release_foo_message()); 1675 EXPECT_FALSE(message.has_foo_message()); 1676} 1677 1678TEST_F(OneofTest, SetAllocatedMessage) { 1679 // Check that set_allocated_foo() works for messages. 1680 UNITTEST::TestOneof2 message; 1681 1682 EXPECT_FALSE(message.has_foo_message()); 1683 1684 message.mutable_foo_message()->set_qux_int(1); 1685 EXPECT_TRUE(message.has_foo_message()); 1686 1687 message.set_allocated_foo_message(NULL); 1688 EXPECT_FALSE(message.has_foo_message()); 1689 EXPECT_EQ(&message.foo_message(), 1690 &UNITTEST::TestOneof2_NestedMessage::default_instance()); 1691 1692 message.mutable_foo_message()->set_qux_int(1); 1693 UNITTEST::TestOneof2_NestedMessage* mes = message.release_foo_message(); 1694 ASSERT_TRUE(mes != NULL); 1695 EXPECT_FALSE(message.has_foo_message()); 1696 1697 message.set_allocated_foo_message(mes); 1698 EXPECT_TRUE(message.has_foo_message()); 1699 EXPECT_EQ(1, message.foo_message().qux_int()); 1700} 1701 1702 1703TEST_F(OneofTest, Clear) { 1704 UNITTEST::TestOneof2 message; 1705 1706 message.set_foo_int(1); 1707 EXPECT_TRUE(message.has_foo_int()); 1708 message.clear_foo_int(); 1709 EXPECT_FALSE(message.has_foo_int()); 1710} 1711 1712TEST_F(OneofTest, Defaults) { 1713 UNITTEST::TestOneof2 message; 1714 1715 EXPECT_FALSE(message.has_foo_int()); 1716 EXPECT_EQ(message.foo_int(), 0); 1717 1718 EXPECT_FALSE(message.has_foo_string()); 1719 EXPECT_EQ(message.foo_string(), ""); 1720 1721 1722 EXPECT_FALSE(message.has_foo_bytes()); 1723 EXPECT_EQ(message.foo_bytes(), ""); 1724 1725 EXPECT_FALSE(message.has_foo_enum()); 1726 EXPECT_EQ(message.foo_enum(), 1); 1727 1728 EXPECT_FALSE(message.has_foo_message()); 1729 EXPECT_EQ(message.foo_message().qux_int(), 0); 1730 1731 EXPECT_FALSE(message.has_foogroup()); 1732 EXPECT_EQ(message.foogroup().a(), 0); 1733 1734 1735 EXPECT_FALSE(message.has_bar_int()); 1736 EXPECT_EQ(message.bar_int(), 5); 1737 1738 EXPECT_FALSE(message.has_bar_string()); 1739 EXPECT_EQ(message.bar_string(), "STRING"); 1740 1741 1742 EXPECT_FALSE(message.has_bar_bytes()); 1743 EXPECT_EQ(message.bar_bytes(), "BYTES"); 1744 1745 EXPECT_FALSE(message.has_bar_enum()); 1746 EXPECT_EQ(message.bar_enum(), 2); 1747} 1748 1749TEST_F(OneofTest, SwapWithEmpty) { 1750 UNITTEST::TestOneof2 message1, message2; 1751 message1.set_foo_string("FOO"); 1752 EXPECT_TRUE(message1.has_foo_string()); 1753 message1.Swap(&message2); 1754 EXPECT_FALSE(message1.has_foo_string()); 1755 EXPECT_TRUE(message2.has_foo_string()); 1756 EXPECT_EQ(message2.foo_string(), "FOO"); 1757} 1758 1759TEST_F(OneofTest, SwapWithSelf) { 1760 UNITTEST::TestOneof2 message; 1761 message.set_foo_string("FOO"); 1762 EXPECT_TRUE(message.has_foo_string()); 1763 message.Swap(&message); 1764 EXPECT_TRUE(message.has_foo_string()); 1765 EXPECT_EQ(message.foo_string(), "FOO"); 1766} 1767 1768TEST_F(OneofTest, SwapBothHasFields) { 1769 UNITTEST::TestOneof2 message1, message2; 1770 1771 message1.set_foo_string("FOO"); 1772 EXPECT_TRUE(message1.has_foo_string()); 1773 message2.mutable_foo_message()->set_qux_int(1); 1774 EXPECT_TRUE(message2.has_foo_message()); 1775 1776 message1.Swap(&message2); 1777 EXPECT_FALSE(message1.has_foo_string()); 1778 EXPECT_FALSE(message2.has_foo_message()); 1779 EXPECT_TRUE(message1.has_foo_message()); 1780 EXPECT_EQ(message1.foo_message().qux_int(), 1); 1781 EXPECT_TRUE(message2.has_foo_string()); 1782 EXPECT_EQ(message2.foo_string(), "FOO"); 1783} 1784 1785TEST_F(OneofTest, CopyConstructor) { 1786 UNITTEST::TestOneof2 message1; 1787 message1.set_foo_bytes("FOO"); 1788 1789 UNITTEST::TestOneof2 message2(message1); 1790 EXPECT_TRUE(message2.has_foo_bytes()); 1791 EXPECT_EQ(message2.foo_bytes(), "FOO"); 1792} 1793 1794TEST_F(OneofTest, CopyFrom) { 1795 UNITTEST::TestOneof2 message1, message2; 1796 message1.set_foo_enum(UNITTEST::TestOneof2::BAR); 1797 EXPECT_TRUE(message1.has_foo_enum()); 1798 1799 message2.CopyFrom(message1); 1800 EXPECT_TRUE(message2.has_foo_enum()); 1801 EXPECT_EQ(message2.foo_enum(), UNITTEST::TestOneof2::BAR); 1802 1803 // Copying from self should be a no-op. 1804 message2.CopyFrom(message2); 1805 EXPECT_TRUE(message2.has_foo_enum()); 1806 EXPECT_EQ(message2.foo_enum(), UNITTEST::TestOneof2::BAR); 1807} 1808 1809TEST_F(OneofTest, CopyAssignmentOperator) { 1810 UNITTEST::TestOneof2 message1; 1811 message1.mutable_foo_message()->set_qux_int(123); 1812 EXPECT_TRUE(message1.has_foo_message()); 1813 1814 UNITTEST::TestOneof2 message2; 1815 message2 = message1; 1816 EXPECT_EQ(message2.foo_message().qux_int(), 123); 1817 1818 // Make sure that self-assignment does something sane. 1819 message2 = *&message2; // Avoid -Wself-assign. 1820 EXPECT_EQ(message2.foo_message().qux_int(), 123); 1821} 1822 1823TEST_F(OneofTest, UpcastCopyFrom) { 1824 // Test the CopyFrom method that takes in the generic const Message& 1825 // parameter. 1826 UNITTEST::TestOneof2 message1, message2; 1827 message1.mutable_foogroup()->set_a(123); 1828 EXPECT_TRUE(message1.has_foogroup()); 1829 1830 const Message* source = implicit_cast<const Message*>(&message1); 1831 message2.CopyFrom(*source); 1832 1833 EXPECT_TRUE(message2.has_foogroup()); 1834 EXPECT_EQ(message2.foogroup().a(), 123); 1835} 1836 1837// Test the generated SerializeWithCachedSizesToArray(), 1838// This indirectly tests MergePartialFromCodedStream() 1839// We have to test each field type separately because we cannot set them at the 1840// same time 1841TEST_F(OneofTest, SerializationToArray) { 1842 // Primitive type 1843 { 1844 UNITTEST::TestOneof2 message1, message2; 1845std::string data; 1846message1.set_foo_int(123); 1847int size = message1.ByteSizeLong(); 1848data.resize(size); 1849uint8* start = reinterpret_cast<uint8*>(::google::protobuf::string_as_array(&data)); 1850uint8* end = message1.SerializeWithCachedSizesToArray(start); 1851EXPECT_EQ(size, end - start); 1852EXPECT_TRUE(message2.ParseFromString(data)); 1853EXPECT_EQ(message2.foo_int(), 123); 1854 } 1855 1856 // String 1857 { 1858 UNITTEST::TestOneof2 message1, message2; 1859 std::string data; 1860 message1.set_foo_string("foo"); 1861 int size = message1.ByteSizeLong(); 1862 data.resize(size); 1863 uint8* start = reinterpret_cast<uint8*>(::google::protobuf::string_as_array(&data)); 1864 uint8* end = message1.SerializeWithCachedSizesToArray(start); 1865 EXPECT_EQ(size, end - start); 1866 EXPECT_TRUE(message2.ParseFromString(data)); 1867 EXPECT_EQ(message2.foo_string(), "foo"); 1868 } 1869 1870 1871 // Bytes 1872 { 1873 UNITTEST::TestOneof2 message1, message2; 1874 std::string data; 1875 message1.set_foo_bytes("qux"); 1876 int size = message1.ByteSizeLong(); 1877 data.resize(size); 1878 uint8* start = reinterpret_cast<uint8*>(::google::protobuf::string_as_array(&data)); 1879 uint8* end = message1.SerializeWithCachedSizesToArray(start); 1880 EXPECT_EQ(size, end - start); 1881 EXPECT_TRUE(message2.ParseFromString(data)); 1882 EXPECT_EQ(message2.foo_bytes(), "qux"); 1883 } 1884 1885 // Enum 1886 { 1887 UNITTEST::TestOneof2 message1, message2; 1888 std::string data; 1889 message1.set_foo_enum(UNITTEST::TestOneof2::FOO); 1890 int size = message1.ByteSizeLong(); 1891 data.resize(size); 1892 uint8* start = reinterpret_cast<uint8*>(::google::protobuf::string_as_array(&data)); 1893 uint8* end = message1.SerializeWithCachedSizesToArray(start); 1894 EXPECT_EQ(size, end - start); 1895 EXPECT_TRUE(message2.ParseFromString(data)); 1896 EXPECT_EQ(message2.foo_enum(), UNITTEST::TestOneof2::FOO); 1897 } 1898 1899 // Message 1900 { 1901 UNITTEST::TestOneof2 message1, message2; 1902 std::string data; 1903 message1.mutable_foo_message()->set_qux_int(234); 1904 int size = message1.ByteSizeLong(); 1905 data.resize(size); 1906 uint8* start = reinterpret_cast<uint8*>(::google::protobuf::string_as_array(&data)); 1907 uint8* end = message1.SerializeWithCachedSizesToArray(start); 1908 EXPECT_EQ(size, end - start); 1909 EXPECT_TRUE(message2.ParseFromString(data)); 1910 EXPECT_EQ(message2.foo_message().qux_int(), 234); 1911 } 1912 1913 // Group 1914 { 1915 UNITTEST::TestOneof2 message1, message2; 1916 std::string data; 1917 message1.mutable_foogroup()->set_a(345); 1918 int size = message1.ByteSizeLong(); 1919 data.resize(size); 1920 uint8* start = reinterpret_cast<uint8*>(::google::protobuf::string_as_array(&data)); 1921 uint8* end = message1.SerializeWithCachedSizesToArray(start); 1922 EXPECT_EQ(size, end - start); 1923 EXPECT_TRUE(message2.ParseFromString(data)); 1924 EXPECT_EQ(message2.foogroup().a(), 345); 1925 } 1926 1927} 1928 1929// Test the generated SerializeWithCachedSizes() by forcing the buffer to write 1930// one byte at a time. 1931// This indirectly tests MergePartialFromCodedStream() 1932// We have to test each field type separately because we cannot set them at the 1933// same time 1934TEST_F(OneofTest, SerializationToStream) { 1935 // Primitive type 1936 { 1937 UNITTEST::TestOneof2 message1, message2; 1938std::string data; 1939message1.set_foo_int(123); 1940int size = message1.ByteSizeLong(); 1941data.resize(size); 1942 1943{ 1944 // Allow the output stream to buffer only one byte at a time. 1945 io::ArrayOutputStream array_stream(::google::protobuf::string_as_array(&data), size, 1); 1946 io::CodedOutputStream output_stream(&array_stream); 1947 message1.SerializeWithCachedSizes(&output_stream); 1948 EXPECT_FALSE(output_stream.HadError()); 1949 EXPECT_EQ(size, output_stream.ByteCount()); 1950} 1951 1952 EXPECT_TRUE(message2.ParseFromString(data)); 1953 EXPECT_EQ(message2.foo_int(), 123); 1954 } 1955 1956 // String 1957 { 1958 UNITTEST::TestOneof2 message1, message2; 1959 std::string data; 1960 message1.set_foo_string("foo"); 1961 int size = message1.ByteSizeLong(); 1962 data.resize(size); 1963 1964 { 1965 // Allow the output stream to buffer only one byte at a time. 1966 io::ArrayOutputStream array_stream(::google::protobuf::string_as_array(&data), size, 1); 1967 io::CodedOutputStream output_stream(&array_stream); 1968 message1.SerializeWithCachedSizes(&output_stream); 1969 EXPECT_FALSE(output_stream.HadError()); 1970 EXPECT_EQ(size, output_stream.ByteCount()); 1971 } 1972 1973 EXPECT_TRUE(message2.ParseFromString(data)); 1974 EXPECT_EQ(message2.foo_string(), "foo"); 1975 } 1976 1977 1978 // Bytes 1979 { 1980 UNITTEST::TestOneof2 message1, message2; 1981 std::string data; 1982 message1.set_foo_bytes("qux"); 1983 int size = message1.ByteSizeLong(); 1984 data.resize(size); 1985 1986 { 1987 // Allow the output stream to buffer only one byte at a time. 1988 io::ArrayOutputStream array_stream(::google::protobuf::string_as_array(&data), size, 1); 1989 io::CodedOutputStream output_stream(&array_stream); 1990 message1.SerializeWithCachedSizes(&output_stream); 1991 EXPECT_FALSE(output_stream.HadError()); 1992 EXPECT_EQ(size, output_stream.ByteCount()); 1993 } 1994 1995 EXPECT_TRUE(message2.ParseFromString(data)); 1996 EXPECT_EQ(message2.foo_bytes(), "qux"); 1997 } 1998 1999 // Enum 2000 { 2001 UNITTEST::TestOneof2 message1, message2; 2002 std::string data; 2003 message1.set_foo_enum(UNITTEST::TestOneof2::FOO); 2004 int size = message1.ByteSizeLong(); 2005 data.resize(size); 2006 2007 { 2008 // Allow the output stream to buffer only one byte at a time. 2009 io::ArrayOutputStream array_stream(::google::protobuf::string_as_array(&data), size, 1); 2010 io::CodedOutputStream output_stream(&array_stream); 2011 message1.SerializeWithCachedSizes(&output_stream); 2012 EXPECT_FALSE(output_stream.HadError()); 2013 EXPECT_EQ(size, output_stream.ByteCount()); 2014 } 2015 2016 EXPECT_TRUE(message2.ParseFromString(data)); 2017 EXPECT_EQ(message2.foo_enum(), UNITTEST::TestOneof2::FOO); 2018 } 2019 2020 // Message 2021 { 2022 UNITTEST::TestOneof2 message1, message2; 2023 std::string data; 2024 message1.mutable_foo_message()->set_qux_int(234); 2025 int size = message1.ByteSizeLong(); 2026 data.resize(size); 2027 2028 { 2029 // Allow the output stream to buffer only one byte at a time. 2030 io::ArrayOutputStream array_stream(::google::protobuf::string_as_array(&data), size, 1); 2031 io::CodedOutputStream output_stream(&array_stream); 2032 message1.SerializeWithCachedSizes(&output_stream); 2033 EXPECT_FALSE(output_stream.HadError()); 2034 EXPECT_EQ(size, output_stream.ByteCount()); 2035 } 2036 2037 EXPECT_TRUE(message2.ParseFromString(data)); 2038 EXPECT_EQ(message2.foo_message().qux_int(), 234); 2039 } 2040 2041 // Group 2042 { 2043 UNITTEST::TestOneof2 message1, message2; 2044 std::string data; 2045 message1.mutable_foogroup()->set_a(345); 2046 int size = message1.ByteSizeLong(); 2047 data.resize(size); 2048 2049 { 2050 // Allow the output stream to buffer only one byte at a time. 2051 io::ArrayOutputStream array_stream(::google::protobuf::string_as_array(&data), size, 1); 2052 io::CodedOutputStream output_stream(&array_stream); 2053 message1.SerializeWithCachedSizes(&output_stream); 2054 EXPECT_FALSE(output_stream.HadError()); 2055 EXPECT_EQ(size, output_stream.ByteCount()); 2056 } 2057 2058 EXPECT_TRUE(message2.ParseFromString(data)); 2059 EXPECT_EQ(message2.foogroup().a(), 345); 2060 } 2061 2062} 2063 2064TEST_F(OneofTest, MergeFrom) { 2065 UNITTEST::TestOneof2 message1, message2; 2066 2067 message1.set_foo_int(123); 2068 message2.MergeFrom(message1); 2069 TestUtil::ExpectAtMostOneFieldSetInOneof(message2); 2070 EXPECT_TRUE(message2.has_foo_int()); 2071 EXPECT_EQ(message2.foo_int(), 123); 2072 2073 message1.set_foo_string("foo"); 2074 message2.MergeFrom(message1); 2075 TestUtil::ExpectAtMostOneFieldSetInOneof(message2); 2076 EXPECT_TRUE(message2.has_foo_string()); 2077 EXPECT_EQ(message2.foo_string(), "foo"); 2078 2079 2080 message1.set_foo_bytes("qux"); 2081 message2.MergeFrom(message1); 2082 TestUtil::ExpectAtMostOneFieldSetInOneof(message2); 2083 EXPECT_TRUE(message2.has_foo_bytes()); 2084 EXPECT_EQ(message2.foo_bytes(), "qux"); 2085 2086 message1.set_foo_enum(UNITTEST::TestOneof2::FOO); 2087 message2.MergeFrom(message1); 2088 TestUtil::ExpectAtMostOneFieldSetInOneof(message2); 2089 EXPECT_TRUE(message2.has_foo_enum()); 2090 EXPECT_EQ(message2.foo_enum(), UNITTEST::TestOneof2::FOO); 2091 2092 message1.mutable_foo_message()->set_qux_int(234); 2093 message2.MergeFrom(message1); 2094 TestUtil::ExpectAtMostOneFieldSetInOneof(message2); 2095 EXPECT_TRUE(message2.has_foo_message()); 2096 EXPECT_EQ(message2.foo_message().qux_int(), 234); 2097 2098 message1.mutable_foogroup()->set_a(345); 2099 message2.MergeFrom(message1); 2100 TestUtil::ExpectAtMostOneFieldSetInOneof(message2); 2101 EXPECT_TRUE(message2.has_foogroup()); 2102 EXPECT_EQ(message2.foogroup().a(), 345); 2103 2104} 2105 2106TEST(HELPERS_TEST_NAME, TestSCC) { 2107 UNITTEST::TestMutualRecursionA a; 2108 MessageSCCAnalyzer scc_analyzer((Options())); 2109 const SCC* scc = scc_analyzer.GetSCC(a.GetDescriptor()); 2110 std::vector<std::string> names; 2111 names.reserve(scc->descriptors.size()); 2112 for (int i = 0; i < scc->descriptors.size(); i++) { 2113 names.push_back(scc->descriptors[i]->full_name()); 2114 } 2115 std::string package = a.GetDescriptor()->file()->package(); 2116 ASSERT_EQ(names.size(), 4); 2117 std::sort(names.begin(), names.end()); 2118 EXPECT_EQ(names[0], package + ".TestMutualRecursionA"); 2119 EXPECT_EQ(names[1], package + ".TestMutualRecursionA.SubGroup"); 2120 EXPECT_EQ(names[2], package + ".TestMutualRecursionA.SubMessage"); 2121 EXPECT_EQ(names[3], package + ".TestMutualRecursionB"); 2122 2123 MessageAnalysis result = scc_analyzer.GetSCCAnalysis(scc); 2124 EXPECT_EQ(result.is_recursive, true); 2125 EXPECT_EQ(result.contains_required, false); 2126 EXPECT_EQ(result.contains_cord, true); // TestAllTypes 2127 EXPECT_EQ(result.contains_extension, false); // TestAllTypes 2128} 2129 2130TEST(HELPERS_TEST_NAME, TestSCCAnalysis) { 2131 { 2132 UNITTEST::TestRecursiveMessage msg; 2133 MessageSCCAnalyzer scc_analyzer((Options())); 2134 const SCC* scc = scc_analyzer.GetSCC(msg.GetDescriptor()); 2135 MessageAnalysis result = scc_analyzer.GetSCCAnalysis(scc); 2136 EXPECT_EQ(result.is_recursive, true); 2137 EXPECT_EQ(result.contains_required, false); 2138 EXPECT_EQ(result.contains_cord, false); 2139 EXPECT_EQ(result.contains_extension, false); 2140 } 2141 { 2142 UNITTEST::TestAllExtensions msg; 2143 MessageSCCAnalyzer scc_analyzer((Options())); 2144 const SCC* scc = scc_analyzer.GetSCC(msg.GetDescriptor()); 2145 MessageAnalysis result = scc_analyzer.GetSCCAnalysis(scc); 2146 EXPECT_EQ(result.is_recursive, false); 2147 EXPECT_EQ(result.contains_required, false); 2148 EXPECT_EQ(result.contains_cord, false); 2149 EXPECT_EQ(result.contains_extension, true); 2150 } 2151 { 2152 UNITTEST::TestRequired msg; 2153 MessageSCCAnalyzer scc_analyzer((Options())); 2154 const SCC* scc = scc_analyzer.GetSCC(msg.GetDescriptor()); 2155 MessageAnalysis result = scc_analyzer.GetSCCAnalysis(scc); 2156 EXPECT_EQ(result.is_recursive, false); 2157 EXPECT_EQ(result.contains_required, true); 2158 EXPECT_EQ(result.contains_cord, false); 2159 EXPECT_EQ(result.contains_extension, false); 2160 } 2161} 2162 2163} // namespace cpp_unittest 2164} // namespace cpp 2165} // namespace compiler 2166 2167namespace no_generic_services_test { 2168 // Verify that no class called "TestService" was defined in 2169 // unittest_no_generic_services.pb.h by defining a different type by the same 2170 // name. If such a service was generated, this will not compile. 2171 struct TestService { 2172 int i; 2173 }; 2174} 2175 2176namespace compiler { 2177namespace cpp { 2178namespace cpp_unittest { 2179 2180TEST_F(GENERATED_SERVICE_TEST_NAME, NoGenericServices) { 2181 // Verify that non-services in unittest_no_generic_services.proto were 2182 // generated. 2183 ::protobuf_unittest::no_generic_services_test::TestMessage message; 2184 message.set_a(1); 2185 message.SetExtension( 2186 ::protobuf_unittest::no_generic_services_test::test_extension, 123); 2187 ::protobuf_unittest::no_generic_services_test::TestEnum e = 2188 ::protobuf_unittest::no_generic_services_test::FOO; 2189 EXPECT_EQ(e, 1); 2190 2191 // Verify that a ServiceDescriptor is generated for the service even if the 2192 // class itself is not. 2193 const FileDescriptor* file = 2194 ::google::protobuf::unittest::no_generic_services_test::TestMessage::descriptor() 2195 ->file(); 2196 2197 ASSERT_EQ(1, file->service_count()); 2198 EXPECT_EQ("TestService", file->service(0)->name()); 2199 ASSERT_EQ(1, file->service(0)->method_count()); 2200 EXPECT_EQ("Foo", file->service(0)->method(0)->name()); 2201} 2202 2203#endif // !PROTOBUF_TEST_NO_DESCRIPTORS 2204 2205// =================================================================== 2206 2207// This test must run last. It verifies that descriptors were or were not 2208// initialized depending on whether PROTOBUF_TEST_NO_DESCRIPTORS was defined. 2209// When this is defined, we skip all tests which are expected to trigger 2210// descriptor initialization. This verifies that everything else still works 2211// if descriptors are not initialized. 2212TEST(DESCRIPTOR_INIT_TEST_NAME, Initialized) { 2213#ifdef PROTOBUF_TEST_NO_DESCRIPTORS 2214 bool should_have_descriptors = false; 2215#else 2216 bool should_have_descriptors = true; 2217#endif 2218 2219 EXPECT_EQ(should_have_descriptors, 2220 DescriptorPool::generated_pool()->InternalIsFileLoaded( 2221 TestUtil::MaybeTranslatePath(UNITTEST_PROTO_PATH))); 2222} 2223 2224} // namespace cpp_unittest 2225 2226} // namespace cpp 2227} // namespace compiler 2228} // namespace protobuf 2229} // namespace google 2230 2231#include <google/protobuf/port_undef.inc> 2232