From 49d89d07594428d377f9630f2990e99d0f24627a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20Sch=C3=BCtte?= Date: Fri, 26 Jan 2018 17:56:36 +0100 Subject: [PATCH] Move mocks out of client_test.cc into testing/ folder. (#854) --- cartographer_grpc/client_server_test.cc | 85 ++++--------------- cartographer_grpc/testing/mock_map_builder.h | 59 +++++++++++++ .../testing/mock_trajectory_builder.h | 61 +++++++++++++ 3 files changed, 138 insertions(+), 67 deletions(-) create mode 100644 cartographer_grpc/testing/mock_map_builder.h create mode 100644 cartographer_grpc/testing/mock_trajectory_builder.h diff --git a/cartographer_grpc/client_server_test.cc b/cartographer_grpc/client_server_test.cc index 3ca6dc9..e63fdc6 100644 --- a/cartographer_grpc/client_server_test.cc +++ b/cartographer_grpc/client_server_test.cc @@ -22,6 +22,8 @@ #include "cartographer_grpc/map_builder_server.h" #include "cartographer_grpc/map_builder_server_options.h" #include "cartographer_grpc/mapping/map_builder_stub.h" +#include "cartographer_grpc/testing/mock_map_builder.h" +#include "cartographer_grpc/testing/mock_trajectory_builder.h" #include "glog/logging.h" #include "gmock/gmock.h" #include "gtest/gtest.h" @@ -42,58 +44,6 @@ constexpr double kDuration = 4.; // Seconds. constexpr double kTimeStep = 0.1; // Seconds. constexpr double kTravelDistance = 1.2; // Meters. -class MockMapBuilder : public cartographer::mapping::MapBuilderInterface { - public: - MOCK_METHOD3(AddTrajectoryBuilder, - int(const std::set& expected_sensor_ids, - const cartographer::mapping::proto::TrajectoryBuilderOptions& - trajectory_options, - LocalSlamResultCallback local_slam_result_callback)); - MOCK_METHOD0(AddTrajectoryForDeserialization, int()); - MOCK_CONST_METHOD1(GetTrajectoryBuilder, - TrajectoryBuilderInterface*(int trajectory_id)); - MOCK_METHOD1(FinishTrajectory, void(int trajectory_id)); - MOCK_METHOD2( - SubmapToProto, - std::string(const cartographer::mapping::SubmapId&, - cartographer::mapping::proto::SubmapQuery::Response*)); - MOCK_METHOD1(SerializeState, - void(cartographer::io::ProtoStreamWriterInterface*)); - MOCK_METHOD1(LoadMap, void(cartographer::io::ProtoStreamReaderInterface*)); - MOCK_CONST_METHOD0(num_trajectory_builders, int()); - MOCK_METHOD0(pose_graph, PoseGraphInterface*()); -}; - -class MockTrajectoryBuilder - : public cartographer::mapping::TrajectoryBuilderInterface { - public: - MockTrajectoryBuilder() = default; - ~MockTrajectoryBuilder() override = default; - - MOCK_METHOD2(AddSensorData, - void(const std::string&, - const cartographer::sensor::TimedPointCloudData&)); - MOCK_METHOD2(AddSensorData, - void(const std::string&, const cartographer::sensor::ImuData&)); - MOCK_METHOD2(AddSensorData, void(const std::string&, - const cartographer::sensor::OdometryData&)); - MOCK_METHOD2(AddSensorData, - void(const std::string&, - const cartographer::sensor::FixedFramePoseData&)); - MOCK_METHOD2(AddSensorData, void(const std::string&, - const cartographer::sensor::LandmarkData&)); - - // Some of the platforms we run on may ship with a version of gmock which does - // not yet support move-only types. - MOCK_METHOD1(DoAddLocalSlamResultData, - void(cartographer::mapping::LocalSlamResultData*)); - void AddLocalSlamResultData( - std::unique_ptr - local_slam_result_data) override { - DoAddLocalSlamResultData(local_slam_result_data.get()); - } -}; - class ClientServerTest : public ::testing::Test { protected: void SetUp() override { @@ -145,13 +95,14 @@ class ClientServerTest : public ::testing::Test { } void InitializeServerWithMockMapBuilder() { - auto mock_map_builder = cartographer::common::make_unique(); + auto mock_map_builder = + cartographer::common::make_unique(); mock_map_builder_ = mock_map_builder.get(); server_ = cartographer::common::make_unique( map_builder_server_options_, std::move(mock_map_builder)); EXPECT_TRUE(server_ != nullptr); mock_trajectory_builder_ = - cartographer::common::make_unique(); + cartographer::common::make_unique(); } void InitializeStub() { @@ -167,8 +118,8 @@ class ClientServerTest : public ::testing::Test { } proto::MapBuilderServerOptions map_builder_server_options_; - MockMapBuilder* mock_map_builder_; - std::unique_ptr mock_trajectory_builder_; + testing::MockMapBuilder* mock_map_builder_; + std::unique_ptr mock_trajectory_builder_; cartographer::mapping::proto::TrajectoryBuilderOptions trajectory_builder_options_; std::unique_ptr server_; @@ -203,10 +154,10 @@ TEST_F(ClientServerTest, AddTrajectoryBuilderWithMock) { std::set expected_sensor_ids = {kSensorId}; EXPECT_CALL( *mock_map_builder_, - AddTrajectoryBuilder(testing::ContainerEq(expected_sensor_ids), _, _)) - .WillOnce(testing::Return(3)); + AddTrajectoryBuilder(::testing::ContainerEq(expected_sensor_ids), _, _)) + .WillOnce(::testing::Return(3)); EXPECT_CALL(*mock_map_builder_, GetTrajectoryBuilder(_)) - .WillRepeatedly(testing::Return(nullptr)); + .WillRepeatedly(::testing::Return(nullptr)); int trajectory_id = stub_->AddTrajectoryBuilder( expected_sensor_ids, trajectory_builder_options_, nullptr); EXPECT_EQ(trajectory_id, 3); @@ -240,23 +191,23 @@ TEST_F(ClientServerTest, AddSensorDataWithMock) { std::set expected_sensor_ids = {kSensorId}; EXPECT_CALL( *mock_map_builder_, - AddTrajectoryBuilder(testing::ContainerEq(expected_sensor_ids), _, _)) - .WillOnce(testing::Return(3)); + AddTrajectoryBuilder(::testing::ContainerEq(expected_sensor_ids), _, _)) + .WillOnce(::testing::Return(3)); int trajectory_id = stub_->AddTrajectoryBuilder( expected_sensor_ids, trajectory_builder_options_, nullptr); EXPECT_EQ(trajectory_id, 3); EXPECT_CALL(*mock_map_builder_, GetTrajectoryBuilder(_)) - .WillRepeatedly(testing::Return(mock_trajectory_builder_.get())); + .WillRepeatedly(::testing::Return(mock_trajectory_builder_.get())); cartographer::mapping::TrajectoryBuilderInterface* trajectory_stub = stub_->GetTrajectoryBuilder(trajectory_id); cartographer::sensor::ImuData imu_data{ cartographer::common::FromUniversal(42), Eigen::Vector3d(0., 0., 9.8), Eigen::Vector3d::Zero()}; - EXPECT_CALL( - *mock_trajectory_builder_, - AddSensorData(testing::StrEq(kSensorId.id), - testing::Matcher(_))) - .WillOnce(testing::Return()); + EXPECT_CALL(*mock_trajectory_builder_, + AddSensorData( + ::testing::StrEq(kSensorId.id), + ::testing::Matcher(_))) + .WillOnce(::testing::Return()); trajectory_stub->AddSensorData(kSensorId.id, imu_data); EXPECT_CALL(*mock_map_builder_, FinishTrajectory(trajectory_id)); stub_->FinishTrajectory(trajectory_id); diff --git a/cartographer_grpc/testing/mock_map_builder.h b/cartographer_grpc/testing/mock_map_builder.h new file mode 100644 index 0000000..1906e2e --- /dev/null +++ b/cartographer_grpc/testing/mock_map_builder.h @@ -0,0 +1,59 @@ +/* + * Copyright 2018 The Cartographer Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef CARTOGRAPHER_GRPC_TESTING_MOCK_MAP_BUILDER_H +#define CARTOGRAPHER_GRPC_TESTING_MOCK_MAP_BUILDER_H + +#include "cartographer/mapping/map_builder_interface.h" +#include "cartographer/mapping/trajectory_builder_interface.h" +#include "glog/logging.h" +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +using testing::_; + +namespace cartographer_grpc { +namespace testing { + +class MockMapBuilder : public cartographer::mapping::MapBuilderInterface { + public: + MOCK_METHOD3( + AddTrajectoryBuilder, + int(const std::set &expected_sensor_ids, + const cartographer::mapping::proto::TrajectoryBuilderOptions + &trajectory_options, + cartographer::mapping::MapBuilderInterface::LocalSlamResultCallback + local_slam_result_callback)); + MOCK_METHOD0(AddTrajectoryForDeserialization, int()); + MOCK_CONST_METHOD1( + GetTrajectoryBuilder, + cartographer::mapping::TrajectoryBuilderInterface *(int trajectory_id)); + MOCK_METHOD1(FinishTrajectory, void(int trajectory_id)); + MOCK_METHOD2( + SubmapToProto, + std::string(const cartographer::mapping::SubmapId &, + cartographer::mapping::proto::SubmapQuery::Response *)); + MOCK_METHOD1(SerializeState, + void(cartographer::io::ProtoStreamWriterInterface *)); + MOCK_METHOD1(LoadMap, void(cartographer::io::ProtoStreamReaderInterface *)); + MOCK_CONST_METHOD0(num_trajectory_builders, int()); + MOCK_METHOD0(pose_graph, cartographer::mapping::PoseGraphInterface *()); +}; + +} // namespace testing +} // namespace cartographer_grpc + +#endif // CARTOGRAPHER_GRPC_TESTING_MOCK_MAP_BUILDER_H diff --git a/cartographer_grpc/testing/mock_trajectory_builder.h b/cartographer_grpc/testing/mock_trajectory_builder.h new file mode 100644 index 0000000..80591df --- /dev/null +++ b/cartographer_grpc/testing/mock_trajectory_builder.h @@ -0,0 +1,61 @@ +/* + * Copyright 2018 The Cartographer Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef CARTOGRAPHER_GRPC_TESTING_MOCK_TRAJECTORY_BUILDER_CONTEXT_H +#define CARTOGRAPHER_GRPC_TESTING_MOCK_TRAJECTORY_BUILDER_CONTEXT_H + +#include "cartographer/mapping/trajectory_builder_interface.h" +#include "glog/logging.h" +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +namespace cartographer_grpc { +namespace testing { + +class MockTrajectoryBuilder + : public cartographer::mapping::TrajectoryBuilderInterface { + public: + MockTrajectoryBuilder() = default; + ~MockTrajectoryBuilder() override = default; + + MOCK_METHOD2(AddSensorData, + void(const std::string &, + const cartographer::sensor::TimedPointCloudData &)); + MOCK_METHOD2(AddSensorData, void(const std::string &, + const cartographer::sensor::ImuData &)); + MOCK_METHOD2(AddSensorData, void(const std::string &, + const cartographer::sensor::OdometryData &)); + MOCK_METHOD2(AddSensorData, + void(const std::string &, + const cartographer::sensor::FixedFramePoseData &)); + MOCK_METHOD2(AddSensorData, void(const std::string &, + const cartographer::sensor::LandmarkData &)); + + // Some of the platforms we run on may ship with a version of gmock which does + // not yet support move-only types. + MOCK_METHOD1(DoAddLocalSlamResultData, + void(cartographer::mapping::LocalSlamResultData *)); + void AddLocalSlamResultData( + std::unique_ptr + local_slam_result_data) override { + DoAddLocalSlamResultData(local_slam_result_data.get()); + } +}; + +} // namespace testing +} // namespace cartographer_grpc + +#endif // CARTOGRAPHER_GRPC_TESTING_MOCK_TRAJECTORY_BUILDER_CONTEXT_H