1 // Copyright (c) 2012-2015 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.
6 #include "test/test_bitcoin.h"
11 #include <boost/algorithm/string.hpp>
12 #include <boost/foreach.hpp>
13 #include <boost/test/unit_test.hpp>
15 BOOST_FIXTURE_TEST_SUITE(getarg_tests
, BasicTestingSetup
)
17 static void ResetArgs(const std::string
& strArg
)
19 std::vector
<std::string
> vecArg
;
21 boost::split(vecArg
, strArg
, boost::is_space(), boost::token_compress_on
);
23 // Insert dummy executable name:
24 vecArg
.insert(vecArg
.begin(), "testbitcoin");
27 std::vector
<const char*> vecChar
;
28 BOOST_FOREACH(std::string
& s
, vecArg
)
29 vecChar
.push_back(s
.c_str());
31 ParseParameters(vecChar
.size(), &vecChar
[0]);
34 BOOST_AUTO_TEST_CASE(boolarg
)
37 BOOST_CHECK(GetBoolArg("-foo", false));
38 BOOST_CHECK(GetBoolArg("-foo", true));
40 BOOST_CHECK(!GetBoolArg("-fo", false));
41 BOOST_CHECK(GetBoolArg("-fo", true));
43 BOOST_CHECK(!GetBoolArg("-fooo", false));
44 BOOST_CHECK(GetBoolArg("-fooo", true));
47 BOOST_CHECK(!GetBoolArg("-foo", false));
48 BOOST_CHECK(!GetBoolArg("-foo", true));
51 BOOST_CHECK(GetBoolArg("-foo", false));
52 BOOST_CHECK(GetBoolArg("-foo", true));
54 // New 0.6 feature: auto-map -nosomething to !-something:
56 BOOST_CHECK(!GetBoolArg("-foo", false));
57 BOOST_CHECK(!GetBoolArg("-foo", true));
59 ResetArgs("-nofoo=1");
60 BOOST_CHECK(!GetBoolArg("-foo", false));
61 BOOST_CHECK(!GetBoolArg("-foo", true));
63 ResetArgs("-foo -nofoo"); // -nofoo should win
64 BOOST_CHECK(!GetBoolArg("-foo", false));
65 BOOST_CHECK(!GetBoolArg("-foo", true));
67 ResetArgs("-foo=1 -nofoo=1"); // -nofoo should win
68 BOOST_CHECK(!GetBoolArg("-foo", false));
69 BOOST_CHECK(!GetBoolArg("-foo", true));
71 ResetArgs("-foo=0 -nofoo=0"); // -nofoo=0 should win
72 BOOST_CHECK(GetBoolArg("-foo", false));
73 BOOST_CHECK(GetBoolArg("-foo", true));
75 // New 0.6 feature: treat -- same as -:
77 BOOST_CHECK(GetBoolArg("-foo", false));
78 BOOST_CHECK(GetBoolArg("-foo", true));
80 ResetArgs("--nofoo=1");
81 BOOST_CHECK(!GetBoolArg("-foo", false));
82 BOOST_CHECK(!GetBoolArg("-foo", true));
86 BOOST_AUTO_TEST_CASE(stringarg
)
89 BOOST_CHECK_EQUAL(GetArg("-foo", ""), "");
90 BOOST_CHECK_EQUAL(GetArg("-foo", "eleven"), "eleven");
92 ResetArgs("-foo -bar");
93 BOOST_CHECK_EQUAL(GetArg("-foo", ""), "");
94 BOOST_CHECK_EQUAL(GetArg("-foo", "eleven"), "");
97 BOOST_CHECK_EQUAL(GetArg("-foo", ""), "");
98 BOOST_CHECK_EQUAL(GetArg("-foo", "eleven"), "");
100 ResetArgs("-foo=11");
101 BOOST_CHECK_EQUAL(GetArg("-foo", ""), "11");
102 BOOST_CHECK_EQUAL(GetArg("-foo", "eleven"), "11");
104 ResetArgs("-foo=eleven");
105 BOOST_CHECK_EQUAL(GetArg("-foo", ""), "eleven");
106 BOOST_CHECK_EQUAL(GetArg("-foo", "eleven"), "eleven");
110 BOOST_AUTO_TEST_CASE(intarg
)
113 BOOST_CHECK_EQUAL(GetArg("-foo", 11), 11);
114 BOOST_CHECK_EQUAL(GetArg("-foo", 0), 0);
116 ResetArgs("-foo -bar");
117 BOOST_CHECK_EQUAL(GetArg("-foo", 11), 0);
118 BOOST_CHECK_EQUAL(GetArg("-bar", 11), 0);
120 ResetArgs("-foo=11 -bar=12");
121 BOOST_CHECK_EQUAL(GetArg("-foo", 0), 11);
122 BOOST_CHECK_EQUAL(GetArg("-bar", 11), 12);
124 ResetArgs("-foo=NaN -bar=NotANumber");
125 BOOST_CHECK_EQUAL(GetArg("-foo", 1), 0);
126 BOOST_CHECK_EQUAL(GetArg("-bar", 11), 0);
129 BOOST_AUTO_TEST_CASE(doubledash
)
132 BOOST_CHECK_EQUAL(GetBoolArg("-foo", false), true);
134 ResetArgs("--foo=verbose --bar=1");
135 BOOST_CHECK_EQUAL(GetArg("-foo", ""), "verbose");
136 BOOST_CHECK_EQUAL(GetArg("-bar", 0), 1);
139 BOOST_AUTO_TEST_CASE(boolargno
)
142 BOOST_CHECK(!GetBoolArg("-foo", true));
143 BOOST_CHECK(!GetBoolArg("-foo", false));
145 ResetArgs("-nofoo=1");
146 BOOST_CHECK(!GetBoolArg("-foo", true));
147 BOOST_CHECK(!GetBoolArg("-foo", false));
149 ResetArgs("-nofoo=0");
150 BOOST_CHECK(GetBoolArg("-foo", true));
151 BOOST_CHECK(GetBoolArg("-foo", false));
153 ResetArgs("-foo --nofoo"); // --nofoo should win
154 BOOST_CHECK(!GetBoolArg("-foo", true));
155 BOOST_CHECK(!GetBoolArg("-foo", false));
157 ResetArgs("-nofoo -foo"); // foo always wins:
158 BOOST_CHECK(GetBoolArg("-foo", true));
159 BOOST_CHECK(GetBoolArg("-foo", false));
162 BOOST_AUTO_TEST_SUITE_END()