Changeset View
Changeset View
Standalone View
Standalone View
src/test/getarg_tests.cpp
Show All 23 Lines | static void ResetArgs(const std::string &strArg) { | ||||
vecArg.insert(vecArg.begin(), "testbitcoin"); | vecArg.insert(vecArg.begin(), "testbitcoin"); | ||||
// Convert to char*: | // Convert to char*: | ||||
std::vector<const char *> vecChar; | std::vector<const char *> vecChar; | ||||
for (std::string &s : vecArg) { | for (std::string &s : vecArg) { | ||||
vecChar.push_back(s.c_str()); | vecChar.push_back(s.c_str()); | ||||
} | } | ||||
gArgs.ParseParameters(vecChar.size(), vecChar.data()); | std::string error; | ||||
gArgs.ParseParameters(vecChar.size(), vecChar.data(), error); | |||||
} | |||||
static void SetupArgs(const std::vector<std::string> &args) { | |||||
gArgs.ClearArgs(); | |||||
for (const std::string &arg : args) { | |||||
gArgs.AddArg(arg, "", false, OptionsCategory::OPTIONS); | |||||
} | |||||
} | } | ||||
BOOST_AUTO_TEST_CASE(boolarg) { | BOOST_AUTO_TEST_CASE(boolarg) { | ||||
SetupArgs({"-foo"}); | |||||
ResetArgs("-foo"); | ResetArgs("-foo"); | ||||
BOOST_CHECK(gArgs.GetBoolArg("-foo", false)); | BOOST_CHECK(gArgs.GetBoolArg("-foo", false)); | ||||
BOOST_CHECK(gArgs.GetBoolArg("-foo", true)); | BOOST_CHECK(gArgs.GetBoolArg("-foo", true)); | ||||
BOOST_CHECK(!gArgs.GetBoolArg("-fo", false)); | BOOST_CHECK(!gArgs.GetBoolArg("-fo", false)); | ||||
BOOST_CHECK(gArgs.GetBoolArg("-fo", true)); | BOOST_CHECK(gArgs.GetBoolArg("-fo", true)); | ||||
BOOST_CHECK(!gArgs.GetBoolArg("-fooo", false)); | BOOST_CHECK(!gArgs.GetBoolArg("-fooo", false)); | ||||
Show All 37 Lines | BOOST_AUTO_TEST_CASE(boolarg) { | ||||
BOOST_CHECK(gArgs.GetBoolArg("-foo", true)); | BOOST_CHECK(gArgs.GetBoolArg("-foo", true)); | ||||
ResetArgs("--nofoo=1"); | ResetArgs("--nofoo=1"); | ||||
BOOST_CHECK(!gArgs.GetBoolArg("-foo", false)); | BOOST_CHECK(!gArgs.GetBoolArg("-foo", false)); | ||||
BOOST_CHECK(!gArgs.GetBoolArg("-foo", true)); | BOOST_CHECK(!gArgs.GetBoolArg("-foo", true)); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(stringarg) { | BOOST_AUTO_TEST_CASE(stringarg) { | ||||
SetupArgs({"-foo", "-bar"}); | |||||
ResetArgs(""); | ResetArgs(""); | ||||
BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", ""), ""); | BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", ""), ""); | ||||
BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", "eleven"), "eleven"); | BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", "eleven"), "eleven"); | ||||
ResetArgs("-foo -bar"); | ResetArgs("-foo -bar"); | ||||
BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", ""), ""); | BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", ""), ""); | ||||
BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", "eleven"), ""); | BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", "eleven"), ""); | ||||
ResetArgs("-foo="); | ResetArgs("-foo="); | ||||
BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", ""), ""); | BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", ""), ""); | ||||
BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", "eleven"), ""); | BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", "eleven"), ""); | ||||
ResetArgs("-foo=11"); | ResetArgs("-foo=11"); | ||||
BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", ""), "11"); | BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", ""), "11"); | ||||
BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", "eleven"), "11"); | BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", "eleven"), "11"); | ||||
ResetArgs("-foo=eleven"); | ResetArgs("-foo=eleven"); | ||||
BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", ""), "eleven"); | BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", ""), "eleven"); | ||||
BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", "eleven"), "eleven"); | BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", "eleven"), "eleven"); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(intarg) { | BOOST_AUTO_TEST_CASE(intarg) { | ||||
SetupArgs({"-foo", "-bar"}); | |||||
ResetArgs(""); | ResetArgs(""); | ||||
BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", 11), 11); | BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", 11), 11); | ||||
BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", 0), 0); | BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", 0), 0); | ||||
ResetArgs("-foo -bar"); | ResetArgs("-foo -bar"); | ||||
BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", 11), 0); | BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", 11), 0); | ||||
BOOST_CHECK_EQUAL(gArgs.GetArg("-bar", 11), 0); | BOOST_CHECK_EQUAL(gArgs.GetArg("-bar", 11), 0); | ||||
ResetArgs("-foo=11 -bar=12"); | ResetArgs("-foo=11 -bar=12"); | ||||
BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", 0), 11); | BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", 0), 11); | ||||
BOOST_CHECK_EQUAL(gArgs.GetArg("-bar", 11), 12); | BOOST_CHECK_EQUAL(gArgs.GetArg("-bar", 11), 12); | ||||
ResetArgs("-foo=NaN -bar=NotANumber"); | ResetArgs("-foo=NaN -bar=NotANumber"); | ||||
BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", 1), 0); | BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", 1), 0); | ||||
BOOST_CHECK_EQUAL(gArgs.GetArg("-bar", 11), 0); | BOOST_CHECK_EQUAL(gArgs.GetArg("-bar", 11), 0); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(doubledash) { | BOOST_AUTO_TEST_CASE(doubledash) { | ||||
SetupArgs({"-foo", "-bar"}); | |||||
ResetArgs("--foo"); | ResetArgs("--foo"); | ||||
BOOST_CHECK_EQUAL(gArgs.GetBoolArg("-foo", false), true); | BOOST_CHECK_EQUAL(gArgs.GetBoolArg("-foo", false), true); | ||||
ResetArgs("--foo=verbose --bar=1"); | ResetArgs("--foo=verbose --bar=1"); | ||||
BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", ""), "verbose"); | BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", ""), "verbose"); | ||||
BOOST_CHECK_EQUAL(gArgs.GetArg("-bar", 0), 1); | BOOST_CHECK_EQUAL(gArgs.GetArg("-bar", 0), 1); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(boolargno) { | BOOST_AUTO_TEST_CASE(boolargno) { | ||||
SetupArgs({"-foo", "-bar"}); | |||||
ResetArgs("-nofoo"); | ResetArgs("-nofoo"); | ||||
BOOST_CHECK(!gArgs.GetBoolArg("-foo", true)); | BOOST_CHECK(!gArgs.GetBoolArg("-foo", true)); | ||||
BOOST_CHECK(!gArgs.GetBoolArg("-foo", false)); | BOOST_CHECK(!gArgs.GetBoolArg("-foo", false)); | ||||
ResetArgs("-nofoo=1"); | ResetArgs("-nofoo=1"); | ||||
BOOST_CHECK(!gArgs.GetBoolArg("-foo", true)); | BOOST_CHECK(!gArgs.GetBoolArg("-foo", true)); | ||||
BOOST_CHECK(!gArgs.GetBoolArg("-foo", false)); | BOOST_CHECK(!gArgs.GetBoolArg("-foo", false)); | ||||
Show All 16 Lines |