Repository navigation
Expand file tree
/
Copy pathtest_blank_list_items.cpp
More file actions
402 lines (349 loc) · 14.3 KB
/
Copy pathtest_blank_list_items.cpp
File metadata and controls
402 lines (349 loc) · 14.3 KB
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
#include <gtest/gtest.h>
#include <array>
#include <memory>
#include <optional>
#include <rfl.hpp>
#include <rfl/cli.hpp>
#include <rfl/json.hpp>
#include <set>
#include <string>
#include <vector>
namespace test_blank_list_items {
using Blanks = rfl::cli::BlankItemsAsMissing;
using Path = std::vector<rfl::ErrorDetail::PathSegment>;
struct Config {
std::vector<std::optional<std::string>> tags;
};
TEST(cli, test_blank_items_skipped_by_default) {
const char* args[] = {"program", "--tags=,a,,b,"};
const auto result = rfl::cli::read<Config>(2, const_cast<char**>(args));
ASSERT_TRUE(result) << result.error().what();
EXPECT_EQ(result.value().tags,
(std::vector<std::optional<std::string>>{"a", "b"}));
}
TEST(cli, test_blank_items_optional) {
const char* args[] = {"program", "--tags=a,,b"};
const auto result =
rfl::cli::read<Config, Blanks>(2, const_cast<char**>(args));
ASSERT_TRUE(result) << result.error().what();
EXPECT_EQ(result.value().tags,
(std::vector<std::optional<std::string>>{"a", std::nullopt, "b"}));
}
TEST(cli, test_blank_items_leading_and_trailing) {
const char* args[] = {"program", "--tags=,a,"};
const auto result =
rfl::cli::read<Config, Blanks>(2, const_cast<char**>(args));
ASSERT_TRUE(result) << result.error().what();
EXPECT_EQ(result.value().tags,
(std::vector<std::optional<std::string>>{std::nullopt, "a",
std::nullopt}));
}
TEST(cli, test_blank_items_empty_value) {
const char* args[] = {"program", "--tags="};
const auto result =
rfl::cli::read<Config, Blanks>(2, const_cast<char**>(args));
ASSERT_TRUE(result) << result.error().what();
EXPECT_TRUE(result.value().tags.empty());
}
struct DefaultValConfig {
std::vector<rfl::DefaultVal<std::string>> names;
std::vector<rfl::DefaultVal<int>> counts;
};
TEST(cli, test_blank_items_default_val) {
const char* args[] = {"program", "--names=a,,b", "--counts=,2"};
const auto result =
rfl::cli::read<DefaultValConfig, Blanks>(3, const_cast<char**>(args));
ASSERT_TRUE(result) << result.error().what();
const auto& names = result.value().names;
ASSERT_EQ(names.size(), 3u);
EXPECT_EQ(names[0].value(), "a");
EXPECT_EQ(names[1].value(), "");
EXPECT_EQ(names[2].value(), "b");
const auto& counts = result.value().counts;
ASSERT_EQ(counts.size(), 2u);
EXPECT_EQ(counts[0].value(), 0);
EXPECT_EQ(counts[1].value(), 2);
}
struct RequiredConfig {
std::vector<std::string> tags;
};
TEST(cli, test_blank_items_required_error) {
const char* args[] = {"program", "--tags=a,,b"};
const auto result =
rfl::cli::read<RequiredConfig, Blanks>(2, const_cast<char**>(args));
ASSERT_FALSE(result);
EXPECT_EQ(result.error().what(),
"Failed to parse field 'tags': Blank item at index 1 of '--tags'.");
const auto details = result.error().details();
ASSERT_EQ(details.size(), 1u);
EXPECT_EQ(details[0].path, (Path{"tags", size_t{1}}));
EXPECT_EQ(details[0].message, "Blank item at index 1 of '--tags'.");
}
struct BoolConfig {
std::vector<bool> flags;
};
TEST(cli, test_blank_items_required_bool_error) {
// A blank bool is not read as `true`, unlike `--flag` or `--flag=`.
const char* args[] = {"program", "--flags=true,"};
const auto result =
rfl::cli::read<BoolConfig, Blanks>(2, const_cast<char**>(args));
ASSERT_FALSE(result);
EXPECT_EQ(
result.error().what(),
"Failed to parse field 'flags': Blank item at index 1 of '--flags'.");
}
struct Inner {
std::vector<std::optional<std::string>> tags;
std::vector<std::string> names;
};
struct Outer {
Inner a;
};
TEST(cli, test_blank_items_nested) {
const char* args[] = {"program", "--a.tags=x,,y", "--a.names=n"};
const auto result =
rfl::cli::read<Outer, Blanks>(3, const_cast<char**>(args));
ASSERT_TRUE(result) << result.error().what();
EXPECT_EQ(result.value().a.tags,
(std::vector<std::optional<std::string>>{"x", std::nullopt, "y"}));
EXPECT_EQ(result.value().a.names, (std::vector<std::string>{"n"}));
}
TEST(cli, test_blank_items_nested_error) {
const char* args[] = {"program", "--a.tags=x", "--a.names=,n"};
const auto result =
rfl::cli::read<Outer, Blanks>(3, const_cast<char**>(args));
ASSERT_FALSE(result);
EXPECT_EQ(result.error().what(),
"Failed to parse field 'a': Failed to parse field 'names': Blank "
"item at index 0 of '--a.names'.");
const auto details = result.error().details();
ASSERT_EQ(details.size(), 1u);
EXPECT_EQ(details[0].path, (Path{"a", "names", size_t{0}}));
}
struct Containers {
std::array<std::optional<int>, 3> numbers;
std::set<std::string> names;
};
TEST(cli, test_blank_items_other_containers) {
const char* ok_args[] = {"program", "--numbers=1,,3", "--names=b,a"};
const auto ok =
rfl::cli::read<Containers, Blanks>(3, const_cast<char**>(ok_args));
ASSERT_TRUE(ok) << ok.error().what();
EXPECT_EQ(ok.value().numbers,
(std::array<std::optional<int>, 3>{1, std::nullopt, 3}));
EXPECT_EQ(ok.value().names, (std::set<std::string>{"a", "b"}));
const char* err_args[] = {"program", "--numbers=1,2,3", "--names=b,,a"};
const auto err =
rfl::cli::read<Containers, Blanks>(3, const_cast<char**>(err_args));
ASSERT_FALSE(err);
EXPECT_EQ(
err.error().what(),
"Failed to parse field 'names': Blank item at index 1 of '--names'.");
}
struct WideConfig {
std::vector<std::wstring> names;
std::vector<std::optional<std::wstring>> tags;
};
TEST(cli, test_blank_items_wstring) {
// A blank std::wstring is an error, as a blank std::string is, not "".
const char* ok_args[] = {"program", "--names=a,b", "--tags=x,,y"};
const auto ok =
rfl::cli::read<WideConfig, Blanks>(3, const_cast<char**>(ok_args));
ASSERT_TRUE(ok) << ok.error().what();
EXPECT_EQ(ok.value().names, (std::vector<std::wstring>{L"a", L"b"}));
EXPECT_EQ(ok.value().tags, (std::vector<std::optional<std::wstring>>{
L"x", std::nullopt, L"y"}));
const char* err_args[] = {"program", "--names=a,,b", "--tags=x"};
const auto err =
rfl::cli::read<WideConfig, Blanks>(3, const_cast<char**>(err_args));
ASSERT_FALSE(err);
EXPECT_EQ(
err.error().what(),
"Failed to parse field 'names': Blank item at index 1 of '--names'.");
const auto details = err.error().details();
ASSERT_EQ(details.size(), 1u);
EXPECT_EQ(details[0].path, (Path{"names", size_t{1}}));
}
struct PointerConfig {
std::vector<std::shared_ptr<std::string>> shared;
std::vector<std::unique_ptr<int>> unique;
};
TEST(cli, test_blank_items_smart_pointers) {
// A smart pointer is never required, so a blank one is null, as a
// missing field of that type is.
const char* args[] = {"program", "--shared=a,", "--unique=,2"};
const auto result =
rfl::cli::read<PointerConfig, Blanks>(3, const_cast<char**>(args));
ASSERT_TRUE(result) << result.error().what();
const auto& shared = result.value().shared;
ASSERT_EQ(shared.size(), 2u);
ASSERT_TRUE(shared[0]);
EXPECT_EQ(*shared[0], "a");
EXPECT_FALSE(shared[1]);
const auto& unique = result.value().unique;
ASSERT_EQ(unique.size(), 2u);
EXPECT_FALSE(unique[0]);
ASSERT_TRUE(unique[1]);
EXPECT_EQ(*unique[1], 2);
}
TEST(cli, test_blank_items_setting_ignored_by_json) {
const auto config = Config{.tags = {"a", std::nullopt}};
EXPECT_EQ(rfl::json::write<Blanks>(config), rfl::json::write(config));
EXPECT_EQ((rfl::json::to_schema<Config, Blanks>()),
rfl::json::to_schema<Config>());
const auto result =
rfl::json::read<Config, Blanks>(rfl::json::write(config));
ASSERT_TRUE(result) << result.error().what();
EXPECT_EQ(result.value().tags, config.tags);
}
// --- With repeated options, ListDelimiter and list positionals ---
template <class T, class... Ps>
rfl::Result<T> read(std::vector<const char*> _args) {
_args.insert(_args.begin(), "program");
return rfl::cli::read<T, Ps...>(static_cast<int>(_args.size()),
const_cast<char**>(_args.data()));
}
using OptStrings = std::vector<std::optional<std::string>>;
using Semicolon = rfl::cli::ListDelimiter<";">;
using NoSplit = rfl::cli::ListDelimiter<"">;
struct RepeatedConfig {
rfl::Short<"t", OptStrings> tags;
};
TEST(cli, test_blank_items_repeated_option) {
// The blank items of every occurrence are kept, at their index in the
// combined list.
const auto result =
read<RepeatedConfig, Blanks>({"--tags", "a,", "-t", ",b", "--tags=c"});
ASSERT_TRUE(result) << result.error().what();
EXPECT_EQ(result.value().tags(),
(OptStrings{"a", std::nullopt, std::nullopt, "b", "c"}));
}
TEST(cli, test_blank_items_repeated_option_skipped_by_default) {
const auto result =
read<RepeatedConfig>({"--tags", "a,", "-t", ",b", "--tags=c"});
ASSERT_TRUE(result) << result.error().what();
EXPECT_EQ(result.value().tags(), (OptStrings{"a", "b", "c"}));
}
TEST(cli, test_blank_items_repeated_empty_occurrence) {
// An empty occurrence adds no items, as an empty value is an empty list.
const auto result =
read<RepeatedConfig, Blanks>({"--tags=a", "--tags=", "-t", "", "-t=b"});
ASSERT_TRUE(result) << result.error().what();
EXPECT_EQ(result.value().tags(), (OptStrings{"a", "b"}));
}
TEST(cli, test_blank_items_repeated_option_error_index) {
const auto result =
read<RequiredConfig, Blanks>({"--tags", "a,b", "--tags", "c,,d"});
ASSERT_FALSE(result);
EXPECT_EQ(result.error().what(),
"Failed to parse field 'tags': Blank item at index 3 of '--tags'.");
const auto details = result.error().details();
ASSERT_EQ(details.size(), 1u);
EXPECT_EQ(details[0].path, (Path{"tags", size_t{3}}));
}
TEST(cli, test_blank_items_custom_delimiter) {
// Blank items are the ones between two delimiters; commas are not split.
const auto result =
read<RepeatedConfig, Semicolon, Blanks>({"--tags=a;;b,,c", "-t", ";"});
ASSERT_TRUE(result) << result.error().what();
EXPECT_EQ(result.value().tags(),
(OptStrings{"a", std::nullopt, "b,,c", std::nullopt,
std::nullopt}));
const auto skipped = read<RepeatedConfig, Semicolon>({"--tags=a;;b,,c"});
ASSERT_TRUE(skipped) << skipped.error().what();
EXPECT_EQ(skipped.value().tags(), (OptStrings{"a", "b,,c"}));
const auto err = read<RequiredConfig, Blanks, Semicolon>({"--tags=a,;;b"});
ASSERT_FALSE(err);
EXPECT_EQ(err.error().what(),
"Failed to parse field 'tags': Blank item at index 1 of '--tags'.");
}
TEST(cli, test_blank_items_no_delimiter) {
// Without a delimiter, every occurrence is one item, so an empty one is a
// blank item. On its own, `--tags=` is one blank item, not an empty list.
const auto result =
read<RepeatedConfig, NoSplit, Blanks>({"--tags", "a,,b", "-t", ""});
ASSERT_TRUE(result) << result.error().what();
EXPECT_EQ(result.value().tags(), (OptStrings{"a,,b", std::nullopt}));
const auto single = read<RepeatedConfig, NoSplit, Blanks>({"--tags="});
ASSERT_TRUE(single) << single.error().what();
EXPECT_EQ(single.value().tags(), (OptStrings{std::nullopt}));
const auto kept = read<RepeatedConfig, NoSplit>({"--tags", "a,,b", "-t", ""});
ASSERT_TRUE(kept) << kept.error().what();
EXPECT_EQ(kept.value().tags(), (OptStrings{"a,,b", ""}));
const auto err =
read<RequiredConfig, NoSplit, Blanks>({"--tags", "a", "--tags", ""});
ASSERT_FALSE(err);
EXPECT_EQ(err.error().what(),
"Failed to parse field 'tags': Blank item at index 1 of '--tags'.");
}
struct PositionalConfig {
rfl::Positional<std::string> command;
rfl::Positional<OptStrings> files;
};
TEST(cli, test_blank_items_list_positional) {
// Each remaining argument is split as an occurrence of the list.
const auto result =
read<PositionalConfig, Blanks>({"run", "a,,b", "c", ",d", "", "e,"});
ASSERT_TRUE(result) << result.error().what();
EXPECT_EQ(result.value().command(), "run");
EXPECT_EQ(result.value().files(),
(OptStrings{"a", std::nullopt, "b", "c", std::nullopt, "d", "e",
std::nullopt}));
const auto skipped =
read<PositionalConfig>({"run", "a,,b", "c", ",d", "", "e,"});
ASSERT_TRUE(skipped) << skipped.error().what();
EXPECT_EQ(skipped.value().files(), (OptStrings{"a", "b", "c", "d", "e"}));
}
struct RequiredPositionalConfig {
rfl::Positional<std::string> command;
rfl::Positional<std::vector<std::string>> files;
};
TEST(cli, test_blank_items_list_positional_error) {
const auto result =
read<RequiredPositionalConfig, Blanks>({"run", "a", "b,,c"});
ASSERT_FALSE(result);
EXPECT_EQ(
result.error().what(),
"Failed to parse field 'files': Blank item at index 2 of '--files'.");
const auto details = result.error().details();
ASSERT_EQ(details.size(), 1u);
EXPECT_EQ(details[0].path, (Path{"files", size_t{2}}));
}
struct Recipe {
rfl::Positional<std::string> name;
rfl::Positional<OptStrings> args;
};
TEST(cli, test_blank_items_options_first_no_delimiter) {
// The rest of the command line, unchanged, except that an empty argument
// is a blank item.
const auto result =
read<Recipe, rfl::cli::OptionsFirst, NoSplit, Blanks>(
{"build", "--jobs", "", "--", "a,,b"});
ASSERT_TRUE(result) << result.error().what();
EXPECT_EQ(result.value().name(), "build");
EXPECT_EQ(result.value().args(),
(OptStrings{"--jobs", std::nullopt, "--", "a,,b"}));
const auto kept = read<Recipe, rfl::cli::OptionsFirst, NoSplit>(
{"build", "--jobs", "", "--", "a,,b"});
ASSERT_TRUE(kept) << kept.error().what();
EXPECT_EQ(kept.value().args(), (OptStrings{"--jobs", "", "--", "a,,b"}));
}
struct PassThrough {
rfl::Positional<std::string> name;
rfl::Positional<rfl::DefaultVal<std::vector<std::string>>> args;
};
TEST(cli, test_blank_items_pass_through_empty_argument) {
// With BlankItemsAsMissing, a list of std::string cannot take an empty
// argument: it is a blank item.
const auto result = read<PassThrough, rfl::cli::OptionsFirst, NoSplit,
Blanks>({"build", "--jobs", "", "x"});
ASSERT_FALSE(result);
EXPECT_EQ(result.error().what(),
"Failed to parse field 'args': Blank item at index 1 of '--args'.");
const auto kept = read<PassThrough, rfl::cli::OptionsFirst, NoSplit>(
{"build", "--jobs", "", "x"});
ASSERT_TRUE(kept) << kept.error().what();
EXPECT_EQ(kept.value().args.value()(),
(std::vector<std::string>{"--jobs", "", "x"}));
}
} // namespace test_blank_list_items