-
Notifications
You must be signed in to change notification settings - Fork 256
/
test_subparsers.cpp
308 lines (246 loc) · 10.5 KB
/
test_subparsers.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#ifdef WITH_MODULE
import argparse;
#else
#include <argparse/argparse.hpp>
#endif
#include <doctest.hpp>
#include <cmath>
#include <string>
#include <vector>
using doctest::test_suite;
TEST_CASE("Add subparsers" * test_suite("subparsers")) {
argparse::ArgumentParser program("test");
program.add_argument("--output");
argparse::ArgumentParser command_1("add");
command_1.add_argument("file").nargs(2);
argparse::ArgumentParser command_2("clean");
program.add_subparser(command_1);
program.add_subparser(command_2);
program.parse_args({"test", "--output", "thrust_profile.csv"});
REQUIRE(program.is_subcommand_used("add") == false);
REQUIRE(program.get("--output") == "thrust_profile.csv");
}
TEST_CASE("Parse subparser command" * test_suite("subparsers")) {
argparse::ArgumentParser program("test");
program.add_argument("--output");
argparse::ArgumentParser command_1("add");
command_1.add_argument("file").nargs(2);
argparse::ArgumentParser command_2("clean");
command_2.add_argument("--fullclean")
.default_value(false)
.implicit_value(true);
program.add_subparser(command_1);
program.add_subparser(command_2);
SUBCASE("command 1") {
program.parse_args({"test", "add", "file1.txt", "file2.txt"});
REQUIRE(program.is_subcommand_used("add") == true);
REQUIRE(command_1.is_used("file"));
REQUIRE((command_1.get<std::vector<std::string>>("file") ==
std::vector<std::string>{"file1.txt", "file2.txt"}));
}
SUBCASE("command 2") {
program.parse_args({"test", "clean", "--fullclean"});
REQUIRE(program.is_subcommand_used("clean") == true);
REQUIRE(command_2.get<bool>("--fullclean") == true);
}
}
TEST_CASE("Parse subparser command with optional argument" *
test_suite("subparsers")) {
argparse::ArgumentParser program("test");
program.add_argument("--verbose").flag();
argparse::ArgumentParser command_1("add");
command_1.add_argument("file");
argparse::ArgumentParser command_2("clean");
command_2.add_argument("--fullclean")
.default_value(false)
.implicit_value(true);
program.add_subparser(command_1);
program.add_subparser(command_2);
SUBCASE("Optional argument BEFORE subcommand") {
program.parse_args({"test", "--verbose", "clean", "--fullclean"});
REQUIRE(program.is_subcommand_used("clean") == true);
REQUIRE(program.get<bool>("--verbose") == true);
REQUIRE(command_2.get<bool>("--fullclean") == true);
}
SUBCASE("Optional argument AFTER subcommand") {
REQUIRE_THROWS_WITH_AS(
program.parse_args({"test", "clean", "--fullclean", "--verbose"}),
"Unknown argument: --verbose", std::runtime_error);
}
}
TEST_CASE("Parse subparser command with parent parser" *
test_suite("subparsers")) {
argparse::ArgumentParser program("test");
argparse::ArgumentParser parent("parent");
parent.add_argument("--verbose").flag();
program.add_parents(parent);
argparse::ArgumentParser command_1("add");
command_1.add_argument("file");
argparse::ArgumentParser command_2("clean");
command_2.add_argument("--fullclean")
.default_value(false)
.implicit_value(true);
program.add_subparser(command_1);
program.add_subparser(command_2);
SUBCASE("Optional argument BEFORE subcommand") {
program.parse_args({"test", "--verbose", "clean", "--fullclean"});
REQUIRE(program.is_subcommand_used("clean") == true);
REQUIRE(program.get<bool>("--verbose") == true);
REQUIRE(command_2.get<bool>("--fullclean") == true);
}
SUBCASE("Optional argument AFTER subcommand") {
REQUIRE_THROWS_WITH_AS(
program.parse_args({"test", "clean", "--fullclean", "--verbose"}),
"Unknown argument: --verbose", std::runtime_error);
}
}
TEST_CASE("Parse git commands" * test_suite("subparsers")) {
argparse::ArgumentParser program("git");
argparse::ArgumentParser add_command("add");
add_command.add_argument("files").remaining();
argparse::ArgumentParser commit_command("commit");
commit_command.add_argument("-a").flag();
commit_command.add_argument("-m");
argparse::ArgumentParser catfile_command("cat-file");
catfile_command.add_argument("-t");
catfile_command.add_argument("-p");
argparse::ArgumentParser submodule_command("submodule");
argparse::ArgumentParser submodule_update_command("update");
submodule_update_command.add_argument("--init")
.default_value(false)
.implicit_value(true);
submodule_update_command.add_argument("--recursive")
.default_value(false)
.implicit_value(true);
submodule_command.add_subparser(submodule_update_command);
program.add_subparser(add_command);
program.add_subparser(commit_command);
program.add_subparser(catfile_command);
program.add_subparser(submodule_command);
SUBCASE("git add") {
program.parse_args({"git", "add", "main.cpp", "foo.hpp", "foo.cpp"});
REQUIRE(program.is_subcommand_used("add") == true);
REQUIRE((add_command.get<std::vector<std::string>>("files") ==
std::vector<std::string>{"main.cpp", "foo.hpp", "foo.cpp"}));
}
SUBCASE("git commit") {
program.parse_args({"git", "commit", "-am", "Initial commit"});
REQUIRE(program.is_subcommand_used("commit") == true);
REQUIRE(commit_command.get<bool>("-a") == true);
REQUIRE(commit_command.get<std::string>("-m") ==
std::string{"Initial commit"});
}
SUBCASE("git cat-file -t") {
program.parse_args({"git", "cat-file", "-t", "3739f5"});
REQUIRE(program.is_subcommand_used("cat-file") == true);
REQUIRE(catfile_command.get<std::string>("-t") == std::string{"3739f5"});
}
SUBCASE("git cat-file -p") {
program.parse_args({"git", "cat-file", "-p", "3739f5"});
REQUIRE(program.is_subcommand_used("cat-file") == true);
REQUIRE(catfile_command.get<std::string>("-p") == std::string{"3739f5"});
}
SUBCASE("git submodule update") {
program.parse_args({"git", "submodule", "update"});
REQUIRE(program.is_subcommand_used("submodule") == true);
REQUIRE(submodule_command.is_subcommand_used("update") == true);
}
SUBCASE("git submodule update --init") {
program.parse_args({"git", "submodule", "update", "--init"});
REQUIRE(program.is_subcommand_used("submodule") == true);
REQUIRE(submodule_command.is_subcommand_used("update") == true);
REQUIRE(submodule_update_command.get<bool>("--init") == true);
REQUIRE(submodule_update_command.get<bool>("--recursive") == false);
}
SUBCASE("git submodule update --recursive") {
program.parse_args({"git", "submodule", "update", "--recursive"});
REQUIRE(program.is_subcommand_used("submodule") == true);
REQUIRE(submodule_command.is_subcommand_used("update") == true);
REQUIRE(submodule_update_command.get<bool>("--recursive") == true);
}
SUBCASE("git submodule update --init --recursive") {
program.parse_args({"git", "submodule", "update", "--init", "--recursive"});
REQUIRE(program.is_subcommand_used("submodule") == true);
REQUIRE(submodule_command.is_subcommand_used("update") == true);
REQUIRE(submodule_update_command.get<bool>("--init") == true);
REQUIRE(submodule_update_command.get<bool>("--recursive") == true);
}
}
TEST_CASE("Check is_subcommand_used after parse" * test_suite("subparsers")) {
argparse::ArgumentParser command_1("add");
argparse::ArgumentParser command_2("clean");
command_2.add_argument("--fullclean")
.default_value(false)
.implicit_value(true);
argparse::ArgumentParser program("test");
program.add_subparser(command_1);
program.add_subparser(command_2);
SUBCASE("command 1") {
program.parse_args({"test", "add"});
REQUIRE(program.is_subcommand_used("add") == true);
REQUIRE(program.is_subcommand_used(command_1) == true);
REQUIRE(program.is_subcommand_used("clean") == false);
REQUIRE(program.is_subcommand_used(command_2) == false);
}
SUBCASE("command 2") {
program.parse_args({"test", "clean", "--fullclean"});
REQUIRE(program.is_subcommand_used("add") == false);
REQUIRE(program.is_subcommand_used(command_1) == false);
REQUIRE(program.is_subcommand_used("clean") == true);
REQUIRE(program.is_subcommand_used(command_2) == true);
}
SUBCASE("none") {
program.parse_args({"test"});
REQUIRE(program.is_subcommand_used("add") == false);
REQUIRE(program.is_subcommand_used(command_1) == false);
REQUIRE(program.is_subcommand_used("clean") == false);
REQUIRE(program.is_subcommand_used(command_2) == false);
}
}
static bool contains(const std::string &haystack, const std::string &needle) {
return haystack.find(needle) != std::string::npos;
}
TEST_CASE("Check set_suppress" * test_suite("subparsers")) {
argparse::ArgumentParser command("cmd");
command.add_argument("arg").remaining();
argparse::ArgumentParser program("test");
program.add_subparser(command);
SUBCASE("help message contain info if subcommand not suppressed") {
command.set_suppress(false);
REQUIRE(contains(program.help().str(), "Subcommands") == true);
REQUIRE(contains(program.help().str(), "cmd") == true);
}
SUBCASE("help message does not contain info if subcommand suppressed") {
command.set_suppress(true);
REQUIRE(contains(program.help().str(), "Subcommands") == false);
REQUIRE(contains(program.help().str(), "cmd") == false);
}
SUBCASE("help message contain info if not all subcommands suppressed") {
argparse::ArgumentParser command_2("command_2");
program.add_subparser(command_2);
command.set_suppress(true);
command_2.set_suppress(false);
REQUIRE(contains(program.help().str(), "Subcommands") == true);
REQUIRE(contains(program.help().str(), "cmd") == false);
REQUIRE(contains(program.help().str(), "command_2") == true);
}
}
TEST_CASE("Help of subparsers" * test_suite("subparsers")) {
argparse::ArgumentParser program("test");
argparse::ArgumentParser command_1("add", "1.0", argparse::default_arguments::version);
std::stringstream buffer;
command_1.add_argument("--help")
.action([&](const auto &) { buffer << command_1; })
.default_value(false)
.implicit_value(true)
.nargs(0);
program.add_subparser(command_1);
REQUIRE(command_1.usage() == "Usage: test add [--version] [--help]");
REQUIRE(buffer.str().empty());
program.parse_args({"test", "add", "--help"});
REQUIRE(buffer.str() == "Usage: test add [--version] [--help]\n"
"\n"
"Optional arguments:\n"
" -v, --version prints version information and exits \n"
" --help \n");
}