cleaning up tests for command line parser

This commit is contained in:
Erik Frojdh
2019-01-21 10:45:43 +01:00
parent 603b217b54
commit e1ec60c9c1
4 changed files with 123 additions and 145 deletions

View File

@ -273,7 +273,7 @@ public:
* @param id sls detector id (position in detectors list) * @param id sls detector id (position in detectors list)
* @param verify true to verify if shared memory version matches existing one * @param verify true to verify if shared memory version matches existing one
*/ */
slsDetector(detectorType type, int multiId = 0, int id = 0, bool verify = true); explicit slsDetector(detectorType type, int multiId = 0, int id = 0, bool verify = true);
/** /**
* Constructor called when opening existing shared memory * Constructor called when opening existing shared memory
@ -281,7 +281,7 @@ public:
* @param id sls detector id (position in detectors list) * @param id sls detector id (position in detectors list)
* @param verify true to verify if shared memory version matches existing one * @param verify true to verify if shared memory version matches existing one
*/ */
slsDetector(int multiId = 0, int id = 0, bool verify = true); explicit slsDetector(int multiId = 0, int id = 0, bool verify = true);
/** /**
* Destructor * Destructor

View File

@ -7,7 +7,7 @@
class CmdLineParser { class CmdLineParser {
public: public:
void Parse(int argc, char *argv[]); void Parse(int argc, char *argv[]);
void Parse(std::string s); void Parse(const std::string &s);
void Print(); void Print();
//getters //getters

View File

@ -2,26 +2,24 @@
#include "CmdLineParser.h" #include "CmdLineParser.h"
#include <cstdio> #include <cstdio>
#include <cstring> #include <cstring>
#include <sstream>
#include <iterator>
#include <iostream> #include <iostream>
#include <iterator>
#include <sstream>
//printing function for debugging //printing function for debugging
void CmdLineParser::Print() void CmdLineParser::Print() {
{
std::cout << "\nCmdLineParser::Print()\n"; std::cout << "\nCmdLineParser::Print()\n";
std::cout << "\tmulti_id: " << multi_id_ << ", detector_id: " << detector_id_ << std::endl; std::cout << "\tmulti_id: " << multi_id_ << ", detector_id: " << detector_id_ << std::endl;
std::cout << "\texecutable: " << executable_ << '\n'; std::cout << "\texecutable: " << executable_ << '\n';
std::cout << "\tcommand: " << command_ << '\n'; std::cout << "\tcommand: " << command_ << '\n';
std::cout << "\tn_arguments: " << n_arguments() << '\n'; std::cout << "\tn_arguments: " << n_arguments() << '\n';
std::cout << "\targuments: "; std::cout << "\targuments: ";
for (size_t i = 0; i < arguments_.size(); ++i) { for (const auto &argument : arguments_) {
std::cout << arguments_[i] << " "; std::cout << argument << " ";
} }
std::cout << "\n\n"; std::cout << "\n\n";
}; };
void CmdLineParser::Parse(int argc, char* argv[]) void CmdLineParser::Parse(int argc, char *argv[]) {
{
//first element of argv is the command used to call the executable ->skipping //first element of argv is the command used to call the executable ->skipping
//and if this is the only command skip all //and if this is the only command skip all
executable_ = argv[0]; executable_ = argv[0];
@ -29,40 +27,44 @@ void CmdLineParser::Parse(int argc, char* argv[])
//second element is cmd string that needs to be decoded //second element is cmd string that needs to be decoded
DecodeIdAndPosition(argv[1]); DecodeIdAndPosition(argv[1]);
//The rest of the arguments goes into a vector for later processing //The rest of the arguments goes into a vector for later processing
for (int i = 2; i < argc; ++i) for (int i = 2; i < argc; ++i) {
arguments_.emplace_back(std::string(argv[i])); arguments_.emplace_back(std::string(argv[i]));
} }
}
}; };
void CmdLineParser::Parse(std::string s){ void CmdLineParser::Parse(const std::string &s) {
std::istringstream iss(s); std::istringstream iss(s);
auto it = std::istream_iterator<std::string>(iss); auto it = std::istream_iterator<std::string>(iss);
//read the first element and increment //read the first element and increment
command_ = *it++; command_ = *it++;
arguments_ = std::vector<std::string>(it, std::istream_iterator<std::string>());; arguments_ = std::vector<std::string>(it, std::istream_iterator<std::string>());
;
DecodeIdAndPosition(command_.c_str()); DecodeIdAndPosition(command_.c_str());
} }
void CmdLineParser::DecodeIdAndPosition(const char* c) void CmdLineParser::DecodeIdAndPosition(const char *c) {
{ bool contains_id = std::strchr(c, '-') != nullptr;
bool contains_id = std::strchr(c, '-'); bool contains_pos = std::strchr(c, ':') != nullptr;
bool contains_pos = std::strchr(c, ':');
char tmp[100]; char tmp[100];
if (contains_id && contains_pos) { if (contains_id && contains_pos) {
int r = sscanf(c, "%d-%d:%s", &multi_id_, &detector_id_, tmp); int r = sscanf(c, "%d-%d:%s", &multi_id_, &detector_id_, tmp);
if (r != 3) if (r != 3) {
throw(std::invalid_argument("Cannot decode client or detector id from: \"" + std::string(c) + "\"\n")); throw(std::invalid_argument("Cannot decode client or detector id from: \"" + std::string(c) + "\"\n"));
}
command_ = tmp; command_ = tmp;
} else if (contains_id && !contains_pos) { } else if (contains_id && !contains_pos) {
int r = sscanf(c, "%d-%s", &multi_id_, tmp); int r = sscanf(c, "%d-%s", &multi_id_, tmp);
if (r != 2) if (r != 2) {
throw(std::invalid_argument("Cannot decode client id from: \"" + std::string(c) + "\"\n")); throw(std::invalid_argument("Cannot decode client id from: \"" + std::string(c) + "\"\n"));
}
command_ = tmp; command_ = tmp;
} else if (!contains_id && contains_pos) { } else if (!contains_id && contains_pos) {
int r = sscanf(c, "%d:%s", &detector_id_, tmp); int r = sscanf(c, "%d:%s", &detector_id_, tmp);
if (r != 2) if (r != 2) {
throw(std::invalid_argument("Cannot decode detector id from: \"" + std::string(c) + "\"\n")); throw(std::invalid_argument("Cannot decode detector id from: \"" + std::string(c) + "\"\n"));
}
command_ = tmp; command_ = tmp;
} else { } else {
command_ = c; command_ = c;
@ -72,7 +74,8 @@ void CmdLineParser::DecodeIdAndPosition(const char* c)
std::vector<char *> CmdLineParser::argv() { std::vector<char *> CmdLineParser::argv() {
std::vector<char *> vec; std::vector<char *> vec;
vec.push_back(&command_.front()); vec.push_back(&command_.front());
for (auto& arg: arguments_) for (auto &arg : arguments_) {
vec.push_back(&arg.front()); vec.push_back(&arg.front());
}
return vec; return vec;
} }

View File

@ -6,88 +6,82 @@
//help for all docs //help for all docs
//command for all depreciated commands //command for all depreciated commands
TEST_CASE("Parse with no arguments results in no command and default id") TEST_CASE("Parse with no arguments results in no command and default id") {
{
//build up argc and argv //build up argc and argv
//first argument is the command used to call the binary //first argument is the command used to call the binary
int argc = 1; int argc = 1;
char *argv[argc]; char *argv[argc];
char a0[] = "call"; char a0[] = "call";
argv[0] = a0; argv[0] = static_cast<char *>(a0);
CmdLineParser p; CmdLineParser p;
p.Parse(argc, argv); p.Parse(argc, argv);
REQUIRE(p.detector_id() == -1); REQUIRE(p.detector_id() == -1);
REQUIRE(p.multi_id() == 0); REQUIRE(p.multi_id() == 0);
REQUIRE(p.command() == std::string("")); REQUIRE(p.command().empty());
REQUIRE(p.arguments().size() == 0); REQUIRE(p.arguments().empty());
} }
TEST_CASE("Parse empty string") TEST_CASE("Parse empty string") {
{ std::string s;
std::string s = "";
CmdLineParser p; CmdLineParser p;
p.Parse(s); p.Parse(s);
REQUIRE(p.detector_id() == -1); REQUIRE(p.detector_id() == -1);
REQUIRE(p.multi_id() == 0); REQUIRE(p.multi_id() == 0);
REQUIRE(p.command() == std::string("")); REQUIRE(p.command().empty());
REQUIRE(p.arguments().size() == 0); REQUIRE(p.arguments().empty());
} }
TEST_CASE("Parse a command without client id and detector id results in default") TEST_CASE("Parse a command without client id and detector id results in default") {
{
int argc = 2; int argc = 2;
char *argv[argc]; char *argv[argc];
char a0[] = "call"; char a0[] = "call";
char a1[] = "vrf"; char a1[] = "vrf";
argv[0] = a0; argv[0] = static_cast<char *>(a0);
argv[1] = a1; argv[1] = static_cast<char *>(a1);
CmdLineParser p; CmdLineParser p;
p.Parse(argc, argv); p.Parse(argc, argv);
REQUIRE(p.detector_id() == -1); REQUIRE(p.detector_id() == -1);
REQUIRE(p.multi_id() == 0); REQUIRE(p.multi_id() == 0);
REQUIRE(p.command() == std::string("vrf")); REQUIRE(p.command() == "vrf");
REQUIRE(p.arguments().size() == 0); REQUIRE(p.arguments().empty());
} }
TEST_CASE("Parse a string without client id and detector id results in default") TEST_CASE("Parse a string without client id and detector id results in default") {
{
std::string s = "vrf"; std::string s = "vrf";
CmdLineParser p; CmdLineParser p;
p.Parse(s); p.Parse(s);
REQUIRE(p.detector_id() == -1); REQUIRE(p.detector_id() == -1);
REQUIRE(p.multi_id() == 0); REQUIRE(p.multi_id() == 0);
REQUIRE(p.command() == std::string("vrf")); REQUIRE(p.command() == "vrf");
REQUIRE(p.arguments().size() == 0); REQUIRE(p.arguments().empty());
} }
TEST_CASE("Parse a command with value but without client or detector id") TEST_CASE("Parse a command with value but without client or detector id") {
{
int argc = 3; int argc = 3;
char *argv[argc]; char *argv[argc];
char a0[] = "call"; char a0[] = "call";
char a1[] = "vrf"; char a1[] = "vrf";
char a2[] = "3000"; char a2[] = "3000";
argv[0] = a0; argv[0] = static_cast<char *>(a0);
argv[1] = a1; argv[1] = static_cast<char *>(a1);
argv[2] = a2; argv[2] = static_cast<char *>(a2);
CmdLineParser p; CmdLineParser p;
p.Parse(argc, argv); p.Parse(argc, argv);
REQUIRE(p.detector_id() == -1); REQUIRE(p.detector_id() == -1);
REQUIRE(p.multi_id() == 0); REQUIRE(p.multi_id() == 0);
REQUIRE(p.command() == std::string("vrf")); REQUIRE(p.command() == "vrf");
REQUIRE(p.arguments().size() == 1); REQUIRE(p.arguments().size() == 1);
REQUIRE(p.arguments()[0] == std::string("3000")); REQUIRE(p.arguments()[0] == "3000");
} }
TEST_CASE("Parse a string with value but without client or detector id") TEST_CASE("Parse a string with value but without client or detector id") {
{
std::string s = "vrf 3000\n"; std::string s = "vrf 3000\n";
CmdLineParser p; CmdLineParser p;
@ -95,30 +89,28 @@ TEST_CASE("Parse a string with value but without client or detector id")
REQUIRE(p.detector_id() == -1); REQUIRE(p.detector_id() == -1);
REQUIRE(p.multi_id() == 0); REQUIRE(p.multi_id() == 0);
REQUIRE(p.command() == std::string("vrf")); REQUIRE(p.command() == "vrf");
REQUIRE(p.arguments().size() == 1); REQUIRE(p.arguments().size() == 1);
REQUIRE(p.arguments()[0] == std::string("3000")); REQUIRE(p.arguments()[0] == "3000");
} }
TEST_CASE("Decodes position") TEST_CASE("Decodes position") {
{
int argc = 2; int argc = 2;
char *argv[argc]; char *argv[argc];
char a0[] = "call"; char a0[] = "call";
char a1[] = "7:vrf"; char a1[] = "7:vrf";
argv[0] = a0; argv[0] = static_cast<char *>(a0);
argv[1] = a1; argv[1] = static_cast<char *>(a1);
CmdLineParser p; CmdLineParser p;
p.Parse(argc, argv); p.Parse(argc, argv);
REQUIRE(p.detector_id() == 7); REQUIRE(p.detector_id() == 7);
REQUIRE(p.multi_id() == 0); REQUIRE(p.multi_id() == 0);
REQUIRE(p.command() == std::string("vrf")); REQUIRE(p.command() == "vrf");
REQUIRE(p.arguments().size() == 0); REQUIRE(p.arguments().empty());
} }
TEST_CASE("Decodes position from string") TEST_CASE("Decodes position from string") {
{
std::string s = "7:vrf\n"; std::string s = "7:vrf\n";
CmdLineParser p; CmdLineParser p;
@ -126,30 +118,28 @@ TEST_CASE("Decodes position from string")
REQUIRE(p.detector_id() == 7); REQUIRE(p.detector_id() == 7);
REQUIRE(p.multi_id() == 0); REQUIRE(p.multi_id() == 0);
REQUIRE(p.command() == std::string("vrf")); REQUIRE(p.command() == "vrf");
REQUIRE(p.arguments().size() == 0); REQUIRE(p.arguments().empty());
} }
TEST_CASE("Decodes double digit position") TEST_CASE("Decodes double digit position") {
{
int argc = 2; int argc = 2;
char *argv[argc]; char *argv[argc];
char a0[] = "call"; char a0[] = "call";
char a1[] = "73:vcmp"; char a1[] = "73:vcmp";
argv[0] = a0; argv[0] = static_cast<char *>(a0);
argv[1] = a1; argv[1] = static_cast<char *>(a1);
CmdLineParser p; CmdLineParser p;
p.Parse(argc, argv); p.Parse(argc, argv);
REQUIRE(p.detector_id() == 73); REQUIRE(p.detector_id() == 73);
REQUIRE(p.multi_id() == 0); REQUIRE(p.multi_id() == 0);
REQUIRE(p.command() == std::string("vcmp")); REQUIRE(p.command() == "vcmp");
REQUIRE(p.arguments().size() == 0); REQUIRE(p.arguments().empty());
} }
TEST_CASE("Decodes double digit position from string") TEST_CASE("Decodes double digit position from string") {
{
std::string s = "73:vcmp"; std::string s = "73:vcmp";
CmdLineParser p; CmdLineParser p;
@ -157,110 +147,95 @@ TEST_CASE("Decodes double digit position from string")
REQUIRE(p.detector_id() == 73); REQUIRE(p.detector_id() == 73);
REQUIRE(p.multi_id() == 0); REQUIRE(p.multi_id() == 0);
REQUIRE(p.command() == std::string("vcmp")); REQUIRE(p.command() == "vcmp");
REQUIRE(p.arguments().size() == 0); REQUIRE(p.arguments().empty());
} }
TEST_CASE("Decodes position and id") TEST_CASE("Decodes position and id") {
{
int argc = 2; int argc = 2;
char *argv[argc]; char *argv[argc];
char a0[] = "call"; char a0[] = "call";
char a1[] = "5-8:vrf"; char a1[] = "5-8:vrf";
argv[0] = a0; argv[0] = static_cast<char *>(a0);
argv[1] = a1; argv[1] = static_cast<char *>(a1);
CmdLineParser p; CmdLineParser p;
p.Parse(argc, argv); p.Parse(argc, argv);
REQUIRE(p.detector_id() == 8); REQUIRE(p.detector_id() == 8);
REQUIRE(p.multi_id() == 5); REQUIRE(p.multi_id() == 5);
REQUIRE(p.command() == std::string("vrf")); REQUIRE(p.command() == "vrf");
REQUIRE(p.arguments().size() == 0); REQUIRE(p.arguments().empty());
} }
TEST_CASE("Decodes position and id from string") TEST_CASE("Decodes position and id from string") {
{
std::string s = "5-8:vrf"; std::string s = "5-8:vrf";
CmdLineParser p; CmdLineParser p;
p.Parse(s); p.Parse(s);
REQUIRE(p.detector_id() == 8); REQUIRE(p.detector_id() == 8);
REQUIRE(p.multi_id() == 5); REQUIRE(p.multi_id() == 5);
REQUIRE(p.command() == std::string("vrf")); REQUIRE(p.command() == "vrf");
REQUIRE(p.arguments().size() == 0); REQUIRE(p.arguments().empty());
} }
TEST_CASE("Double digit id") TEST_CASE("Double digit id") {
{
int argc = 2; int argc = 2;
char *argv[argc]; char *argv[argc];
char a0[] = "call"; char a0[] = "call";
char a1[] = "56-8:vrf"; char a1[] = "56-8:vrf";
argv[0] = a0; argv[0] = static_cast<char *>(a0);
argv[1] = a1; argv[1] = static_cast<char *>(a1);
CmdLineParser p; CmdLineParser p;
p.Parse(argc, argv); p.Parse(argc, argv);
REQUIRE(p.detector_id() == 8); REQUIRE(p.detector_id() == 8);
REQUIRE(p.multi_id() == 56); REQUIRE(p.multi_id() == 56);
REQUIRE(p.command() == std::string("vrf")); REQUIRE(p.command() == "vrf");
REQUIRE(p.arguments().size() == 0); REQUIRE(p.arguments().empty());
} }
TEST_CASE("Double digit id from string") TEST_CASE("Double digit id from string") {
{
std::string s = "56-8:vrf"; std::string s = "56-8:vrf";
CmdLineParser p; CmdLineParser p;
p.Parse(s); p.Parse(s);
REQUIRE(p.detector_id() == 8); REQUIRE(p.detector_id() == 8);
REQUIRE(p.multi_id() == 56); REQUIRE(p.multi_id() == 56);
REQUIRE(p.command() == std::string("vrf")); REQUIRE(p.command() == std::string("vrf"));
REQUIRE(p.arguments().size() == 0); REQUIRE(p.arguments().empty());
} }
TEST_CASE("Calling with wrong id throws invalid_argument") TEST_CASE("Calling with wrong id throws invalid_argument") {
{
int argc = 2; int argc = 2;
char *argv[argc]; char *argv[argc];
char a0[] = "call"; char a0[] = "call";
char a1[] = "asvldkn:vrf"; char a1[] = "asvldkn:vrf";
argv[0] = a0; argv[0] = static_cast<char *>(a0);
argv[1] = a1; argv[1] = static_cast<char *>(a1);
CmdLineParser p; CmdLineParser p;
CHECK_THROWS(p.Parse(argc, argv)); CHECK_THROWS(p.Parse(argc, argv));
} }
TEST_CASE("Calling with string with wrong id throws invalid_argument") TEST_CASE("Calling with string with wrong id throws invalid_argument") {
{
std::string s = "asvldkn:vrf"; std::string s = "asvldkn:vrf";
CmdLineParser p; CmdLineParser p;
CHECK_THROWS(p.Parse(s)); CHECK_THROWS(p.Parse(s));
} }
TEST_CASE("Calling with wrong client throws invalid_argument") {
TEST_CASE("Calling with wrong client throws invalid_argument")
{
int argc = 2; int argc = 2;
char *argv[argc]; char *argv[argc];
char a0[] = "call"; char a0[] = "call";
char a1[] = "lki-3:vrf"; char a1[] = "lki-3:vrf";
argv[0] = a0; argv[0] = static_cast<char *>(a0);
argv[1] = a1; argv[1] = static_cast<char *>(a1);
CmdLineParser p; CmdLineParser p;
CHECK_THROWS(p.Parse(argc, argv)); CHECK_THROWS(p.Parse(argc, argv));
} }
TEST_CASE("Calling with string with wrong client throws invalid_argument") TEST_CASE("Calling with string with wrong client throws invalid_argument") {
{
std::string s = "lki-3:vrf"; std::string s = "lki-3:vrf";
CmdLineParser p; CmdLineParser p;
CHECK_THROWS(p.Parse(s)); CHECK_THROWS(p.Parse(s));
@ -274,5 +249,5 @@ TEST_CASE("Parses string with two arguments"){
REQUIRE("trimen" == p.command()); REQUIRE("trimen" == p.command());
REQUIRE("3000" == p.arguments()[0]); REQUIRE("3000" == p.arguments()[0]);
REQUIRE("4000" == p.arguments()[1]); REQUIRE("4000" == p.arguments()[1]);
REQUIRE(2 == p.arguments().size()); REQUIRE(p.arguments().size() == 2);
} }