-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.cpp
266 lines (201 loc) · 8.23 KB
/
tests.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include "testdefns.hpp"
namespace bpomodes {
namespace testing {
/*
* ==== TestBare ====
*/
TestBare::TestBare()
: TestSuite("non-modal operation")
{
add(BOOST_TEST_CASE(basic));
}
void TestBare::basic()
{ BoostPO::options_description opts;
opts.add_options()
("alpha,a", BoostPO::value<float>(), "alpha-param")
("beta", BoostPO::value<double>()->default_value(3.14), "beta-param");
BpoModes parser(opts);
{ const auto vm = parser.parse("dummy_prog", std::vector<std::string>());
BOOST_CHECK(vm.find("alpha") == vm.cend());
BOOST_CHECK_EQUAL(vm["beta"].as<double>(), 3.14);
}
{ const auto vm = parser.parse("dummy_prog",
split("-a 1.25 --beta 6.28"));
BOOST_CHECK_EQUAL(vm["alpha"].as<float>(), 1.25f);
BOOST_CHECK_EQUAL(vm["beta"].as<double>(), 6.28);
}
{ const auto vm = parser.parse("dummy_prog",
split("--alpha=2.5"));
BOOST_CHECK_EQUAL(vm["alpha"].as<float>(), 2.5f);
BOOST_CHECK_EQUAL(vm["beta"].as<double>(), 3.14);
}
}
/*
* ==== TestModes ====
*/
TestModes::TestModes()
: TestSuite("subcommand modes")
{
add(BOOST_TEST_CASE(dispatch));
add(BOOST_TEST_CASE(modename));
}
void TestModes::dispatch() {
BoostPO::options_description common_opts("common_options"),
alpha_opts("mode alpha"), beta_opts("mode beta"), gamma_opts("mode gamma");
common_opts.add_options()
("loglevel,L",
BoostPO::value<int>()->default_value(0), "logging level");
BpoModes parser(common_opts);
alpha_opts.add_options()
("stuff", BoostPO::value<std::string>());
parser.add("alpha", alpha_opts);
beta_opts.add_options()
("things", BoostPO::value<unsigned short>());
parser.add("beta", beta_opts);
gamma_opts.add_options()
("vacuum", BoostPO::value<double>()->default_value(0.0));
parser.add("gamma", gamma_opts);
{ const auto vm = parser.parse("dummy_prog",
split("-L 2 alpha --stuff lemon"));
check_vm_keys(vm, { "subcommand",
"loglevel", "stuff" });
BOOST_CHECK_EQUAL(vm["loglevel"].as<int>(), 2);
BOOST_CHECK_EQUAL(vm["subcommand"].as<std::string>(), "alpha");
BOOST_CHECK_EQUAL(vm["stuff"].as<std::string>(), "lemon");
}
{ const auto vm = parser.parse("dummy_prog",
split("-L 3 beta --things 17"));
check_vm_keys(vm, { "subcommand",
"loglevel", "things" });
BOOST_CHECK_EQUAL(vm["loglevel"].as<int>(), 3);
BOOST_CHECK_EQUAL(vm["subcommand"].as<std::string>(), "beta");
BOOST_CHECK_EQUAL(vm["things"].as<unsigned short>(),17);
}
{ const auto vm = parser.parse("dummy_prog",
split("-L 5 gamma"));
check_vm_keys(vm, { "subcommand",
"loglevel", "vacuum" });
BOOST_CHECK_EQUAL(vm["loglevel"].as<int>(), 5);
BOOST_CHECK_EQUAL(vm["subcommand"].as<std::string>(), "gamma");
BOOST_CHECK_EQUAL(vm["vacuum"].as<double>(), 0.0);
}
{ const auto vm = parser.parse("dummy_prog",
split("-L 7 gamma --vacuum 1e-15"));
check_vm_keys(vm, { "subcommand",
"loglevel", "vacuum" });
BOOST_CHECK_EQUAL(vm["loglevel"].as<int>(), 7);
BOOST_CHECK_EQUAL(vm["subcommand"].as<std::string>(), "gamma");
BOOST_CHECK_EQUAL(vm["vacuum"].as<double>(), 1e-15);
}
}
void TestModes::modename() {
BoostPO::options_description common_opts, opts_alpha, opts_beta, opts_gamma;
auto parser = BpoModes(common_opts)
.add("alpha", opts_alpha);
parser.subcommand_param = "chosen_mode";
parser.add("beta", opts_beta)
.add("gamma", opts_gamma);
const std::vector<std::string> modes = { "alpha", "beta", "gamma" };
for (auto m : modes) {
const auto vm = parser.parse("dummy_prog", { m });
BOOST_CHECK_EQUAL(vm["chosen_mode"].as<std::string>(), m);
}
}
/*
* ==== TestModeAPI ====
*/
TestModeAPI::TestModeAPI()
: TestSuite("ModeHandler API")
{
add(BOOST_TEST_CASE(basic));
add(BOOST_TEST_CASE(positional));
}
void TestModeAPI::basic() {
BoostPO::options_description alpha_opts("mode alpha"), beta_opts("mode beta");
std::shared_ptr mh_alpha = std::make_shared<MHstats>(),
mh_beta = std::make_shared<MHstats>();
BpoModes parser;
parser.subcommand_param = "the_mode";
alpha_opts.add_options()
("a0", BoostPO::value<int>())
("a1", BoostPO::value<int>())
("a2", BoostPO::value<int>()->default_value(5));
parser.add("alpha", alpha_opts, mh_alpha);
beta_opts.add_options()
("b0", BoostPO::value<int>())
("b1", BoostPO::value<int>()->default_value(17))
("b2", BoostPO::value<int>());
parser.add("beta", beta_opts, mh_beta);
BOOST_CHECK_EQUAL(mh_alpha->prep_count, 0);
BOOST_CHECK_EQUAL(mh_beta->prep_count, 0);
parser.parse("dummy_prog", split("alpha --a0 23"));
BOOST_CHECK_EQUAL(mh_alpha->prep_count, 1);
BOOST_CHECK_EQUAL(mh_alpha->ingest_count, 1);
BOOST_CHECK_EQUAL(mh_alpha->run_count, 0);
BOOST_CHECK_EQUAL(mh_alpha->vm_keys, "a0|a2|the_mode");
parser.parse("dummy_prog", split("beta --b1 23"));
BOOST_CHECK_EQUAL(mh_beta->prep_count, 1);
BOOST_CHECK_EQUAL(mh_beta->ingest_count, 1);
BOOST_CHECK_EQUAL(mh_beta->run_count, 0);
BOOST_CHECK_EQUAL(mh_beta->vm_keys, "b1|the_mode");
const auto vm = parser.parse("dummy_prog", split("beta --b0 17 --b2 29"));
const int stat = parser.run_subcommand(vm);
BOOST_CHECK_EQUAL(mh_beta->prep_count, 2);
BOOST_CHECK_EQUAL(mh_beta->ingest_count, 2);
BOOST_CHECK_EQUAL(mh_beta->run_count, 1);
BOOST_CHECK_EQUAL(mh_beta->vm_keys, "b0|b1|b2|the_mode");
BOOST_CHECK_EQUAL(stat, 7);
}
void TestModeAPI::positional() {
BpoModes parser;
BoostPO::options_description alpha_opts("mode_alpha");
alpha_opts.add_options()
("count,c", BoostPO::value<int>())
("input", BoostPO::value<std::string>())
("output", BoostPO::value<std::string>());
BoostPO::positional_options_description alpha_pos;
alpha_pos.add("input", 1);
alpha_pos.add("output", 1);
parser.add("alpha", alpha_opts, std::make_shared<MHpos>(alpha_pos));
BoostPO::options_description beta_opts("mode_beta");
beta_opts.add_options()
("value,x", BoostPO::value<double>()->default_value(-2.0))
("source", BoostPO::value<std::string>())
("outputs", BoostPO::value<std::vector<std::string>>());
BoostPO::positional_options_description beta_pos;
beta_pos.add("source", 1);
beta_pos.add("outputs", -1);
parser.add("beta", beta_opts, std::make_shared<MHpos>(beta_pos));
{ const auto vm = parser.parse("dummy_prog",
split("alpha the_input the_output"));
BOOST_CHECK_EQUAL(vm["subcommand"].as<std::string>(), "alpha");
BOOST_CHECK_EQUAL(vm["input"].as<std::string>(), "the_input");
BOOST_CHECK_EQUAL(vm["output"].as<std::string>(), "the_output");
}
{ const auto vm = parser.parse("dummy_prog",
split("alpha -c 17 --input the_input --output the_output"));
BOOST_CHECK_EQUAL(vm["subcommand"].as<std::string>(), "alpha");
BOOST_CHECK_EQUAL(vm["count"].as<int>(), 17);
BOOST_CHECK_EQUAL(vm["input"].as<std::string>(), "the_input");
BOOST_CHECK_EQUAL(vm["output"].as<std::string>(), "the_output");
}
{ const auto vm = parser.parse("dummy_prog",
split("beta the_source output0"));
BOOST_CHECK_EQUAL(vm["subcommand"].as<std::string>(), "beta");
BOOST_CHECK_EQUAL(vm["value"].as<double>(), -2.0);
BOOST_CHECK_EQUAL(vm["source"].as<std::string>(), "the_source");
BOOST_CHECK_EQUAL(vm["outputs"].as<std::vector<std::string>>().front(), "output0");
}
{ const auto vm = parser.parse("dummy_prog",
split("beta -x 23.0 the_source output0 output1 output2"));
BOOST_CHECK_EQUAL(vm["subcommand"].as<std::string>(), "beta");
BOOST_CHECK_EQUAL(vm["value"].as<double>(), 23.0);
BOOST_CHECK_EQUAL(vm["source"].as<std::string>(), "the_source");
const auto outs = vm["outputs"].as<std::vector<std::string>>();
const auto expected = std::vector<std::string> { "output0", "output1", "output2" };
BOOST_CHECK_EQUAL_COLLECTIONS(outs.cbegin(), outs.cend(),
expected.cbegin(), expected.cend());
}
}
} // namespace testing
} // namespace bpomodes