Browse Source

Test: Add test cases for string options

master
Riyyi 3 years ago
parent
commit
e813ab0f73
  1. 34
      test/main.cpp
  2. 215
      test/testutilargparser.cpp

34
test/main.cpp

@ -4,39 +4,5 @@ int main(int, const char*[])
{
Test::TestSuite::the().run();
// // bool tests
// test('o', "option", { "-o" }, true);
// test('o', "option", { "-n" }, false);
// test('o', "option", { "--option" }, true);
// test('o', "option", { "--noexist" }, false);
// // string tests
// test('o', "option", Util::ArgParser::Required::Yes, { "-o", "my-argument" }, "my-argument", 0);
// test('o', "option", Util::ArgParser::Required::Optional, { "-o", "my-argument" }, {}, 0);
// test('o', "option", Util::ArgParser::Required::No, { "-o", "my-argument" }, {}, 0);
// test('o', "option", Util::ArgParser::Required::Yes, { "-omy-argument" }, "my-argument", 0);
// test('o', "option", Util::ArgParser::Required::Optional, { "-omy-argument" }, "my-argument", 0);
// test('o', "option", Util::ArgParser::Required::No, { "-omy-argument" }, {}, 0);
// test('o', "option", Util::ArgParser::Required::Yes, { "--option", "my-argument" }, "my-argument", 0);
// test('o', "option", Util::ArgParser::Required::Optional, { "--option", "my-argument" }, {}, 0);
// test('o', "option", Util::ArgParser::Required::No, { "--option", "my-argument" }, {}, 0);
// test('o', "option", Util::ArgParser::Required::Yes, { "--option=my-argument" }, "my-argument", 0);
// test('o', "option", Util::ArgParser::Required::Optional, { "--option=my-argument" }, "my-argument", 0);
// test('o', "option", Util::ArgParser::Required::No , { "--option=my-argument" }, {}, 0);
// test('o', "option", Util::ArgParser::Required::Yes, { "-o", "my-argument" }, "not-same", -1);
// test('o', "option", Util::ArgParser::Required::Yes, { "-omy-argument" }, "not-same", -1);
// test('o', "option", Util::ArgParser::Required::Optional, { "-omy-argument" }, "not-same", -1);
// test('o', "option", Util::ArgParser::Required::Yes, { "--option", "my-argument" }, "not-same", -1);
// test('o', "option", Util::ArgParser::Required::Yes, { "--option=my-argument" }, "not-same", -1);
// test('o', "option", Util::ArgParser::Required::Optional, { "--option=my-argument" }, "not-same", -1);
// ./help -o something -a my-value
// -a has required argument, but something should stop option parsing
return 0;
}

215
test/testutilargparser.cpp

@ -1,3 +1,5 @@
#include <string>
#include "macro.h"
#include "testcase.h"
#include "testsuite.h"
@ -19,12 +21,16 @@ bool runParser(std::vector<const char*> arguments, std::function<void(Util::ArgP
return result;
}
// -----------------------------------------
TEST_CASE(NoArguments)
{
auto result = runParser({});
EXPECT_EQ(result, true);
}
// -----------------------------------------
TEST_CASE(BoolOptions)
{
// Short option
@ -84,6 +90,215 @@ TEST_CASE(BoolOptions)
EXPECT_EQ(boolOpt1, true);
}
// -----------------------------------------
TEST_CASE(SingleRequiredStringOptions)
{
// Single required string short option
std::string stringOpt1 = "";
auto result = runParser({ "-s", "my-required-argument" }, [&](auto& parser) {
parser.addOption(stringOpt1, 's', nullptr, nullptr, nullptr, nullptr, Util::ArgParser::Required::Yes);
});
EXPECT_EQ(result, true);
EXPECT_EQ(stringOpt1, "my-required-argument");
// Single required string short option, given directly after
stringOpt1 = "";
result = runParser({ "-smy-required-argument" }, [&](auto& parser) {
parser.addOption(stringOpt1, 's', nullptr, nullptr, nullptr, nullptr, Util::ArgParser::Required::Yes);
});
EXPECT_EQ(result, true);
EXPECT_EQ(stringOpt1, "my-required-argument");
// Single required string short option, empty given
stringOpt1 = "";
result = runParser({ "-s" }, [&](auto& parser) {
parser.addOption(stringOpt1, 's', nullptr, nullptr, nullptr, nullptr, Util::ArgParser::Required::Yes);
});
EXPECT_EQ(result, false);
EXPECT_EQ(stringOpt1, "");
// Single required string short option, not given
stringOpt1 = "";
result = runParser({}, [&](auto& parser) {
parser.addOption(stringOpt1, 's', nullptr, nullptr, nullptr, nullptr, Util::ArgParser::Required::Yes);
});
EXPECT_EQ(result, true);
EXPECT_EQ(stringOpt1, "");
// Single required string long option
stringOpt1 = "";
result = runParser({ "--string", "my-required-argument" }, [&](auto& parser) {
parser.addOption(stringOpt1, '\0', "string", nullptr, nullptr, nullptr, Util::ArgParser::Required::Yes);
});
EXPECT_EQ(result, true);
EXPECT_EQ(stringOpt1, "my-required-argument");
// Single required string long option, given directly after
stringOpt1 = "";
result = runParser({ "--string=my-required-argument" }, [&](auto& parser) {
parser.addOption(stringOpt1, '\0', "string", nullptr, nullptr, nullptr, Util::ArgParser::Required::Yes);
});
EXPECT_EQ(result, true);
EXPECT_EQ(stringOpt1, "my-required-argument");
// Single required string long option, empty given
stringOpt1 = "";
result = runParser({ "--string" }, [&](auto& parser) {
parser.addOption(stringOpt1, '\0', "string", nullptr, nullptr, nullptr, Util::ArgParser::Required::Yes);
});
EXPECT_EQ(result, false);
EXPECT_EQ(stringOpt1, "");
// Single required string long option, not given
stringOpt1 = "";
result = runParser({}, [&](auto& parser) {
parser.addOption(stringOpt1, '\0', "string", nullptr, nullptr, nullptr, Util::ArgParser::Required::Yes);
});
EXPECT_EQ(result, true);
EXPECT_EQ(stringOpt1, "");
}
// -----------------------------------------
TEST_CASE(SingleOptionalStringOptions)
{
// Single optional string short option
std::string stringOpt1 = "";
auto result = runParser({ "-s", "my-optional-argument" }, [&](auto& parser) {
parser.addOption(stringOpt1, 's', nullptr, nullptr, nullptr, nullptr, Util::ArgParser::Required::Optional);
});
EXPECT_EQ(result, true);
EXPECT_EQ(stringOpt1, "");
// Single optional string short option, given directly after
stringOpt1 = "";
result = runParser({ "-smy-optional-argument" }, [&](auto& parser) {
parser.addOption(stringOpt1, 's', nullptr, nullptr, nullptr, nullptr, Util::ArgParser::Required::Optional);
});
EXPECT_EQ(result, true);
EXPECT_EQ(stringOpt1, "my-optional-argument");
// Single optional string short option, empty given
stringOpt1 = "";
result = runParser({ "-s" }, [&](auto& parser) {
parser.addOption(stringOpt1, 's', nullptr, nullptr, nullptr, nullptr, Util::ArgParser::Required::Optional);
});
EXPECT_EQ(result, true);
EXPECT_EQ(stringOpt1, "");
// Single optional string short option, not given
stringOpt1 = "";
result = runParser({}, [&](auto& parser) {
parser.addOption(stringOpt1, 's', nullptr, nullptr, nullptr, nullptr, Util::ArgParser::Required::Optional);
});
EXPECT_EQ(result, true);
EXPECT_EQ(stringOpt1, "");
// Single optional string long option
stringOpt1 = "";
result = runParser({ "--string", "my-optional-argument" }, [&](auto& parser) {
parser.addOption(stringOpt1, '\0', "string", nullptr, nullptr, nullptr, Util::ArgParser::Required::Optional);
});
EXPECT_EQ(result, true);
EXPECT_EQ(stringOpt1, "");
// Single optional string long option, given directly after
stringOpt1 = "";
result = runParser({ "--string=my-optional-argument" }, [&](auto& parser) {
parser.addOption(stringOpt1, '\0', "string", nullptr, nullptr, nullptr, Util::ArgParser::Required::Optional);
});
EXPECT_EQ(result, true);
EXPECT_EQ(stringOpt1, "my-optional-argument");
// Single optional string long option, empty given
stringOpt1 = "";
result = runParser({ "--string" }, [&](auto& parser) {
parser.addOption(stringOpt1, '\0', "string", nullptr, nullptr, nullptr, Util::ArgParser::Required::Optional);
});
EXPECT_EQ(result, true);
EXPECT_EQ(stringOpt1, "");
// Single optional string long option, not given
stringOpt1 = "";
result = runParser({}, [&](auto& parser) {
parser.addOption(stringOpt1, '\0', "string", nullptr, nullptr, nullptr, Util::ArgParser::Required::Optional);
});
EXPECT_EQ(result, true);
EXPECT_EQ(stringOpt1, "");
}
// -----------------------------------------
TEST_CASE(SingleNonRequiredStringOptions)
{
// Single non-required string short option
std::string stringOpt1 = "";
auto result = runParser({ "-s", "my-non-required-argument" }, [&](auto& parser) {
parser.addOption(stringOpt1, 's', nullptr, nullptr, nullptr, nullptr, Util::ArgParser::Required::No);
});
EXPECT_EQ(result, true);
EXPECT_EQ(stringOpt1, "");
// Single non-required string short option, given directly after
stringOpt1 = "";
result = runParser({ "-smy-non-required-argument" }, [&](auto& parser) {
parser.addOption(stringOpt1, 's', nullptr, nullptr, nullptr, nullptr, Util::ArgParser::Required::No);
});
EXPECT_EQ(result, false);
EXPECT_EQ(stringOpt1, "");
// Single non-required string short option, empty given
stringOpt1 = "";
result = runParser({ "-s" }, [&](auto& parser) {
parser.addOption(stringOpt1, 's', nullptr, nullptr, nullptr, nullptr, Util::ArgParser::Required::No);
});
EXPECT_EQ(result, true);
EXPECT_EQ(stringOpt1, "");
// Single non-required string short option, not given
stringOpt1 = "";
result = runParser({}, [&](auto& parser) {
parser.addOption(stringOpt1, 's', nullptr, nullptr, nullptr, nullptr, Util::ArgParser::Required::No);
});
EXPECT_EQ(result, true);
EXPECT_EQ(stringOpt1, "");
// Single non-required string long option
stringOpt1 = "";
result = runParser({ "--string", "my-non-required-argument" }, [&](auto& parser) {
parser.addOption(stringOpt1, '\0', "string", nullptr, nullptr, nullptr, Util::ArgParser::Required::No);
});
EXPECT_EQ(result, true);
EXPECT_EQ(stringOpt1, "");
// Single non-required string long option, given directly after
stringOpt1 = "";
result = runParser({ "--string=my-non-required-argument" }, [&](auto& parser) {
parser.addOption(stringOpt1, '\0', "string", nullptr, nullptr, nullptr, Util::ArgParser::Required::No);
});
EXPECT_EQ(result, false);
EXPECT_EQ(stringOpt1, "");
// Single non-required string long option, empty given
stringOpt1 = "";
result = runParser({ "--string" }, [&](auto& parser) {
parser.addOption(stringOpt1, '\0', "string", nullptr, nullptr, nullptr, Util::ArgParser::Required::No);
});
EXPECT_EQ(result, true);
EXPECT_EQ(stringOpt1, "");
// Single non-required string long option, not given
stringOpt1 = "";
result = runParser({}, [&](auto& parser) {
parser.addOption(stringOpt1, '\0', "string", nullptr, nullptr, nullptr, Util::ArgParser::Required::No);
});
EXPECT_EQ(result, true);
EXPECT_EQ(stringOpt1, "");
}
// -----------------------------------------
TEST_CASE(MultipleOptions)
{
// Both short options, second is required, with a non-option argument in-between

Loading…
Cancel
Save