From c087d4431eb63b2f5a939f23cba823b280943a7e Mon Sep 17 00:00:00 2001 From: BoubacarDiene Date: Wed, 30 Dec 2020 23:05:51 +0100 Subject: [PATCH] [DEV] Make Parser#parse() method static Since a recent rework, it has no member variables anymore. --- src/plugins/firewall/Rule.cpp | 3 +- src/plugins/network/interface/Interface.cpp | 3 +- src/utils/command/parser/Parser.cpp | 43 ++++++++++----------- src/utils/command/parser/Parser.h | 32 +-------------- test/plugins/firewall/RuleTest.cpp | 2 +- test/plugins/network/InterfaceTest.cpp | 2 +- test/plugins/network/NetworkTest.cpp | 2 +- test/utils/command/ParserTest.cpp | 14 ++----- 8 files changed, 31 insertions(+), 70 deletions(-) diff --git a/src/plugins/firewall/Rule.cpp b/src/plugins/firewall/Rule.cpp index a3a067f..92f7bc6 100644 --- a/src/plugins/firewall/Rule.cpp +++ b/src/plugins/firewall/Rule.cpp @@ -39,7 +39,6 @@ struct Rule::Internal { const std::string& name; const std::vector& commands; - const Parser parser; explicit Internal(const IExecutor& providedExecutor, const std::string& providedName, @@ -62,7 +61,7 @@ void Rule::applyCommands() const { for (const std::string& command : m_internal->commands) { const std::unique_ptr& parsedCommand - = m_internal->parser.parse(command); + = Parser::parse(command); const IExecutor::ProgramParams params = {parsedCommand->pathname, parsedCommand->argv, nullptr}; diff --git a/src/plugins/network/interface/Interface.cpp b/src/plugins/network/interface/Interface.cpp index 4551810..65ad971 100644 --- a/src/plugins/network/interface/Interface.cpp +++ b/src/plugins/network/interface/Interface.cpp @@ -36,7 +36,6 @@ using namespace utils::command; struct Interface::Internal { const IExecutor& executor; - const Parser parser; explicit Internal(const IExecutor& providedExecutor) : executor(providedExecutor) {} @@ -50,7 +49,7 @@ Interface::~Interface() = default; void Interface::applyCommand(const std::string& command) const { - const auto& parsedCommand = m_internal->parser.parse(command); + const auto& parsedCommand = Parser::parse(command); const IExecutor::ProgramParams params = {parsedCommand->pathname, parsedCommand->argv, nullptr}; diff --git a/src/utils/command/parser/Parser.cpp b/src/utils/command/parser/Parser.cpp index 6145467..179df74 100644 --- a/src/utils/command/parser/Parser.cpp +++ b/src/utils/command/parser/Parser.cpp @@ -34,23 +34,25 @@ using namespace utils::command; -struct Parser::Internal { - /* Provided "delimiter", return an array of strings resulting from the - * splitting of "stringToSplit" into substrings */ - std::vector splitString(const std::string& stringToSplit, - char delimiter = ' ') - { - std::vector results; - std::string substring; - - std::istringstream iss(stringToSplit); - while (std::getline(iss, substring, delimiter)) { - results.push_back(substring); - } - - return results; +namespace { + +/* Provided "delimiter", return an array of strings resulting from the + * splitting of "stringToSplit" into substrings */ +std::vector splitString(const std::string& stringToSplit, + char delimiter = ' ') +{ + std::vector results; + std::string substring; + + std::istringstream iss(stringToSplit); + while (std::getline(iss, substring, delimiter)) { + results.push_back(substring); } -}; + + return results; +} + +} void Parser::CommandDeleter::operator()(Command* command) { @@ -64,16 +66,11 @@ void Parser::CommandDeleter::operator()(Command* command) delete command; } -Parser::Parser() : m_internal(std::make_unique()) {} - -Parser::~Parser() = default; - std::unique_ptr - Parser::parse(const std::string& commandToParse, char delimiter) const + Parser::parse(const std::string& commandToParse, char delimiter) { /* Split command into substrings */ - std::vector results - = m_internal->splitString(commandToParse, delimiter); + std::vector results = splitString(commandToParse, delimiter); std::size_t nbResults = results.size(); std::size_t oneResultSize = 0llu; diff --git a/src/utils/command/parser/Parser.h b/src/utils/command/parser/Parser.h index 384e068..0a46319 100644 --- a/src/utils/command/parser/Parser.h +++ b/src/utils/command/parser/Parser.h @@ -40,12 +40,6 @@ namespace utils::command { * @brief A helper class to parse a string representing a command so * as to create a real command that can be passed to @ref IExecutor. * - * @note Copy contructor, copy-assignment operator, move constructor and - * move-assignment operator are defined to be compliant with the - * "Rule of five" - * - * @see https://en.cppreference.com/w/cpp/language/rule_of_three - * * @author Boubacar DIENE * @date April 2020 */ @@ -74,24 +68,6 @@ class Parser { void operator()(Command* command); }; - /* Class constructor */ - Parser(); - - /** Class destructor */ - ~Parser(); - - /** Class copy constructor */ - Parser(const Parser&) = delete; - - /** Class copy-assignment operator */ - Parser& operator=(const Parser&) = delete; - - /** Class move constructor */ - Parser(Parser&&) = delete; - - /** Class move-assignment operator */ - Parser& operator=(Parser&&) = delete; - /** * @brief Parse the given string to create a command * @@ -103,12 +79,8 @@ class Parser { * * @see Command */ - [[nodiscard]] std::unique_ptr - parse(const std::string& commandToParse, char delimiter = ' ') const; - -private: - struct Internal; - std::unique_ptr m_internal; + [[nodiscard]] static std::unique_ptr + parse(const std::string& commandToParse, char delimiter = ' '); }; } diff --git a/test/plugins/firewall/RuleTest.cpp b/test/plugins/firewall/RuleTest.cpp index 3fb25db..68389e1 100644 --- a/test/plugins/firewall/RuleTest.cpp +++ b/test/plugins/firewall/RuleTest.cpp @@ -68,7 +68,7 @@ TEST_F(RuleTestFixture, shouldCallExecutorWithExpectedValues) // Parser is deterministic meaning that for the same input, it will // always produce the same output so it's fine using it. - const auto& parsedCommand = Parser().parse(commands[0]); + const auto& parsedCommand = Parser::parse(commands[0]); const IExecutor::ProgramParams expectedParams = {parsedCommand->pathname, parsedCommand->argv, nullptr}; diff --git a/test/plugins/network/InterfaceTest.cpp b/test/plugins/network/InterfaceTest.cpp index ec3b57e..fc8e4f8 100644 --- a/test/plugins/network/InterfaceTest.cpp +++ b/test/plugins/network/InterfaceTest.cpp @@ -56,7 +56,7 @@ TEST_F(InterfaceTestFixture, shouldCallExecutorWithExpectedValues) // Parser is deterministic meaning that for the same input, it will // always produce the same output so it's fine using it. - const auto& parsedCommand = Parser().parse(command); + const auto& parsedCommand = Parser::parse(command); const IExecutor::ProgramParams expectedParams = {parsedCommand->pathname, parsedCommand->argv, nullptr}; diff --git a/test/plugins/network/NetworkTest.cpp b/test/plugins/network/NetworkTest.cpp index 227a889..130a210 100644 --- a/test/plugins/network/NetworkTest.cpp +++ b/test/plugins/network/NetworkTest.cpp @@ -127,7 +127,7 @@ TEST_F(NetworkTestFixture, applyInterfaceCommandsShouldNotFailWithValidParameter // Parser is deterministic meaning that for the same input, it will // always produce the same output so it's fine using it. - const auto& parsedCommand = Parser().parse(interfaceCommands[0]); + const auto& parsedCommand = Parser::parse(interfaceCommands[0]); const IExecutor::ProgramParams expectedParams = {parsedCommand->pathname, parsedCommand->argv, nullptr}; diff --git a/test/utils/command/ParserTest.cpp b/test/utils/command/ParserTest.cpp index 034abbf..d9a056f 100644 --- a/test/utils/command/ParserTest.cpp +++ b/test/utils/command/ParserTest.cpp @@ -34,17 +34,11 @@ using namespace utils::command; namespace { -class ParserTestFixture : public ::testing::Test { - -protected: - Parser m_parser; -}; - // NOLINTNEXTLINE(cert-err58-cpp, hicpp-special-member-functions) -TEST_F(ParserTestFixture, returnTheSameStringIfInvalidDelimiter) +TEST(ParserTest, returnTheSameStringIfInvalidDelimiter) { const std::string command("/sbin/iptables -P OUTPUT ACCEPT"); - auto result = m_parser.parse(command, ';'); + auto result = Parser::parse(command, ';'); ASSERT_STREQ(result->pathname, command.c_str()); ASSERT_EQ(result->argc, 1); @@ -52,10 +46,10 @@ TEST_F(ParserTestFixture, returnTheSameStringIfInvalidDelimiter) } // NOLINTNEXTLINE(cert-err58-cpp, hicpp-special-member-functions) -TEST_F(ParserTestFixture, returnSplittedStringIfValidDelimiter) +TEST(ParserTest, returnSplittedStringIfValidDelimiter) { const std::string command("/sbin/iptables -P OUTPUT ACCEPT"); - auto result = m_parser.parse(command); + auto result = Parser::parse(command); ASSERT_STREQ(result->pathname, "/sbin/iptables"); ASSERT_EQ(result->argc, 4);