5#include "flutter/fml/command_line.h"
10#include "flutter/fml/macros.h"
11#include "flutter/fml/size.h"
12#include "gtest/gtest.h"
17TEST(CommandLineTest, Basic) {
20 {
"my_program",
"--flag1",
"--flag2=value2",
"arg1",
"arg2",
"arg3"});
23 EXPECT_EQ(
"my_program", cl.argv0());
25 EXPECT_EQ(2u, cl.options().size());
26 EXPECT_EQ(
"flag1", cl.options()[0].name);
27 EXPECT_EQ(std::string(), cl.options()[0].value);
28 EXPECT_EQ(
"flag2", cl.options()[1].name);
29 EXPECT_EQ(
"value2", cl.options()[1].value);
31 EXPECT_EQ(3u, cl.positional_args().size());
32 EXPECT_EQ(
"arg1", cl.positional_args()[0]);
33 EXPECT_EQ(
"arg2", cl.positional_args()[1]);
34 EXPECT_EQ(
"arg3", cl.positional_args()[2]);
38 size_t index =
static_cast<size_t>(-1);
41 EXPECT_FALSE(cl.HasOption(
"flag3"));
42 EXPECT_FALSE(cl.HasOption(
"flag3",
nullptr));
44 std::string
value =
"nonempty";
46 EXPECT_EQ(std::string(),
value);
48 EXPECT_EQ(
"value2",
value);
49 EXPECT_FALSE(cl.GetOptionValue(
"flag3", &
value));
51 EXPECT_EQ(std::string(), cl.GetOptionValueWithDefault(
"flag1",
"nope"));
52 EXPECT_EQ(
"value2", cl.GetOptionValueWithDefault(
"flag2",
"nope"));
53 EXPECT_EQ(
"nope", cl.GetOptionValueWithDefault(
"flag3",
"nope"));
56TEST(CommandLineTest, DefaultConstructor) {
58 EXPECT_FALSE(cl.has_argv0());
59 EXPECT_EQ(std::string(), cl.argv0());
60 EXPECT_EQ(std::vector<CommandLine::Option>(), cl.options());
61 EXPECT_EQ(std::vector<std::string>(), cl.positional_args());
64TEST(CommandLineTest, ComponentConstructor) {
65 const std::string argv0 =
"my_program";
66 const std::vector<CommandLine::Option>
options = {
67 CommandLine::Option(
"flag",
"value")};
68 const std::vector<std::string> positional_args = {
"arg"};
70 CommandLine cl(argv0,
options, positional_args);
72 EXPECT_EQ(argv0, cl.argv0());
73 EXPECT_EQ(
options, cl.options());
74 EXPECT_EQ(positional_args, cl.positional_args());
75 EXPECT_EQ(
"value", cl.GetOptionValueWithDefault(
"flag",
"nope"));
81 static std::vector<std::string>
argv = {
"my_program",
"--flag1",
82 "--flag2",
"subcommand",
83 "--subflag",
"subarg"};
84 auto first =
argv.cbegin();
85 auto last =
argv.cend();
86 std::vector<std::string>::const_iterator sub_first;
90 EXPECT_EQ(
argv[0], cl.argv0());
91 std::vector<CommandLine::Option> expected_options = {
92 CommandLine::Option(
"flag1"), CommandLine::Option(
"flag2")};
93 EXPECT_EQ(expected_options, cl.options());
94 std::vector<std::string> expected_positional_args = {
argv[3],
argv[4],
96 EXPECT_EQ(expected_positional_args, cl.positional_args());
99 EXPECT_FALSE(cl.HasOption(
"subflag",
nullptr));
101 EXPECT_EQ(first + 3, sub_first);
104 EXPECT_EQ(
argv[3], sub_cl.argv0());
105 std::vector<CommandLine::Option> expected_sub_options = {
106 CommandLine::Option(
"subflag")};
107 EXPECT_EQ(expected_sub_options, sub_cl.options());
108 std::vector<std::string> expected_sub_positional_args = {
argv[5]};
109 EXPECT_EQ(expected_sub_positional_args, sub_cl.positional_args());
110 EXPECT_FALSE(sub_cl.HasOption(
"flag1",
nullptr));
111 EXPECT_FALSE(sub_cl.HasOption(
"flag2",
nullptr));
117 static std::vector<std::string>
argv = {
"my_program",
"--flag"};
118 std::vector<std::string>::const_iterator sub_first;
120 argv.cbegin(),
argv.cend(), &sub_first);
121 EXPECT_EQ(
argv.cend(), sub_first);
126 static std::vector<std::string>
argv = {
"my_program",
"arg1",
"arg2"};
127 std::vector<std::string>::const_iterator sub_first;
129 argv.cbegin(),
argv.cend(), &sub_first);
130 EXPECT_EQ(
argv.cbegin() + 1, sub_first);
135 static std::vector<std::string>
argv = {
"my_program",
"--",
"--arg"};
136 std::vector<std::string>::const_iterator sub_first;
138 argv.cbegin(),
argv.cend(), &sub_first);
139 EXPECT_EQ(
argv.cbegin() + 2, sub_first);
143TEST(CommandLineTest, CommmandLineFromIterators) {
147 const std::vector<std::string>
argv = {
"my_program",
"--flag=value",
"arg"};
151 EXPECT_EQ(
argv[0], cl.argv0());
152 std::vector<CommandLine::Option> expected_options = {
153 CommandLine::Option(
"flag",
"value")};
154 EXPECT_EQ(expected_options, cl.options());
155 std::vector<std::string> expected_positional_args = {
argv[2]};
156 EXPECT_EQ(expected_positional_args, cl.positional_args());
157 EXPECT_EQ(
"value", cl.GetOptionValueWithDefault(
"flag",
"nope"));
162 const std::vector<std::string>
argv;
165 EXPECT_FALSE(cl.has_argv0());
166 EXPECT_EQ(std::string(), cl.argv0());
167 EXPECT_EQ(std::vector<CommandLine::Option>(), cl.options());
168 EXPECT_EQ(std::vector<std::string>(), cl.positional_args());
173 const std::vector<std::string>
argv = {
""};
177 EXPECT_EQ(std::string(), cl.argv0());
178 EXPECT_EQ(std::vector<CommandLine::Option>(), cl.options());
179 EXPECT_EQ(std::vector<std::string>(), cl.positional_args());
184 const std::vector<const char*>
argv = {
"my_program",
"--flag=value",
"arg"};
188 EXPECT_EQ(
argv[0], cl.argv0());
189 std::vector<CommandLine::Option> expected_options = {
190 CommandLine::Option(
"flag",
"value")};
191 EXPECT_EQ(expected_options, cl.options());
192 std::vector<std::string> expected_positional_args = {
argv[2]};
193 EXPECT_EQ(expected_positional_args, cl.positional_args());
194 EXPECT_EQ(
"value", cl.GetOptionValueWithDefault(
"flag",
"nope"));
199 static const char*
const argv[] = {
"my_program",
"--flag=value",
"arg"};
203 EXPECT_EQ(
argv[0], cl.argv0());
204 std::vector<CommandLine::Option> expected_options = {
205 CommandLine::Option(
"flag",
"value")};
206 EXPECT_EQ(expected_options, cl.options());
207 std::vector<std::string> expected_positional_args = {
argv[2]};
208 EXPECT_EQ(expected_positional_args, cl.positional_args());
209 EXPECT_EQ(
"value", cl.GetOptionValueWithDefault(
"flag",
"nope"));
214 static const char*
const argv[] = {
"my_program",
"--flag=value",
"arg"};
219 EXPECT_EQ(
argv[0], cl.argv0());
220 std::vector<CommandLine::Option> expected_options = {
221 CommandLine::Option(
"flag",
"value")};
222 EXPECT_EQ(expected_options, cl.options());
223 std::vector<std::string> expected_positional_args = {
argv[2]};
224 EXPECT_EQ(expected_positional_args, cl.positional_args());
225 EXPECT_EQ(
"value", cl.GetOptionValueWithDefault(
"flag",
"nope"));
230 std::initializer_list<const char*> il = {
"my_program",
"--flag=value",
234 EXPECT_EQ(
"my_program", cl.argv0());
235 std::vector<CommandLine::Option> expected_options = {
236 CommandLine::Option(
"flag",
"value")};
237 EXPECT_EQ(expected_options, cl.options());
238 std::vector<std::string> expected_positional_args = {
"arg"};
239 EXPECT_EQ(expected_positional_args, cl.positional_args());
240 EXPECT_EQ(
"value", cl.GetOptionValueWithDefault(
"flag",
"nope"));
244 std::initializer_list<std::string> il = {
"my_program",
"--flag=value",
248 EXPECT_EQ(
"my_program", cl.argv0());
249 std::vector<CommandLine::Option> expected_options = {
250 CommandLine::Option(
"flag",
"value")};
251 EXPECT_EQ(expected_options, cl.options());
252 std::vector<std::string> expected_positional_args = {
"arg"};
253 EXPECT_EQ(expected_positional_args, cl.positional_args());
254 EXPECT_EQ(
"value", cl.GetOptionValueWithDefault(
"flag",
"nope"));
258TEST(CommandLineTest, OddArguments) {
262 {
"my_program",
"--=",
"--=foo",
"--bar=",
"--==",
"--===",
"--==x",
265 EXPECT_EQ(
"my_program", cl.argv0());
266 std::vector<CommandLine::Option> expected_options = {
267 CommandLine::Option(
"="), CommandLine::Option(
"=foo"),
268 CommandLine::Option(
"bar"), CommandLine::Option(
"="),
269 CommandLine::Option(
"=",
"="), CommandLine::Option(
"=",
"x")};
270 EXPECT_EQ(expected_options, cl.options());
271 std::vector<std::string> expected_positional_args = {
"arg"};
272 EXPECT_EQ(expected_positional_args, cl.positional_args());
279 EXPECT_EQ(std::string(), cl.argv0());
280 EXPECT_EQ(std::vector<CommandLine::Option>(), cl.options());
281 std::vector<std::string> expected_positional_args = {
"-x"};
282 EXPECT_EQ(expected_positional_args, cl.positional_args());
289 EXPECT_EQ(std::string(), cl.argv0());
290 EXPECT_EQ(std::vector<CommandLine::Option>(), cl.options());
291 std::vector<std::string> expected_positional_args = {
"-"};
292 EXPECT_EQ(expected_positional_args, cl.positional_args());
299 {
"",
"--flag=value",
"--",
"--not-a-flag",
"arg",
"--"});
301 EXPECT_EQ(std::string(), cl.argv0());
302 std::vector<CommandLine::Option> expected_options = {
303 CommandLine::Option(
"flag",
"value")};
304 std::vector<std::string> expected_positional_args = {
"--not-a-flag",
"arg",
306 EXPECT_EQ(expected_positional_args, cl.positional_args());
310TEST(CommandLineTest, MultipleOccurrencesOfOption) {
312 {
"my_program",
"--flag1=value1",
"--flag2=value2",
"--flag1=value3"});
313 std::vector<CommandLine::Option> expected_options = {
314 CommandLine::Option(
"flag1",
"value1"),
315 CommandLine::Option(
"flag2",
"value2"),
316 CommandLine::Option(
"flag1",
"value3")};
317 EXPECT_EQ(
"value3", cl.GetOptionValueWithDefault(
"flag1",
"nope"));
318 EXPECT_EQ(
"value2", cl.GetOptionValueWithDefault(
"flag2",
"nope"));
319 std::vector<std::string_view>
values = cl.GetOptionValues(
"flag1");
320 ASSERT_EQ(2u,
values.size());
321 EXPECT_EQ(
"value1",
values[0]);
322 EXPECT_EQ(
"value3",
values[1]);
326void ExpectNotEqual(
const char*
message,
327 std::initializer_list<std::string> c1,
328 std::initializer_list<std::string> c2) {
336 EXPECT_FALSE(cl1 != cl1);
338 EXPECT_FALSE(cl2 != cl2);
341 EXPECT_FALSE(cl1 == cl2);
343 EXPECT_FALSE(cl2 == cl1);
347void ExpectEqual(
const char*
message,
348 std::initializer_list<std::string> c1,
349 std::initializer_list<std::string> c2) {
357 EXPECT_FALSE(cl1 != cl1);
359 EXPECT_FALSE(cl2 != cl2);
363 EXPECT_FALSE(cl1 != cl2);
365 EXPECT_FALSE(cl2 != cl1);
368TEST(CommandLineTest, ComparisonOperators) {
369 ExpectNotEqual(
"1", {}, {
""});
370 ExpectNotEqual(
"2", {
"abc"}, {
"def"});
371 ExpectNotEqual(
"3", {
"abc",
"--flag"}, {
"abc"});
372 ExpectNotEqual(
"4", {
"abc",
"--flag1"}, {
"abc",
"--flag2"});
373 ExpectNotEqual(
"5", {
"abc",
"--flag1",
"--flag2"}, {
"abc",
"--flag1"});
374 ExpectNotEqual(
"6", {
"abc",
"arg"}, {
"abc"});
375 ExpectNotEqual(
"7", {
"abc",
"arg1"}, {
"abc",
"arg2"});
376 ExpectNotEqual(
"8", {
"abc",
"arg1",
"arg2"}, {
"abc",
"arg1"});
377 ExpectNotEqual(
"9", {
"abc",
"--flag",
"arg1"}, {
"abc",
"--flag",
"arg2"});
381 ExpectEqual(
"10", {
"abc",
"--flag",
"arg"}, {
"abc",
"--flag",
"--",
"arg"});
384TEST(CommandLineTest, MoveAndCopy) {
386 {
"my_program",
"--flag1=value1",
"--flag2",
"arg"});
392 EXPECT_EQ(
"value1", cl2.GetOptionValueWithDefault(
"flag1",
"nope"));
395 CommandLine cl3(std::move(cl2));
397 EXPECT_EQ(
"value1", cl3.GetOptionValueWithDefault(
"flag1",
"nope"));
404 EXPECT_EQ(
"value1", cl4.GetOptionValueWithDefault(
"flag1",
"nope"));
409 cl5 = std::move(cl4);
411 EXPECT_EQ(
"value1", cl5.GetOptionValueWithDefault(
"flag1",
"nope"));
414void ToArgvHelper(
const char*
message, std::initializer_list<std::string> c) {
416 std::vector<std::string>
argv = c;
422 ToArgvHelper(
"1", {});
423 ToArgvHelper(
"2", {
""});
424 ToArgvHelper(
"3", {
"my_program"});
425 ToArgvHelper(
"4", {
"my_program",
"--flag"});
426 ToArgvHelper(
"5", {
"my_program",
"--flag1",
"--flag2=value"});
427 ToArgvHelper(
"6", {
"my_program",
"arg"});
428 ToArgvHelper(
"7", {
"my_program",
"arg1",
"arg2"});
429 ToArgvHelper(
"8", {
"my_program",
"--flag1",
"--flag2=value",
"arg1",
"arg2"});
430 ToArgvHelper(
"9", {
"my_program",
"--flag",
"--",
"--not-a-flag"});
431 ToArgvHelper(
"10", {
"my_program",
"--flag",
"arg",
"--"});
436 std::vector<std::string>
argv = {
"my_program"};
442 std::vector<std::string>
argv = {
"my_program",
"--flag",
"arg"};
CommandLine CommandLineFromArgcArgv(int argc, const char *const *argv)
constexpr std::size_t size(T(&array)[N])
CommandLine CommandLineFromInitializerList(std::initializer_list< StringType > argv)
std::vector< std::string > CommandLineToArgv(const CommandLine &command_line)
CommandLine CommandLineFromIterators(InputIterator first, InputIterator last)
CommandLine CommandLineFromIteratorsFindFirstPositionalArg(InputIterator first, InputIterator last, InputIterator *first_positional_arg)
TEST(MallocMapping, EmptyContructor)
#define EXPECT_TRUE(handle)