proto3_lite_unittest.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. #include <string>
  31. #include <memory>
  32. #include <vector>
  33. #include <google/protobuf/unittest_proto3_lite.pb.h>
  34. #include <google/protobuf/arena.h>
  35. #include <google/protobuf/testing/googletest.h>
  36. #include <gtest/gtest.h>
  37. namespace google {
  38. using proto3_lite_unittest::TestAllTypes;
  39. namespace protobuf {
  40. namespace {
  41. // We selectively set/check a few representative fields rather than all fields
  42. // as this test is only expected to cover the basics of lite support.
  43. void SetAllFields(TestAllTypes* m) {
  44. m->set_optional_int32(100);
  45. m->set_optional_string("asdf");
  46. m->set_optional_bytes("jkl;");
  47. m->mutable_optional_nested_message()->set_bb(42);
  48. m->mutable_optional_foreign_message()->set_c(43);
  49. m->set_optional_nested_enum(
  50. proto3_lite_unittest::TestAllTypes_NestedEnum_BAZ);
  51. m->set_optional_foreign_enum(
  52. proto3_lite_unittest::FOREIGN_BAZ);
  53. m->mutable_optional_lazy_message()->set_bb(45);
  54. m->add_repeated_int32(100);
  55. m->add_repeated_string("asdf");
  56. m->add_repeated_bytes("jkl;");
  57. m->add_repeated_nested_message()->set_bb(46);
  58. m->add_repeated_foreign_message()->set_c(47);
  59. m->add_repeated_nested_enum(
  60. proto3_lite_unittest::TestAllTypes_NestedEnum_BAZ);
  61. m->add_repeated_foreign_enum(
  62. proto3_lite_unittest::FOREIGN_BAZ);
  63. m->add_repeated_lazy_message()->set_bb(49);
  64. m->set_oneof_uint32(1);
  65. m->mutable_oneof_nested_message()->set_bb(50);
  66. m->set_oneof_string("test"); // only this one remains set
  67. }
  68. void ExpectAllFieldsSet(const TestAllTypes& m) {
  69. EXPECT_EQ(100, m.optional_int32());
  70. EXPECT_EQ("asdf", m.optional_string());
  71. EXPECT_EQ("jkl;", m.optional_bytes());
  72. EXPECT_EQ(true, m.has_optional_nested_message());
  73. EXPECT_EQ(42, m.optional_nested_message().bb());
  74. EXPECT_EQ(true, m.has_optional_foreign_message());
  75. EXPECT_EQ(43, m.optional_foreign_message().c());
  76. EXPECT_EQ(proto3_lite_unittest::TestAllTypes_NestedEnum_BAZ,
  77. m.optional_nested_enum());
  78. EXPECT_EQ(proto3_lite_unittest::FOREIGN_BAZ,
  79. m.optional_foreign_enum());
  80. EXPECT_EQ(true, m.has_optional_lazy_message());
  81. EXPECT_EQ(45, m.optional_lazy_message().bb());
  82. EXPECT_EQ(1, m.repeated_int32_size());
  83. EXPECT_EQ(100, m.repeated_int32(0));
  84. EXPECT_EQ(1, m.repeated_string_size());
  85. EXPECT_EQ("asdf", m.repeated_string(0));
  86. EXPECT_EQ(1, m.repeated_bytes_size());
  87. EXPECT_EQ("jkl;", m.repeated_bytes(0));
  88. EXPECT_EQ(1, m.repeated_nested_message_size());
  89. EXPECT_EQ(46, m.repeated_nested_message(0).bb());
  90. EXPECT_EQ(1, m.repeated_foreign_message_size());
  91. EXPECT_EQ(47, m.repeated_foreign_message(0).c());
  92. EXPECT_EQ(1, m.repeated_nested_enum_size());
  93. EXPECT_EQ(proto3_lite_unittest::TestAllTypes_NestedEnum_BAZ,
  94. m.repeated_nested_enum(0));
  95. EXPECT_EQ(1, m.repeated_foreign_enum_size());
  96. EXPECT_EQ(proto3_lite_unittest::FOREIGN_BAZ,
  97. m.repeated_foreign_enum(0));
  98. EXPECT_EQ(1, m.repeated_lazy_message_size());
  99. EXPECT_EQ(49, m.repeated_lazy_message(0).bb());
  100. EXPECT_EQ(proto3_lite_unittest::TestAllTypes::kOneofString,
  101. m.oneof_field_case());
  102. EXPECT_EQ("test", m.oneof_string());
  103. }
  104. // In this file we only test some basic functionalities of in proto3 and expect
  105. // the rest is fully tested in proto2 unittests because proto3 shares most code
  106. // with proto2.
  107. TEST(Proto3LiteTest, Parsing) {
  108. TestAllTypes original;
  109. SetAllFields(&original);
  110. TestAllTypes msg;
  111. msg.ParseFromString(original.SerializeAsString());
  112. ExpectAllFieldsSet(msg);
  113. }
  114. TEST(Proto3LiteTest, Swap) {
  115. // Test Swap().
  116. TestAllTypes msg1;
  117. TestAllTypes msg2;
  118. msg1.set_optional_string("123");
  119. msg2.set_optional_string("3456");
  120. msg1.Swap(&msg2);
  121. EXPECT_EQ("3456", msg1.optional_string());
  122. EXPECT_EQ("123", msg2.optional_string());
  123. EXPECT_EQ(msg1.ByteSize(), msg2.ByteSize() + 1);
  124. }
  125. } // namespace
  126. } // namespace protobuf
  127. } // namespace google