cpp_unittest.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 the code generator, we actually use it to generate code for
  35. // google/protobuf/unittest.proto, then test that. This means that we
  36. // are actually testing the parser and other parts of the system at the same
  37. // time, and that problems in the generator may show up as compile-time errors
  38. // rather than unittest failures, which may be surprising. However, testing
  39. // the output of the C++ generator directly would be very hard. We can't very
  40. // well just check it against golden files since those files would have to be
  41. // updated for any small change; such a test would be very brittle and probably
  42. // not very helpful. What we really want to test is that the code compiles
  43. // correctly and produces the interfaces we expect, which is why this test
  44. // is written this way.
  45. #include <google/protobuf/compiler/cpp/cpp_unittest.h>
  46. #include <google/protobuf/unittest.pb.h>
  47. #include <google/protobuf/unittest_optimize_for.pb.h>
  48. #include <google/protobuf/unittest_embed_optimize_for.pb.h>
  49. #include <google/protobuf/test_util.h>
  50. #define MESSAGE_TEST_NAME MessageTest
  51. #define GENERATED_DESCRIPTOR_TEST_NAME GeneratedDescriptorTest
  52. #define GENERATED_MESSAGE_TEST_NAME GeneratedMessageTest
  53. #define GENERATED_ENUM_TEST_NAME GeneratedEnumTest
  54. #define GENERATED_SERVICE_TEST_NAME GeneratedServiceTest
  55. #define HELPERS_TEST_NAME HelpersTest
  56. #define DESCRIPTOR_INIT_TEST_NAME DescriptorInitializationTest
  57. #define UNITTEST_PROTO_PATH "google/protobuf/unittest.proto"
  58. #define UNITTEST ::protobuf_unittest
  59. #define UNITTEST_IMPORT ::protobuf_unittest_import
  60. // Must include after the above macros.
  61. #include <google/protobuf/compiler/cpp/cpp_unittest.inc>
  62. namespace google {
  63. namespace protobuf {
  64. namespace compiler {
  65. namespace cpp {
  66. // Can't use an anonymous namespace here due to brokenness of Tru64 compiler.
  67. namespace cpp_unittest {
  68. namespace protobuf_unittest = ::protobuf_unittest;
  69. TEST(GENERATED_MESSAGE_TEST_NAME, TestConflictingSymbolNames) {
  70. // test_bad_identifiers.proto successfully compiled, then it works. The
  71. // following is just a token usage to insure that the code is, in fact,
  72. // being compiled and linked.
  73. protobuf_unittest::TestConflictingSymbolNames message;
  74. message.set_uint32(1);
  75. EXPECT_EQ(3, message.ByteSizeLong());
  76. message.set_friend_(5);
  77. EXPECT_EQ(5, message.friend_());
  78. message.set_class_(6);
  79. EXPECT_EQ(6, message.class_());
  80. // Instantiate extension template functions to test conflicting template
  81. // parameter names.
  82. typedef protobuf_unittest::TestConflictingSymbolNamesExtension ExtensionMessage;
  83. message.AddExtension(ExtensionMessage::repeated_int32_ext, 123);
  84. EXPECT_EQ(123,
  85. message.GetExtension(ExtensionMessage::repeated_int32_ext, 0));
  86. }
  87. TEST(GENERATED_MESSAGE_TEST_NAME, TestConflictingEnumNames) {
  88. protobuf_unittest::TestConflictingEnumNames message;
  89. message.set_conflicting_enum(protobuf_unittest::TestConflictingEnumNames_NestedConflictingEnum_and_);
  90. EXPECT_EQ(1, message.conflicting_enum());
  91. message.set_conflicting_enum(protobuf_unittest::TestConflictingEnumNames_NestedConflictingEnum_XOR);
  92. EXPECT_EQ(5, message.conflicting_enum());
  93. protobuf_unittest::ConflictingEnum conflicting_enum;
  94. conflicting_enum = protobuf_unittest::NOT_EQ;
  95. EXPECT_EQ(1, conflicting_enum);
  96. conflicting_enum = protobuf_unittest::return_;
  97. EXPECT_EQ(3, conflicting_enum);
  98. }
  99. } // namespace cpp_unittest
  100. } // namespace cpp
  101. } // namespace compiler
  102. } // namespace protobuf
  103. } // namespace google