-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#ifndef THIRD_PARTY_LLVM_PROPELLER_TEXT_PROTO_FLAG_H | ||
#define THIRD_PARTY_LLVM_PROPELLER_TEXT_PROTO_FLAG_H | ||
|
||
#include <string> | ||
|
||
#include "absl/log/check.h" | ||
#include "absl/strings/string_view.h" | ||
#include "google/protobuf/text_format.h" | ||
|
||
namespace propeller { | ||
|
||
// A wrapper around a proto message that can be used as a flag. | ||
template <typename Proto> | ||
struct TextProtoFlag { | ||
Proto message; | ||
}; | ||
|
||
template <typename Proto> | ||
inline bool AbslParseFlag(absl::string_view text, TextProtoFlag<Proto>* flag, | ||
std::string* err) { | ||
return google::protobuf::TextFormat::ParseFromString(text, &flag->message); | ||
} | ||
|
||
template <typename Proto> | ||
inline std::string AbslUnparseFlag(const TextProtoFlag<Proto>& flag) { | ||
std::string text_proto; | ||
CHECK(google::protobuf::TextFormat::PrintToString(flag.message, &text_proto)); | ||
return text_proto; | ||
} | ||
} // namespace propeller | ||
|
||
#endif // THIRD_PARTY_LLVM_PROPELLER_TEXT_PROTO_FLAG_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#include "propeller/text_proto_flag.h" | ||
|
||
#include <string> | ||
|
||
#include "absl/flags/flag.h" | ||
#include "base/commandlineflags.h" | ||
#include "gmock/gmock.h" | ||
#include "gtest/gtest.h" | ||
#include "propeller/parse_text_proto.h" | ||
#include "propeller/propeller_options.pb.h" | ||
#include "propeller/status_testing_macros.h" | ||
|
||
ABSL_FLAG(propeller::TextProtoFlag<propeller::PropellerOptions>, | ||
propeller_options, {}, | ||
"Test flag for propeller::PropellerOptions encoded as a TextProto."); | ||
|
||
namespace { | ||
|
||
using ::propeller::PropellerOptions; | ||
using ::propeller_testing::ParseTextProtoOrDie; | ||
using ::testing::EqualsProto; | ||
|
||
TEST(TextProtoFlagTest, ValidParse) { | ||
ASSERT_FALSE(SetCommandLineOption("propeller_options", | ||
"binary_name: '/path/to/binary'") | ||
.empty()); | ||
|
||
EXPECT_THAT(absl::GetFlag(FLAGS_propeller_options).message, | ||
EqualsProto(R"pb(binary_name: "/path/to/binary")pb")); | ||
} | ||
|
||
TEST(TextProtoFlagTest, InvalidParse) { | ||
ASSERT_TRUE(SetCommandLineOption("propeller_options", "UNKNOWN_FIELD : true") | ||
.empty()); | ||
|
||
// Since the parsing failed, the flag should be empty. | ||
EXPECT_THAT(absl::GetFlag(FLAGS_propeller_options).message, | ||
EqualsProto(PropellerOptions())); | ||
} | ||
|
||
TEST(TextProtoFlagTest, SetFlag) { | ||
// Clear the flag. | ||
ASSERT_FALSE(SetCommandLineOption("propeller_options", "").empty()); | ||
|
||
absl::SetFlag(&FLAGS_propeller_options, | ||
propeller::TextProtoFlag<PropellerOptions>{ | ||
.message = ParseTextProtoOrDie( | ||
R"pb(binary_name: "/path/to/binary")pb")}); | ||
EXPECT_THAT(absl::GetFlag(FLAGS_propeller_options).message, | ||
EqualsProto(R"pb(binary_name: "/path/to/binary")pb")); | ||
} | ||
} // namespace |