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