Skip to content

Commit

Permalink
merge bitcoin#16097: Make tests arg type specific
Browse files Browse the repository at this point in the history
  • Loading branch information
kwvg committed Nov 11, 2021
1 parent 715a988 commit dbcc19e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 21 deletions.
26 changes: 18 additions & 8 deletions src/test/getarg_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <test/setup_common.h>

#include <string>
#include <utility>
#include <vector>

#include <boost/algorithm/string.hpp>
Expand All @@ -32,17 +33,18 @@ static void ResetArgs(const std::string& strArg)
gArgs.ParseParameters(vecChar.size(), vecChar.data(), error);
}

static void SetupArgs(const std::vector<std::string>& args)
static void SetupArgs(const std::vector<std::pair<std::string, unsigned int>>& args)
{
gArgs.ClearArgs();
for (const std::string& arg : args) {
gArgs.AddArg(arg, "", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
for (const auto& arg : args) {
gArgs.AddArg(arg.first, "", arg.second, OptionsCategory::OPTIONS);
}
}

BOOST_AUTO_TEST_CASE(boolarg)
{
SetupArgs({"-foo"});
const auto foo = std::make_pair("-foo", ArgsManager::ALLOW_BOOL);
SetupArgs({foo});
ResetArgs("-foo");
BOOST_CHECK(gArgs.GetBoolArg("-foo", false));
BOOST_CHECK(gArgs.GetBoolArg("-foo", true));
Expand Down Expand Up @@ -95,7 +97,9 @@ BOOST_AUTO_TEST_CASE(boolarg)

BOOST_AUTO_TEST_CASE(stringarg)
{
SetupArgs({"-foo", "-bar"});
const auto foo = std::make_pair("-foo", ArgsManager::ALLOW_STRING);
const auto bar = std::make_pair("-bar", ArgsManager::ALLOW_STRING);
SetupArgs({foo, bar});
ResetArgs("");
BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", ""), "");
BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", "eleven"), "eleven");
Expand All @@ -120,7 +124,9 @@ BOOST_AUTO_TEST_CASE(stringarg)

BOOST_AUTO_TEST_CASE(intarg)
{
SetupArgs({"-foo", "-bar"});
const auto foo = std::make_pair("-foo", ArgsManager::ALLOW_INT);
const auto bar = std::make_pair("-bar", ArgsManager::ALLOW_INT);
SetupArgs({foo, bar});
ResetArgs("");
BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", 11), 11);
BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", 0), 0);
Expand All @@ -140,7 +146,9 @@ BOOST_AUTO_TEST_CASE(intarg)

BOOST_AUTO_TEST_CASE(doubledash)
{
SetupArgs({"-foo", "-bar"});
const auto foo = std::make_pair("-foo", ArgsManager::ALLOW_ANY);
const auto bar = std::make_pair("-bar", ArgsManager::ALLOW_ANY);
SetupArgs({foo, bar});
ResetArgs("--foo");
BOOST_CHECK_EQUAL(gArgs.GetBoolArg("-foo", false), true);

Expand All @@ -151,7 +159,9 @@ BOOST_AUTO_TEST_CASE(doubledash)

BOOST_AUTO_TEST_CASE(boolargno)
{
SetupArgs({"-foo", "-bar"});
const auto foo = std::make_pair("-foo", ArgsManager::ALLOW_BOOL);
const auto bar = std::make_pair("-bar", ArgsManager::ALLOW_BOOL);
SetupArgs({foo, bar});
ResetArgs("-nofoo");
BOOST_CHECK(!gArgs.GetBoolArg("-foo", true));
BOOST_CHECK(!gArgs.GetBoolArg("-foo", false));
Expand Down
48 changes: 35 additions & 13 deletions src/test/util_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <util/spanparsing.h>

#include <stdint.h>
#include <utility>
#include <vector>
#ifndef WIN32
#include <signal.h>
Expand Down Expand Up @@ -150,22 +151,26 @@ struct TestArgsManager : public ArgsManager
LOCK(cs_args);
m_network_only_args.insert(arg);
}
void SetupArgs(int argv, const char* args[])
void SetupArgs(const std::vector<std::pair<std::string, unsigned int>>& args)
{
for (int i = 0; i < argv; ++i) {
AddArg(args[i], "", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
for (const auto& arg : args) {
AddArg(arg.first, "", arg.second, OptionsCategory::OPTIONS);
}
}
};

BOOST_AUTO_TEST_CASE(util_ParseParameters)
{
TestArgsManager testArgs;
const char* avail_args[] = {"-a", "-b", "-ccc", "-d"};
const auto a = std::make_pair("-a", ArgsManager::ALLOW_ANY);
const auto b = std::make_pair("-b", ArgsManager::ALLOW_ANY);
const auto ccc = std::make_pair("-ccc", ArgsManager::ALLOW_ANY);
const auto d = std::make_pair("-d", ArgsManager::ALLOW_ANY);

const char *argv_test[] = {"-ignored", "-a", "-b", "-ccc=argument", "-ccc=multiple", "f", "-d=e"};

std::string error;
testArgs.SetupArgs(4, avail_args);
testArgs.SetupArgs({a, b, ccc, d});
testArgs.ParseParameters(0, (char**)argv_test, error);
BOOST_CHECK(testArgs.GetOverrideArgs().empty() && testArgs.GetConfigArgs().empty());

Expand Down Expand Up @@ -193,11 +198,17 @@ BOOST_AUTO_TEST_CASE(util_ParseParameters)
BOOST_AUTO_TEST_CASE(util_GetBoolArg)
{
TestArgsManager testArgs;
const char* avail_args[] = {"-a", "-b", "-c", "-d", "-e", "-f"};
const auto a = std::make_pair("-a", ArgsManager::ALLOW_BOOL);
const auto b = std::make_pair("-b", ArgsManager::ALLOW_BOOL);
const auto c = std::make_pair("-c", ArgsManager::ALLOW_BOOL);
const auto d = std::make_pair("-d", ArgsManager::ALLOW_BOOL);
const auto e = std::make_pair("-e", ArgsManager::ALLOW_BOOL);
const auto f = std::make_pair("-f", ArgsManager::ALLOW_BOOL);

const char *argv_test[] = {
"ignored", "-a", "-nob", "-c=0", "-d=1", "-e=false", "-f=true"};
std::string error;
testArgs.SetupArgs(6, avail_args);
testArgs.SetupArgs({a, b, c, d, e, f});
testArgs.ParseParameters(7, (char**)argv_test, error);

// Each letter should be set.
Expand Down Expand Up @@ -230,9 +241,10 @@ BOOST_AUTO_TEST_CASE(util_GetBoolArgEdgeCases)
TestArgsManager testArgs;

// Params test
const char* avail_args[] = {"-foo", "-bar"};
const auto foo = std::make_pair("-foo", ArgsManager::ALLOW_BOOL);
const auto bar = std::make_pair("-bar", ArgsManager::ALLOW_BOOL);
const char *argv_test[] = {"ignored", "-nofoo", "-foo", "-nobar=0"};
testArgs.SetupArgs(2, avail_args);
testArgs.SetupArgs({foo, bar});
std::string error;
testArgs.ParseParameters(4, (char**)argv_test, error);

Expand Down Expand Up @@ -301,8 +313,17 @@ BOOST_AUTO_TEST_CASE(util_ReadConfigStream)
"iii=2\n";

TestArgsManager test_args;
const char* avail_args[] = {"-a", "-b", "-ccc", "-d", "-e", "-fff", "-ggg", "-h", "-i", "-iii"};
test_args.SetupArgs(10, avail_args);
const auto a = std::make_pair("-a", ArgsManager::ALLOW_BOOL);
const auto b = std::make_pair("-b", ArgsManager::ALLOW_BOOL);
const auto ccc = std::make_pair("-ccc", ArgsManager::ALLOW_STRING);
const auto d = std::make_pair("-d", ArgsManager::ALLOW_STRING);
const auto e = std::make_pair("-e", ArgsManager::ALLOW_ANY);
const auto fff = std::make_pair("-fff", ArgsManager::ALLOW_BOOL);
const auto ggg = std::make_pair("-ggg", ArgsManager::ALLOW_BOOL);
const auto h = std::make_pair("-h", ArgsManager::ALLOW_BOOL);
const auto i = std::make_pair("-i", ArgsManager::ALLOW_BOOL);
const auto iii = std::make_pair("-iii", ArgsManager::ALLOW_INT);
test_args.SetupArgs({a, b, ccc, d, e, fff, ggg, h, i, iii});

test_args.ReadConfigString(str_config);
// expectation: a, b, ccc, d, fff, ggg, h, i end up in map
Expand Down Expand Up @@ -500,8 +521,9 @@ BOOST_AUTO_TEST_CASE(util_GetArg)
BOOST_AUTO_TEST_CASE(util_GetChainName)
{
TestArgsManager test_args;
const char* avail_args[] = {"-testnet", "-regtest"};
test_args.SetupArgs(2, avail_args);
const auto testnet = std::make_pair("-testnet", ArgsManager::ALLOW_BOOL);
const auto regtest = std::make_pair("-regtest", ArgsManager::ALLOW_BOOL);
test_args.SetupArgs({testnet, regtest});

const char* argv_testnet[] = {"cmd", "-testnet"};
const char* argv_regtest[] = {"cmd", "-regtest"};
Expand Down

0 comments on commit dbcc19e

Please sign in to comment.