|
5 | 5 | #include <util/system.h>
|
6 | 6 |
|
7 | 7 | #include <clientversion.h>
|
| 8 | +#include <optional.h> |
8 | 9 | #include <sync.h>
|
9 | 10 | #include <test/util/setup_common.h>
|
10 | 11 | #include <test/util/str.h>
|
@@ -189,12 +190,119 @@ struct TestArgsManager : public ArgsManager
|
189 | 190 | AddArg(arg.first, "", arg.second, OptionsCategory::OPTIONS);
|
190 | 191 | }
|
191 | 192 | }
|
| 193 | + using ArgsManager::GetSetting; |
| 194 | + using ArgsManager::GetSettingsList; |
192 | 195 | using ArgsManager::ReadConfigStream;
|
193 | 196 | using ArgsManager::cs_args;
|
194 | 197 | using ArgsManager::m_network;
|
195 | 198 | using ArgsManager::m_settings;
|
196 | 199 | };
|
197 | 200 |
|
| 201 | +//! Test GetSetting and GetArg type coercion, negation, and default value handling. |
| 202 | +class CheckValueTest : public TestChain100Setup |
| 203 | +{ |
| 204 | +public: |
| 205 | + struct Expect { |
| 206 | + util::SettingsValue setting; |
| 207 | + bool default_string = false; |
| 208 | + bool default_int = false; |
| 209 | + bool default_bool = false; |
| 210 | + const char* string_value = nullptr; |
| 211 | + Optional<int64_t> int_value; |
| 212 | + Optional<bool> bool_value; |
| 213 | + Optional<std::vector<std::string>> list_value; |
| 214 | + const char* error = nullptr; |
| 215 | + |
| 216 | + Expect(util::SettingsValue s) : setting(std::move(s)) {} |
| 217 | + Expect& DefaultString() { default_string = true; return *this; } |
| 218 | + Expect& DefaultInt() { default_int = true; return *this; } |
| 219 | + Expect& DefaultBool() { default_bool = true; return *this; } |
| 220 | + Expect& String(const char* s) { string_value = s; return *this; } |
| 221 | + Expect& Int(int64_t i) { int_value = i; return *this; } |
| 222 | + Expect& Bool(bool b) { bool_value = b; return *this; } |
| 223 | + Expect& List(std::vector<std::string> m) { list_value = std::move(m); return *this; } |
| 224 | + Expect& Error(const char* e) { error = e; return *this; } |
| 225 | + }; |
| 226 | + |
| 227 | + void CheckValue(unsigned int flags, const char* arg, const Expect& expect) |
| 228 | + { |
| 229 | + TestArgsManager test; |
| 230 | + test.SetupArgs({{"-value", flags}}); |
| 231 | + const char* argv[] = {"ignored", arg}; |
| 232 | + std::string error; |
| 233 | + bool success = test.ParseParameters(arg ? 2 : 1, (char**)argv, error); |
| 234 | + |
| 235 | + BOOST_CHECK_EQUAL(test.GetSetting("-value").write(), expect.setting.write()); |
| 236 | + auto settings_list = test.GetSettingsList("-value"); |
| 237 | + if (expect.setting.isNull() || expect.setting.isFalse()) { |
| 238 | + BOOST_CHECK_EQUAL(settings_list.size(), 0); |
| 239 | + } else { |
| 240 | + BOOST_CHECK_EQUAL(settings_list.size(), 1); |
| 241 | + BOOST_CHECK_EQUAL(settings_list[0].write(), expect.setting.write()); |
| 242 | + } |
| 243 | + |
| 244 | + if (expect.error) { |
| 245 | + BOOST_CHECK(!success); |
| 246 | + BOOST_CHECK_NE(error.find(expect.error), std::string::npos); |
| 247 | + } else { |
| 248 | + BOOST_CHECK(success); |
| 249 | + BOOST_CHECK_EQUAL(error, ""); |
| 250 | + } |
| 251 | + |
| 252 | + if (expect.default_string) { |
| 253 | + BOOST_CHECK_EQUAL(test.GetArg("-value", "zzzzz"), "zzzzz"); |
| 254 | + } else if (expect.string_value) { |
| 255 | + BOOST_CHECK_EQUAL(test.GetArg("-value", "zzzzz"), expect.string_value); |
| 256 | + } else { |
| 257 | + BOOST_CHECK(!success); |
| 258 | + } |
| 259 | + |
| 260 | + if (expect.default_int) { |
| 261 | + BOOST_CHECK_EQUAL(test.GetArg("-value", 99999), 99999); |
| 262 | + } else if (expect.int_value) { |
| 263 | + BOOST_CHECK_EQUAL(test.GetArg("-value", 99999), *expect.int_value); |
| 264 | + } else { |
| 265 | + BOOST_CHECK(!success); |
| 266 | + } |
| 267 | + |
| 268 | + if (expect.default_bool) { |
| 269 | + BOOST_CHECK_EQUAL(test.GetBoolArg("-value", false), false); |
| 270 | + BOOST_CHECK_EQUAL(test.GetBoolArg("-value", true), true); |
| 271 | + } else if (expect.bool_value) { |
| 272 | + BOOST_CHECK_EQUAL(test.GetBoolArg("-value", false), *expect.bool_value); |
| 273 | + BOOST_CHECK_EQUAL(test.GetBoolArg("-value", true), *expect.bool_value); |
| 274 | + } else { |
| 275 | + BOOST_CHECK(!success); |
| 276 | + } |
| 277 | + |
| 278 | + if (expect.list_value) { |
| 279 | + auto l = test.GetArgs("-value"); |
| 280 | + BOOST_CHECK_EQUAL_COLLECTIONS(l.begin(), l.end(), expect.list_value->begin(), expect.list_value->end()); |
| 281 | + } else { |
| 282 | + BOOST_CHECK(!success); |
| 283 | + } |
| 284 | + } |
| 285 | +}; |
| 286 | + |
| 287 | +BOOST_FIXTURE_TEST_CASE(util_CheckValue, CheckValueTest) |
| 288 | +{ |
| 289 | + using M = ArgsManager; |
| 290 | + |
| 291 | + CheckValue(M::ALLOW_ANY, nullptr, Expect{{}}.DefaultString().DefaultInt().DefaultBool().List({})); |
| 292 | + CheckValue(M::ALLOW_ANY, "-novalue", Expect{false}.String("0").Int(0).Bool(false).List({})); |
| 293 | + CheckValue(M::ALLOW_ANY, "-novalue=", Expect{false}.String("0").Int(0).Bool(false).List({})); |
| 294 | + CheckValue(M::ALLOW_ANY, "-novalue=0", Expect{true}.String("1").Int(1).Bool(true).List({"1"})); |
| 295 | + CheckValue(M::ALLOW_ANY, "-novalue=1", Expect{false}.String("0").Int(0).Bool(false).List({})); |
| 296 | + CheckValue(M::ALLOW_ANY, "-novalue=2", Expect{false}.String("0").Int(0).Bool(false).List({})); |
| 297 | + CheckValue(M::ALLOW_ANY, "-novalue=abc", Expect{true}.String("1").Int(1).Bool(true).List({"1"})); |
| 298 | + CheckValue(M::ALLOW_ANY, "-value", Expect{""}.String("").Int(0).Bool(true).List({""})); |
| 299 | + CheckValue(M::ALLOW_ANY, "-value=", Expect{""}.String("").Int(0).Bool(true).List({""})); |
| 300 | + CheckValue(M::ALLOW_ANY, "-value=0", Expect{"0"}.String("0").Int(0).Bool(false).List({"0"})); |
| 301 | + CheckValue(M::ALLOW_ANY, "-value=1", Expect{"1"}.String("1").Int(1).Bool(true).List({"1"})); |
| 302 | + CheckValue(M::ALLOW_ANY, "-value=2", Expect{"2"}.String("2").Int(2).Bool(true).List({"2"})); |
| 303 | + CheckValue(M::ALLOW_ANY, "-value=abc", Expect{"abc"}.String("abc").Int(0).Bool(false).List({"abc"})); |
| 304 | +} |
| 305 | + |
198 | 306 | BOOST_AUTO_TEST_CASE(util_ParseParameters)
|
199 | 307 | {
|
200 | 308 | TestArgsManager testArgs;
|
@@ -289,12 +397,12 @@ BOOST_AUTO_TEST_CASE(util_ArgParsing)
|
289 | 397 | BOOST_AUTO_TEST_CASE(util_GetBoolArg)
|
290 | 398 | {
|
291 | 399 | TestArgsManager testArgs;
|
292 |
| - const auto a = std::make_pair("-a", ArgsManager::ALLOW_BOOL); |
293 |
| - const auto b = std::make_pair("-b", ArgsManager::ALLOW_BOOL); |
294 |
| - const auto c = std::make_pair("-c", ArgsManager::ALLOW_BOOL); |
295 |
| - const auto d = std::make_pair("-d", ArgsManager::ALLOW_BOOL); |
296 |
| - const auto e = std::make_pair("-e", ArgsManager::ALLOW_BOOL); |
297 |
| - const auto f = std::make_pair("-f", ArgsManager::ALLOW_BOOL); |
| 400 | + const auto a = std::make_pair("-a", ArgsManager::ALLOW_ANY); |
| 401 | + const auto b = std::make_pair("-b", ArgsManager::ALLOW_ANY); |
| 402 | + const auto c = std::make_pair("-c", ArgsManager::ALLOW_ANY); |
| 403 | + const auto d = std::make_pair("-d", ArgsManager::ALLOW_ANY); |
| 404 | + const auto e = std::make_pair("-e", ArgsManager::ALLOW_ANY); |
| 405 | + const auto f = std::make_pair("-f", ArgsManager::ALLOW_ANY); |
298 | 406 |
|
299 | 407 | const char *argv_test[] = {
|
300 | 408 | "ignored", "-a", "-nob", "-c=0", "-d=1", "-e=false", "-f=true"};
|
@@ -333,8 +441,8 @@ BOOST_AUTO_TEST_CASE(util_GetBoolArgEdgeCases)
|
333 | 441 | TestArgsManager testArgs;
|
334 | 442 |
|
335 | 443 | // Params test
|
336 |
| - const auto foo = std::make_pair("-foo", ArgsManager::ALLOW_BOOL); |
337 |
| - const auto bar = std::make_pair("-bar", ArgsManager::ALLOW_BOOL); |
| 444 | + const auto foo = std::make_pair("-foo", ArgsManager::ALLOW_ANY); |
| 445 | + const auto bar = std::make_pair("-bar", ArgsManager::ALLOW_ANY); |
338 | 446 | const char *argv_test[] = {"ignored", "-nofoo", "-foo", "-nobar=0"};
|
339 | 447 | testArgs.SetupArgs({foo, bar});
|
340 | 448 | std::string error;
|
@@ -406,16 +514,16 @@ BOOST_AUTO_TEST_CASE(util_ReadConfigStream)
|
406 | 514 |
|
407 | 515 | TestArgsManager test_args;
|
408 | 516 | LOCK(test_args.cs_args);
|
409 |
| - const auto a = std::make_pair("-a", ArgsManager::ALLOW_BOOL); |
410 |
| - const auto b = std::make_pair("-b", ArgsManager::ALLOW_BOOL); |
411 |
| - const auto ccc = std::make_pair("-ccc", ArgsManager::ALLOW_STRING); |
412 |
| - const auto d = std::make_pair("-d", ArgsManager::ALLOW_STRING); |
| 517 | + const auto a = std::make_pair("-a", ArgsManager::ALLOW_ANY); |
| 518 | + const auto b = std::make_pair("-b", ArgsManager::ALLOW_ANY); |
| 519 | + const auto ccc = std::make_pair("-ccc", ArgsManager::ALLOW_ANY); |
| 520 | + const auto d = std::make_pair("-d", ArgsManager::ALLOW_ANY); |
413 | 521 | const auto e = std::make_pair("-e", ArgsManager::ALLOW_ANY);
|
414 |
| - const auto fff = std::make_pair("-fff", ArgsManager::ALLOW_BOOL); |
415 |
| - const auto ggg = std::make_pair("-ggg", ArgsManager::ALLOW_BOOL); |
416 |
| - const auto h = std::make_pair("-h", ArgsManager::ALLOW_BOOL); |
417 |
| - const auto i = std::make_pair("-i", ArgsManager::ALLOW_BOOL); |
418 |
| - const auto iii = std::make_pair("-iii", ArgsManager::ALLOW_INT); |
| 522 | + const auto fff = std::make_pair("-fff", ArgsManager::ALLOW_ANY); |
| 523 | + const auto ggg = std::make_pair("-ggg", ArgsManager::ALLOW_ANY); |
| 524 | + const auto h = std::make_pair("-h", ArgsManager::ALLOW_ANY); |
| 525 | + const auto i = std::make_pair("-i", ArgsManager::ALLOW_ANY); |
| 526 | + const auto iii = std::make_pair("-iii", ArgsManager::ALLOW_ANY); |
419 | 527 | test_args.SetupArgs({a, b, ccc, d, e, fff, ggg, h, i, iii});
|
420 | 528 |
|
421 | 529 | test_args.ReadConfigString(str_config);
|
@@ -618,8 +726,8 @@ BOOST_AUTO_TEST_CASE(util_GetArg)
|
618 | 726 | BOOST_AUTO_TEST_CASE(util_GetChainName)
|
619 | 727 | {
|
620 | 728 | TestArgsManager test_args;
|
621 |
| - const auto testnet = std::make_pair("-testnet", ArgsManager::ALLOW_BOOL); |
622 |
| - const auto regtest = std::make_pair("-regtest", ArgsManager::ALLOW_BOOL); |
| 729 | + const auto testnet = std::make_pair("-testnet", ArgsManager::ALLOW_ANY); |
| 730 | + const auto regtest = std::make_pair("-regtest", ArgsManager::ALLOW_ANY); |
623 | 731 | test_args.SetupArgs({testnet, regtest});
|
624 | 732 |
|
625 | 733 | const char* argv_testnet[] = {"cmd", "-testnet"};
|
|
0 commit comments