Skip to content

Commit cb88de3

Browse files
author
MarcoFalke
committed
Merge #18926: test: Pass ArgsManager into getarg_tests
f871f15 scripted-diff: replace gArgs with argsman (glowang) 357f02b Create a local class inherited from BasicTestingSetup with a localized args manager and put it into the getarg_tests namespace (glowang) Pull request description: Replaced the global argsManager gArgs with a locally defined one in getarg_tests. This is to avoid confusion in arg settings between the test's ArgsManager and the #18804 ACKs for top commit: MarcoFalke: ACK f871f15 ryanofsky: Code review ACK f871f15. Changes look good and thanks for updating. In future would recommend using clang-format-diff and following [coding style](https://github.com/bitcoin/bitcoin/blob/master/doc/developer-notes.md#coding-style-c) notes, because it's atypical to indent namespace content, or indent protected keywords or put spaces around ::. Also it's fragile to define test setup class in a namespace, but test setup methods outside of the namespace and inside the test fixture instead. Would be simpler to just define the testing setup completely before using it without a namespace like: https://github.com/bitcoin/bitcoin/blob/8ad5f1c376fe99eeccac2696ac0e18e662323a4d/src/test/rpc_tests.cpp#L23 and it would have been a slightly smaller change too. Tree-SHA512: 016594639396d60667fadec8ea80ef7af634fbb2014c704f02406fe3251c5362757c21f1763d8bdb94ca4a3026ab9dc786a92a9a934efc8cd807655d9deee779
2 parents 8ad5f1c + f871f15 commit cb88de3

File tree

1 file changed

+71
-62
lines changed

1 file changed

+71
-62
lines changed

src/test/getarg_tests.cpp

Lines changed: 71 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,18 @@
1313
#include <boost/algorithm/string.hpp>
1414
#include <boost/test/unit_test.hpp>
1515

16-
BOOST_FIXTURE_TEST_SUITE(getarg_tests, BasicTestingSetup)
16+
namespace getarg_tests{
17+
class LocalTestingSetup : BasicTestingSetup {
18+
protected:
19+
void SetupArgs(const std::vector<std::pair<std::string, unsigned int>>& args);
20+
void ResetArgs(const std::string& strArg);
21+
ArgsManager m_args;
22+
};
23+
}
24+
25+
BOOST_FIXTURE_TEST_SUITE(getarg_tests, LocalTestingSetup)
1726

18-
static void ResetArgs(const std::string& strArg)
27+
void LocalTestingSetup :: ResetArgs(const std::string& strArg)
1928
{
2029
std::vector<std::string> vecArg;
2130
if (strArg.size())
@@ -30,14 +39,14 @@ static void ResetArgs(const std::string& strArg)
3039
vecChar.push_back(s.c_str());
3140

3241
std::string error;
33-
BOOST_CHECK(gArgs.ParseParameters(vecChar.size(), vecChar.data(), error));
42+
BOOST_CHECK(m_args.ParseParameters(vecChar.size(), vecChar.data(), error));
3443
}
3544

36-
static void SetupArgs(const std::vector<std::pair<std::string, unsigned int>>& args)
45+
void LocalTestingSetup :: SetupArgs(const std::vector<std::pair<std::string, unsigned int>>& args)
3746
{
38-
gArgs.ClearArgs();
47+
m_args.ClearArgs();
3948
for (const auto& arg : args) {
40-
gArgs.AddArg(arg.first, "", arg.second, OptionsCategory::OPTIONS);
49+
m_args.AddArg(arg.first, "", arg.second, OptionsCategory::OPTIONS);
4150
}
4251
}
4352

@@ -46,52 +55,52 @@ BOOST_AUTO_TEST_CASE(boolarg)
4655
const auto foo = std::make_pair("-foo", ArgsManager::ALLOW_ANY);
4756
SetupArgs({foo});
4857
ResetArgs("-foo");
49-
BOOST_CHECK(gArgs.GetBoolArg("-foo", false));
50-
BOOST_CHECK(gArgs.GetBoolArg("-foo", true));
58+
BOOST_CHECK(m_args.GetBoolArg("-foo", false));
59+
BOOST_CHECK(m_args.GetBoolArg("-foo", true));
5160

52-
BOOST_CHECK(!gArgs.GetBoolArg("-fo", false));
53-
BOOST_CHECK(gArgs.GetBoolArg("-fo", true));
61+
BOOST_CHECK(!m_args.GetBoolArg("-fo", false));
62+
BOOST_CHECK(m_args.GetBoolArg("-fo", true));
5463

55-
BOOST_CHECK(!gArgs.GetBoolArg("-fooo", false));
56-
BOOST_CHECK(gArgs.GetBoolArg("-fooo", true));
64+
BOOST_CHECK(!m_args.GetBoolArg("-fooo", false));
65+
BOOST_CHECK(m_args.GetBoolArg("-fooo", true));
5766

5867
ResetArgs("-foo=0");
59-
BOOST_CHECK(!gArgs.GetBoolArg("-foo", false));
60-
BOOST_CHECK(!gArgs.GetBoolArg("-foo", true));
68+
BOOST_CHECK(!m_args.GetBoolArg("-foo", false));
69+
BOOST_CHECK(!m_args.GetBoolArg("-foo", true));
6170

6271
ResetArgs("-foo=1");
63-
BOOST_CHECK(gArgs.GetBoolArg("-foo", false));
64-
BOOST_CHECK(gArgs.GetBoolArg("-foo", true));
72+
BOOST_CHECK(m_args.GetBoolArg("-foo", false));
73+
BOOST_CHECK(m_args.GetBoolArg("-foo", true));
6574

6675
// New 0.6 feature: auto-map -nosomething to !-something:
6776
ResetArgs("-nofoo");
68-
BOOST_CHECK(!gArgs.GetBoolArg("-foo", false));
69-
BOOST_CHECK(!gArgs.GetBoolArg("-foo", true));
77+
BOOST_CHECK(!m_args.GetBoolArg("-foo", false));
78+
BOOST_CHECK(!m_args.GetBoolArg("-foo", true));
7079

7180
ResetArgs("-nofoo=1");
72-
BOOST_CHECK(!gArgs.GetBoolArg("-foo", false));
73-
BOOST_CHECK(!gArgs.GetBoolArg("-foo", true));
81+
BOOST_CHECK(!m_args.GetBoolArg("-foo", false));
82+
BOOST_CHECK(!m_args.GetBoolArg("-foo", true));
7483

7584
ResetArgs("-foo -nofoo"); // -nofoo should win
76-
BOOST_CHECK(!gArgs.GetBoolArg("-foo", false));
77-
BOOST_CHECK(!gArgs.GetBoolArg("-foo", true));
85+
BOOST_CHECK(!m_args.GetBoolArg("-foo", false));
86+
BOOST_CHECK(!m_args.GetBoolArg("-foo", true));
7887

7988
ResetArgs("-foo=1 -nofoo=1"); // -nofoo should win
80-
BOOST_CHECK(!gArgs.GetBoolArg("-foo", false));
81-
BOOST_CHECK(!gArgs.GetBoolArg("-foo", true));
89+
BOOST_CHECK(!m_args.GetBoolArg("-foo", false));
90+
BOOST_CHECK(!m_args.GetBoolArg("-foo", true));
8291

8392
ResetArgs("-foo=0 -nofoo=0"); // -nofoo=0 should win
84-
BOOST_CHECK(gArgs.GetBoolArg("-foo", false));
85-
BOOST_CHECK(gArgs.GetBoolArg("-foo", true));
93+
BOOST_CHECK(m_args.GetBoolArg("-foo", false));
94+
BOOST_CHECK(m_args.GetBoolArg("-foo", true));
8695

8796
// New 0.6 feature: treat -- same as -:
8897
ResetArgs("--foo=1");
89-
BOOST_CHECK(gArgs.GetBoolArg("-foo", false));
90-
BOOST_CHECK(gArgs.GetBoolArg("-foo", true));
98+
BOOST_CHECK(m_args.GetBoolArg("-foo", false));
99+
BOOST_CHECK(m_args.GetBoolArg("-foo", true));
91100

92101
ResetArgs("--nofoo=1");
93-
BOOST_CHECK(!gArgs.GetBoolArg("-foo", false));
94-
BOOST_CHECK(!gArgs.GetBoolArg("-foo", true));
102+
BOOST_CHECK(!m_args.GetBoolArg("-foo", false));
103+
BOOST_CHECK(!m_args.GetBoolArg("-foo", true));
95104

96105
}
97106

@@ -101,24 +110,24 @@ BOOST_AUTO_TEST_CASE(stringarg)
101110
const auto bar = std::make_pair("-bar", ArgsManager::ALLOW_ANY);
102111
SetupArgs({foo, bar});
103112
ResetArgs("");
104-
BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", ""), "");
105-
BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", "eleven"), "eleven");
113+
BOOST_CHECK_EQUAL(m_args.GetArg("-foo", ""), "");
114+
BOOST_CHECK_EQUAL(m_args.GetArg("-foo", "eleven"), "eleven");
106115

107116
ResetArgs("-foo -bar");
108-
BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", ""), "");
109-
BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", "eleven"), "");
117+
BOOST_CHECK_EQUAL(m_args.GetArg("-foo", ""), "");
118+
BOOST_CHECK_EQUAL(m_args.GetArg("-foo", "eleven"), "");
110119

111120
ResetArgs("-foo=");
112-
BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", ""), "");
113-
BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", "eleven"), "");
121+
BOOST_CHECK_EQUAL(m_args.GetArg("-foo", ""), "");
122+
BOOST_CHECK_EQUAL(m_args.GetArg("-foo", "eleven"), "");
114123

115124
ResetArgs("-foo=11");
116-
BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", ""), "11");
117-
BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", "eleven"), "11");
125+
BOOST_CHECK_EQUAL(m_args.GetArg("-foo", ""), "11");
126+
BOOST_CHECK_EQUAL(m_args.GetArg("-foo", "eleven"), "11");
118127

119128
ResetArgs("-foo=eleven");
120-
BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", ""), "eleven");
121-
BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", "eleven"), "eleven");
129+
BOOST_CHECK_EQUAL(m_args.GetArg("-foo", ""), "eleven");
130+
BOOST_CHECK_EQUAL(m_args.GetArg("-foo", "eleven"), "eleven");
122131

123132
}
124133

@@ -128,20 +137,20 @@ BOOST_AUTO_TEST_CASE(intarg)
128137
const auto bar = std::make_pair("-bar", ArgsManager::ALLOW_ANY);
129138
SetupArgs({foo, bar});
130139
ResetArgs("");
131-
BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", 11), 11);
132-
BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", 0), 0);
140+
BOOST_CHECK_EQUAL(m_args.GetArg("-foo", 11), 11);
141+
BOOST_CHECK_EQUAL(m_args.GetArg("-foo", 0), 0);
133142

134143
ResetArgs("-foo -bar");
135-
BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", 11), 0);
136-
BOOST_CHECK_EQUAL(gArgs.GetArg("-bar", 11), 0);
144+
BOOST_CHECK_EQUAL(m_args.GetArg("-foo", 11), 0);
145+
BOOST_CHECK_EQUAL(m_args.GetArg("-bar", 11), 0);
137146

138147
ResetArgs("-foo=11 -bar=12");
139-
BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", 0), 11);
140-
BOOST_CHECK_EQUAL(gArgs.GetArg("-bar", 11), 12);
148+
BOOST_CHECK_EQUAL(m_args.GetArg("-foo", 0), 11);
149+
BOOST_CHECK_EQUAL(m_args.GetArg("-bar", 11), 12);
141150

142151
ResetArgs("-foo=NaN -bar=NotANumber");
143-
BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", 1), 0);
144-
BOOST_CHECK_EQUAL(gArgs.GetArg("-bar", 11), 0);
152+
BOOST_CHECK_EQUAL(m_args.GetArg("-foo", 1), 0);
153+
BOOST_CHECK_EQUAL(m_args.GetArg("-bar", 11), 0);
145154
}
146155

147156
BOOST_AUTO_TEST_CASE(doubledash)
@@ -150,11 +159,11 @@ BOOST_AUTO_TEST_CASE(doubledash)
150159
const auto bar = std::make_pair("-bar", ArgsManager::ALLOW_ANY);
151160
SetupArgs({foo, bar});
152161
ResetArgs("--foo");
153-
BOOST_CHECK_EQUAL(gArgs.GetBoolArg("-foo", false), true);
162+
BOOST_CHECK_EQUAL(m_args.GetBoolArg("-foo", false), true);
154163

155164
ResetArgs("--foo=verbose --bar=1");
156-
BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", ""), "verbose");
157-
BOOST_CHECK_EQUAL(gArgs.GetArg("-bar", 0), 1);
165+
BOOST_CHECK_EQUAL(m_args.GetArg("-foo", ""), "verbose");
166+
BOOST_CHECK_EQUAL(m_args.GetArg("-bar", 0), 1);
158167
}
159168

160169
BOOST_AUTO_TEST_CASE(boolargno)
@@ -163,24 +172,24 @@ BOOST_AUTO_TEST_CASE(boolargno)
163172
const auto bar = std::make_pair("-bar", ArgsManager::ALLOW_ANY);
164173
SetupArgs({foo, bar});
165174
ResetArgs("-nofoo");
166-
BOOST_CHECK(!gArgs.GetBoolArg("-foo", true));
167-
BOOST_CHECK(!gArgs.GetBoolArg("-foo", false));
175+
BOOST_CHECK(!m_args.GetBoolArg("-foo", true));
176+
BOOST_CHECK(!m_args.GetBoolArg("-foo", false));
168177

169178
ResetArgs("-nofoo=1");
170-
BOOST_CHECK(!gArgs.GetBoolArg("-foo", true));
171-
BOOST_CHECK(!gArgs.GetBoolArg("-foo", false));
179+
BOOST_CHECK(!m_args.GetBoolArg("-foo", true));
180+
BOOST_CHECK(!m_args.GetBoolArg("-foo", false));
172181

173182
ResetArgs("-nofoo=0");
174-
BOOST_CHECK(gArgs.GetBoolArg("-foo", true));
175-
BOOST_CHECK(gArgs.GetBoolArg("-foo", false));
183+
BOOST_CHECK(m_args.GetBoolArg("-foo", true));
184+
BOOST_CHECK(m_args.GetBoolArg("-foo", false));
176185

177186
ResetArgs("-foo --nofoo"); // --nofoo should win
178-
BOOST_CHECK(!gArgs.GetBoolArg("-foo", true));
179-
BOOST_CHECK(!gArgs.GetBoolArg("-foo", false));
187+
BOOST_CHECK(!m_args.GetBoolArg("-foo", true));
188+
BOOST_CHECK(!m_args.GetBoolArg("-foo", false));
180189

181190
ResetArgs("-nofoo -foo"); // foo always wins:
182-
BOOST_CHECK(gArgs.GetBoolArg("-foo", true));
183-
BOOST_CHECK(gArgs.GetBoolArg("-foo", false));
191+
BOOST_CHECK(m_args.GetBoolArg("-foo", true));
192+
BOOST_CHECK(m_args.GetBoolArg("-foo", false));
184193
}
185194

186195
BOOST_AUTO_TEST_CASE(logargs)
@@ -200,7 +209,7 @@ BOOST_AUTO_TEST_CASE(logargs)
200209
});
201210

202211
// Log the arguments
203-
gArgs.LogArgs();
212+
m_args.LogArgs();
204213

205214
LogInstance().DeleteCallback(print_connection);
206215
// Check that what should appear does, and what shouldn't doesn't.

0 commit comments

Comments
 (0)