|
|
|
@ -165,6 +165,22 @@ TEST(GeneratedMessageReflectionTest, GetStringReference) { |
|
|
|
|
"a reference to the underlying string."; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
TEST(GeneratedMessageReflectionTest, GetStringReferenceCopy) { |
|
|
|
|
// Test that GetStringReference() returns the scratch string when the
|
|
|
|
|
// underlying representation is not a normal string.
|
|
|
|
|
unittest::TestCord cord_message; |
|
|
|
|
cord_message.set_optional_bytes_cord("bytes_cord"); |
|
|
|
|
|
|
|
|
|
const Reflection* cord_reflection = cord_message.GetReflection(); |
|
|
|
|
const Descriptor* descriptor = unittest::TestCord::descriptor(); |
|
|
|
|
std::string cord_scratch; |
|
|
|
|
EXPECT_EQ( |
|
|
|
|
&cord_scratch, |
|
|
|
|
&cord_reflection->GetStringReference( |
|
|
|
|
cord_message, descriptor->FindFieldByName("optional_bytes_cord"), |
|
|
|
|
&cord_scratch)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class GeneratedMessageReflectionSwapTest : public testing::TestWithParam<bool> { |
|
|
|
|
protected: |
|
|
|
@ -981,6 +997,8 @@ TEST(GeneratedMessageReflectionTest, Oneof) { |
|
|
|
|
EXPECT_NE(&unittest::TestOneof2::FooGroup::default_instance(), |
|
|
|
|
&reflection->GetMessage( |
|
|
|
|
message, descriptor->FindFieldByName("foo_lazy_message"))); |
|
|
|
|
EXPECT_EQ("", reflection->GetString( |
|
|
|
|
message, descriptor->FindFieldByName("foo_bytes_cord"))); |
|
|
|
|
EXPECT_EQ( |
|
|
|
|
5, reflection->GetInt32(message, descriptor->FindFieldByName("bar_int"))); |
|
|
|
|
EXPECT_EQ("STRING", reflection->GetString( |
|
|
|
@ -1009,6 +1027,11 @@ TEST(GeneratedMessageReflectionTest, Oneof) { |
|
|
|
|
"bytes"); |
|
|
|
|
EXPECT_EQ("bytes", reflection->GetString( |
|
|
|
|
message, descriptor->FindFieldByName("foo_bytes"))); |
|
|
|
|
reflection->SetString(&message, descriptor->FindFieldByName("foo_bytes_cord"), |
|
|
|
|
"bytes_cord"); |
|
|
|
|
EXPECT_EQ("bytes_cord", |
|
|
|
|
reflection->GetString( |
|
|
|
|
message, descriptor->FindFieldByName("foo_bytes_cord"))); |
|
|
|
|
reflection->SetString(&message, descriptor->FindFieldByName("bar_cord"), |
|
|
|
|
"change_cord"); |
|
|
|
|
EXPECT_EQ( |
|
|
|
@ -1321,6 +1344,222 @@ TEST(GeneratedMessageReflectionTest, UsageErrors) { |
|
|
|
|
|
|
|
|
|
#endif // GTEST_HAS_DEATH_TEST
|
|
|
|
|
|
|
|
|
|
class GeneratedMessageReflectionCordAccessorsTest : public testing::Test { |
|
|
|
|
protected: |
|
|
|
|
const FieldDescriptor* optional_string_; |
|
|
|
|
const FieldDescriptor* optional_string_piece_; |
|
|
|
|
const FieldDescriptor* optional_cord_; |
|
|
|
|
const FieldDescriptor* repeated_string_; |
|
|
|
|
const FieldDescriptor* repeated_string_piece_; |
|
|
|
|
const FieldDescriptor* repeated_cord_; |
|
|
|
|
const FieldDescriptor* default_string_; |
|
|
|
|
const FieldDescriptor* default_string_piece_; |
|
|
|
|
const FieldDescriptor* default_cord_; |
|
|
|
|
|
|
|
|
|
const FieldDescriptor* optional_string_extension_; |
|
|
|
|
const FieldDescriptor* repeated_string_extension_; |
|
|
|
|
|
|
|
|
|
unittest::TestAllTypes message_; |
|
|
|
|
const Reflection* reflection_; |
|
|
|
|
unittest::TestAllExtensions extensions_message_; |
|
|
|
|
const Reflection* extensions_reflection_; |
|
|
|
|
|
|
|
|
|
void SetUp() override { |
|
|
|
|
const Descriptor* descriptor = unittest::TestAllTypes::descriptor(); |
|
|
|
|
|
|
|
|
|
optional_string_ = descriptor->FindFieldByName("optional_string"); |
|
|
|
|
optional_string_piece_ = |
|
|
|
|
descriptor->FindFieldByName("optional_string_piece"); |
|
|
|
|
optional_cord_ = descriptor->FindFieldByName("optional_cord"); |
|
|
|
|
repeated_string_ = descriptor->FindFieldByName("repeated_string"); |
|
|
|
|
repeated_string_piece_ = |
|
|
|
|
descriptor->FindFieldByName("repeated_string_piece"); |
|
|
|
|
repeated_cord_ = descriptor->FindFieldByName("repeated_cord"); |
|
|
|
|
default_string_ = descriptor->FindFieldByName("default_string"); |
|
|
|
|
default_string_piece_ = descriptor->FindFieldByName("default_string_piece"); |
|
|
|
|
default_cord_ = descriptor->FindFieldByName("default_cord"); |
|
|
|
|
|
|
|
|
|
optional_string_extension_ = |
|
|
|
|
descriptor->file()->FindExtensionByName("optional_string_extension"); |
|
|
|
|
repeated_string_extension_ = |
|
|
|
|
descriptor->file()->FindExtensionByName("repeated_string_extension"); |
|
|
|
|
|
|
|
|
|
ASSERT_TRUE(optional_string_ != nullptr); |
|
|
|
|
ASSERT_TRUE(optional_string_piece_ != nullptr); |
|
|
|
|
ASSERT_TRUE(optional_cord_ != nullptr); |
|
|
|
|
ASSERT_TRUE(repeated_string_ != nullptr); |
|
|
|
|
ASSERT_TRUE(repeated_string_piece_ != nullptr); |
|
|
|
|
ASSERT_TRUE(repeated_cord_ != nullptr); |
|
|
|
|
ASSERT_TRUE(optional_string_extension_ != nullptr); |
|
|
|
|
ASSERT_TRUE(repeated_string_extension_ != nullptr); |
|
|
|
|
|
|
|
|
|
reflection_ = message_.GetReflection(); |
|
|
|
|
extensions_reflection_ = extensions_message_.GetReflection(); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
TEST_F(GeneratedMessageReflectionCordAccessorsTest, GetCord) { |
|
|
|
|
message_.set_optional_string("foo"); |
|
|
|
|
|
|
|
|
|
extensions_message_.SetExtension(unittest::optional_string_extension, "moo"); |
|
|
|
|
|
|
|
|
|
EXPECT_EQ("foo", reflection_->GetCord(message_, optional_string_)); |
|
|
|
|
EXPECT_EQ("moo", extensions_reflection_->GetCord(extensions_message_, |
|
|
|
|
optional_string_extension_)); |
|
|
|
|
|
|
|
|
|
EXPECT_EQ("hello", reflection_->GetCord(message_, default_string_)); |
|
|
|
|
EXPECT_EQ("abc", reflection_->GetCord(message_, default_string_piece_)); |
|
|
|
|
EXPECT_EQ("123", reflection_->GetCord(message_, default_cord_)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unittest::TestCord message; |
|
|
|
|
const Descriptor* descriptor = unittest::TestCord::descriptor(); |
|
|
|
|
const Reflection* reflection = message.GetReflection(); |
|
|
|
|
|
|
|
|
|
message.set_optional_bytes_cord("bytes_cord"); |
|
|
|
|
EXPECT_EQ("bytes_cord", |
|
|
|
|
reflection->GetCord( |
|
|
|
|
message, descriptor->FindFieldByName("optional_bytes_cord"))); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
TEST_F(GeneratedMessageReflectionCordAccessorsTest, GetOneofCord) { |
|
|
|
|
unittest::TestOneof2 message; |
|
|
|
|
const Descriptor* descriptor = unittest::TestOneof2::descriptor(); |
|
|
|
|
const Reflection* reflection = message.GetReflection(); |
|
|
|
|
|
|
|
|
|
message.set_foo_bytes_cord("bytes_cord"); |
|
|
|
|
EXPECT_EQ("bytes_cord", |
|
|
|
|
reflection->GetCord(message, |
|
|
|
|
descriptor->FindFieldByName("foo_bytes_cord"))); |
|
|
|
|
|
|
|
|
|
message.set_foo_string("foo"); |
|
|
|
|
EXPECT_EQ("foo", reflection->GetCord( |
|
|
|
|
message, descriptor->FindFieldByName("foo_string"))); |
|
|
|
|
|
|
|
|
|
message.set_foo_bytes("bytes"); |
|
|
|
|
EXPECT_EQ("bytes", reflection->GetCord( |
|
|
|
|
message, descriptor->FindFieldByName("foo_bytes"))); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
TEST_F(GeneratedMessageReflectionCordAccessorsTest, SetStringFromCord) { |
|
|
|
|
reflection_->SetString(&message_, optional_string_, absl::Cord("foo")); |
|
|
|
|
reflection_->SetString(&message_, optional_string_piece_, absl::Cord("bar")); |
|
|
|
|
reflection_->SetString(&message_, optional_cord_, absl::Cord("baz")); |
|
|
|
|
extensions_reflection_->SetString( |
|
|
|
|
&extensions_message_, optional_string_extension_, absl::Cord("moo")); |
|
|
|
|
|
|
|
|
|
EXPECT_TRUE(message_.has_optional_string()); |
|
|
|
|
EXPECT_TRUE(message_.has_optional_string_piece()); |
|
|
|
|
EXPECT_TRUE(message_.has_optional_cord()); |
|
|
|
|
EXPECT_TRUE( |
|
|
|
|
extensions_message_.HasExtension(unittest::optional_string_extension)); |
|
|
|
|
|
|
|
|
|
EXPECT_EQ("foo", message_.optional_string()); |
|
|
|
|
EXPECT_EQ("bar", std::string( |
|
|
|
|
reflection_->GetCord(message_, optional_string_piece_))); |
|
|
|
|
EXPECT_EQ("baz", std::string(reflection_->GetCord(message_, optional_cord_))); |
|
|
|
|
EXPECT_EQ("moo", extensions_message_.GetExtension( |
|
|
|
|
unittest::optional_string_extension)); |
|
|
|
|
|
|
|
|
|
unittest::TestCord message; |
|
|
|
|
const Descriptor* descriptor = unittest::TestCord::descriptor(); |
|
|
|
|
const Reflection* reflection = message.GetReflection(); |
|
|
|
|
|
|
|
|
|
reflection->SetString(&message, |
|
|
|
|
descriptor->FindFieldByName("optional_bytes_cord"), |
|
|
|
|
absl::Cord("cord")); |
|
|
|
|
EXPECT_TRUE(message.has_optional_bytes_cord()); |
|
|
|
|
EXPECT_EQ("cord", message.optional_bytes_cord()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
TEST_F(GeneratedMessageReflectionCordAccessorsTest, SetOneofStringFromCord) { |
|
|
|
|
unittest::TestOneof2 message; |
|
|
|
|
const Descriptor* descriptor = unittest::TestOneof2::descriptor(); |
|
|
|
|
const Reflection* reflection = message.GetReflection(); |
|
|
|
|
|
|
|
|
|
reflection->SetString(&message, descriptor->FindFieldByName("foo_string"), |
|
|
|
|
absl::Cord("foo")); |
|
|
|
|
EXPECT_TRUE(message.has_foo_string()); |
|
|
|
|
EXPECT_EQ("foo", message.foo_string()); |
|
|
|
|
|
|
|
|
|
reflection->SetString(&message, descriptor->FindFieldByName("foo_bytes"), |
|
|
|
|
absl::Cord("bytes")); |
|
|
|
|
EXPECT_TRUE(message.has_foo_bytes()); |
|
|
|
|
EXPECT_EQ("bytes", message.foo_bytes()); |
|
|
|
|
|
|
|
|
|
reflection->SetString(&message, descriptor->FindFieldByName("foo_cord"), |
|
|
|
|
absl::Cord("cord")); |
|
|
|
|
EXPECT_EQ("cord", std::string(reflection->GetCord( |
|
|
|
|
message, descriptor->FindFieldByName("foo_cord")))); |
|
|
|
|
|
|
|
|
|
reflection->SetString(&message, |
|
|
|
|
descriptor->FindFieldByName("foo_string_piece"), |
|
|
|
|
absl::Cord("string_piece")); |
|
|
|
|
EXPECT_EQ("string_piece", |
|
|
|
|
reflection->GetCord( |
|
|
|
|
message, descriptor->FindFieldByName("foo_string_piece"))); |
|
|
|
|
|
|
|
|
|
reflection->SetString(&message, descriptor->FindFieldByName("foo_bytes_cord"), |
|
|
|
|
absl::Cord("bytes_cord")); |
|
|
|
|
EXPECT_TRUE(message.has_foo_bytes_cord()); |
|
|
|
|
EXPECT_EQ("bytes_cord", message.foo_bytes_cord()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
TEST_F(GeneratedMessageReflectionCordAccessorsTest, CordSingularBytes) { |
|
|
|
|
unittest::TestCord message; |
|
|
|
|
absl::Cord cord_value("test"); |
|
|
|
|
message.set_optional_bytes_cord(cord_value); |
|
|
|
|
EXPECT_EQ("test", message.optional_bytes_cord()); |
|
|
|
|
|
|
|
|
|
EXPECT_TRUE(message.has_optional_bytes_cord()); |
|
|
|
|
message.clear_optional_bytes_cord(); |
|
|
|
|
EXPECT_FALSE(message.has_optional_bytes_cord()); |
|
|
|
|
|
|
|
|
|
std::string string_value = "test"; |
|
|
|
|
message.set_optional_bytes_cord(string_value); |
|
|
|
|
EXPECT_EQ("test", message.optional_bytes_cord()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
TEST_F(GeneratedMessageReflectionCordAccessorsTest, CordSingularBytesDefault) { |
|
|
|
|
unittest::TestCord message; |
|
|
|
|
EXPECT_EQ("hello", message.optional_bytes_cord_default()); |
|
|
|
|
absl::Cord cord_value("world"); |
|
|
|
|
message.set_optional_bytes_cord_default(cord_value); |
|
|
|
|
EXPECT_EQ("world", message.optional_bytes_cord_default()); |
|
|
|
|
message.clear_optional_bytes_cord_default(); |
|
|
|
|
EXPECT_EQ("hello", message.optional_bytes_cord_default()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
TEST_F(GeneratedMessageReflectionCordAccessorsTest, CordSingularOneofBytes) { |
|
|
|
|
unittest::TestOneof2 message; |
|
|
|
|
absl::Cord cord_value("test"); |
|
|
|
|
message.set_foo_bytes_cord(cord_value); |
|
|
|
|
EXPECT_EQ("test", message.foo_bytes_cord()); |
|
|
|
|
|
|
|
|
|
EXPECT_TRUE(message.has_foo_bytes_cord()); |
|
|
|
|
message.clear_foo(); |
|
|
|
|
EXPECT_FALSE(message.has_foo_bytes_cord()); |
|
|
|
|
|
|
|
|
|
std::string string_value = "test"; |
|
|
|
|
message.set_foo_bytes_cord(string_value); |
|
|
|
|
EXPECT_EQ("test", message.foo_bytes_cord()); |
|
|
|
|
EXPECT_TRUE(message.has_foo_bytes_cord()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
TEST_F(GeneratedMessageReflectionCordAccessorsTest, ClearOneofCord) { |
|
|
|
|
unittest::TestOneof2 message; |
|
|
|
|
absl::Cord cord_value("test"); |
|
|
|
|
message.set_foo_bytes_cord(cord_value); |
|
|
|
|
|
|
|
|
|
const Descriptor* descriptor = unittest::TestOneof2::descriptor(); |
|
|
|
|
const Reflection* reflection = message.GetReflection(); |
|
|
|
|
|
|
|
|
|
EXPECT_TRUE(message.has_foo_bytes_cord()); |
|
|
|
|
reflection->ClearOneof(&message, descriptor->FindOneofByName("foo")); |
|
|
|
|
EXPECT_FALSE(message.has_foo_bytes_cord()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
using internal::IsDescendant; |
|
|
|
|
|
|
|
|
|