Repository navigation
Expand file tree
/
Copy pathtest_short_clusters.cpp
More file actions
300 lines (262 loc) · 10.9 KB
/
Copy pathtest_short_clusters.cpp
File metadata and controls
300 lines (262 loc) · 10.9 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
#include <gtest/gtest.h>
#include <optional>
#include <rfl.hpp>
#include <rfl/cli.hpp>
#include <string>
#include <vector>
namespace test_short_clusters {
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()));
}
template <class T>
std::string error_of(const rfl::Result<T>& _result) {
return _result ? std::string("(no error)")
: std::string(_result.error().what());
}
struct Opts {
rfl::Short<"a", bool> all;
rfl::Short<"b", rfl::DefaultVal<bool>> brief = false;
rfl::Short<"c", std::optional<bool>> color;
rfl::Short<"q", rfl::DefaultVal<bool>> quiet = false;
rfl::Short<"f", std::optional<std::string>> format;
rfl::Short<"p", std::optional<int>> port;
rfl::Short<"h", rfl::ImplicitValue<std::optional<std::string>>> help;
rfl::Short<"l", rfl::ImplicitValue<std::optional<int>, "3">> level;
rfl::Positional<std::optional<std::string>> input;
rfl::Positional<std::optional<std::string>> output;
};
TEST(cli, test_short_cluster_flags) {
const auto result = read<Opts>({"-abc"});
ASSERT_TRUE(result) << result.error().what();
const auto& o = result.value();
EXPECT_TRUE(o.all());
EXPECT_TRUE(o.brief().value());
EXPECT_EQ(o.color(), true);
EXPECT_FALSE(o.quiet().value());
EXPECT_FALSE(o.format());
}
TEST(cli, test_short_cluster_flags_any_order) {
const auto result = read<Opts>({"-cba", "in.txt"});
ASSERT_TRUE(result) << result.error().what();
EXPECT_TRUE(result.value().all());
EXPECT_TRUE(result.value().brief().value());
EXPECT_EQ(result.value().color(), true);
EXPECT_EQ(result.value().input(), "in.txt");
}
TEST(cli, test_short_attached_value) {
const auto result = read<Opts>({"-a", "-fcsv"});
ASSERT_TRUE(result) << result.error().what();
EXPECT_EQ(result.value().format(), "csv");
}
TEST(cli, test_short_cluster_then_attached_value) {
const auto result = read<Opts>({"-a", "-qfcsv", "in.txt"});
ASSERT_TRUE(result) << result.error().what();
EXPECT_TRUE(result.value().quiet().value());
EXPECT_EQ(result.value().format(), "csv");
EXPECT_EQ(result.value().input(), "in.txt");
}
TEST(cli, test_short_cluster_value_takes_next_token) {
const auto result = read<Opts>({"-a", "-qf", "csv", "in.txt"});
ASSERT_TRUE(result) << result.error().what();
EXPECT_TRUE(result.value().quiet().value());
EXPECT_EQ(result.value().format(), "csv");
EXPECT_EQ(result.value().input(), "in.txt");
}
TEST(cli, test_short_cluster_value_takes_rest_verbatim) {
// Everything after the first option that takes a value is its value, even
// letters that are short options themselves.
const auto result = read<Opts>({"-afabc"});
ASSERT_TRUE(result) << result.error().what();
EXPECT_TRUE(result.value().all());
EXPECT_FALSE(result.value().brief().value());
EXPECT_EQ(result.value().format(), "abc");
}
TEST(cli, test_short_cluster_value_missing) {
for (const auto& args : std::vector<std::vector<const char*>>{
{"-a", "-qf"}, {"-aqf", "-b"}, {"-aqf", "--format=x"}}) {
const auto result = read<Opts>(args);
EXPECT_EQ(error_of(result), "Missing value for option '-f'.");
}
}
TEST(cli, test_short_equals_value) {
// rfl keeps `-f=csv` meaning "csv", also in a group. getopt would give
// "=csv".
for (const auto& args : std::vector<std::vector<const char*>>{
{"-a", "-f=csv"}, {"-aqf=csv"}}) {
const auto result = read<Opts>(args);
ASSERT_TRUE(result) << result.error().what();
EXPECT_EQ(result.value().format(), "csv");
}
const auto empty = read<Opts>({"-af="});
ASSERT_TRUE(empty) << empty.error().what();
EXPECT_EQ(empty.value().format(), "");
const auto twice = read<Opts>({"-af==x"});
ASSERT_TRUE(twice) << twice.error().what();
EXPECT_EQ(twice.value().format(), "=x");
}
TEST(cli, test_short_flag_equals_value_alone) {
const auto result = read<Opts>({"-a=false", "-q=true"});
ASSERT_TRUE(result) << result.error().what();
EXPECT_FALSE(result.value().all());
EXPECT_TRUE(result.value().quiet().value());
}
TEST(cli, test_short_cluster_flag_takes_no_attached_value) {
const auto result = read<Opts>({"-aq=false"});
EXPECT_EQ(error_of(result),
"Option '-q' does not take a value in '-aq=false'. Write "
"'-q=false' on its own.");
const auto second = read<Opts>({"-a", "-bq=false"});
EXPECT_EQ(error_of(second),
"Option '-q' does not take a value in '-bq=false'. Write "
"'-q=false' on its own.");
}
TEST(cli, test_short_cluster_last_flag_takes_bool_literal) {
// The last option of a group treats the next token as it would alone.
const auto result = read<Opts>({"-qa", "false", "in.txt"});
ASSERT_TRUE(result) << result.error().what();
EXPECT_TRUE(result.value().quiet().value());
EXPECT_FALSE(result.value().all());
EXPECT_EQ(result.value().input(), "in.txt");
EXPECT_FALSE(result.value().output());
}
TEST(cli, test_short_cluster_flag_does_not_take_other_tokens) {
const auto result = read<Opts>({"-ab", "in.txt", "out.txt"});
ASSERT_TRUE(result) << result.error().what();
EXPECT_TRUE(result.value().all());
EXPECT_TRUE(result.value().brief().value());
EXPECT_EQ(result.value().input(), "in.txt");
EXPECT_EQ(result.value().output(), "out.txt");
}
TEST(cli, test_short_cluster_unknown_letter) {
const auto middle = read<Opts>({"-axf", "csv"});
EXPECT_EQ(error_of(middle), "Unknown short argument: -x (in '-axf').");
const auto first = read<Opts>({"-xa"});
EXPECT_EQ(error_of(first), "Unknown short argument: -x (in '-xa').");
const auto last = read<Opts>({"-abz"});
EXPECT_EQ(error_of(last), "Unknown short argument: -z (in '-abz').");
// A character that takes several bytes in UTF-8 is named whole.
const auto utf8 = read<Opts>({"-a\xC3\xA9", "-\xE2\x82\xAC"});
EXPECT_EQ(error_of(utf8),
"Unknown short argument: -\xC3\xA9 (in '-a\xC3\xA9').");
const auto utf8_alone = read<Opts>({"-a", "-\xE2\x82\xAC"});
EXPECT_EQ(error_of(utf8_alone), "Unknown short argument: -\xE2\x82\xAC");
}
TEST(cli, test_short_unknown_alone) {
EXPECT_FALSE(read<Opts>({"-a", "-x"}));
EXPECT_FALSE(read<Opts>({"-a", "-x=1"}));
}
TEST(cli, test_short_cluster_duplicate) {
EXPECT_FALSE(read<Opts>({"-aa"}));
EXPECT_FALSE(read<Opts>({"-ab", "-ba"}));
}
TEST(cli, test_short_cluster_conflicts_with_long) {
EXPECT_FALSE(read<Opts>({"-afcsv", "--format=json"}));
}
TEST(cli, test_short_cluster_implicit_value_attached) {
// getopt's optional argument: the rest of the token, if any.
const auto result = read<Opts>({"-a", "-hfoo"});
ASSERT_TRUE(result) << result.error().what();
EXPECT_EQ(result.value().help().value(), "foo");
const auto grouped = read<Opts>({"-aqhfoo", "-l7"});
ASSERT_TRUE(grouped) << grouped.error().what();
EXPECT_TRUE(grouped.value().quiet().value());
EXPECT_EQ(grouped.value().help().value(), "foo");
EXPECT_EQ(grouped.value().level().value(), 7);
const auto equals = read<Opts>({"-aqh=foo"});
ASSERT_TRUE(equals) << equals.error().what();
EXPECT_EQ(equals.value().help().value(), "foo");
}
TEST(cli, test_short_negative_number_value) {
for (const auto& args : std::vector<std::vector<const char*>>{
{"-a", "-p", "-42"}, {"-a", "-p-42"}, {"-ap", "-42"},
{"-ap-42"}, {"-a", "-p=-42"}}) {
const auto result = read<Opts>(args);
ASSERT_TRUE(result) << result.error().what();
EXPECT_EQ(result.value().port(), -42);
}
}
struct Numbers {
rfl::Short<"v", bool> verbose;
rfl::Short<"n", std::optional<double>> number;
rfl::Positional<double> x;
rfl::Positional<std::optional<double>> y;
};
TEST(cli, test_short_negative_number_positional) {
const auto result = read<Numbers>({"-v", "-42", "-.5"});
ASSERT_TRUE(result) << result.error().what();
EXPECT_TRUE(result.value().verbose());
EXPECT_DOUBLE_EQ(result.value().x(), -42.0);
EXPECT_DOUBLE_EQ(result.value().y().value(), -0.5);
const auto value = read<Numbers>({"-vn", "-1.5", "-3"});
ASSERT_TRUE(value) << value.error().what();
EXPECT_DOUBLE_EQ(value.value().number().value(), -1.5);
EXPECT_DOUBLE_EQ(value.value().x(), -3.0);
}
struct DigitShort {
rfl::Short<"1", bool> one_per_line;
rfl::Short<"n", std::optional<int>> count;
rfl::Positional<std::optional<int>> x;
};
struct DigitShortNumbers {
rfl::Short<"1", bool> one_per_line;
rfl::Positional<std::optional<double>> x;
rfl::Positional<std::optional<double>> y;
rfl::Positional<std::optional<double>> z;
};
TEST(cli, test_short_digit_option_negative_numbers_are_groups) {
// Once a short option's name is a digit, a token that looks like a
// negative number is a group of short options.
const auto grouped = read<DigitShort>({"-1n2"});
ASSERT_TRUE(grouped) << grouped.error().what();
EXPECT_TRUE(grouped.value().one_per_line());
EXPECT_EQ(grouped.value().count(), 2);
EXPECT_EQ(error_of(read<DigitShort>({"-1", "-5"})),
"Unknown short argument: -5");
EXPECT_EQ(error_of(read<DigitShort>({"-2"})), "Unknown short argument: -2");
EXPECT_EQ(error_of(read<DigitShort>({"-.5"})),
"Unknown short argument: -. (in '-.5').");
EXPECT_EQ(error_of(read<DigitShortNumbers>({"-1.5"})),
"Unknown short argument: -. (in '-1.5').");
EXPECT_EQ(error_of(read<DigitShortNumbers>({"-10"})),
"Unknown short argument: -0 (in '-10').");
EXPECT_EQ(error_of(read<DigitShortNumbers>({"-15"})),
"Unknown short argument: -5 (in '-15').");
const auto separated =
read<DigitShortNumbers>({"-1", "--", "-1.5", "-10", "-15"});
ASSERT_TRUE(separated) << separated.error().what();
EXPECT_TRUE(separated.value().one_per_line());
EXPECT_DOUBLE_EQ(separated.value().x().value(), -1.5);
EXPECT_DOUBLE_EQ(separated.value().y().value(), -10.0);
EXPECT_DOUBLE_EQ(separated.value().z().value(), -15.0);
}
TEST(cli, test_short_cluster_after_separator) {
const auto result = read<Opts>({"-ab", "--", "-qfcsv", "-x"});
ASSERT_TRUE(result) << result.error().what();
EXPECT_TRUE(result.value().all());
EXPECT_FALSE(result.value().quiet().value());
EXPECT_FALSE(result.value().format());
EXPECT_EQ(result.value().input(), "-qfcsv");
EXPECT_EQ(result.value().output(), "-x");
}
TEST(cli, test_short_cluster_value_after_separator_token) {
// `--` after an option that needs a value is not its value.
const auto result = read<Opts>({"-af", "--", "x"});
EXPECT_EQ(error_of(result), "Missing value for option '-f'.");
}
struct Nested {
rfl::Description<"Be quiet", rfl::Short<"q", bool>> quiet;
rfl::DefaultVal<rfl::Short<"o", std::string>> output;
std::optional<rfl::Short<"v", bool>> verbose;
};
TEST(cli, test_short_cluster_nested_wrappers) {
const auto result = read<Nested>({"-vqoout.csv"});
ASSERT_TRUE(result) << result.error().what();
EXPECT_TRUE(result.value().quiet().value());
EXPECT_EQ(result.value().output().value(), "out.csv");
ASSERT_TRUE(result.value().verbose.has_value());
EXPECT_TRUE(result.value().verbose.value().value());
}
} // namespace test_short_clusters