forked from CLIUtils/CLI11
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NewParseTest.cpp
495 lines (373 loc) · 13.2 KB
/
NewParseTest.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
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
// Copyright (c) 2017-2022, University of Cincinnati, developed by Henry Schreiner
// under NSF AWARD 1414736 and by the respective contributors.
// All rights reserved.
//
// SPDX-License-Identifier: BSD-3-Clause
#include "app_helper.hpp"
#include <complex>
#include <cstdint>
#include <utility>
using Catch::Matchers::Contains;
using cx = std::complex<double>;
TEST_CASE_METHOD(TApp, "ComplexOption", "[newparse]") {
cx comp{1, 2};
app.add_option("-c,--complex", comp)->capture_default_str();
args = {"-c", "4", "3"};
std::string help = app.help();
CHECK_THAT(help, Contains("1"));
CHECK_THAT(help, Contains("2"));
CHECK_THAT(help, Contains("COMPLEX"));
CHECK(comp.real() == Approx(1));
CHECK(comp.imag() == Approx(2));
run();
CHECK(comp.real() == Approx(4));
CHECK(comp.imag() == Approx(3));
}
TEST_CASE_METHOD(TApp, "ComplexFloatOption", "[newparse]") {
std::complex<float> comp{1, 2};
app.add_option("-c,--complex", comp)->capture_default_str();
args = {"-c", "4", "3"};
std::string help = app.help();
CHECK_THAT(help, Contains("1"));
CHECK_THAT(help, Contains("2"));
CHECK_THAT(help, Contains("COMPLEX"));
CHECK(comp.real() == Approx(1));
CHECK(comp.imag() == Approx(2));
run();
CHECK(comp.real() == Approx(4));
CHECK(comp.imag() == Approx(3));
}
TEST_CASE_METHOD(TApp, "ComplexWithDelimiterOption", "[newparse]") {
cx comp{1, 2};
app.add_option("-c,--complex", comp)->capture_default_str()->delimiter('+');
args = {"-c", "4+3i"};
std::string help = app.help();
CHECK_THAT(help, Contains("1"));
CHECK_THAT(help, Contains("2"));
CHECK_THAT(help, Contains("COMPLEX"));
CHECK(comp.real() == Approx(1));
CHECK(comp.imag() == Approx(2));
run();
CHECK(comp.real() == Approx(4));
CHECK(comp.imag() == Approx(3));
args = {"-c", "5+-3i"};
run();
CHECK(comp.real() == Approx(5));
CHECK(comp.imag() == Approx(-3));
args = {"-c", "6", "-4i"};
run();
CHECK(comp.real() == Approx(6));
CHECK(comp.imag() == Approx(-4));
}
TEST_CASE_METHOD(TApp, "ComplexIgnoreIOption", "[newparse]") {
cx comp{1, 2};
app.add_option("-c,--complex", comp);
args = {"-c", "4", "3i"};
run();
CHECK(comp.real() == Approx(4));
CHECK(comp.imag() == Approx(3));
}
TEST_CASE_METHOD(TApp, "ComplexSingleArgOption", "[newparse]") {
cx comp{1, 2};
app.add_option("-c,--complex", comp);
args = {"-c", "4"};
run();
CHECK(comp.real() == Approx(4));
CHECK(comp.imag() == Approx(0));
args = {"-c", "4-2i"};
run();
CHECK(comp.real() == Approx(4));
CHECK(comp.imag() == Approx(-2));
args = {"-c", "4+2i"};
run();
CHECK(comp.real() == Approx(4));
CHECK(comp.imag() == Approx(2));
args = {"-c", "-4+2j"};
run();
CHECK(comp.real() == Approx(-4));
CHECK(comp.imag() == Approx(2));
args = {"-c", "-4.2-2j"};
run();
CHECK(comp.real() == Approx(-4.2));
CHECK(comp.imag() == Approx(-2));
args = {"-c", "-4.2-2.7i"};
run();
CHECK(comp.real() == Approx(-4.2));
CHECK(comp.imag() == Approx(-2.7));
}
TEST_CASE_METHOD(TApp, "ComplexSingleImagOption", "[newparse]") {
cx comp{1, 2};
app.add_option("-c,--complex", comp);
args = {"-c", "4j"};
run();
CHECK(comp.real() == Approx(0));
CHECK(comp.imag() == Approx(4));
args = {"-c", "-4j"};
run();
CHECK(comp.real() == Approx(0));
CHECK(comp.imag() == Approx(-4));
args = {"-c", "-4"};
run();
CHECK(comp.real() == Approx(-4));
CHECK(comp.imag() == Approx(0));
args = {"-c", "+4"};
run();
CHECK(comp.real() == Approx(4));
CHECK(comp.imag() == Approx(0));
}
/// Simple class containing two strings useful for testing lexical cast and conversions
class spair {
public:
spair() = default;
spair(std::string s1, std::string s2) : first(std::move(s1)), second(std::move(s2)) {}
std::string first{};
std::string second{};
};
// an example of custom converter that can be used to add new parsing options
// On MSVC and possibly some other new compilers this can be a free standing function without the template
// specialization but this is compiler dependent
namespace CLI {
namespace detail {
template <> bool lexical_cast<spair>(const std::string &input, spair &output) {
auto sep = input.find_first_of(':');
if((sep == std::string::npos) && (sep > 0)) {
return false;
}
output = {input.substr(0, sep), input.substr(sep + 1)};
return true;
}
} // namespace detail
} // namespace CLI
TEST_CASE_METHOD(TApp, "custom_string_converter", "[newparse]") {
spair val;
app.add_option("-d,--dual_string", val);
args = {"-d", "string1:string2"};
run();
CHECK("string1" == val.first);
CHECK("string2" == val.second);
}
TEST_CASE_METHOD(TApp, "custom_string_converterFail", "[newparse]") {
spair val;
app.add_option("-d,--dual_string", val);
args = {"-d", "string2"};
CHECK_THROWS_AS(run(), CLI::ConversionError);
}
/// simple class to wrap another with a very specific type constructor and assignment operators to test out some of the
/// option assignments
template <class X> class objWrapper {
public:
objWrapper() = default;
explicit objWrapper(X obj) : val_{std::move(obj)} {};
objWrapper(const objWrapper &ow) = default;
template <class TT> objWrapper(const TT &obj) = delete;
objWrapper &operator=(const objWrapper &) = default;
// noexcept not allowed below by GCC 4.8
objWrapper &operator=(objWrapper &&) = default; // NOLINT(performance-noexcept-move-constructor)
// delete all other assignment operators
template <typename TT> void operator=(TT &&obj) = delete;
CLI11_NODISCARD const X &value() const { return val_; }
private:
X val_{};
};
// I think there is a bug with the is_assignable in visual studio 2015 it is fixed in later versions
// so this test will not compile in that compiler
#if !defined(_MSC_VER) || _MSC_VER >= 1910
static_assert(CLI::detail::is_direct_constructible<objWrapper<std::string>, std::string>::value,
"string wrapper isn't properly constructible");
static_assert(!std::is_assignable<objWrapper<std::string>, std::string>::value,
"string wrapper is improperly assignable");
TEST_CASE_METHOD(TApp, "stringWrapper", "[newparse]") {
objWrapper<std::string> sWrapper;
app.add_option("-v", sWrapper);
args = {"-v", "string test"};
run();
CHECK("string test" == sWrapper.value());
}
static_assert(CLI::detail::is_direct_constructible<objWrapper<double>, double>::value,
"double wrapper isn't properly assignable");
static_assert(!CLI::detail::is_direct_constructible<objWrapper<double>, int>::value,
"double wrapper can be assigned from int");
static_assert(!CLI::detail::is_istreamable<objWrapper<double>>::value,
"double wrapper is input streamable and it shouldn't be");
TEST_CASE_METHOD(TApp, "doubleWrapper", "[newparse]") {
objWrapper<double> dWrapper;
app.add_option("-v", dWrapper);
args = {"-v", "2.36"};
run();
CHECK(2.36 == dWrapper.value());
args = {"-v", "thing"};
CHECK_THROWS_AS(run(), CLI::ConversionError);
}
static_assert(CLI::detail::is_direct_constructible<objWrapper<int>, int>::value,
"int wrapper is not constructible from int64");
static_assert(!CLI::detail::is_direct_constructible<objWrapper<int>, double>::value,
"int wrapper is constructible from double");
static_assert(!CLI::detail::is_istreamable<objWrapper<int>>::value,
"int wrapper is input streamable and it shouldn't be");
TEST_CASE_METHOD(TApp, "intWrapper", "[newparse]") {
objWrapper<int> iWrapper;
app.add_option("-v", iWrapper);
args = {"-v", "45"};
run();
CHECK(45 == iWrapper.value());
args = {"-v", "thing"};
CHECK_THROWS_AS(run(), CLI::ConversionError);
}
static_assert(!CLI::detail::is_direct_constructible<objWrapper<float>, int>::value,
"float wrapper is constructible from int");
static_assert(!CLI::detail::is_direct_constructible<objWrapper<float>, double>::value,
"float wrapper is constructible from double");
static_assert(!CLI::detail::is_istreamable<objWrapper<float>>::value,
"float wrapper is input streamable and it shouldn't be");
TEST_CASE_METHOD(TApp, "floatWrapper", "[newparse]") {
objWrapper<float> iWrapper;
app.add_option<objWrapper<float>, float>("-v", iWrapper);
args = {"-v", "45.3"};
run();
CHECK(45.3f == iWrapper.value());
args = {"-v", "thing"};
CHECK_THROWS_AS(run(), CLI::ConversionError);
}
#endif
/// simple class to wrap another with a very specific type constructor to test out some of the option assignments
class dobjWrapper {
public:
dobjWrapper() = default;
explicit dobjWrapper(double obj) : dval_{obj} {};
explicit dobjWrapper(int obj) : ival_{obj} {};
CLI11_NODISCARD double dvalue() const { return dval_; }
CLI11_NODISCARD int ivalue() const { return ival_; }
private:
double dval_{0.0};
int ival_{0};
};
TEST_CASE_METHOD(TApp, "dobjWrapper", "[newparse]") {
dobjWrapper iWrapper;
app.add_option("-v", iWrapper);
args = {"-v", "45"};
run();
CHECK(45 == iWrapper.ivalue());
CHECK(0.0 == iWrapper.dvalue());
args = {"-v", "thing"};
CHECK_THROWS_AS(run(), CLI::ConversionError);
iWrapper = dobjWrapper{};
args = {"-v", "45.1"};
run();
CHECK(0 == iWrapper.ivalue());
CHECK(45.1 == iWrapper.dvalue());
}
/// simple class to wrap another with a very specific type constructor and assignment operators to test out some of the
/// option assignments
template <class X> class AobjWrapper {
public:
AobjWrapper() = default;
// delete all other constructors
template <class TT> AobjWrapper(TT &&obj) = delete;
// single assignment operator
AobjWrapper &operator=(X val) {
val_ = val;
return *this;
}
// delete all other assignment operators
template <typename TT> void operator=(TT &&obj) = delete;
CLI11_NODISCARD const X &value() const { return val_; }
private:
X val_{};
};
static_assert(std::is_assignable<AobjWrapper<std::uint16_t> &, std::uint16_t>::value,
"AobjWrapper not assignable like it should be ");
TEST_CASE_METHOD(TApp, "uint16Wrapper", "[newparse]") {
AobjWrapper<std::uint16_t> sWrapper;
app.add_option<AobjWrapper<std::uint16_t>, std::uint16_t>("-v", sWrapper);
args = {"-v", "9"};
run();
CHECK(9u == sWrapper.value());
args = {"-v", "thing"};
CHECK_THROWS_AS(run(), CLI::ConversionError);
args = {"-v", "72456245754"};
CHECK_THROWS_AS(run(), CLI::ConversionError);
args = {"-v", "-3"};
CHECK_THROWS_AS(run(), CLI::ConversionError);
}
template <class T> class SimpleWrapper {
public:
SimpleWrapper() = default;
explicit SimpleWrapper(T initial) : val_{std::move(initial)} {};
T &getRef() { return val_; }
using value_type = T;
private:
T val_{};
};
TEST_CASE_METHOD(TApp, "wrapperInt", "[newparse]") {
SimpleWrapper<int> wrap;
app.add_option("--val", wrap);
args = {"--val", "2"};
run();
CHECK(2 == wrap.getRef());
}
TEST_CASE_METHOD(TApp, "wrapperString", "[newparse]") {
SimpleWrapper<std::string> wrap;
app.add_option("--val", wrap);
args = {"--val", "str"};
run();
CHECK("str" == wrap.getRef());
}
TEST_CASE_METHOD(TApp, "wrapperVector", "[newparse]") {
SimpleWrapper<std::vector<int>> wrap;
app.add_option("--val", wrap);
args = {"--val", "1", "2", "3", "4"};
run();
auto v1 = wrap.getRef();
auto v2 = std::vector<int>{1, 2, 3, 4};
CHECK(v2 == v1);
}
TEST_CASE_METHOD(TApp, "wrapperwrapperString", "[newparse]") {
SimpleWrapper<SimpleWrapper<std::string>> wrap;
app.add_option("--val", wrap);
args = {"--val", "arg"};
run();
auto v1 = wrap.getRef().getRef();
const auto *v2 = "arg";
CHECK(v2 == v1);
}
TEST_CASE_METHOD(TApp, "wrapperwrapperVector", "[newparse]") {
SimpleWrapper<SimpleWrapper<std::vector<int>>> wrap;
auto *opt = app.add_option("--val", wrap);
args = {"--val", "1", "2", "3", "4"};
run();
auto v1 = wrap.getRef().getRef();
auto v2 = std::vector<int>{1, 2, 3, 4};
CHECK(v2 == v1);
opt->type_size(0, 5);
args = {"--val"};
run();
CHECK(wrap.getRef().getRef().empty());
args = {"--val", "happy", "sad"};
CHECK_THROWS_AS(run(), CLI::ConversionError);
}
TEST_CASE_METHOD(TApp, "wrapperComplex", "[newparse]") {
SimpleWrapper<std::complex<double>> wrap;
app.add_option("--val", wrap);
args = {"--val", "1", "2"};
run();
auto &v1 = wrap.getRef();
auto v2 = std::complex<double>{1, 2};
CHECK(v2.real() == v1.real());
CHECK(v2.imag() == v1.imag());
args = {"--val", "1.4-4j"};
run();
v2 = std::complex<double>{1.4, -4};
CHECK(v2.real() == v1.real());
CHECK(v2.imag() == v1.imag());
}
TEST_CASE_METHOD(TApp, "vectorComplex", "[newparse]") {
std::vector<std::complex<double>> vcomplex;
app.add_option("--val", vcomplex);
args = {"--val", "1", "2", "--val", "1.4-4j"};
run();
REQUIRE(2U == vcomplex.size());
CHECK(1.0 == vcomplex[0].real());
CHECK(2.0 == vcomplex[0].imag());
CHECK(1.4 == vcomplex[1].real());
CHECK(-4.0 == vcomplex[1].imag());
}