Protocol Buffers - Google's data interchange format (grpc依赖)
https://developers.google.com/protocol-buffers/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
156 lines
5.2 KiB
156 lines
5.2 KiB
1 year ago
|
// Protocol Buffers - Google's data interchange format
|
||
|
// Copyright 2023 Google Inc. All rights reserved.
|
||
|
//
|
||
1 year ago
|
// Use of this source code is governed by a BSD-style
|
||
|
// license that can be found in the LICENSE file or at
|
||
|
// https://developers.google.com/open-source/licenses/bsd
|
||
1 year ago
|
|
||
10 months ago
|
#include "google/protobuf/descriptor.pb.h"
|
||
1 year ago
|
#include <gmock/gmock.h>
|
||
|
#include <gtest/gtest.h>
|
||
|
#include "google/protobuf/descriptor.h"
|
||
7 months ago
|
#include "editions/golden/test_messages_proto2_editions.pb.h"
|
||
|
#include "editions/golden/test_messages_proto3_editions.pb.h"
|
||
|
#include "editions/proto/test_editions_default_features.pb.h"
|
||
1 year ago
|
#include "google/protobuf/test_textproto.h"
|
||
|
|
||
|
// These tests provide some basic minimal coverage that protos work as expected.
|
||
|
// Full coverage will come as we migrate test protos to editions.
|
||
|
|
||
|
namespace google {
|
||
|
namespace protobuf {
|
||
|
namespace {
|
||
|
|
||
1 year ago
|
using ::protobuf_editions_test::EditionsDefaultMessage;
|
||
1 year ago
|
using ::protobuf_test_messages::editions::proto2::TestAllRequiredTypesProto2;
|
||
|
using ::protobuf_test_messages::editions::proto2::TestAllTypesProto2;
|
||
|
using ::protobuf_test_messages::editions::proto3::TestAllTypesProto3;
|
||
1 year ago
|
using ::testing::NotNull;
|
||
|
|
||
1 year ago
|
TEST(Generated, Parsing) {
|
||
1 year ago
|
TestAllTypesProto2 message = ParseTextOrDie(R"pb(
|
||
|
Data { group_int32: 9 }
|
||
|
)pb");
|
||
|
|
||
|
EXPECT_EQ(message.data().group_int32(), 9);
|
||
|
}
|
||
|
|
||
1 year ago
|
TEST(Generated, GeneratedMethods) {
|
||
1 year ago
|
TestAllTypesProto3 message;
|
||
|
message.set_optional_int32(9);
|
||
|
|
||
|
EXPECT_THAT(message, EqualsProto(R"pb(optional_int32: 9)pb"));
|
||
|
}
|
||
|
|
||
1 year ago
|
TEST(Generated, ExplicitPresence) {
|
||
1 year ago
|
const FieldDescriptor* field =
|
||
|
TestAllTypesProto2::descriptor()->FindFieldByName("default_int32");
|
||
|
ASSERT_THAT(field, NotNull());
|
||
|
EXPECT_TRUE(field->has_presence());
|
||
|
}
|
||
|
|
||
1 year ago
|
TEST(Generated, RequiredPresence) {
|
||
1 year ago
|
const FieldDescriptor* field =
|
||
|
TestAllRequiredTypesProto2::descriptor()->FindFieldByName(
|
||
|
"required_int32");
|
||
|
ASSERT_THAT(field, NotNull());
|
||
|
EXPECT_TRUE(field->has_presence());
|
||
|
EXPECT_TRUE(field->is_required());
|
||
|
EXPECT_EQ(field->label(), FieldDescriptor::LABEL_REQUIRED);
|
||
|
}
|
||
|
|
||
1 year ago
|
TEST(Generated, ImplicitPresence) {
|
||
1 year ago
|
const FieldDescriptor* field =
|
||
|
TestAllTypesProto3::descriptor()->FindFieldByName("optional_int32");
|
||
|
ASSERT_THAT(field, NotNull());
|
||
|
EXPECT_FALSE(field->has_presence());
|
||
|
}
|
||
|
|
||
1 year ago
|
TEST(Generated, ClosedEnum) {
|
||
1 year ago
|
const EnumDescriptor* enm =
|
||
|
TestAllTypesProto2::descriptor()->FindEnumTypeByName("NestedEnum");
|
||
|
ASSERT_THAT(enm, NotNull());
|
||
|
EXPECT_TRUE(enm->is_closed());
|
||
|
}
|
||
|
|
||
1 year ago
|
TEST(Generated, OpenEnum) {
|
||
1 year ago
|
const EnumDescriptor* enm =
|
||
|
TestAllTypesProto3::descriptor()->FindEnumTypeByName("NestedEnum");
|
||
|
ASSERT_THAT(enm, NotNull());
|
||
|
EXPECT_FALSE(enm->is_closed());
|
||
|
}
|
||
|
|
||
1 year ago
|
TEST(Generated, PackedRepeated) {
|
||
1 year ago
|
const FieldDescriptor* field =
|
||
|
TestAllTypesProto2::descriptor()->FindFieldByName("packed_int32");
|
||
|
ASSERT_THAT(field, NotNull());
|
||
|
EXPECT_TRUE(field->is_packed());
|
||
|
}
|
||
|
|
||
1 year ago
|
TEST(Generated, ExpandedRepeated) {
|
||
1 year ago
|
const FieldDescriptor* field =
|
||
|
TestAllTypesProto2::descriptor()->FindFieldByName("repeated_int32");
|
||
|
ASSERT_THAT(field, NotNull());
|
||
|
EXPECT_FALSE(field->is_packed());
|
||
|
}
|
||
|
|
||
1 year ago
|
TEST(Generated, DoesNotEnforceUtf8) {
|
||
1 year ago
|
const FieldDescriptor* field =
|
||
|
TestAllTypesProto2::descriptor()->FindFieldByName("optional_string");
|
||
|
ASSERT_THAT(field, NotNull());
|
||
|
EXPECT_FALSE(field->requires_utf8_validation());
|
||
|
}
|
||
|
|
||
1 year ago
|
TEST(Generated, EnforceUtf8) {
|
||
1 year ago
|
const FieldDescriptor* field =
|
||
|
TestAllTypesProto3::descriptor()->FindFieldByName("optional_string");
|
||
|
ASSERT_THAT(field, NotNull());
|
||
|
EXPECT_TRUE(field->requires_utf8_validation());
|
||
|
}
|
||
|
|
||
1 year ago
|
TEST(Generated, DelimitedEncoding) {
|
||
1 year ago
|
const FieldDescriptor* field =
|
||
|
TestAllTypesProto2::descriptor()->FindFieldByName("data");
|
||
|
ASSERT_THAT(field, NotNull());
|
||
|
EXPECT_EQ(field->type(), FieldDescriptor::TYPE_GROUP);
|
||
|
}
|
||
|
|
||
1 year ago
|
TEST(Generated, LengthPrefixedEncoding) {
|
||
1 year ago
|
const FieldDescriptor* field =
|
||
|
TestAllTypesProto2::descriptor()->FindFieldByName(
|
||
|
"optional_nested_message");
|
||
|
ASSERT_THAT(field, NotNull());
|
||
|
EXPECT_EQ(field->type(), FieldDescriptor::TYPE_MESSAGE);
|
||
|
}
|
||
|
|
||
1 year ago
|
TEST(Generated, EditionDefaults2023) {
|
||
|
const Descriptor* desc = EditionsDefaultMessage::descriptor();
|
||
|
EXPECT_TRUE(desc->FindFieldByName("int32_field")->has_presence());
|
||
|
EXPECT_TRUE(
|
||
|
desc->FindFieldByName("string_field")->requires_utf8_validation());
|
||
|
EXPECT_FALSE(desc->FindFieldByName("enum_field")
|
||
|
->legacy_enum_field_treated_as_closed());
|
||
|
EXPECT_FALSE(desc->FindFieldByName("enum_field")->enum_type()->is_closed());
|
||
|
EXPECT_TRUE(desc->FindFieldByName("repeated_int32_field")->is_packed());
|
||
|
EXPECT_EQ(desc->FindFieldByName("sub_message_field")->type(),
|
||
|
FieldDescriptor::TYPE_MESSAGE);
|
||
|
}
|
||
|
|
||
|
TEST(Generated, EditionDefaults2023InternalFeatures) {
|
||
1 year ago
|
EXPECT_THAT(internal::InternalFeatureHelper::GetFeatures(
|
||
|
*EditionsDefaultMessage::descriptor()),
|
||
|
google::protobuf::EqualsProto(R"pb(
|
||
|
field_presence: EXPLICIT
|
||
|
enum_type: OPEN
|
||
|
repeated_field_encoding: PACKED
|
||
|
utf8_validation: VERIFY
|
||
|
message_encoding: LENGTH_PREFIXED
|
||
|
json_format: ALLOW
|
||
10 months ago
|
[pb.cpp] { legacy_closed_enum: false string_type: STRING }
|
||
1 year ago
|
)pb"));
|
||
1 year ago
|
}
|
||
|
|
||
1 year ago
|
} // namespace
|
||
|
} // namespace protobuf
|
||
|
} // namespace google
|