@ -44,6 +44,7 @@
# include "google/protobuf/test_util.h"
# include "google/protobuf/test_util2.h"
# include "google/protobuf/unittest.pb.h"
# include "google/protobuf/unittest.pb.h"
# include "google/protobuf/unittest_mset.pb.h"
# include "google/protobuf/unittest_mset_wire_format.pb.h"
# include "google/protobuf/unittest_proto3.pb.h"
@ -59,21 +60,13 @@ namespace protobuf {
namespace internal {
class UnsetFieldsMetadataTextFormatTestUtil {
public :
static std : : vector < const void * > GetRawAddresse s(
static const auto & GetRawId s(
const TextFormat : : Parser : : UnsetFieldsMetadata & metadata ) {
return std : : vector < const void * > ( metadata . addresses_ . begin ( ) ,
metadata . addresses_ . end ( ) ) ;
}
static const void * GetAddress ( const Message & message ,
const Reflection & reflection ,
const FieldDescriptor & fd ) {
return TextFormat : : Parser : : UnsetFieldsMetadata : : GetUnsetFieldAddress (
message , reflection , fd ) ;
return metadata . ids_ ;
}
static int32_t NumFields (
const TextFormat : : Parser : : UnsetFieldsMetadata & metadata ) {
return metadata . addresses_ . size ( ) ;
static auto GetId ( const Message & message , absl : : string_view field ) {
return TextFormat : : Parser : : UnsetFieldsMetadata : : GetUnsetFieldId (
message , * message . GetDescriptor ( ) - > FindFieldByName ( field ) ) ;
}
} ;
} // namespace internal
@ -85,6 +78,7 @@ using ::google::protobuf::internal::kDebugStringSilentMarker;
using : : google : : protobuf : : internal : : UnsetFieldsMetadataTextFormatTestUtil ;
using : : testing : : AllOf ;
using : : testing : : HasSubstr ;
using : : testing : : UnorderedElementsAre ;
// A basic string with different escapable characters for testing.
constexpr absl : : string_view kEscapeTestString =
@ -1029,14 +1023,15 @@ TEST_F(TextFormatTest, PopulatesNoOpFields) {
TextFormat : : Parser parser ;
TextFormat : : Parser : : UnsetFieldsMetadata no_op_fields ;
parser . OutputNoOpFields ( & no_op_fields ) ;
using Peer = UnsetFieldsMetadataTextFormatTestUtil ;
{
no_op_fields = { } ;
const absl : : string_view singular_int_parse_string = " optional_int32: 0 " ;
EXPECT_TRUE ( TextFormat : : ParseFromString ( singular_int_parse_string , & proto ) ) ;
EXPECT_TRUE ( parser . ParseFromString ( singular_int_parse_string , & proto ) ) ;
EXPECT_EQ ( UnsetFieldsMetadataTextFormatTestUtil : : NumFiel ds ( no_op_fields ) ,
1 ) ;
EXPECT_THAT ( Peer : : GetRawI ds ( no_op_fields ) ,
UnorderedElementsAre ( Peer : : GetId ( proto , " optional_int32 " ) ) ) ;
}
{
no_op_fields = { } ;
@ -1044,8 +1039,8 @@ TEST_F(TextFormatTest, PopulatesNoOpFields) {
EXPECT_TRUE (
TextFormat : : ParseFromString ( singular_bool_parse_string , & proto ) ) ;
EXPECT_TRUE ( parser . ParseFromString ( singular_bool_parse_string , & proto ) ) ;
EXPECT_EQ ( UnsetFieldsMetadataTextFormatTestUtil : : NumFiel ds ( no_op_fields ) ,
1 ) ;
EXPECT_THAT ( Peer : : GetRawI ds ( no_op_fields ) ,
UnorderedElementsAre ( Peer : : GetId ( proto , " optional_bool " ) ) ) ;
}
{
no_op_fields = { } ;
@ -1054,8 +1049,8 @@ TEST_F(TextFormatTest, PopulatesNoOpFields) {
EXPECT_TRUE (
TextFormat : : ParseFromString ( singular_string_parse_string , & proto ) ) ;
EXPECT_TRUE ( parser . ParseFromString ( singular_string_parse_string , & proto ) ) ;
EXPECT_EQ ( UnsetFieldsMetadataTextFormatTestUtil : : NumFiel ds ( no_op_fields ) ,
1 ) ;
EXPECT_THAT ( Peer : : GetRawI ds ( no_op_fields ) ,
UnorderedElementsAre ( Peer : : GetId ( proto , " optional_string " ) ) ) ;
}
{
no_op_fields = { } ;
@ -1064,8 +1059,9 @@ TEST_F(TextFormatTest, PopulatesNoOpFields) {
EXPECT_TRUE (
TextFormat : : ParseFromString ( nested_message_parse_string , & proto ) ) ;
EXPECT_TRUE ( parser . ParseFromString ( nested_message_parse_string , & proto ) ) ;
EXPECT_EQ ( UnsetFieldsMetadataTextFormatTestUtil : : NumFields ( no_op_fields ) ,
1 ) ;
EXPECT_THAT ( Peer : : GetRawIds ( no_op_fields ) ,
UnorderedElementsAre (
Peer : : GetId ( proto . optional_nested_message ( ) , " bb " ) ) ) ;
}
{
no_op_fields = { } ;
@ -1074,8 +1070,7 @@ TEST_F(TextFormatTest, PopulatesNoOpFields) {
EXPECT_TRUE (
TextFormat : : ParseFromString ( nested_message_parse_string , & proto ) ) ;
EXPECT_TRUE ( parser . ParseFromString ( nested_message_parse_string , & proto ) ) ;
EXPECT_EQ ( UnsetFieldsMetadataTextFormatTestUtil : : NumFields ( no_op_fields ) ,
0 ) ;
EXPECT_THAT ( Peer : : GetRawIds ( no_op_fields ) , UnorderedElementsAre ( ) ) ;
}
{
no_op_fields = { } ;
@ -1084,8 +1079,9 @@ TEST_F(TextFormatTest, PopulatesNoOpFields) {
EXPECT_TRUE (
TextFormat : : ParseFromString ( foreign_message_parse_string , & proto ) ) ;
EXPECT_TRUE ( parser . ParseFromString ( foreign_message_parse_string , & proto ) ) ;
EXPECT_EQ ( UnsetFieldsMetadataTextFormatTestUtil : : NumFields ( no_op_fields ) ,
1 ) ;
EXPECT_THAT ( Peer : : GetRawIds ( no_op_fields ) ,
UnorderedElementsAre (
Peer : : GetId ( proto . optional_foreign_message ( ) , " c " ) ) ) ;
}
{
no_op_fields = { } ;
@ -1093,8 +1089,9 @@ TEST_F(TextFormatTest, PopulatesNoOpFields) {
" optional_nested_enum: ZERO " ;
EXPECT_TRUE ( TextFormat : : ParseFromString ( nested_enum_parse_string , & proto ) ) ;
EXPECT_TRUE ( parser . ParseFromString ( nested_enum_parse_string , & proto ) ) ;
EXPECT_EQ ( UnsetFieldsMetadataTextFormatTestUtil : : NumFields ( no_op_fields ) ,
1 ) ;
EXPECT_THAT (
Peer : : GetRawIds ( no_op_fields ) ,
UnorderedElementsAre ( Peer : : GetId ( proto , " optional_nested_enum " ) ) ) ;
}
{
no_op_fields = { } ;
@ -1102,8 +1099,9 @@ TEST_F(TextFormatTest, PopulatesNoOpFields) {
" optional_foreign_enum: FOREIGN_ZERO " ;
EXPECT_TRUE ( TextFormat : : ParseFromString ( foreign_enum_parse_string , & proto ) ) ;
EXPECT_TRUE ( parser . ParseFromString ( foreign_enum_parse_string , & proto ) ) ;
EXPECT_EQ ( UnsetFieldsMetadataTextFormatTestUtil : : NumFields ( no_op_fields ) ,
1 ) ;
EXPECT_THAT (
Peer : : GetRawIds ( no_op_fields ) ,
UnorderedElementsAre ( Peer : : GetId ( proto , " optional_foreign_enum " ) ) ) ;
}
{
no_op_fields = { } ;
@ -1111,16 +1109,17 @@ TEST_F(TextFormatTest, PopulatesNoOpFields) {
" optional_string_piece: '' " ;
EXPECT_TRUE ( TextFormat : : ParseFromString ( string_piece_parse_string , & proto ) ) ;
EXPECT_TRUE ( parser . ParseFromString ( string_piece_parse_string , & proto ) ) ;
EXPECT_EQ ( UnsetFieldsMetadataTextFormatTestUtil : : NumFields ( no_op_fields ) ,
1 ) ;
EXPECT_THAT (
Peer : : GetRawIds ( no_op_fields ) ,
UnorderedElementsAre ( Peer : : GetId ( proto , " optional_string_piece " ) ) ) ;
}
{
no_op_fields = { } ;
const absl : : string_view cord_parse_string = " optional_cord: '' " ;
EXPECT_TRUE ( TextFormat : : ParseFromString ( cord_parse_string , & proto ) ) ;
EXPECT_TRUE ( parser . ParseFromString ( cord_parse_string , & proto ) ) ;
EXPECT_EQ ( UnsetFieldsMetadataTextFormatTestUtil : : NumFiel ds ( no_op_fields ) ,
1 ) ;
EXPECT_THAT ( Peer : : GetRawI ds ( no_op_fields ) ,
UnorderedElementsAre ( Peer : : GetId ( proto , " optional_cord " ) ) ) ;
}
{
no_op_fields = { } ;
@ -1129,8 +1128,7 @@ TEST_F(TextFormatTest, PopulatesNoOpFields) {
EXPECT_TRUE (
TextFormat : : ParseFromString ( repeated_int32_parse_string , & proto ) ) ;
EXPECT_TRUE ( parser . ParseFromString ( repeated_int32_parse_string , & proto ) ) ;
EXPECT_EQ ( UnsetFieldsMetadataTextFormatTestUtil : : NumFields ( no_op_fields ) ,
0 ) ;
EXPECT_THAT ( Peer : : GetRawIds ( no_op_fields ) , UnorderedElementsAre ( ) ) ;
}
{
no_op_fields = { } ;
@ -1139,8 +1137,7 @@ TEST_F(TextFormatTest, PopulatesNoOpFields) {
EXPECT_TRUE (
TextFormat : : ParseFromString ( repeated_bool_parse_string , & proto ) ) ;
EXPECT_TRUE ( parser . ParseFromString ( repeated_bool_parse_string , & proto ) ) ;
EXPECT_EQ ( UnsetFieldsMetadataTextFormatTestUtil : : NumFields ( no_op_fields ) ,
0 ) ;
EXPECT_THAT ( Peer : : GetRawIds ( no_op_fields ) , UnorderedElementsAre ( ) ) ;
}
{
no_op_fields = { } ;
@ -1149,12 +1146,12 @@ TEST_F(TextFormatTest, PopulatesNoOpFields) {
EXPECT_TRUE (
TextFormat : : ParseFromString ( repeated_string_parse_string , & proto ) ) ;
EXPECT_TRUE ( parser . ParseFromString ( repeated_string_parse_string , & proto ) ) ;
EXPECT_EQ ( UnsetFieldsMetadataTextFormatTestUtil : : NumFields ( no_op_fields ) ,
0 ) ;
EXPECT_THAT ( Peer : : GetRawIds ( no_op_fields ) , UnorderedElementsAre ( ) ) ;
}
}
TEST_F ( TextFormatTest , FieldsPopulatedCorrectly ) {
using Peer = UnsetFieldsMetadataTextFormatTestUtil ;
proto3_unittest : : TestAllTypes proto ;
TextFormat : : Parser parser ;
TextFormat : : Parser : : UnsetFieldsMetadata no_op_fields ;
@ -1167,18 +1164,10 @@ TEST_F(TextFormatTest, FieldsPopulatedCorrectly) {
optional_nested_message { bb : 0 }
) pb " ;
EXPECT_TRUE ( parser . ParseFromString ( parse_string , & proto ) ) ;
ASSERT_EQ ( UnsetFieldsMetadataTextFormatTestUtil : : NumFields ( no_op_fields ) ,
2 ) ;
std : : vector < const void * > ptrs =
UnsetFieldsMetadataTextFormatTestUtil : : GetRawAddresses ( no_op_fields ) ;
EXPECT_EQ ( * static_cast < const int32_t * > ( ptrs [ 0 ] ) , 0 ) ;
EXPECT_EQ ( * static_cast < const int32_t * > ( ptrs [ 1 ] ) , 0 ) ;
proto . set_optional_int32 ( 20 ) ;
proto . mutable_optional_nested_message ( ) - > set_bb ( 30 ) ;
const std : : vector < int32_t > new_values {
* static_cast < const int32_t * > ( ptrs [ 0 ] ) ,
* static_cast < const int32_t * > ( ptrs [ 1 ] ) } ;
EXPECT_THAT ( new_values , testing : : UnorderedElementsAre ( 20 , 30 ) ) ;
EXPECT_THAT ( Peer : : GetRawIds ( no_op_fields ) ,
UnorderedElementsAre (
Peer : : GetId ( proto , " optional_int32 " ) ,
Peer : : GetId ( proto . optional_nested_message ( ) , " bb " ) ) ) ;
}
{
no_op_fields = { } ;
@ -1188,13 +1177,8 @@ TEST_F(TextFormatTest, FieldsPopulatedCorrectly) {
optional_nested_message { bb : 20 }
) pb " ;
EXPECT_TRUE ( parser . ParseFromString ( parse_string , & proto ) ) ;
ASSERT_EQ ( UnsetFieldsMetadataTextFormatTestUtil : : NumFields ( no_op_fields ) ,
1 ) ;
std : : vector < const void * > ptrs =
UnsetFieldsMetadataTextFormatTestUtil : : GetRawAddresses ( no_op_fields ) ;
EXPECT_EQ ( * static_cast < const bool * > ( ptrs [ 0 ] ) , false ) ;
proto . set_optional_bool ( true ) ;
EXPECT_EQ ( * static_cast < const bool * > ( ptrs [ 0 ] ) , true ) ;
EXPECT_THAT ( Peer : : GetRawIds ( no_op_fields ) ,
UnorderedElementsAre ( Peer : : GetId ( proto , " optional_bool " ) ) ) ;
}
{
// The address returned by the field is a string_view, which is a separate
@ -1202,14 +1186,8 @@ TEST_F(TextFormatTest, FieldsPopulatedCorrectly) {
no_op_fields = { } ;
const absl : : string_view parse_string = " optional_string: \" \" " ;
EXPECT_TRUE ( parser . ParseFromString ( parse_string , & proto ) ) ;
ASSERT_EQ ( UnsetFieldsMetadataTextFormatTestUtil : : NumFields ( no_op_fields ) ,
1 ) ;
const Reflection * reflection = proto . GetReflection ( ) ;
const FieldDescriptor * field = proto . GetDescriptor ( ) - > FindFieldByNumber ( 14 ) ;
EXPECT_EQ (
UnsetFieldsMetadataTextFormatTestUtil : : GetRawAddresses ( no_op_fields ) [ 0 ] ,
UnsetFieldsMetadataTextFormatTestUtil : : GetAddress ( proto , * reflection ,
* field ) ) ;
EXPECT_THAT ( Peer : : GetRawIds ( no_op_fields ) ,
UnorderedElementsAre ( Peer : : GetId ( proto , " optional_string " ) ) ) ;
}
{
// The address returned by the field is a string_view, which is a separate
@ -1217,14 +1195,8 @@ TEST_F(TextFormatTest, FieldsPopulatedCorrectly) {
no_op_fields = { } ;
const absl : : string_view parse_string = " optional_bytes: \" \" " ;
EXPECT_TRUE ( parser . ParseFromString ( parse_string , & proto ) ) ;
ASSERT_EQ ( UnsetFieldsMetadataTextFormatTestUtil : : NumFields ( no_op_fields ) ,
1 ) ;
const Reflection * reflection = proto . GetReflection ( ) ;
const FieldDescriptor * field = proto . GetDescriptor ( ) - > FindFieldByNumber ( 15 ) ;
EXPECT_EQ (
UnsetFieldsMetadataTextFormatTestUtil : : GetRawAddresses ( no_op_fields ) [ 0 ] ,
UnsetFieldsMetadataTextFormatTestUtil : : GetAddress ( proto , * reflection ,
* field ) ) ;
EXPECT_THAT ( Peer : : GetRawIds ( no_op_fields ) ,
UnorderedElementsAre ( Peer : : GetId ( proto , " optional_bytes " ) ) ) ;
}
}