updated test for argument parser

This commit is contained in:
Erik Frojdh 2019-05-17 10:00:49 +02:00
parent 591ff53b84
commit 3e5d34647e
3 changed files with 13 additions and 59 deletions

View File

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

View File

@ -20,7 +20,7 @@ void CmdLineParser::Print() {
std::cout << "\n\n"; std::cout << "\n\n";
}; };
void CmdLineParser::Parse(int argc, char *argv[]) { void CmdLineParser::Parse(int argc, const char * const argv[]) {
executable_ = argv[0]; //first arg is calling binary executable_ = argv[0]; //first arg is calling binary
if (argc > 1) { if (argc > 1) {
DecodeIdAndPosition(argv[1]); DecodeIdAndPosition(argv[1]);

View File

@ -92,10 +92,10 @@ SCENARIO("Parsing a string with the command line parser", "[support]") {
} }
} }
WHEN("Parsing string with cmd and multiple arguments"){ WHEN("Parsing string with cmd and multiple arguments") {
std::string s = "trimen 5000 6000 7000"; std::string s = "trimen 5000 6000 7000";
p.Parse(s); p.Parse(s);
THEN("cmd and args are correct"){ THEN("cmd and args are correct") {
REQUIRE(p.command() == "trimen"); REQUIRE(p.command() == "trimen");
REQUIRE(p.arguments().size() == 3); REQUIRE(p.arguments().size() == 3);
REQUIRE(p.arguments()[0] == "5000"); REQUIRE(p.arguments()[0] == "5000");
@ -121,10 +121,7 @@ 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]; const char* const argv[]{"call"};
char a0[] = "call";
argv[0] = static_cast<char *>(a0);
CmdLineParser p; CmdLineParser p;
p.Parse(argc, argv); p.Parse(argc, argv);
@ -138,12 +135,7 @@ TEST_CASE(
"Parse a command without client id and detector id results in default", "Parse a command without client id and detector id results in default",
"[support]") { "[support]") {
int argc = 2; int argc = 2;
char *argv[argc]; const char*const argv[]{"caller", "vrf"};
char a0[] = "call";
char a1[] = "vrf";
argv[0] = static_cast<char *>(a0);
argv[1] = static_cast<char *>(a1);
CmdLineParser p; CmdLineParser p;
p.Parse(argc, argv); p.Parse(argc, argv);
@ -156,14 +148,7 @@ TEST_CASE(
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",
"[support]") { "[support]") {
int argc = 3; int argc = 3;
char *argv[argc]; const char* const argv[]{"caller", "vrf", "3000"};
char a0[] = "call";
char a1[] = "vrf";
char a2[] = "3000";
argv[0] = static_cast<char *>(a0);
argv[1] = static_cast<char *>(a1);
argv[2] = static_cast<char *>(a2);
CmdLineParser p; CmdLineParser p;
p.Parse(argc, argv); p.Parse(argc, argv);
@ -176,11 +161,7 @@ TEST_CASE("Parse a command with value but without client or detector id",
TEST_CASE("Decodes position") { TEST_CASE("Decodes position") {
int argc = 2; int argc = 2;
char *argv[argc]; const char*const argv[]{"caller", "7:vrf"};
char a0[] = "call";
char a1[] = "7:vrf";
argv[0] = static_cast<char *>(a0);
argv[1] = static_cast<char *>(a1);
CmdLineParser p; CmdLineParser p;
p.Parse(argc, argv); p.Parse(argc, argv);
@ -193,12 +174,7 @@ TEST_CASE("Decodes position") {
TEST_CASE("Decodes double digit position", "[support]") { TEST_CASE("Decodes double digit position", "[support]") {
int argc = 2; int argc = 2;
char *argv[argc]; const char* const argv[]{"caller", "73:vcmp"};
char a0[] = "call";
char a1[] = "73:vcmp";
argv[0] = static_cast<char *>(a0);
argv[1] = static_cast<char *>(a1);
CmdLineParser p; CmdLineParser p;
p.Parse(argc, argv); p.Parse(argc, argv);
@ -210,12 +186,7 @@ TEST_CASE("Decodes double digit position", "[support]") {
TEST_CASE("Decodes position and id", "[support]") { TEST_CASE("Decodes position and id", "[support]") {
int argc = 2; int argc = 2;
char *argv[argc]; const char* const argv[]{"caller", "5-8:vrf"};
char a0[] = "call";
char a1[] = "5-8:vrf";
argv[0] = static_cast<char *>(a0);
argv[1] = static_cast<char *>(a1);
CmdLineParser p; CmdLineParser p;
p.Parse(argc, argv); p.Parse(argc, argv);
@ -227,15 +198,9 @@ TEST_CASE("Decodes position and id", "[support]") {
TEST_CASE("Double digit id", "[support]") { TEST_CASE("Double digit id", "[support]") {
int argc = 2; int argc = 2;
char *argv[argc]; const char *const argv[]{"caller", "56-8:vrf"};
char a0[] = "call";
char a1[] = "56-8:vrf";
argv[0] = static_cast<char *>(a0);
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() == "vrf"); REQUIRE(p.command() == "vrf");
@ -243,26 +208,15 @@ TEST_CASE("Double digit id", "[support]") {
} }
TEST_CASE("Calling with wrong id throws invalid_argument", "[support]") { TEST_CASE("Calling with wrong id throws invalid_argument", "[support]") {
int argc = 2; int argc = 2;
char *argv[argc]; const char *const argv[]{"caller", "asvldkn:vrf"};
char a0[] = "call";
char a1[] = "asvldkn:vrf";
argv[0] = static_cast<char *>(a0);
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 wrong client throws invalid_argument", "[support]") { TEST_CASE("Calling with wrong client throws invalid_argument", "[support]") {
int argc = 2; int argc = 2;
char *argv[argc]; const char *const argv[]{"caller", "lki-3:vrf"};
char a0[] = "call";
char a1[] = "lki-3:vrf";
argv[0] = static_cast<char *>(a0);
argv[1] = static_cast<char *>(a1);
CmdLineParser p; CmdLineParser p;
CHECK_THROWS(p.Parse(argc, argv)); CHECK_THROWS(p.Parse(argc, argv));
} }