Making a test nicer: procrastination and nits. (#1175)
parent
876a41f06f
commit
87370371ec
|
@ -36,49 +36,19 @@ using ::google::protobuf::util::MessageDifferencer;
|
||||||
using ::testing::Eq;
|
using ::testing::Eq;
|
||||||
using ::testing::Not;
|
using ::testing::Not;
|
||||||
|
|
||||||
static constexpr char kSerializationHeaderProtoString[] = R"PROTO(
|
constexpr char kSerializationHeaderProtoString[] = "format_version: 1";
|
||||||
format_version: 1
|
constexpr char kUnsupportedSerializationHeaderProtoString[] =
|
||||||
)PROTO";
|
"format_version: 123";
|
||||||
|
constexpr char kPoseGraphProtoString[] = "pose_graph {}";
|
||||||
static constexpr char kUnsupportedSerializationHeaderProtoString[] = R"PROTO(
|
constexpr char kAllTrajectoryBuilderOptionsProtoString[] =
|
||||||
format_version: 123
|
"all_trajectory_builder_options {}";
|
||||||
)PROTO";
|
constexpr char kSubmapProtoString[] = "submap {}";
|
||||||
|
constexpr char kNodeProtoString[] = "node {}";
|
||||||
static constexpr char kPoseGraphProtoString[] = R"PROTO(
|
constexpr char kTrajectoryDataProtoString[] = "trajectory_data {}";
|
||||||
pose_graph {}
|
constexpr char kImuDataProtoString[] = "imu_data {}";
|
||||||
)PROTO";
|
constexpr char kOdometryDataProtoString[] = "odometry_data {}";
|
||||||
|
constexpr char kFixedFramePoseDataProtoString[] = "fixed_frame_pose_data {}";
|
||||||
static constexpr char kAllTrajectoryBuilderOptionsProtoString[] = R"PROTO(
|
constexpr char kLandmarkDataProtoString[] = "landmark_data {}";
|
||||||
all_trajectory_builder_options {}
|
|
||||||
)PROTO";
|
|
||||||
|
|
||||||
static constexpr char kSubmapProtoString[] = R"PROTO(
|
|
||||||
submap {}
|
|
||||||
)PROTO";
|
|
||||||
|
|
||||||
static constexpr char kNodeProtoString[] = R"PROTO(
|
|
||||||
node {}
|
|
||||||
)PROTO";
|
|
||||||
|
|
||||||
static constexpr char kTrajectoryDataProtoString[] = R"PROTO(
|
|
||||||
trajectory_data {}
|
|
||||||
)PROTO";
|
|
||||||
|
|
||||||
static constexpr char kImuDataProtoString[] = R"PROTO(
|
|
||||||
imu_data {}
|
|
||||||
)PROTO";
|
|
||||||
|
|
||||||
static constexpr char kOdometryDataProtoString[] = R"PROTO(
|
|
||||||
odometry_data {}
|
|
||||||
)PROTO";
|
|
||||||
|
|
||||||
static constexpr char kFixedFramePoseDataProtoString[] = R"PROTO(
|
|
||||||
fixed_frame_pose_data {}
|
|
||||||
)PROTO";
|
|
||||||
|
|
||||||
static constexpr char kLandmarkDataProtoString[] = R"PROTO(
|
|
||||||
landmark_data {}
|
|
||||||
)PROTO";
|
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
T ProtoFromStringOrDie(const std::string& proto_string) {
|
T ProtoFromStringOrDie(const std::string& proto_string) {
|
||||||
|
@ -87,45 +57,41 @@ T ProtoFromStringOrDie(const std::string& proto_string) {
|
||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
class ProtoStreamDeserializerTest : public ::testing::Test {
|
||||||
std::unique_ptr<T> ProtoUPtrFromStringOrDie(const std::string& proto_string) {
|
protected:
|
||||||
return make_unique<T>(ProtoFromStringOrDie<T>(proto_string));
|
void InitializeProtoReader(
|
||||||
}
|
const std::string& header_textpb,
|
||||||
|
const std::initializer_list<std::string>& data_textpbs) {
|
||||||
std::unique_ptr<InMemoryProtoStreamReader>
|
|
||||||
CreateInMemoryReaderFromTextProtos() {
|
|
||||||
std::queue<std::unique_ptr<Message>> proto_queue;
|
std::queue<std::unique_ptr<Message>> proto_queue;
|
||||||
|
proto_queue.emplace(make_unique<SerializationHeader>(
|
||||||
|
ProtoFromStringOrDie<SerializationHeader>(header_textpb)));
|
||||||
|
for (const std::string& data_textpb : data_textpbs) {
|
||||||
|
proto_queue.emplace(make_unique<SerializedData>(
|
||||||
|
ProtoFromStringOrDie<SerializedData>(data_textpb)));
|
||||||
|
}
|
||||||
|
reader_ = make_unique<InMemoryProtoStreamReader>(std::move(proto_queue));
|
||||||
|
}
|
||||||
|
|
||||||
proto_queue.emplace(ProtoUPtrFromStringOrDie<SerializationHeader>(
|
std::unique_ptr<InMemoryProtoStreamReader> reader_;
|
||||||
kSerializationHeaderProtoString));
|
};
|
||||||
proto_queue.emplace(
|
|
||||||
ProtoUPtrFromStringOrDie<SerializedData>(kPoseGraphProtoString));
|
|
||||||
proto_queue.emplace(ProtoUPtrFromStringOrDie<SerializedData>(
|
|
||||||
kAllTrajectoryBuilderOptionsProtoString));
|
|
||||||
proto_queue.emplace(
|
|
||||||
ProtoUPtrFromStringOrDie<SerializedData>(kSubmapProtoString));
|
|
||||||
proto_queue.emplace(
|
|
||||||
ProtoUPtrFromStringOrDie<SerializedData>(kNodeProtoString));
|
|
||||||
proto_queue.emplace(
|
|
||||||
ProtoUPtrFromStringOrDie<SerializedData>(kTrajectoryDataProtoString));
|
|
||||||
proto_queue.emplace(
|
|
||||||
ProtoUPtrFromStringOrDie<SerializedData>(kImuDataProtoString));
|
|
||||||
proto_queue.emplace(
|
|
||||||
ProtoUPtrFromStringOrDie<SerializedData>(kOdometryDataProtoString));
|
|
||||||
proto_queue.emplace(
|
|
||||||
ProtoUPtrFromStringOrDie<SerializedData>(kFixedFramePoseDataProtoString));
|
|
||||||
proto_queue.emplace(
|
|
||||||
ProtoUPtrFromStringOrDie<SerializedData>(kLandmarkDataProtoString));
|
|
||||||
return make_unique<InMemoryProtoStreamReader>(std::move(proto_queue));
|
|
||||||
}
|
|
||||||
|
|
||||||
// This test checks if the serialization works.
|
// This test checks if the serialization works.
|
||||||
TEST(ProtoStreamDeserializerTest, WorksOnGoldenTextStream) {
|
TEST_F(ProtoStreamDeserializerTest, WorksOnGoldenTextStream) {
|
||||||
// Load text proto into in_memory_reader.
|
// Load text proto into in_memory_reader.
|
||||||
std::unique_ptr<InMemoryProtoStreamReader> reader =
|
InitializeProtoReader(kSerializationHeaderProtoString,
|
||||||
CreateInMemoryReaderFromTextProtos();
|
{
|
||||||
|
kPoseGraphProtoString,
|
||||||
|
kAllTrajectoryBuilderOptionsProtoString,
|
||||||
|
kSubmapProtoString,
|
||||||
|
kNodeProtoString,
|
||||||
|
kTrajectoryDataProtoString,
|
||||||
|
kImuDataProtoString,
|
||||||
|
kOdometryDataProtoString,
|
||||||
|
kFixedFramePoseDataProtoString,
|
||||||
|
kLandmarkDataProtoString,
|
||||||
|
});
|
||||||
|
|
||||||
io::ProtoStreamDeserializer deserializer(reader.get());
|
io::ProtoStreamDeserializer deserializer(reader_.get());
|
||||||
|
|
||||||
EXPECT_TRUE(MessageDifferencer::Equals(
|
EXPECT_TRUE(MessageDifferencer::Equals(
|
||||||
deserializer.header(), ProtoFromStringOrDie<SerializationHeader>(
|
deserializer.header(), ProtoFromStringOrDie<SerializationHeader>(
|
||||||
|
@ -179,16 +145,12 @@ TEST(ProtoStreamDeserializerTest, WorksOnGoldenTextStream) {
|
||||||
ProtoFromStringOrDie<SerializedData>(kLandmarkDataProtoString)));
|
ProtoFromStringOrDie<SerializedData>(kLandmarkDataProtoString)));
|
||||||
|
|
||||||
EXPECT_FALSE(deserializer.ReadNextSerializedData(&serialized_data));
|
EXPECT_FALSE(deserializer.ReadNextSerializedData(&serialized_data));
|
||||||
EXPECT_TRUE(reader->eof());
|
EXPECT_TRUE(reader_->eof());
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(ProtoStreamDeserializerDeathTests, FailsIfVersionNotSupported) {
|
TEST_F(ProtoStreamDeserializerTest, FailsIfVersionNotSupported) {
|
||||||
std::queue<std::unique_ptr<Message>> proto_queue;
|
InitializeProtoReader(kUnsupportedSerializationHeaderProtoString, {});
|
||||||
proto_queue.emplace(ProtoUPtrFromStringOrDie<SerializationHeader>(
|
EXPECT_DEATH(common::make_unique<ProtoStreamDeserializer>(reader_.get()),
|
||||||
kUnsupportedSerializationHeaderProtoString));
|
|
||||||
InMemoryProtoStreamReader reader(std::move(proto_queue));
|
|
||||||
|
|
||||||
EXPECT_DEATH(common::make_unique<ProtoStreamDeserializer>(&reader),
|
|
||||||
"Unsupported serialization format");
|
"Unsupported serialization format");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue