Skip to content

Commit

Permalink
[DEV] Make Parser#parse() method static
Browse files Browse the repository at this point in the history
Since a recent rework, it has no member variables anymore.
  • Loading branch information
BoubacarDiene committed Dec 30, 2020
1 parent 95c211d commit c087d44
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 70 deletions.
3 changes: 1 addition & 2 deletions src/plugins/firewall/Rule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ struct Rule::Internal {

const std::string& name;
const std::vector<std::string>& commands;
const Parser parser;

explicit Internal(const IExecutor& providedExecutor,
const std::string& providedName,
Expand All @@ -62,7 +61,7 @@ void Rule::applyCommands() const
{
for (const std::string& command : m_internal->commands) {
const std::unique_ptr<Parser::Command, Parser::CommandDeleter>& parsedCommand
= m_internal->parser.parse(command);
= Parser::parse(command);

const IExecutor::ProgramParams params
= {parsedCommand->pathname, parsedCommand->argv, nullptr};
Expand Down
3 changes: 1 addition & 2 deletions src/plugins/network/interface/Interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ using namespace utils::command;

struct Interface::Internal {
const IExecutor& executor;
const Parser parser;

explicit Internal(const IExecutor& providedExecutor) : executor(providedExecutor)
{}
Expand All @@ -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};
Expand Down
43 changes: 20 additions & 23 deletions src/utils/command/parser/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> splitString(const std::string& stringToSplit,
char delimiter = ' ')
{
std::vector<std::string> 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<std::string> splitString(const std::string& stringToSplit,
char delimiter = ' ')
{
std::vector<std::string> 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)
{
Expand All @@ -64,16 +66,11 @@ void Parser::CommandDeleter::operator()(Command* command)
delete command;
}

Parser::Parser() : m_internal(std::make_unique<Internal>()) {}

Parser::~Parser() = default;

std::unique_ptr<Parser::Command, Parser::CommandDeleter>
Parser::parse(const std::string& commandToParse, char delimiter) const
Parser::parse(const std::string& commandToParse, char delimiter)
{
/* Split command into substrings */
std::vector<std::string> results
= m_internal->splitString(commandToParse, delimiter);
std::vector<std::string> results = splitString(commandToParse, delimiter);

std::size_t nbResults = results.size();
std::size_t oneResultSize = 0llu;
Expand Down
32 changes: 2 additions & 30 deletions src/utils/command/parser/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
* @date April 2020
*/
Expand Down Expand Up @@ -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
*
Expand All @@ -103,12 +79,8 @@ class Parser {
*
* @see Command
*/
[[nodiscard]] std::unique_ptr<Command, CommandDeleter>
parse(const std::string& commandToParse, char delimiter = ' ') const;

private:
struct Internal;
std::unique_ptr<Internal> m_internal;
[[nodiscard]] static std::unique_ptr<Command, CommandDeleter>
parse(const std::string& commandToParse, char delimiter = ' ');
};

}
Expand Down
2 changes: 1 addition & 1 deletion test/plugins/firewall/RuleTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down
2 changes: 1 addition & 1 deletion test/plugins/network/InterfaceTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down
2 changes: 1 addition & 1 deletion test/plugins/network/NetworkTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down
14 changes: 4 additions & 10 deletions test/utils/command/ParserTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,22 @@ 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);
ASSERT_STREQ(result->argv[0], command.c_str());
}

// 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);
Expand Down

0 comments on commit c087d44

Please sign in to comment.