@@ -190,6 +190,11 @@ struct TestArgsManager : public ArgsManager
190
190
std::map<std::string, std::string>& GetMapArgs () { return mapArgs; }
191
191
const std::map<std::string, std::vector<std::string> >& GetMapMultiArgs () { return mapMultiArgs; }
192
192
const std::unordered_set<std::string>& GetNegatedArgs () { return m_negated_args; }
193
+ void ReadConfigString (const std::string str_config)
194
+ {
195
+ std::istringstream stream (str_config);
196
+ ReadConfigStream (stream);
197
+ }
193
198
};
194
199
195
200
BOOST_AUTO_TEST_CASE (util_ParseParameters)
@@ -253,16 +258,154 @@ BOOST_AUTO_TEST_CASE(util_GetBoolArgEdgeCases)
253
258
{
254
259
// Test some awful edge cases that hopefully no user will ever exercise.
255
260
TestArgsManager testArgs;
261
+
262
+ // Params test
256
263
const char *argv_test[] = {" ignored" , " -nofoo" , " -foo" , " -nobar=0" };
257
264
testArgs.ParseParameters (4 , (char **)argv_test);
258
265
259
266
// This was passed twice, second one overrides the negative setting.
260
267
BOOST_CHECK (!testArgs.IsArgNegated (" -foo" ));
261
- BOOST_CHECK (testArgs.GetBoolArg (" -foo" , false ) == true );
268
+ BOOST_CHECK (testArgs.GetArg (" -foo" , " xxx" ) == " " );
269
+
270
+ // A double negative is a positive.
271
+ BOOST_CHECK (testArgs.IsArgNegated (" -bar" ));
272
+ BOOST_CHECK (testArgs.GetArg (" -bar" , " xxx" ) == " 1" );
273
+
274
+ // Config test
275
+ const char *conf_test = " nofoo=1\n foo=1\n nobar=0\n " ;
276
+ testArgs.ParseParameters (1 , (char **)argv_test);
277
+ testArgs.ReadConfigString (conf_test);
278
+
279
+ // This was passed twice, second one overrides the negative setting,
280
+ // but not the value.
281
+ BOOST_CHECK (!testArgs.IsArgNegated (" -foo" ));
282
+ BOOST_CHECK (testArgs.GetArg (" -foo" , " xxx" ) == " 0" );
262
283
263
284
// A double negative is a positive.
264
285
BOOST_CHECK (testArgs.IsArgNegated (" -bar" ));
265
- BOOST_CHECK (testArgs.GetBoolArg (" -bar" , false ) == true );
286
+ BOOST_CHECK (testArgs.GetArg (" -bar" , " xxx" ) == " 1" );
287
+
288
+ // Combined test
289
+ const char *combo_test_args[] = {" ignored" , " -nofoo" , " -bar" };
290
+ const char *combo_test_conf = " foo=1\n nobar=1\n " ;
291
+ testArgs.ParseParameters (3 , (char **)combo_test_args);
292
+ testArgs.ReadConfigString (combo_test_conf);
293
+
294
+ // Command line overrides, but doesn't erase old setting
295
+ BOOST_CHECK (!testArgs.IsArgNegated (" -foo" ));
296
+ BOOST_CHECK (testArgs.GetArg (" -foo" , " xxx" ) == " 0" );
297
+ BOOST_CHECK (testArgs.GetArgs (" -foo" ).size () == 2
298
+ && testArgs.GetArgs (" -foo" ).front () == " 0"
299
+ && testArgs.GetArgs (" -foo" ).back () == " 1" );
300
+
301
+ // Command line overrides, but doesn't erase old setting
302
+ BOOST_CHECK (testArgs.IsArgNegated (" -bar" ));
303
+ BOOST_CHECK (testArgs.GetArg (" -bar" , " xxx" ) == " " );
304
+ BOOST_CHECK (testArgs.GetArgs (" -bar" ).size () == 2
305
+ && testArgs.GetArgs (" -bar" ).front () == " "
306
+ && testArgs.GetArgs (" -bar" ).back () == " 0" );
307
+ }
308
+
309
+ BOOST_AUTO_TEST_CASE (util_ReadConfigStream)
310
+ {
311
+ const char *str_config =
312
+ " a=\n "
313
+ " b=1\n "
314
+ " ccc=argument\n "
315
+ " ccc=multiple\n "
316
+ " d=e\n "
317
+ " nofff=1\n "
318
+ " noggg=0\n "
319
+ " h=1\n "
320
+ " noh=1\n "
321
+ " noi=1\n "
322
+ " i=1\n " ;
323
+
324
+ TestArgsManager test_args;
325
+
326
+ test_args.ReadConfigString (str_config);
327
+ // expectation: a, b, ccc, d, fff, ggg, h, i end up in map
328
+
329
+ BOOST_CHECK (test_args.GetMapArgs ().size () == 8 );
330
+ BOOST_CHECK (test_args.GetMapMultiArgs ().size () == 8 );
331
+
332
+ BOOST_CHECK (test_args.GetMapArgs ().count (" -a" )
333
+ && test_args.GetMapArgs ().count (" -b" )
334
+ && test_args.GetMapArgs ().count (" -ccc" )
335
+ && test_args.GetMapArgs ().count (" -d" )
336
+ && test_args.GetMapArgs ().count (" -fff" )
337
+ && test_args.GetMapArgs ().count (" -ggg" )
338
+ && test_args.GetMapArgs ().count (" -h" )
339
+ && test_args.GetMapArgs ().count (" -i" )
340
+ );
341
+
342
+ BOOST_CHECK (test_args.IsArgSet (" -a" )
343
+ && test_args.IsArgSet (" -b" )
344
+ && test_args.IsArgSet (" -ccc" )
345
+ && test_args.IsArgSet (" -d" )
346
+ && test_args.IsArgSet (" -fff" )
347
+ && test_args.IsArgSet (" -ggg" )
348
+ && test_args.IsArgSet (" -h" )
349
+ && test_args.IsArgSet (" -i" )
350
+ && !test_args.IsArgSet (" -zzz" )
351
+ );
352
+
353
+ BOOST_CHECK (test_args.GetArg (" -a" , " xxx" ) == " "
354
+ && test_args.GetArg (" -b" , " xxx" ) == " 1"
355
+ && test_args.GetArg (" -ccc" , " xxx" ) == " argument"
356
+ && test_args.GetArg (" -d" , " xxx" ) == " e"
357
+ && test_args.GetArg (" -fff" , " xxx" ) == " 0"
358
+ && test_args.GetArg (" -ggg" , " xxx" ) == " 1"
359
+ && test_args.GetArg (" -h" , " xxx" ) == " 1" // 1st value takes precedence
360
+ && test_args.GetArg (" -i" , " xxx" ) == " 0" // 1st value takes precedence
361
+ && test_args.GetArg (" -zzz" , " xxx" ) == " xxx"
362
+ );
363
+
364
+ for (bool def : {false , true }) {
365
+ BOOST_CHECK (test_args.GetBoolArg (" -a" , def)
366
+ && test_args.GetBoolArg (" -b" , def)
367
+ && !test_args.GetBoolArg (" -ccc" , def)
368
+ && !test_args.GetBoolArg (" -d" , def)
369
+ && !test_args.GetBoolArg (" -fff" , def)
370
+ && test_args.GetBoolArg (" -ggg" , def)
371
+ && test_args.GetBoolArg (" -h" , def)
372
+ && !test_args.GetBoolArg (" -i" , def)
373
+ && test_args.GetBoolArg (" -zzz" , def) == def
374
+ );
375
+ }
376
+
377
+ BOOST_CHECK (test_args.GetArgs (" -a" ).size () == 1
378
+ && test_args.GetArgs (" -a" ).front () == " " );
379
+ BOOST_CHECK (test_args.GetArgs (" -b" ).size () == 1
380
+ && test_args.GetArgs (" -b" ).front () == " 1" );
381
+ BOOST_CHECK (test_args.GetArgs (" -ccc" ).size () == 2
382
+ && test_args.GetArgs (" -ccc" ).front () == " argument"
383
+ && test_args.GetArgs (" -ccc" ).back () == " multiple" );
384
+ BOOST_CHECK (test_args.GetArgs (" -fff" ).size () == 1
385
+ && test_args.GetArgs (" -fff" ).front () == " 0" );
386
+ BOOST_CHECK (test_args.GetArgs (" -nofff" ).size () == 0 );
387
+ BOOST_CHECK (test_args.GetArgs (" -ggg" ).size () == 1
388
+ && test_args.GetArgs (" -ggg" ).front () == " 1" );
389
+ BOOST_CHECK (test_args.GetArgs (" -noggg" ).size () == 0 );
390
+ BOOST_CHECK (test_args.GetArgs (" -h" ).size () == 2
391
+ && test_args.GetArgs (" -h" ).front () == " 1"
392
+ && test_args.GetArgs (" -h" ).back () == " 0" );
393
+ BOOST_CHECK (test_args.GetArgs (" -noh" ).size () == 0 );
394
+ BOOST_CHECK (test_args.GetArgs (" -i" ).size () == 2
395
+ && test_args.GetArgs (" -i" ).front () == " 0"
396
+ && test_args.GetArgs (" -i" ).back () == " 1" );
397
+ BOOST_CHECK (test_args.GetArgs (" -noi" ).size () == 0 );
398
+ BOOST_CHECK (test_args.GetArgs (" -zzz" ).size () == 0 );
399
+
400
+ BOOST_CHECK (!test_args.IsArgNegated (" -a" ));
401
+ BOOST_CHECK (!test_args.IsArgNegated (" -b" ));
402
+ BOOST_CHECK (!test_args.IsArgNegated (" -ccc" ));
403
+ BOOST_CHECK (!test_args.IsArgNegated (" -d" ));
404
+ BOOST_CHECK (test_args.IsArgNegated (" -fff" ));
405
+ BOOST_CHECK (test_args.IsArgNegated (" -ggg" )); // IsArgNegated==true when noggg=0
406
+ BOOST_CHECK (test_args.IsArgNegated (" -h" )); // last setting takes precedence
407
+ BOOST_CHECK (!test_args.IsArgNegated (" -i" )); // last setting takes precedence
408
+ BOOST_CHECK (!test_args.IsArgNegated (" -zzz" ));
266
409
}
267
410
268
411
BOOST_AUTO_TEST_CASE (util_GetArg)
@@ -290,6 +433,54 @@ BOOST_AUTO_TEST_CASE(util_GetArg)
290
433
BOOST_CHECK_EQUAL (testArgs.GetBoolArg (" booltest4" , false ), true );
291
434
}
292
435
436
+ BOOST_AUTO_TEST_CASE (util_GetChainName)
437
+ {
438
+ TestArgsManager test_args;
439
+
440
+ const char * argv_testnet[] = {" cmd" , " -testnet" };
441
+ const char * argv_regtest[] = {" cmd" , " -regtest" };
442
+ const char * argv_test_no_reg[] = {" cmd" , " -testnet" , " -noregtest" };
443
+ const char * argv_both[] = {" cmd" , " -testnet" , " -regtest" };
444
+
445
+ // equivalent to "-testnet"
446
+ const char * testnetconf = " testnet=1\n regtest=0\n " ;
447
+
448
+ test_args.ParseParameters (0 , (char **)argv_testnet);
449
+ BOOST_CHECK_EQUAL (test_args.GetChainName (), " main" );
450
+
451
+ test_args.ParseParameters (2 , (char **)argv_testnet);
452
+ BOOST_CHECK_EQUAL (test_args.GetChainName (), " test" );
453
+
454
+ test_args.ParseParameters (2 , (char **)argv_regtest);
455
+ BOOST_CHECK_EQUAL (test_args.GetChainName (), " regtest" );
456
+
457
+ test_args.ParseParameters (3 , (char **)argv_test_no_reg);
458
+ BOOST_CHECK_EQUAL (test_args.GetChainName (), " test" );
459
+
460
+ test_args.ParseParameters (3 , (char **)argv_both);
461
+ BOOST_CHECK_THROW (test_args.GetChainName (), std::runtime_error);
462
+
463
+ test_args.ParseParameters (0 , (char **)argv_testnet);
464
+ test_args.ReadConfigString (testnetconf);
465
+ BOOST_CHECK_EQUAL (test_args.GetChainName (), " test" );
466
+
467
+ test_args.ParseParameters (2 , (char **)argv_testnet);
468
+ test_args.ReadConfigString (testnetconf);
469
+ BOOST_CHECK_EQUAL (test_args.GetChainName (), " test" );
470
+
471
+ test_args.ParseParameters (2 , (char **)argv_regtest);
472
+ test_args.ReadConfigString (testnetconf);
473
+ BOOST_CHECK_THROW (test_args.GetChainName (), std::runtime_error);
474
+
475
+ test_args.ParseParameters (3 , (char **)argv_test_no_reg);
476
+ test_args.ReadConfigString (testnetconf);
477
+ BOOST_CHECK_EQUAL (test_args.GetChainName (), " test" );
478
+
479
+ test_args.ParseParameters (3 , (char **)argv_both);
480
+ test_args.ReadConfigString (testnetconf);
481
+ BOOST_CHECK_THROW (test_args.GetChainName (), std::runtime_error);
482
+ }
483
+
293
484
BOOST_AUTO_TEST_CASE (util_FormatMoney)
294
485
{
295
486
BOOST_CHECK_EQUAL (FormatMoney (0 ), " 0.00" );
0 commit comments