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/test/unit_test.hpp>
14 BOOST_FIXTURE_TEST_SUITE(getarg_tests
, BasicTestingSetup
)
16 static void ResetArgs(const std::string
& strArg
)
18 std::vector
<std::string
> vecArg
;
20 boost::split(vecArg
, strArg
, boost::is_space(), boost::token_compress_on
);
22 // Insert dummy executable name:
23 vecArg
.insert(vecArg
.begin(), "testbitcoin");
26 std::vector
<const char*> vecChar
;
27 for (std::string
& s
: vecArg
)
28 vecChar
.push_back(s
.c_str());
30 gArgs
.ParseParameters(vecChar
.size(), &vecChar
[0]);
33 BOOST_AUTO_TEST_CASE(boolarg
)
36 BOOST_CHECK(gArgs
.GetBoolArg("-foo", false));
37 BOOST_CHECK(gArgs
.GetBoolArg("-foo", true));
39 BOOST_CHECK(!gArgs
.GetBoolArg("-fo", false));
40 BOOST_CHECK(gArgs
.GetBoolArg("-fo", true));
42 BOOST_CHECK(!gArgs
.GetBoolArg("-fooo", false));
43 BOOST_CHECK(gArgs
.GetBoolArg("-fooo", true));
46 BOOST_CHECK(!gArgs
.GetBoolArg("-foo", false));
47 BOOST_CHECK(!gArgs
.GetBoolArg("-foo", true));
50 BOOST_CHECK(gArgs
.GetBoolArg("-foo", false));
51 BOOST_CHECK(gArgs
.GetBoolArg("-foo", true));
53 // New 0.6 feature: auto-map -nosomething to !-something:
55 BOOST_CHECK(!gArgs
.GetBoolArg("-foo", false));
56 BOOST_CHECK(!gArgs
.GetBoolArg("-foo", true));
58 ResetArgs("-nofoo=1");
59 BOOST_CHECK(!gArgs
.GetBoolArg("-foo", false));
60 BOOST_CHECK(!gArgs
.GetBoolArg("-foo", true));
62 ResetArgs("-foo -nofoo"); // -nofoo should win
63 BOOST_CHECK(!gArgs
.GetBoolArg("-foo", false));
64 BOOST_CHECK(!gArgs
.GetBoolArg("-foo", true));
66 ResetArgs("-foo=1 -nofoo=1"); // -nofoo should win
67 BOOST_CHECK(!gArgs
.GetBoolArg("-foo", false));
68 BOOST_CHECK(!gArgs
.GetBoolArg("-foo", true));
70 ResetArgs("-foo=0 -nofoo=0"); // -nofoo=0 should win
71 BOOST_CHECK(gArgs
.GetBoolArg("-foo", false));
72 BOOST_CHECK(gArgs
.GetBoolArg("-foo", true));
74 // New 0.6 feature: treat -- same as -:
76 BOOST_CHECK(gArgs
.GetBoolArg("-foo", false));
77 BOOST_CHECK(gArgs
.GetBoolArg("-foo", true));
79 ResetArgs("--nofoo=1");
80 BOOST_CHECK(!gArgs
.GetBoolArg("-foo", false));
81 BOOST_CHECK(!gArgs
.GetBoolArg("-foo", true));
85 BOOST_AUTO_TEST_CASE(stringarg
)
88 BOOST_CHECK_EQUAL(gArgs
.GetArg("-foo", ""), "");
89 BOOST_CHECK_EQUAL(gArgs
.GetArg("-foo", "eleven"), "eleven");
91 ResetArgs("-foo -bar");
92 BOOST_CHECK_EQUAL(gArgs
.GetArg("-foo", ""), "");
93 BOOST_CHECK_EQUAL(gArgs
.GetArg("-foo", "eleven"), "");
96 BOOST_CHECK_EQUAL(gArgs
.GetArg("-foo", ""), "");
97 BOOST_CHECK_EQUAL(gArgs
.GetArg("-foo", "eleven"), "");
100 BOOST_CHECK_EQUAL(gArgs
.GetArg("-foo", ""), "11");
101 BOOST_CHECK_EQUAL(gArgs
.GetArg("-foo", "eleven"), "11");
103 ResetArgs("-foo=eleven");
104 BOOST_CHECK_EQUAL(gArgs
.GetArg("-foo", ""), "eleven");
105 BOOST_CHECK_EQUAL(gArgs
.GetArg("-foo", "eleven"), "eleven");
109 BOOST_AUTO_TEST_CASE(intarg
)
112 BOOST_CHECK_EQUAL(gArgs
.GetArg("-foo", 11), 11);
113 BOOST_CHECK_EQUAL(gArgs
.GetArg("-foo", 0), 0);
115 ResetArgs("-foo -bar");
116 BOOST_CHECK_EQUAL(gArgs
.GetArg("-foo", 11), 0);
117 BOOST_CHECK_EQUAL(gArgs
.GetArg("-bar", 11), 0);
119 ResetArgs("-foo=11 -bar=12");
120 BOOST_CHECK_EQUAL(gArgs
.GetArg("-foo", 0), 11);
121 BOOST_CHECK_EQUAL(gArgs
.GetArg("-bar", 11), 12);
123 ResetArgs("-foo=NaN -bar=NotANumber");
124 BOOST_CHECK_EQUAL(gArgs
.GetArg("-foo", 1), 0);
125 BOOST_CHECK_EQUAL(gArgs
.GetArg("-bar", 11), 0);
128 BOOST_AUTO_TEST_CASE(doubledash
)
131 BOOST_CHECK_EQUAL(gArgs
.GetBoolArg("-foo", false), true);
133 ResetArgs("--foo=verbose --bar=1");
134 BOOST_CHECK_EQUAL(gArgs
.GetArg("-foo", ""), "verbose");
135 BOOST_CHECK_EQUAL(gArgs
.GetArg("-bar", 0), 1);
138 BOOST_AUTO_TEST_CASE(boolargno
)
141 BOOST_CHECK(!gArgs
.GetBoolArg("-foo", true));
142 BOOST_CHECK(!gArgs
.GetBoolArg("-foo", false));
144 ResetArgs("-nofoo=1");
145 BOOST_CHECK(!gArgs
.GetBoolArg("-foo", true));
146 BOOST_CHECK(!gArgs
.GetBoolArg("-foo", false));
148 ResetArgs("-nofoo=0");
149 BOOST_CHECK(gArgs
.GetBoolArg("-foo", true));
150 BOOST_CHECK(gArgs
.GetBoolArg("-foo", false));
152 ResetArgs("-foo --nofoo"); // --nofoo should win
153 BOOST_CHECK(!gArgs
.GetBoolArg("-foo", true));
154 BOOST_CHECK(!gArgs
.GetBoolArg("-foo", false));
156 ResetArgs("-nofoo -foo"); // foo always wins:
157 BOOST_CHECK(gArgs
.GetBoolArg("-foo", true));
158 BOOST_CHECK(gArgs
.GetBoolArg("-foo", false));
161 BOOST_AUTO_TEST_SUITE_END()