Merge #17390: test: Add util_ArgParsing test

286f197704 Add util_ArgParsing test (Russell Yanofsky)

Pull request description:

  ArgsManager test coverage for parsing of integer and boolean values is
  currently very poor and doesn't give us a way of knowing whether changes to
  ArgsManager may unintentionally break backwards compatibility, so this adds a
  new test to catch regressions.

ACKs for top commit:
  promag:
    ACK 286f197, more surprising results 😱
  laanwj:
    ACK 286f197704

Tree-SHA512: 9e1db3ef87e55abbc280af60c088f35765a1f9e2ec20507ad0c1992027b875490016868dcb8cc287e6df279dd0e00f10550901af3de3d36287867249e0bd8207
pull/764/head
Wladimir J. van der Laan 5 years ago
commit 6f4e247357
No known key found for this signature in database
GPG Key ID: 1E4AED62986CD25D

@ -231,6 +231,60 @@ BOOST_AUTO_TEST_CASE(util_ParseParameters)
BOOST_CHECK(testArgs.GetArgs("-ccc").size() == 2);
}
static void TestParse(const std::string& str, bool expected_bool, int64_t expected_int)
{
TestArgsManager test;
test.SetupArgs({{"-value", ArgsManager::ALLOW_ANY}});
std::string arg = "-value=" + str;
const char* argv[] = {"ignored", arg.c_str()};
std::string error;
BOOST_CHECK(test.ParseParameters(2, (char**)argv, error));
BOOST_CHECK_EQUAL(test.GetBoolArg("-value", false), expected_bool);
BOOST_CHECK_EQUAL(test.GetBoolArg("-value", true), expected_bool);
BOOST_CHECK_EQUAL(test.GetArg("-value", 99998), expected_int);
BOOST_CHECK_EQUAL(test.GetArg("-value", 99999), expected_int);
}
// Test bool and int parsing.
BOOST_AUTO_TEST_CASE(util_ArgParsing)
{
// Some of these cases could be ambiguous or surprising to users, and might
// be worth triggering errors or warnings in the future. But for now basic
// test coverage is useful to avoid breaking backwards compatibility
// unintentionally.
TestParse("", true, 0);
TestParse(" ", false, 0);
TestParse("0", false, 0);
TestParse("0 ", false, 0);
TestParse(" 0", false, 0);
TestParse("+0", false, 0);
TestParse("-0", false, 0);
TestParse("5", true, 5);
TestParse("5 ", true, 5);
TestParse(" 5", true, 5);
TestParse("+5", true, 5);
TestParse("-5", true, -5);
TestParse("0 5", false, 0);
TestParse("5 0", true, 5);
TestParse("050", true, 50);
TestParse("0.", false, 0);
TestParse("5.", true, 5);
TestParse("0.0", false, 0);
TestParse("0.5", false, 0);
TestParse("5.0", true, 5);
TestParse("5.5", true, 5);
TestParse("x", false, 0);
TestParse("x0", false, 0);
TestParse("x5", false, 0);
TestParse("0x", false, 0);
TestParse("5x", true, 5);
TestParse("0x5", false, 0);
TestParse("false", false, 0);
TestParse("true", false, 0);
TestParse("yes", false, 0);
TestParse("no", false, 0);
}
BOOST_AUTO_TEST_CASE(util_GetBoolArg)
{
TestArgsManager testArgs;

Loading…
Cancel
Save