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