forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command_line_unittest.cc
448 lines (391 loc) · 16.4 KB
/
command_line_unittest.cc
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "flutter/fml/command_line.h"
#include <string_view>
#include <utility>
#include "flutter/fml/macros.h"
#include "flutter/fml/size.h"
#include "gtest/gtest.h"
namespace fml {
namespace {
TEST(CommandLineTest, Basic) {
// Making this const verifies that the methods called are const.
const auto cl = CommandLineFromInitializerList(
{"my_program", "--flag1", "--flag2=value2", "arg1", "arg2", "arg3"});
EXPECT_TRUE(cl.has_argv0());
EXPECT_EQ("my_program", cl.argv0());
EXPECT_EQ(2u, cl.options().size());
EXPECT_EQ("flag1", cl.options()[0].name);
EXPECT_EQ(std::string(), cl.options()[0].value);
EXPECT_EQ("flag2", cl.options()[1].name);
EXPECT_EQ("value2", cl.options()[1].value);
EXPECT_EQ(3u, cl.positional_args().size());
EXPECT_EQ("arg1", cl.positional_args()[0]);
EXPECT_EQ("arg2", cl.positional_args()[1]);
EXPECT_EQ("arg3", cl.positional_args()[2]);
EXPECT_TRUE(cl.HasOption("flag1"));
EXPECT_TRUE(cl.HasOption("flag1", nullptr));
size_t index = static_cast<size_t>(-1);
EXPECT_TRUE(cl.HasOption("flag2", &index));
EXPECT_EQ(1u, index);
EXPECT_FALSE(cl.HasOption("flag3"));
EXPECT_FALSE(cl.HasOption("flag3", nullptr));
std::string value = "nonempty";
EXPECT_TRUE(cl.GetOptionValue("flag1", &value));
EXPECT_EQ(std::string(), value);
EXPECT_TRUE(cl.GetOptionValue("flag2", &value));
EXPECT_EQ("value2", value);
EXPECT_FALSE(cl.GetOptionValue("flag3", &value));
EXPECT_EQ(std::string(), cl.GetOptionValueWithDefault("flag1", "nope"));
EXPECT_EQ("value2", cl.GetOptionValueWithDefault("flag2", "nope"));
EXPECT_EQ("nope", cl.GetOptionValueWithDefault("flag3", "nope"));
}
TEST(CommandLineTest, DefaultConstructor) {
CommandLine cl;
EXPECT_FALSE(cl.has_argv0());
EXPECT_EQ(std::string(), cl.argv0());
EXPECT_EQ(std::vector<CommandLine::Option>(), cl.options());
EXPECT_EQ(std::vector<std::string>(), cl.positional_args());
}
TEST(CommandLineTest, ComponentConstructor) {
const std::string argv0 = "my_program";
const std::vector<CommandLine::Option> options = {
CommandLine::Option("flag", "value")};
const std::vector<std::string> positional_args = {"arg"};
CommandLine cl(argv0, options, positional_args);
EXPECT_TRUE(cl.has_argv0());
EXPECT_EQ(argv0, cl.argv0());
EXPECT_EQ(options, cl.options());
EXPECT_EQ(positional_args, cl.positional_args());
EXPECT_EQ("value", cl.GetOptionValueWithDefault("flag", "nope"));
}
TEST(CommandLineTest, CommandLineFromIteratorsFindFirstPositionalArg) {
// This shows how one might process subcommands.
{
static std::vector<std::string> argv = {"my_program", "--flag1",
"--flag2", "subcommand",
"--subflag", "subarg"};
auto first = argv.cbegin();
auto last = argv.cend();
std::vector<std::string>::const_iterator sub_first;
auto cl =
CommandLineFromIteratorsFindFirstPositionalArg(first, last, &sub_first);
EXPECT_TRUE(cl.has_argv0());
EXPECT_EQ(argv[0], cl.argv0());
std::vector<CommandLine::Option> expected_options = {
CommandLine::Option("flag1"), CommandLine::Option("flag2")};
EXPECT_EQ(expected_options, cl.options());
std::vector<std::string> expected_positional_args = {argv[3], argv[4],
argv[5]};
EXPECT_EQ(expected_positional_args, cl.positional_args());
EXPECT_TRUE(cl.HasOption("flag1", nullptr));
EXPECT_TRUE(cl.HasOption("flag2", nullptr));
EXPECT_FALSE(cl.HasOption("subflag", nullptr));
EXPECT_EQ(first + 3, sub_first);
auto sub_cl = CommandLineFromIterators(sub_first, last);
EXPECT_TRUE(sub_cl.has_argv0());
EXPECT_EQ(argv[3], sub_cl.argv0());
std::vector<CommandLine::Option> expected_sub_options = {
CommandLine::Option("subflag")};
EXPECT_EQ(expected_sub_options, sub_cl.options());
std::vector<std::string> expected_sub_positional_args = {argv[5]};
EXPECT_EQ(expected_sub_positional_args, sub_cl.positional_args());
EXPECT_FALSE(sub_cl.HasOption("flag1", nullptr));
EXPECT_FALSE(sub_cl.HasOption("flag2", nullptr));
EXPECT_TRUE(sub_cl.HasOption("subflag", nullptr));
}
// No positional argument.
{
static std::vector<std::string> argv = {"my_program", "--flag"};
std::vector<std::string>::const_iterator sub_first;
auto cl = CommandLineFromIteratorsFindFirstPositionalArg(
argv.cbegin(), argv.cend(), &sub_first);
EXPECT_EQ(argv.cend(), sub_first);
}
// Multiple positional arguments.
{
static std::vector<std::string> argv = {"my_program", "arg1", "arg2"};
std::vector<std::string>::const_iterator sub_first;
auto cl = CommandLineFromIteratorsFindFirstPositionalArg(
argv.cbegin(), argv.cend(), &sub_first);
EXPECT_EQ(argv.cbegin() + 1, sub_first);
}
// "--".
{
static std::vector<std::string> argv = {"my_program", "--", "--arg"};
std::vector<std::string>::const_iterator sub_first;
auto cl = CommandLineFromIteratorsFindFirstPositionalArg(
argv.cbegin(), argv.cend(), &sub_first);
EXPECT_EQ(argv.cbegin() + 2, sub_first);
}
}
TEST(CommandLineTest, CommmandLineFromIterators) {
{
// Note (here and below): The |const| ensures that the factory method can
// accept const iterators.
const std::vector<std::string> argv = {"my_program", "--flag=value", "arg"};
auto cl = CommandLineFromIterators(argv.begin(), argv.end());
EXPECT_TRUE(cl.has_argv0());
EXPECT_EQ(argv[0], cl.argv0());
std::vector<CommandLine::Option> expected_options = {
CommandLine::Option("flag", "value")};
EXPECT_EQ(expected_options, cl.options());
std::vector<std::string> expected_positional_args = {argv[2]};
EXPECT_EQ(expected_positional_args, cl.positional_args());
EXPECT_EQ("value", cl.GetOptionValueWithDefault("flag", "nope"));
}
// Can handle empty argv.
{
const std::vector<std::string> argv;
auto cl = CommandLineFromIterators(argv.begin(), argv.end());
EXPECT_FALSE(cl.has_argv0());
EXPECT_EQ(std::string(), cl.argv0());
EXPECT_EQ(std::vector<CommandLine::Option>(), cl.options());
EXPECT_EQ(std::vector<std::string>(), cl.positional_args());
}
// Can handle empty |argv[0]|.
{
const std::vector<std::string> argv = {""};
auto cl = CommandLineFromIterators(argv.begin(), argv.end());
EXPECT_TRUE(cl.has_argv0());
EXPECT_EQ(std::string(), cl.argv0());
EXPECT_EQ(std::vector<CommandLine::Option>(), cl.options());
EXPECT_EQ(std::vector<std::string>(), cl.positional_args());
}
// Can also take a vector of |const char*|s.
{
const std::vector<const char*> argv = {"my_program", "--flag=value", "arg"};
auto cl = CommandLineFromIterators(argv.begin(), argv.end());
EXPECT_TRUE(cl.has_argv0());
EXPECT_EQ(argv[0], cl.argv0());
std::vector<CommandLine::Option> expected_options = {
CommandLine::Option("flag", "value")};
EXPECT_EQ(expected_options, cl.options());
std::vector<std::string> expected_positional_args = {argv[2]};
EXPECT_EQ(expected_positional_args, cl.positional_args());
EXPECT_EQ("value", cl.GetOptionValueWithDefault("flag", "nope"));
}
// Or a plain old array.
{
static const char* const argv[] = {"my_program", "--flag=value", "arg"};
auto cl = CommandLineFromIterators(argv, argv + fml::size(argv));
EXPECT_TRUE(cl.has_argv0());
EXPECT_EQ(argv[0], cl.argv0());
std::vector<CommandLine::Option> expected_options = {
CommandLine::Option("flag", "value")};
EXPECT_EQ(expected_options, cl.options());
std::vector<std::string> expected_positional_args = {argv[2]};
EXPECT_EQ(expected_positional_args, cl.positional_args());
EXPECT_EQ("value", cl.GetOptionValueWithDefault("flag", "nope"));
}
}
TEST(CommandLineTest, CommandLineFromArgcArgv) {
static const char* const argv[] = {"my_program", "--flag=value", "arg"};
const int argc = static_cast<int>(fml::size(argv));
auto cl = CommandLineFromArgcArgv(argc, argv);
EXPECT_TRUE(cl.has_argv0());
EXPECT_EQ(argv[0], cl.argv0());
std::vector<CommandLine::Option> expected_options = {
CommandLine::Option("flag", "value")};
EXPECT_EQ(expected_options, cl.options());
std::vector<std::string> expected_positional_args = {argv[2]};
EXPECT_EQ(expected_positional_args, cl.positional_args());
EXPECT_EQ("value", cl.GetOptionValueWithDefault("flag", "nope"));
}
TEST(CommandLineTest, CommandLineFromInitializerList) {
{
std::initializer_list<const char*> il = {"my_program", "--flag=value",
"arg"};
auto cl = CommandLineFromInitializerList(il);
EXPECT_TRUE(cl.has_argv0());
EXPECT_EQ("my_program", cl.argv0());
std::vector<CommandLine::Option> expected_options = {
CommandLine::Option("flag", "value")};
EXPECT_EQ(expected_options, cl.options());
std::vector<std::string> expected_positional_args = {"arg"};
EXPECT_EQ(expected_positional_args, cl.positional_args());
EXPECT_EQ("value", cl.GetOptionValueWithDefault("flag", "nope"));
}
{
std::initializer_list<std::string> il = {"my_program", "--flag=value",
"arg"};
auto cl = CommandLineFromInitializerList(il);
EXPECT_TRUE(cl.has_argv0());
EXPECT_EQ("my_program", cl.argv0());
std::vector<CommandLine::Option> expected_options = {
CommandLine::Option("flag", "value")};
EXPECT_EQ(expected_options, cl.options());
std::vector<std::string> expected_positional_args = {"arg"};
EXPECT_EQ(expected_positional_args, cl.positional_args());
EXPECT_EQ("value", cl.GetOptionValueWithDefault("flag", "nope"));
}
}
TEST(CommandLineTest, OddArguments) {
{
// Except for "arg", these are all options.
auto cl = CommandLineFromInitializerList(
{"my_program", "--=", "--=foo", "--bar=", "--==", "--===", "--==x",
"arg"});
EXPECT_TRUE(cl.has_argv0());
EXPECT_EQ("my_program", cl.argv0());
std::vector<CommandLine::Option> expected_options = {
CommandLine::Option("="), CommandLine::Option("=foo"),
CommandLine::Option("bar"), CommandLine::Option("="),
CommandLine::Option("=", "="), CommandLine::Option("=", "x")};
EXPECT_EQ(expected_options, cl.options());
std::vector<std::string> expected_positional_args = {"arg"};
EXPECT_EQ(expected_positional_args, cl.positional_args());
}
// "-x" is an argument, not an options.
{
auto cl = CommandLineFromInitializerList({"", "-x"});
EXPECT_TRUE(cl.has_argv0());
EXPECT_EQ(std::string(), cl.argv0());
EXPECT_EQ(std::vector<CommandLine::Option>(), cl.options());
std::vector<std::string> expected_positional_args = {"-x"};
EXPECT_EQ(expected_positional_args, cl.positional_args());
}
// Ditto for "-".
{
auto cl = CommandLineFromInitializerList({"", "-"});
EXPECT_TRUE(cl.has_argv0());
EXPECT_EQ(std::string(), cl.argv0());
EXPECT_EQ(std::vector<CommandLine::Option>(), cl.options());
std::vector<std::string> expected_positional_args = {"-"};
EXPECT_EQ(expected_positional_args, cl.positional_args());
}
// "--" terminates option processing, but isn't an argument in the first
// occurrence.
{
auto cl = CommandLineFromInitializerList(
{"", "--flag=value", "--", "--not-a-flag", "arg", "--"});
EXPECT_TRUE(cl.has_argv0());
EXPECT_EQ(std::string(), cl.argv0());
std::vector<CommandLine::Option> expected_options = {
CommandLine::Option("flag", "value")};
std::vector<std::string> expected_positional_args = {"--not-a-flag", "arg",
"--"};
EXPECT_EQ(expected_positional_args, cl.positional_args());
}
}
TEST(CommandLineTest, MultipleOccurrencesOfOption) {
auto cl = CommandLineFromInitializerList(
{"my_program", "--flag1=value1", "--flag2=value2", "--flag1=value3"});
std::vector<CommandLine::Option> expected_options = {
CommandLine::Option("flag1", "value1"),
CommandLine::Option("flag2", "value2"),
CommandLine::Option("flag1", "value3")};
EXPECT_EQ("value3", cl.GetOptionValueWithDefault("flag1", "nope"));
EXPECT_EQ("value2", cl.GetOptionValueWithDefault("flag2", "nope"));
std::vector<std::string_view> values = cl.GetOptionValues("flag1");
ASSERT_EQ(2u, values.size());
EXPECT_EQ("value1", values[0]);
EXPECT_EQ("value3", values[1]);
}
// |cl1| and |cl2| should be not equal.
void ExpectNotEqual(const char* message,
std::initializer_list<std::string> c1,
std::initializer_list<std::string> c2) {
SCOPED_TRACE(message);
const auto cl1 = CommandLineFromInitializerList(c1);
const auto cl2 = CommandLineFromInitializerList(c2);
// These are tautological.
EXPECT_TRUE(cl1 == cl1);
EXPECT_FALSE(cl1 != cl1);
EXPECT_TRUE(cl2 == cl2);
EXPECT_FALSE(cl2 != cl2);
// These rely on |cl1| not being equal to |cl2|.
EXPECT_FALSE(cl1 == cl2);
EXPECT_TRUE(cl1 != cl2);
EXPECT_FALSE(cl2 == cl1);
EXPECT_TRUE(cl2 != cl1);
}
void ExpectEqual(const char* message,
std::initializer_list<std::string> c1,
std::initializer_list<std::string> c2) {
SCOPED_TRACE(message);
const auto cl1 = CommandLineFromInitializerList(c1);
const auto cl2 = CommandLineFromInitializerList(c2);
// These are tautological.
EXPECT_TRUE(cl1 == cl1);
EXPECT_FALSE(cl1 != cl1);
EXPECT_TRUE(cl2 == cl2);
EXPECT_FALSE(cl2 != cl2);
// These rely on |cl1| being equal to |cl2|.
EXPECT_TRUE(cl1 == cl2);
EXPECT_FALSE(cl1 != cl2);
EXPECT_TRUE(cl2 == cl1);
EXPECT_FALSE(cl2 != cl1);
}
TEST(CommandLineTest, ComparisonOperators) {
ExpectNotEqual("1", {}, {""});
ExpectNotEqual("2", {"abc"}, {"def"});
ExpectNotEqual("3", {"abc", "--flag"}, {"abc"});
ExpectNotEqual("4", {"abc", "--flag1"}, {"abc", "--flag2"});
ExpectNotEqual("5", {"abc", "--flag1", "--flag2"}, {"abc", "--flag1"});
ExpectNotEqual("6", {"abc", "arg"}, {"abc"});
ExpectNotEqual("7", {"abc", "arg1"}, {"abc", "arg2"});
ExpectNotEqual("8", {"abc", "arg1", "arg2"}, {"abc", "arg1"});
ExpectNotEqual("9", {"abc", "--flag", "arg1"}, {"abc", "--flag", "arg2"});
// However, the presence of an unnecessary "--" shouldn't affect what's
// constructed.
ExpectEqual("10", {"abc", "--flag", "arg"}, {"abc", "--flag", "--", "arg"});
}
TEST(CommandLineTest, MoveAndCopy) {
const auto cl = CommandLineFromInitializerList(
{"my_program", "--flag1=value1", "--flag2", "arg"});
// Copy constructor.
CommandLine cl2(cl);
EXPECT_EQ(cl, cl2);
// Check that |option_index_| gets copied too.
EXPECT_EQ("value1", cl2.GetOptionValueWithDefault("flag1", "nope"));
// Move constructor.
CommandLine cl3(std::move(cl2));
EXPECT_EQ(cl, cl3);
EXPECT_EQ("value1", cl3.GetOptionValueWithDefault("flag1", "nope"));
// Copy assignment.
CommandLine cl4;
EXPECT_NE(cl, cl4);
cl4 = cl;
EXPECT_EQ(cl, cl4);
EXPECT_EQ("value1", cl4.GetOptionValueWithDefault("flag1", "nope"));
// Move assignment.
CommandLine cl5;
EXPECT_NE(cl, cl5);
cl5 = std::move(cl4);
EXPECT_EQ(cl, cl5);
EXPECT_EQ("value1", cl5.GetOptionValueWithDefault("flag1", "nope"));
}
void ToArgvHelper(const char* message, std::initializer_list<std::string> c) {
SCOPED_TRACE(message);
std::vector<std::string> argv = c;
auto cl = CommandLineFromInitializerList(c);
EXPECT_EQ(argv, CommandLineToArgv(cl));
}
TEST(CommandLineTest, CommandLineToArgv) {
ToArgvHelper("1", {});
ToArgvHelper("2", {""});
ToArgvHelper("3", {"my_program"});
ToArgvHelper("4", {"my_program", "--flag"});
ToArgvHelper("5", {"my_program", "--flag1", "--flag2=value"});
ToArgvHelper("6", {"my_program", "arg"});
ToArgvHelper("7", {"my_program", "arg1", "arg2"});
ToArgvHelper("8", {"my_program", "--flag1", "--flag2=value", "arg1", "arg2"});
ToArgvHelper("9", {"my_program", "--flag", "--", "--not-a-flag"});
ToArgvHelper("10", {"my_program", "--flag", "arg", "--"});
// However, |CommandLineToArgv()| will "strip" an unneeded "--".
{
auto cl = CommandLineFromInitializerList({"my_program", "--"});
std::vector<std::string> argv = {"my_program"};
EXPECT_EQ(argv, CommandLineToArgv(cl));
}
{
auto cl =
CommandLineFromInitializerList({"my_program", "--flag", "--", "arg"});
std::vector<std::string> argv = {"my_program", "--flag", "arg"};
EXPECT_EQ(argv, CommandLineToArgv(cl));
}
}
} // namespace
} // namespace fml