any_test.cc
Go to the documentation of this file.
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 #include <google/protobuf/any_test.pb.h>
32 #include <google/protobuf/unittest.pb.h>
33 #include <gtest/gtest.h>
34 
35 
36 
37 namespace google {
38 namespace protobuf {
39 namespace {
40 
41 TEST(AnyTest, TestPackAndUnpack) {
42  protobuf_unittest::TestAny submessage;
43  submessage.set_int32_value(12345);
44  protobuf_unittest::TestAny message;
45  message.mutable_any_value()->PackFrom(submessage);
46 
47  std::string data = message.SerializeAsString();
48 
49  ASSERT_TRUE(message.ParseFromString(data));
50  EXPECT_TRUE(message.has_any_value());
51  submessage.Clear();
52  ASSERT_TRUE(message.any_value().UnpackTo(&submessage));
53  EXPECT_EQ(12345, submessage.int32_value());
54 }
55 
56 TEST(AnyTest, TestUnpackWithTypeMismatch) {
57  protobuf_unittest::TestAny payload;
58  payload.set_int32_value(13);
60  any.PackFrom(payload);
61 
62  // Attempt to unpack into the wrong type.
63  protobuf_unittest::TestAllTypes dest;
64  EXPECT_FALSE(any.UnpackTo(&dest));
65 }
66 
67 TEST(AnyTest, TestPackAndUnpackAny) {
68  // We can pack a Any message inside another Any message.
69  protobuf_unittest::TestAny submessage;
70  submessage.set_int32_value(12345);
72  any.PackFrom(submessage);
73  protobuf_unittest::TestAny message;
74  message.mutable_any_value()->PackFrom(any);
75 
76  std::string data = message.SerializeAsString();
77 
78  ASSERT_TRUE(message.ParseFromString(data));
79  EXPECT_TRUE(message.has_any_value());
80  any.Clear();
81  submessage.Clear();
82  ASSERT_TRUE(message.any_value().UnpackTo(&any));
83  ASSERT_TRUE(any.UnpackTo(&submessage));
84  EXPECT_EQ(12345, submessage.int32_value());
85 }
86 
87 TEST(AnyTest, TestPackWithCustomTypeUrl) {
88  protobuf_unittest::TestAny submessage;
89  submessage.set_int32_value(12345);
91  // Pack with a custom type URL prefix.
92  any.PackFrom(submessage, "type.myservice.com");
93  EXPECT_EQ("type.myservice.com/protobuf_unittest.TestAny", any.type_url());
94  // Pack with a custom type URL prefix ending with '/'.
95  any.PackFrom(submessage, "type.myservice.com/");
96  EXPECT_EQ("type.myservice.com/protobuf_unittest.TestAny", any.type_url());
97  // Pack with an empty type URL prefix.
98  any.PackFrom(submessage, "");
99  EXPECT_EQ("/protobuf_unittest.TestAny", any.type_url());
100 
101  // Test unpacking the type.
102  submessage.Clear();
103  EXPECT_TRUE(any.UnpackTo(&submessage));
104  EXPECT_EQ(12345, submessage.int32_value());
105 }
106 
107 TEST(AnyTest, TestIs) {
108  protobuf_unittest::TestAny submessage;
109  submessage.set_int32_value(12345);
111  any.PackFrom(submessage);
112  ASSERT_TRUE(any.ParseFromString(any.SerializeAsString()));
113  EXPECT_TRUE(any.Is<protobuf_unittest::TestAny>());
115 
116  protobuf_unittest::TestAny message;
117  message.mutable_any_value()->PackFrom(any);
118  ASSERT_TRUE(message.ParseFromString(message.SerializeAsString()));
119  EXPECT_FALSE(message.any_value().Is<protobuf_unittest::TestAny>());
120  EXPECT_TRUE(message.any_value().Is<google::protobuf::Any>());
121 
122  any.set_type_url("/protobuf_unittest.TestAny");
123  EXPECT_TRUE(any.Is<protobuf_unittest::TestAny>());
124  // The type URL must contain at least one "/".
125  any.set_type_url("protobuf_unittest.TestAny");
126  EXPECT_FALSE(any.Is<protobuf_unittest::TestAny>());
127  // The type name after the slash must be fully qualified.
128  any.set_type_url("/TestAny");
129  EXPECT_FALSE(any.Is<protobuf_unittest::TestAny>());
130 }
131 
132 TEST(AnyTest, MoveConstructor) {
133  protobuf_unittest::TestAny payload;
134  payload.set_int32_value(12345);
135 
137  src.PackFrom(payload);
138 
139  const char* type_url = src.type_url().data();
140 
141  google::protobuf::Any dst(std::move(src));
142  EXPECT_EQ(type_url, dst.type_url().data());
143  payload.Clear();
144  ASSERT_TRUE(dst.UnpackTo(&payload));
145  EXPECT_EQ(12345, payload.int32_value());
146 }
147 
148 TEST(AnyTest, MoveAssignment) {
149  protobuf_unittest::TestAny payload;
150  payload.set_int32_value(12345);
151 
153  src.PackFrom(payload);
154 
155  const char* type_url = src.type_url().data();
156 
158  dst = std::move(src);
159  EXPECT_EQ(type_url, dst.type_url().data());
160  payload.Clear();
161  ASSERT_TRUE(dst.UnpackTo(&payload));
162  EXPECT_EQ(12345, payload.int32_value());
163 }
164 
165 
166 } // namespace
167 } // namespace protobuf
168 } // namespace google
src
GLenum src
Definition: glcorearb.h:3364
gtest.h
EXPECT_EQ
#define EXPECT_EQ(val1, val2)
Definition: glog/src/googletest.h:155
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
google::protobuf::TEST
TEST(ArenaTest, ArenaConstructable)
Definition: arena_unittest.cc:156
EXPECT_TRUE
#define EXPECT_TRUE(cond)
Definition: glog/src/googletest.h:137
ASSERT_TRUE
#define ASSERT_TRUE(condition)
Definition: gtest.h:1995
Any
struct Any Any
Definition: php/ext/google/protobuf/protobuf.h:624
benchmarks.python.py_benchmark.dest
dest
Definition: py_benchmark.py:13
EXPECT_FALSE
#define EXPECT_FALSE(cond)
Definition: glog/src/googletest.h:145
dst
GLenum GLenum dst
Definition: glcorearb.h:3364
Any::PackFrom
void PackFrom(const ::PROTOBUF_NAMESPACE_ID::Message &message)
Definition: any.pb.cc:89
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: glcorearb.h:2879
type_url
string * type_url
Definition: conformance_cpp.cc:98
google
Definition: data_proto2_to_proto3_util.h:11
message
GLenum GLuint GLenum GLsizei const GLchar * message
Definition: glcorearb.h:2695


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:47