HDFS-16250. Refactor AllowSnapshotMock using GMock (#3513)

This commit is contained in:
Gautham B A 2021-10-05 21:46:42 +05:30 committed by GitHub
parent 5b1d594005
commit 84f10fd78b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 26 deletions

View File

@ -17,17 +17,8 @@
under the License. under the License.
*/ */
#include <string>
#include <gtest/gtest.h>
#include "hdfs-allow-snapshot-mock.h" #include "hdfs-allow-snapshot-mock.h"
namespace hdfs::tools::test { namespace hdfs::tools::test {
bool AllowSnapshotMock::HandleHelp() const { return true; } AllowSnapshotMock::~AllowSnapshotMock() {}
bool AllowSnapshotMock::HandlePath(const std::string &path) const {
EXPECT_STREQ(path.c_str(), "a/b/c") << "Expecting the path a/b/c here";
return true;
}
} // namespace hdfs::tools::test } // namespace hdfs::tools::test

View File

@ -21,6 +21,8 @@
#include <string> #include <string>
#include <gmock/gmock.h>
#include "hdfs-allow-snapshot.h" #include "hdfs-allow-snapshot.h"
namespace hdfs::tools::test { namespace hdfs::tools::test {
@ -36,21 +38,15 @@ public:
AllowSnapshotMock(const int argc, char **argv) : AllowSnapshot(argc, argv) {} AllowSnapshotMock(const int argc, char **argv) : AllowSnapshot(argc, argv) {}
// Abiding to the Rule of 5 // Abiding to the Rule of 5
AllowSnapshotMock(const AllowSnapshotMock &) = default; AllowSnapshotMock(const AllowSnapshotMock &) = delete;
AllowSnapshotMock(AllowSnapshotMock &&) = default; AllowSnapshotMock(AllowSnapshotMock &&) = delete;
AllowSnapshotMock &operator=(const AllowSnapshotMock &) = delete; AllowSnapshotMock &operator=(const AllowSnapshotMock &) = delete;
AllowSnapshotMock &operator=(AllowSnapshotMock &&) = delete; AllowSnapshotMock &operator=(AllowSnapshotMock &&) = delete;
~AllowSnapshotMock() override = default; ~AllowSnapshotMock() override;
/** MOCK_METHOD(bool, HandleHelp, (), (const, override));
* {@inheritdoc}
*/
[[nodiscard]] bool HandleHelp() const override;
/** MOCK_METHOD(bool, HandlePath, (const std::string &), (const, override));
* {@inheritdoc}
*/
[[nodiscard]] bool HandlePath(const std::string &path) const override;
}; };
} // namespace hdfs::tools::test } // namespace hdfs::tools::test

View File

@ -25,6 +25,7 @@
#include <string> #include <string>
#include <tuple> #include <tuple>
#include <gmock/gmock.h>
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "hdfs-tool.h" #include "hdfs-tool.h"
@ -81,25 +82,35 @@ TEST_P(HdfsToolNegativeTest, RunTool) {
EXPECT_ANY_THROW({ std::ignore = this->hdfs_tool_->Do(); }); EXPECT_ANY_THROW({ std::ignore = this->hdfs_tool_->Do(); });
} }
template <class T> std::unique_ptr<hdfs::tools::HdfsTool> PassAPath() { template <class T> std::unique_ptr<T> PassAPath() {
constexpr auto argc = 2; constexpr auto argc = 2;
static std::string exe("hdfs_tool_name"); static std::string exe("hdfs_tool_name");
static std::string arg1("a/b/c"); static std::string arg1("a/b/c");
static char *argv[] = {exe.data(), arg1.data()}; static char *argv[] = {exe.data(), arg1.data()};
return std::make_unique<T>(argc, argv);
auto hdfs_tool = std::make_unique<T>(argc, argv);
EXPECT_CALL(*hdfs_tool, HandlePath(arg1))
.Times(1)
.WillOnce(testing::Return(true));
return hdfs_tool;
} }
template <class T> std::unique_ptr<hdfs::tools::HdfsTool> CallHelp() { template <class T> std::unique_ptr<T> CallHelp() {
constexpr auto argc = 2; constexpr auto argc = 2;
static std::string exe("hdfs_tool_name"); static std::string exe("hdfs_tool_name");
static std::string arg1("-h"); static std::string arg1("-h");
static char *argv[] = {exe.data(), arg1.data()}; static char *argv[] = {exe.data(), arg1.data()};
return std::make_unique<T>(argc, argv);
auto hdfs_tool = std::make_unique<T>(argc, argv);
EXPECT_CALL(*hdfs_tool, HandleHelp())
.Times(1)
.WillOnce(testing::Return(true));
return hdfs_tool;
} }
template <class T> std::unique_ptr<hdfs::tools::HdfsTool> Pass2Paths() { template <class T> std::unique_ptr<T> Pass2Paths() {
constexpr auto argc = 3; constexpr auto argc = 3;
static std::string exe("hdfs_tool_name"); static std::string exe("hdfs_tool_name");
static std::string arg1("a/b/c"); static std::string arg1("a/b/c");