Skip to content

Commit

Permalink
Added cmd parser config key test + store config keys in map.
Browse files Browse the repository at this point in the history
  • Loading branch information
Peguen committed May 16, 2024
1 parent 0dc7398 commit e454368
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ecal/core/include/ecal/types/user_arg_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include <string>
#include <vector>
#include <map>

namespace eCAL
{
Expand All @@ -37,5 +38,8 @@ namespace eCAL
std::string specified_config{};
bool dump_config{};
};

// Map[Section][Option] = Value
using ConfigKey2DMap = std::map<std::string, std::map<std::string, std::string>>;
}
}
21 changes: 21 additions & 0 deletions ecal/core/src/config/ecal_cmd_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,25 @@ namespace
else
return path_ + path_separator + file_name_;
}

void parseConfigKeysToMap(std::vector<std::string> config_keys_, eCAL::Config::ConfigKey2DMap& map_)
{
// each string has the format "section/key:value"
for (const auto& full_key : config_keys_)
{
auto sec_pos = full_key.find_last_of('/');
if (sec_pos == std::string::npos) continue;
const std::string section = full_key.substr(0, sec_pos);
std::string key = full_key.substr(sec_pos+1);

auto val_pos = key.find_first_of(':');
if (val_pos == std::string::npos) continue;
const std::string value = key.substr(val_pos+1);
key = key.substr(0, val_pos);

map_[section][key] = value;
}
}
}

namespace eCAL
Expand Down Expand Up @@ -182,6 +201,7 @@ namespace eCAL
if (set_config_key_arg.isSet())
{
m_config_keys = set_config_key_arg.getValue();
parseConfigKeysToMap(set_config_key_arg.getValue(), m_config_key_map);
}
}
#endif
Expand Down Expand Up @@ -241,5 +261,6 @@ namespace eCAL
std::vector<std::string>& CmdParser::getConfigKeys() { return m_config_keys; };
std::vector<std::string>& CmdParser::getTaskParameter() { return m_task_parameter; };
std::string& CmdParser::getUserIni() { return m_user_ini; };
ConfigKey2DMap& CmdParser::getConfigKeysMap() { return m_config_key_map; };
}
}
4 changes: 4 additions & 0 deletions ecal/core/src/config/ecal_cmd_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include <vector>
#include <string>
#include <map>

namespace eCAL
{
Expand All @@ -41,6 +42,7 @@ namespace eCAL
class CmdParser
{
public:
using ConfigKey2DMap = std::map<std::string, std::map<std::string, std::string>>;
CmdParser(int argc_ , char **argv_);
CmdParser();

Expand All @@ -50,11 +52,13 @@ namespace eCAL
std::vector<std::string>& getConfigKeys();
std::vector<std::string>& getTaskParameter();
std::string& getUserIni();
ConfigKey2DMap& getConfigKeysMap();

private:
std::string checkForValidConfigFilePath(std::string config_file_);

std::vector<std::string> m_config_keys;
ConfigKey2DMap m_config_key_map;
bool m_dump_config;
std::vector<std::string> m_task_parameter;
std::string m_user_ini;
Expand Down
31 changes: 31 additions & 0 deletions ecal/tests/cpp/config_test/src/config_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,32 @@ TEST(core_cpp_config, config_cmd_parser)

std::vector<const char*> arguments;

const std::string set_config_key = "--ecal-set-config-key ";
const std::string sep_slash = "/";
const std::string sep_col = ":";

const std::string network = "network";
const std::string host_group_name = "host_group_name";
const std::string config_test_machine = "ConfigTestMachine";
const std::string network_enabled = "network_enabled";
const std::string is_network_enabled = "true";

const std::string common = "common";
const std::string registration_timeout = "registration_timeout";
const std::string registration_refresh = "registration_refresh";
const std::string reg_to_value = "6000";
const std::string reg_rf_value = "1000";

arguments.push_back("test_config_cmd_parser");
arguments.push_back("--ecal-ini-file customIni.ini");
std::string host_group_string = set_config_key + network + sep_slash + host_group_name + sep_col + config_test_machine;
arguments.push_back(host_group_string.data());
std::string network_enabled_string = set_config_key + network + sep_slash + network_enabled + sep_col + is_network_enabled;
arguments.push_back(network_enabled_string.data());
std::string registration_to_string = set_config_key + common + sep_slash + registration_timeout + sep_col + reg_to_value;
arguments.push_back(registration_to_string.data());
std::string registration_rf_string = set_config_key + common + sep_slash + registration_refresh + sep_col + reg_rf_value;
arguments.push_back(registration_rf_string.data());

try
{
Expand All @@ -249,7 +273,14 @@ TEST(core_cpp_config, config_cmd_parser)
std::cerr << e.what() << '\n';
}

// Expect a valid ini file
EXPECT_NE(parser.getUserIni(), std::string(""));

// Expect a proper key-value map in the config key map
EXPECT_EQ(parser.getConfigKeysMap()[network][host_group_name], config_test_machine);
EXPECT_EQ(parser.getConfigKeysMap()[network][network_enabled], is_network_enabled);
EXPECT_EQ(parser.getConfigKeysMap()[common][registration_timeout], reg_to_value);
EXPECT_EQ(parser.getConfigKeysMap()[common][registration_refresh], reg_rf_value);
}

TEST(CmdParserDeathTest, config_cmd_parser_death_test)
Expand Down

0 comments on commit e454368

Please sign in to comment.