You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

getarg_tests.cpp 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Copyright (c) 2012-2013 The Bitcoin Core developers
  2. // Distributed under the MIT software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #include "util.h"
  5. #include "test/test_bitcoin.h"
  6. #include <string>
  7. #include <vector>
  8. #include <boost/algorithm/string.hpp>
  9. #include <boost/foreach.hpp>
  10. #include <boost/test/unit_test.hpp>
  11. BOOST_FIXTURE_TEST_SUITE(getarg_tests, BasicTestingSetup)
  12. static void ResetArgs(const std::string& strArg)
  13. {
  14. std::vector<std::string> vecArg;
  15. if (strArg.size())
  16. boost::split(vecArg, strArg, boost::is_space(), boost::token_compress_on);
  17. // Insert dummy executable name:
  18. vecArg.insert(vecArg.begin(), "testbitcoin");
  19. // Convert to char*:
  20. std::vector<const char*> vecChar;
  21. BOOST_FOREACH(std::string& s, vecArg)
  22. vecChar.push_back(s.c_str());
  23. ParseParameters(vecChar.size(), &vecChar[0]);
  24. }
  25. BOOST_AUTO_TEST_CASE(boolarg)
  26. {
  27. ResetArgs("-foo");
  28. BOOST_CHECK(GetBoolArg("-foo", false));
  29. BOOST_CHECK(GetBoolArg("-foo", true));
  30. BOOST_CHECK(!GetBoolArg("-fo", false));
  31. BOOST_CHECK(GetBoolArg("-fo", true));
  32. BOOST_CHECK(!GetBoolArg("-fooo", false));
  33. BOOST_CHECK(GetBoolArg("-fooo", true));
  34. ResetArgs("-foo=0");
  35. BOOST_CHECK(!GetBoolArg("-foo", false));
  36. BOOST_CHECK(!GetBoolArg("-foo", true));
  37. ResetArgs("-foo=1");
  38. BOOST_CHECK(GetBoolArg("-foo", false));
  39. BOOST_CHECK(GetBoolArg("-foo", true));
  40. // New 0.6 feature: auto-map -nosomething to !-something:
  41. ResetArgs("-nofoo");
  42. BOOST_CHECK(!GetBoolArg("-foo", false));
  43. BOOST_CHECK(!GetBoolArg("-foo", true));
  44. ResetArgs("-nofoo=1");
  45. BOOST_CHECK(!GetBoolArg("-foo", false));
  46. BOOST_CHECK(!GetBoolArg("-foo", true));
  47. ResetArgs("-foo -nofoo"); // -foo should win
  48. BOOST_CHECK(GetBoolArg("-foo", false));
  49. BOOST_CHECK(GetBoolArg("-foo", true));
  50. ResetArgs("-foo=1 -nofoo=1"); // -foo should win
  51. BOOST_CHECK(GetBoolArg("-foo", false));
  52. BOOST_CHECK(GetBoolArg("-foo", true));
  53. ResetArgs("-foo=0 -nofoo=0"); // -foo should win
  54. BOOST_CHECK(!GetBoolArg("-foo", false));
  55. BOOST_CHECK(!GetBoolArg("-foo", true));
  56. // New 0.6 feature: treat -- same as -:
  57. ResetArgs("--foo=1");
  58. BOOST_CHECK(GetBoolArg("-foo", false));
  59. BOOST_CHECK(GetBoolArg("-foo", true));
  60. ResetArgs("--nofoo=1");
  61. BOOST_CHECK(!GetBoolArg("-foo", false));
  62. BOOST_CHECK(!GetBoolArg("-foo", true));
  63. }
  64. BOOST_AUTO_TEST_CASE(stringarg)
  65. {
  66. ResetArgs("");
  67. BOOST_CHECK_EQUAL(GetArg("-foo", ""), "");
  68. BOOST_CHECK_EQUAL(GetArg("-foo", "eleven"), "eleven");
  69. ResetArgs("-foo -bar");
  70. BOOST_CHECK_EQUAL(GetArg("-foo", ""), "");
  71. BOOST_CHECK_EQUAL(GetArg("-foo", "eleven"), "");
  72. ResetArgs("-foo=");
  73. BOOST_CHECK_EQUAL(GetArg("-foo", ""), "");
  74. BOOST_CHECK_EQUAL(GetArg("-foo", "eleven"), "");
  75. ResetArgs("-foo=11");
  76. BOOST_CHECK_EQUAL(GetArg("-foo", ""), "11");
  77. BOOST_CHECK_EQUAL(GetArg("-foo", "eleven"), "11");
  78. ResetArgs("-foo=eleven");
  79. BOOST_CHECK_EQUAL(GetArg("-foo", ""), "eleven");
  80. BOOST_CHECK_EQUAL(GetArg("-foo", "eleven"), "eleven");
  81. }
  82. BOOST_AUTO_TEST_CASE(intarg)
  83. {
  84. ResetArgs("");
  85. BOOST_CHECK_EQUAL(GetArg("-foo", 11), 11);
  86. BOOST_CHECK_EQUAL(GetArg("-foo", 0), 0);
  87. ResetArgs("-foo -bar");
  88. BOOST_CHECK_EQUAL(GetArg("-foo", 11), 0);
  89. BOOST_CHECK_EQUAL(GetArg("-bar", 11), 0);
  90. ResetArgs("-foo=11 -bar=12");
  91. BOOST_CHECK_EQUAL(GetArg("-foo", 0), 11);
  92. BOOST_CHECK_EQUAL(GetArg("-bar", 11), 12);
  93. ResetArgs("-foo=NaN -bar=NotANumber");
  94. BOOST_CHECK_EQUAL(GetArg("-foo", 1), 0);
  95. BOOST_CHECK_EQUAL(GetArg("-bar", 11), 0);
  96. }
  97. BOOST_AUTO_TEST_CASE(doubledash)
  98. {
  99. ResetArgs("--foo");
  100. BOOST_CHECK_EQUAL(GetBoolArg("-foo", false), true);
  101. ResetArgs("--foo=verbose --bar=1");
  102. BOOST_CHECK_EQUAL(GetArg("-foo", ""), "verbose");
  103. BOOST_CHECK_EQUAL(GetArg("-bar", 0), 1);
  104. }
  105. BOOST_AUTO_TEST_CASE(boolargno)
  106. {
  107. ResetArgs("-nofoo");
  108. BOOST_CHECK(!GetBoolArg("-foo", true));
  109. BOOST_CHECK(!GetBoolArg("-foo", false));
  110. ResetArgs("-nofoo=1");
  111. BOOST_CHECK(!GetBoolArg("-foo", true));
  112. BOOST_CHECK(!GetBoolArg("-foo", false));
  113. ResetArgs("-nofoo=0");
  114. BOOST_CHECK(GetBoolArg("-foo", true));
  115. BOOST_CHECK(GetBoolArg("-foo", false));
  116. ResetArgs("-foo --nofoo");
  117. BOOST_CHECK(GetBoolArg("-foo", true));
  118. BOOST_CHECK(GetBoolArg("-foo", false));
  119. ResetArgs("-nofoo -foo"); // foo always wins:
  120. BOOST_CHECK(GetBoolArg("-foo", true));
  121. BOOST_CHECK(GetBoolArg("-foo", false));
  122. }
  123. BOOST_AUTO_TEST_SUITE_END()