Skip to content

Commit

Permalink
Migrate const std::string& to alternatives:
Browse files Browse the repository at this point in the history
 - In most cases, migrate to `absl::string_view`.
 - When capturing a temporary string in a variable, use `const std::string` without the ref.

We avoid changing virtual function parameters in this CL, since that is a breaking change.

PiperOrigin-RevId: 647686085
  • Loading branch information
protobuf-github-bot authored and Copybara-Service committed Jun 28, 2024
1 parent 99a335e commit 10c00b8
Show file tree
Hide file tree
Showing 68 changed files with 670 additions and 630 deletions.
88 changes: 44 additions & 44 deletions conformance/binary_json_conformance_suite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ std::string fixed64(void* data) {
return std::string(reinterpret_cast<char*>(&data_le), 8);
}

std::string delim(const std::string& buf) {
std::string delim(absl::string_view buf) {
return absl::StrCat(varint(buf.size()), buf);
}
std::string u32(uint32_t u32) { return fixed32(&u32); }
Expand Down Expand Up @@ -239,7 +239,7 @@ std::string UpperCase(std::string str) {
}

bool IsProto3Default(FieldDescriptor::Type type,
const std::string& binary_data) {
absl::string_view binary_data) {
switch (type) {
case FieldDescriptor::TYPE_DOUBLE:
return binary_data == dbl(0);
Expand Down Expand Up @@ -299,7 +299,7 @@ bool BinaryAndJsonConformanceSuite::ParseResponse(
const ConformanceRequestSetting& setting, Message* test_message) {
const ConformanceRequest& request = setting.GetRequest();
WireFormat requested_output = request.requested_output_format();
const std::string& test_name = setting.GetTestName();
const std::string test_name = setting.GetTestName();
ConformanceLevel level = setting.GetLevel();

switch (response.result_case()) {
Expand Down Expand Up @@ -421,8 +421,8 @@ void BinaryAndJsonConformanceSuite::RunDelimitedFieldTests() {

template <typename MessageType>
void BinaryAndJsonConformanceSuiteImpl<MessageType>::
ExpectParseFailureForProtoWithProtoVersion(const std::string& proto,
const std::string& test_name,
ExpectParseFailureForProtoWithProtoVersion(absl::string_view proto,
absl::string_view test_name,
ConformanceLevel level) {
MessageType prototype;
// We don't expect output, but if the program erroneously accepts the protobuf
Expand Down Expand Up @@ -451,7 +451,7 @@ void BinaryAndJsonConformanceSuiteImpl<MessageType>::
// Expect that this precise protobuf will cause a parse error.
template <typename MessageType>
void BinaryAndJsonConformanceSuiteImpl<MessageType>::ExpectParseFailureForProto(
const std::string& proto, const std::string& test_name,
absl::string_view proto, absl::string_view test_name,
ConformanceLevel level) {
ExpectParseFailureForProtoWithProtoVersion(proto, test_name, level);
}
Expand All @@ -463,27 +463,27 @@ void BinaryAndJsonConformanceSuiteImpl<MessageType>::ExpectParseFailureForProto(
// TODO: implement the second of these.
template <typename MessageType>
void BinaryAndJsonConformanceSuiteImpl<
MessageType>::ExpectHardParseFailureForProto(const std::string& proto,
const std::string& test_name,
MessageType>::ExpectHardParseFailureForProto(absl::string_view proto,
absl::string_view test_name,
ConformanceLevel level) {
return ExpectParseFailureForProto(proto, test_name, level);
}

template <typename MessageType>
void BinaryAndJsonConformanceSuiteImpl<MessageType>::RunValidJsonTest(
const std::string& test_name, ConformanceLevel level,
const std::string& input_json, const std::string& equivalent_text_format) {
absl::string_view test_name, ConformanceLevel level,
absl::string_view input_json, absl::string_view equivalent_text_format) {
MessageType prototype;
RunValidJsonTestWithMessage(test_name, level, input_json,
equivalent_text_format, prototype);
}

template <typename MessageType>
void BinaryAndJsonConformanceSuiteImpl<MessageType>::
RunValidJsonTestWithMessage(const std::string& test_name,
RunValidJsonTestWithMessage(absl::string_view test_name,
ConformanceLevel level,
const std::string& input_json,
const std::string& equivalent_text_format,
absl::string_view input_json,
absl::string_view equivalent_text_format,
const Message& prototype) {
ConformanceRequestSetting setting1(
level, conformance::JSON, conformance::PROTOBUF, conformance::JSON_TEST,
Expand All @@ -498,8 +498,8 @@ void BinaryAndJsonConformanceSuiteImpl<MessageType>::
template <typename MessageType>
void BinaryAndJsonConformanceSuiteImpl<MessageType>::
RunValidJsonTestWithProtobufInput(
const std::string& test_name, ConformanceLevel level,
const MessageType& input, const std::string& equivalent_text_format) {
absl::string_view test_name, ConformanceLevel level,
const MessageType& input, absl::string_view equivalent_text_format) {
ConformanceRequestSetting setting(
level, conformance::PROTOBUF, conformance::JSON, conformance::JSON_TEST,
input, test_name, input.SerializeAsString());
Expand All @@ -508,10 +508,10 @@ void BinaryAndJsonConformanceSuiteImpl<MessageType>::

template <typename MessageType>
void BinaryAndJsonConformanceSuiteImpl<MessageType>::
RunValidJsonIgnoreUnknownTest(const std::string& test_name,
RunValidJsonIgnoreUnknownTest(absl::string_view test_name,
ConformanceLevel level,
const std::string& input_json,
const std::string& equivalent_text_format) {
absl::string_view input_json,
absl::string_view equivalent_text_format) {
MessageType prototype;
ConformanceRequestSetting setting(
level, conformance::JSON, conformance::PROTOBUF,
Expand All @@ -522,9 +522,9 @@ void BinaryAndJsonConformanceSuiteImpl<MessageType>::

template <typename MessageType>
void BinaryAndJsonConformanceSuite::RunValidBinaryProtobufTest(
const std::string& test_name, ConformanceLevel level,
const std::string& input_protobuf,
const std::string& equivalent_text_format) {
absl::string_view test_name, ConformanceLevel level,
absl::string_view input_protobuf,
absl::string_view equivalent_text_format) {
MessageType prototype;

ConformanceRequestSetting binary_to_binary(
Expand All @@ -535,9 +535,9 @@ void BinaryAndJsonConformanceSuite::RunValidBinaryProtobufTest(

template <typename MessageType>
void BinaryAndJsonConformanceSuite::RunValidProtobufTest(
const std::string& test_name, ConformanceLevel level,
const std::string& input_protobuf,
const std::string& equivalent_text_format) {
absl::string_view test_name, ConformanceLevel level,
absl::string_view input_protobuf,
absl::string_view equivalent_text_format) {
MessageType prototype;

ConformanceRequestSetting binary_to_binary(
Expand All @@ -553,24 +553,24 @@ void BinaryAndJsonConformanceSuite::RunValidProtobufTest(

template <typename MessageType>
void BinaryAndJsonConformanceSuiteImpl<MessageType>::RunValidProtobufTest(
const std::string& test_name, ConformanceLevel level,
const std::string& input_protobuf,
const std::string& equivalent_text_format) {
absl::string_view test_name, ConformanceLevel level,
absl::string_view input_protobuf,
absl::string_view equivalent_text_format) {
suite_.RunValidProtobufTest<MessageType>(test_name, level, input_protobuf,
equivalent_text_format);
}

template <typename MessageType>
void BinaryAndJsonConformanceSuiteImpl<MessageType>::RunValidBinaryProtobufTest(
const std::string& test_name, ConformanceLevel level,
const std::string& input_protobuf) {
absl::string_view test_name, ConformanceLevel level,
absl::string_view input_protobuf) {
RunValidBinaryProtobufTest(test_name, level, input_protobuf, input_protobuf);
}

template <typename MessageType>
void BinaryAndJsonConformanceSuiteImpl<MessageType>::RunValidBinaryProtobufTest(
const std::string& test_name, ConformanceLevel level,
const std::string& input_protobuf, const std::string& expected_protobuf) {
absl::string_view test_name, ConformanceLevel level,
absl::string_view input_protobuf, absl::string_view expected_protobuf) {
MessageType prototype;
ConformanceRequestSetting setting(
level, conformance::PROTOBUF, conformance::PROTOBUF,
Expand All @@ -580,8 +580,8 @@ void BinaryAndJsonConformanceSuiteImpl<MessageType>::RunValidBinaryProtobufTest(

template <typename MessageType>
void BinaryAndJsonConformanceSuiteImpl<MessageType>::
RunBinaryPerformanceMergeMessageWithField(const std::string& test_name,
const std::string& field_proto) {
RunBinaryPerformanceMergeMessageWithField(absl::string_view test_name,
absl::string_view field_proto) {
std::string message_tag = tag(27, WireFormatLite::WIRETYPE_LENGTH_DELIMITED);
std::string message_proto = absl::StrCat(message_tag, delim(field_proto));

Expand All @@ -592,7 +592,7 @@ void BinaryAndJsonConformanceSuiteImpl<MessageType>::

std::string multiple_repeated_field_proto;
for (size_t i = 0; i < kPerformanceRepeatCount; i++) {
multiple_repeated_field_proto.append(field_proto);
multiple_repeated_field_proto.append(std::string(field_proto));
}
std::string expected_proto =
absl::StrCat(message_tag, delim(multiple_repeated_field_proto));
Expand All @@ -602,10 +602,10 @@ void BinaryAndJsonConformanceSuiteImpl<MessageType>::

template <typename MessageType>
void BinaryAndJsonConformanceSuiteImpl<MessageType>::
RunValidProtobufTestWithMessage(const std::string& test_name,
RunValidProtobufTestWithMessage(absl::string_view test_name,
ConformanceLevel level,
const Message* input,
const std::string& equivalent_text_format) {
absl::string_view equivalent_text_format) {
RunValidProtobufTest(test_name, level, input->SerializeAsString(),
equivalent_text_format);
}
Expand All @@ -617,9 +617,9 @@ void BinaryAndJsonConformanceSuiteImpl<MessageType>::

template <typename MessageType> // the JSON output directly.
void BinaryAndJsonConformanceSuiteImpl<
MessageType>::RunValidJsonTestWithValidator(const std::string& test_name,
MessageType>::RunValidJsonTestWithValidator(absl::string_view test_name,
ConformanceLevel level,
const std::string& input_json,
absl::string_view input_json,
const Validator& validator) {
MessageType prototype;
ConformanceRequestSetting setting(level, conformance::JSON, conformance::JSON,
Expand Down Expand Up @@ -663,8 +663,8 @@ void BinaryAndJsonConformanceSuiteImpl<

template <typename MessageType>
void BinaryAndJsonConformanceSuiteImpl<MessageType>::ExpectParseFailureForJson(
const std::string& test_name, ConformanceLevel level,
const std::string& input_json) {
absl::string_view test_name, ConformanceLevel level,
absl::string_view input_json) {
MessageType prototype;
// We don't expect output, but if the program erroneously accepts the protobuf
// we let it send its response as this. We must not leave it unspecified.
Expand All @@ -689,10 +689,10 @@ void BinaryAndJsonConformanceSuiteImpl<MessageType>::ExpectParseFailureForJson(
}

template <typename MessageType>
void BinaryAndJsonConformanceSuiteImpl<MessageType>::
ExpectSerializeFailureForJson(const std::string& test_name,
ConformanceLevel level,
const std::string& text_format) {
void BinaryAndJsonConformanceSuiteImpl<
MessageType>::ExpectSerializeFailureForJson(absl::string_view test_name,
ConformanceLevel level,
absl::string_view text_format) {
MessageType payload_message;
ABSL_CHECK(TextFormat::ParseFromString(text_format, &payload_message))
<< "Failed to parse: " << text_format;
Expand Down
84 changes: 41 additions & 43 deletions conformance/binary_json_conformance_suite.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,15 @@ class BinaryAndJsonConformanceSuite : public ConformanceTestSuite {
}

template <typename MessageType>
void RunValidBinaryProtobufTest(const std::string& test_name,
void RunValidBinaryProtobufTest(absl::string_view test_name,
ConformanceLevel level,
const std::string& input_protobuf,
const std::string& equivalent_text_format);
absl::string_view input_protobuf,
absl::string_view equivalent_text_format);

template <typename MessageType>
void RunValidProtobufTest(const std::string& test_name,
ConformanceLevel level,
const std::string& input_protobuf,
const std::string& equivalent_text_format);
void RunValidProtobufTest(absl::string_view test_name, ConformanceLevel level,
absl::string_view input_protobuf,
absl::string_view equivalent_text_format);

void RunDelimitedFieldTests();

Expand Down Expand Up @@ -88,58 +87,57 @@ class BinaryAndJsonConformanceSuiteImpl {
void RunJsonTestsForValue();
void RunJsonTestsForAny();
void RunJsonTestsForUnknownEnumStringValues();
void RunValidJsonTest(const std::string& test_name, ConformanceLevel level,
const std::string& input_json,
const std::string& equivalent_text_format);
void RunValidJsonTestWithMessage(const std::string& test_name,
void RunValidJsonTest(absl::string_view test_name, ConformanceLevel level,
absl::string_view input_json,
absl::string_view equivalent_text_format);
void RunValidJsonTestWithMessage(absl::string_view test_name,
ConformanceLevel level,
const std::string& input_json,
const std::string& equivalent_text_forma,
absl::string_view input_json,
absl::string_view equivalent_text_forma,
const Message& prototype);
void RunValidJsonTestWithProtobufInput(
const std::string& test_name, ConformanceLevel level,
const MessageType& input, const std::string& equivalent_text_format);
void RunValidJsonIgnoreUnknownTest(const std::string& test_name,
absl::string_view test_name, ConformanceLevel level,
const MessageType& input, absl::string_view equivalent_text_format);
void RunValidJsonIgnoreUnknownTest(absl::string_view test_name,
ConformanceLevel level,
const std::string& input_json,
const std::string& equivalent_text_format);
void RunValidProtobufTest(const std::string& test_name,
ConformanceLevel level,
const std::string& input_protobuf,
const std::string& equivalent_text_format);
void RunValidBinaryProtobufTest(const std::string& test_name,
absl::string_view input_json,
absl::string_view equivalent_text_format);
void RunValidProtobufTest(absl::string_view test_name, ConformanceLevel level,
absl::string_view input_protobuf,
absl::string_view equivalent_text_format);
void RunValidBinaryProtobufTest(absl::string_view test_name,
ConformanceLevel level,
const std::string& input_protobuf);
void RunValidBinaryProtobufTest(const std::string& test_name,
absl::string_view input_protobuf);
void RunValidBinaryProtobufTest(absl::string_view test_name,
ConformanceLevel level,
const std::string& input_protobuf,
const std::string& expected_protobuf);
void RunBinaryPerformanceMergeMessageWithField(
const std::string& test_name, const std::string& field_proto);
absl::string_view input_protobuf,
absl::string_view expected_protobuf);
void RunBinaryPerformanceMergeMessageWithField(absl::string_view test_name,
absl::string_view field_proto);

void RunValidProtobufTestWithMessage(
const std::string& test_name, ConformanceLevel level,
const Message* input, const std::string& equivalent_text_format);
absl::string_view test_name, ConformanceLevel level, const Message* input,
absl::string_view equivalent_text_format);

typedef std::function<bool(const Json::Value&)> Validator;
void RunValidJsonTestWithValidator(const std::string& test_name,
void RunValidJsonTestWithValidator(absl::string_view test_name,
ConformanceLevel level,
const std::string& input_json,
absl::string_view input_json,
const Validator& validator);
void ExpectParseFailureForJson(const std::string& test_name,
void ExpectParseFailureForJson(absl::string_view test_name,
ConformanceLevel level,
const std::string& input_json);
void ExpectSerializeFailureForJson(const std::string& test_name,
absl::string_view input_json);
void ExpectSerializeFailureForJson(absl::string_view test_name,
ConformanceLevel level,
const std::string& text_format);
void ExpectParseFailureForProtoWithProtoVersion(const std::string& proto,
const std::string& test_name,
absl::string_view text_format);
void ExpectParseFailureForProtoWithProtoVersion(absl::string_view proto,
absl::string_view test_name,
ConformanceLevel level);
void ExpectParseFailureForProto(const std::string& proto,
const std::string& test_name,
void ExpectParseFailureForProto(absl::string_view proto,
absl::string_view test_name,
ConformanceLevel level);
void ExpectHardParseFailureForProto(const std::string& proto,
const std::string& test_name,
void ExpectHardParseFailureForProto(absl::string_view proto,
absl::string_view test_name,
ConformanceLevel level);
void TestPrematureEOFForType(google::protobuf::FieldDescriptor::Type type);
void TestIllegalTags();
Expand Down
Loading

0 comments on commit 10c00b8

Please sign in to comment.